⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 importodbc.cpp

📁 一个英国人写的GIS查看/编辑工具。支持标准的shapefile地图文件格式和coverage地图文件格式。同时可以编辑相应的dbf文件。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
                lFeature = 0;
                fprintf(importDB.m_pLogFile, "%i: " + BDString(IDS_INVALIDDATE)+ ": %s\r\n", m_nRecord, sDate);                     
             }
          } 
          // If no date supplied then request a date from the user
          else
          {
             CDlgImportDate dlg;
             if (!dateDefault.IsValid())
             {
                if (!m_bAutoCreate)
                {
                   if (dlg.DoModal() == IDOK)
                   {
                      dateDefault = dlg.GetDate();
                   } else
                   {
                      bOK = FALSE;
                   }                
                } else
                {
                   dateDefault.AsSystemDate();
                }
             }   
             date = dateDefault;
          }
          if (bOK && lFeature != 0)
          {
             aAttr.m_lDate = date.GetDateLong();
          };

          // For links, determine their ids

          if (!DetermineLink(importDB, aAttr))
          {
             bOK = FALSE;             
          }

          // For booleans, determine value

          if (!DetermineBool(importDB, aAttr))
          {
             lFeature = 0;
          }

          // Determine value of numbers

          if (!DetermineNumber(importDB, aAttr))
          {
             lFeature = 0;
          }

          // For latitude/lontitude determine value

          if (!DetermineLatLon(importDB, aAttr))
          {
             lFeature = 0;
          }

          // Update the feature attributes as these are used by DetermineShapefile

          aAttr.m_lFeature = lFeature;
          aAttr.m_lFType = ftype.m_lId;
          aAttr.m_lOrder = importDB.m_nImported;

          // For shapefiles, determine value

          int nFlag = BDADD;
          
          nShape = DetermineShapefile(aAttr, &maplayer);

          if (nShape == SHPPolygon)
          {
             // Store features at the end when the polylines have been
             // merged

             lFeature = 0;                     
          }
		    else if (nShape == -1)
          {
			   bOK = FALSE;
          }

          // Save values to database
                    
             if (lFeature != 0 && bOK)
             {                
                // Possibly causes memory error
                //MergeData(aAttr, nFlag);         

                if (BDAttribute(BDHandle(), &aAttr, nFlag))
                {
                   importDB.m_nImported++;                                      
                };                
             };
             BDEnd(BDHandle());          
          } 
          catch (CDBException* pEx)
          {             
            // If duplicate value then create a new feature          
            try
            {
            // If unable to write to database then assume this is because of duplicate
            // attributes.  Create a new feature and write the data for this

               long lParent = 0;
               long lFeature = CreateFeature(ftype.m_lId, sFeature, sParentFeature, sGrandParentFeature, sGreatGrandParentFeature, lParent);
               if (lFeature)
               {
                  aAttr.m_lFeature = lFeature;

            
                  if (BDAttribute(BDHandle(), &aAttr, BDADD))
                  {
                     importDB.m_nImported++;                                      
                  };                                                         
               } 
          
            }
            catch (CDBException* pEx)
            {
               BDGetError(BDHandle(), sError);
               fprintf(importDB.m_pLogFile, "%i: %s\r\n", m_nRecord,(LPCSTR)sError);                           
               pEx->Delete();
            }          

            pEx->Delete();
          } 
          catch (CException* pEx)
          {
             char szError[128];
             pEx->GetErrorMessage(szError, sizeof(szError));
             fprintf(importDB.m_pLogFile, "%i: %s\r\n", m_nRecord, szError);                                 
             pEx->Delete();

             // Prevent from committing changes, 
             importDB.m_nImported = 0; 

             bOK = FALSE;
          }
          
          // Update progress

          if (nShape != SHPPolygon)
          {
             sProgress.Format(BDString(IDS_IMPORTED) + ": %i "+ BDString(IDS_ERRORS) +": %i",importDB.m_nImported, m_nRecord-importDB.m_nImported);
             BDProgressText(sProgress);
          } else
          {
             sProgress.Format(BDString(IDS_READING) + " %i",m_nRecord);
             BDProgressText(sProgress);             
          }
                   
          BDProgressPos(m_nRecord);
          if (importDB.m_pDlgProgress->IsCancel()) bCancel = TRUE;

          m_nRecord++;
	

          // Clean up ready for next string

          for (int i = 0; i < m_aColumn.GetSize(); i++)
          {                           
             m_aColumn[i].m_sText[0] = '\0';
          }                 
          for (i = 0; i < aAttr.GetSize(); i++)
          {
             CAttribute* pAttr = aAttr.GetAt(i);
             if (pAttr->GetDataType() == BDTEXT) pAttr->SetString("");             
			    if (pAttr->GetDataType() == BDLONGTEXT) pAttr->SetLongText("");
          }

          // Reset all names otherwise if reach bottom of excel file still appears
          // to be (repeating) data

          sFeature[0] = '\0';
          sParentFeature[0] = '\0';
          sGrandParentFeature[0] = '\0';
          sGreatGrandParentFeature[0] = '\0';
       }   

   // Release statement on end
      
       SQLFreeStmt(hstmt,SQL_DROP);         
   }      

    // Once finished update the records with the shapefile data

   if (bOK && !bCancel)
   {
      bOK = WriteShapefile(importDB);      
   };
        
   return bOK;
}

///////////////////////////////////////////////////////////////////////////////
//
// For BDLONGTEXT attributes, retrieves subsequent data
//

void CImportTable::RetrieveLongText(HSTMT hstmt, CAttrArray& aAttr)
{
   char sBuffer[BD_SHORTSTR];
   int iCol = 1;
   long nLength;
   int nRet;

   // Find any columns containing longtext attributes

   for (int i = 0; i < m_aColumn.GetSize(); i++)
   {
      if (m_aColumn[i].m_bAttr)
      {
         CAttribute* pAttr = NULL;
         int iAttr = GetAttr(aAttr, m_aColumn[i].m_sNameAttr);         

         if (iAttr != Undefined)
         {
            for (int j = 0; j < aAttr.GetSize(); j++)
            {
               if (aAttr.GetAt(j)->GetAttrId() == LOWORD(iAttr))
               {
                  pAttr = aAttr.GetAt(j);
               }
            }
		 }

		 // Retrieve data

		 if (pAttr->GetDataType() == BDLONGTEXT)
		 {			 
			 pAttr->SetLongText("");			 

			 do 
			 {
			    nRet = SQLGetData(hstmt, iCol, SQL_C_CHAR, sBuffer, BD_SHORTSTR, &nLength);

				// Append the retrieve values to the string

             if (SQL_SUCCEEDED(nRet) && nLength > 0)
             {
				    pAttr->SetLongText(*pAttr->GetLongText() + CString(sBuffer, nLength));
             };
             // Loop whilst data truncated
			 } while (nLength > 0 && nRet == SQL_SUCCESS_WITH_INFO);
		 }
	  };

	  if (!m_aColumn[i].m_bShapeFile) iCol++;
   }; 
}


///////////////////////////////////////////////////////////////////////////////

void CImportDB::CompactDatabase()
{
	// Determine the file name of the database

   CString sVersion;
   BDGetDriver(BDHandle(), sVersion);   
   if (sVersion == "odbcjt32.dll")
   {

      AfxGetApp()->BeginWaitCursor();

      HKEY hKey;
      UCHAR sRegKey[_MAX_PATH];
      CString sDBPath;
      char szDBPath[_MAX_PATH];
      long lFileLength = 0;
      long lFileLengthNew = 0;

      if (RegOpenKey(HKEY_CURRENT_USER, "SOFTWARE\\ODBC\\ODBC.INI\\" + CString(BDGetDataSource()), &hKey) == ERROR_SUCCESS)
      {
         DWORD dw = sizeof(sRegKey);           
	     DWORD dwDataType;

         if (RegQueryValueEx(hKey, "DBQ", NULL, &dwDataType, sRegKey, &dw) == ERROR_SUCCESS)
         {
            sDBPath = sRegKey;         
         }
      }   
      RegCloseKey(hKey);

      // Disconnect 

	   BDDisconnect(BDHandle());

       CMainFrame* pWnd = (CMainFrame*)AfxGetMainWnd();
	   pWnd->SetStatusText("Compacting database...");

      GetShortPathName(sDBPath, szDBPath, sizeof(szDBPath));   

	   SQLConfigDataSource(NULL,ODBC_ADD_DSN, BDString(IDS_ACCESSDRIVER),
			     "COMPACT_DB=" + CString(szDBPath) + " " + CString(szDBPath) + " General\0"
			     "MaxScanRows=20\0\0");    		   

	   BDConnect(BDGetDataSource(), &BDHandleRef());    				 

	   AfxGetApp()->EndWaitCursor();
   };	      
}


///////////////////////////////////////////////////////////////////////////////

BOOL CImportTable::DetermineLink(CImportDB& importDB, CAttrArray &aAttr)
{
   for (int i = 0; i < m_aColumn.GetSize(); i++)
   {
      // Determine attribute for each column

      if (m_aColumn[i].m_bAttr)
      {
         CAttribute* pAttr = NULL;
         int iAttr = GetAttr(aAttr, m_aColumn[i].m_sNameAttr);

         if (iAttr != Undefined)
         {
            for (int j = 0; j < aAttr.GetSize(); j++)
            {
               if (aAttr.GetAt(j)->GetAttrId() == LOWORD(iAttr))
               {
                  pAttr = aAttr.GetAt(j);
               }
            }

            // For each link, search for the feature

            if (pAttr->GetDataType() == BDLINK)
            {				    
                // Set link to undefined

                pAttr->SetLink(0);

                if (m_aColumn[i].m_sText[0] != '\0') 
                {
                   long lAttr = DetermineFeature(pAttr->GetFTypeLink(), m_aColumn[i].m_sText, "", "", "");

                   // Reset link value so not re-used if next line in file is blank                 

                   if (lAttr == -1) return FALSE;
                   pAttr->SetLink(lAttr);
                }
            }     
         };
      };
   };

   // For any link attributes not being imported then set their value to
   // undefined

   for (i = 0; i < aAttr.GetSize(); i++)
   {
      CAttribute* pAttr = aAttr.GetAt(i);
      if (pAttr->GetDataType() == BDLINK &&
          *pAttr->GetLink() == 0)
      {
         long lAttr = DetermineFeature(pAttr->GetFTypeLink(), BDString(IDS_UNDEFINED), "", "", "");
         if (lAttr == -1) return FALSE;
         pAttr->SetLink(lAttr);                               
      }
   }

   return TRUE;
}

///////////////////////////////////////////////////////////////////////////////

BOOL CImportTable::DetermineBool(CImportDB& importDB, CAttrArray &aAttr)
{
   for (int i = 0; i < m_aColumn.GetSize(); i++)
   {
      // Determine attribute for each column

      if (m_aColumn[i].m_bAttr)
      {
         CAttribute* pAttr = NULL;
         int iAttr = GetAttr(aAttr, m_aColumn[i].m_sNameAttr);
         ASSERT(iAttr != Undefined);
         if (iAttr == Undefined) return FALSE;

         for (int j = 0; j < aAttr.GetSize(); j++)
         {
            if (aAttr.GetAt(j)->GetAttrId() == LOWORD(iAttr))
            {
               pAttr = aAttr.GetAt(j);
            }
         }

         // For each link, search for the feature

         if (pAttr->GetDataType() == BDBOOLEAN)
         {             
             if (stricmp(m_aColumn[i].m_sText,BDString(IDS_YES)) == 0 ||
				 stricmp(m_aColumn[i].m_sText,BDString(IDS_Y)) == 0 ||
                 stricmp(m_aColumn[i].m_sText,BDString(IDS_TRUE)) == 0 ||
                 stricmp(m_aColumn[i].m_sText, "1") == 0)
             {
                pAttr->SetBoolean(TRUE);
             }
             else if (stricmp(m_aColumn[i].m_sText,BDString(IDS_NO)) == 0 ||
				 stricmp(m_aColumn[i].m_sText,BDString(IDS_N)) == 0 ||
                 stricmp(m_aColumn[i].m_sText,BDString(IDS_FALSE)) == 0 || 
                 stricmp(m_aColumn[i].m_sText, "0") == 0)
             {
                pAttr->SetBoolean(FALSE);
             }
			 else if (m_aColumn[i].m_sText[0] == '\0')
			 {
				 pAttr->SetBoolean(AFX_RFX_BOOL_PSEUDO_NULL);
			 }
             else
             {
                 fprintf(importDB.m_pLogFile, "%i: " + BDString(IDS_INVALIDVALUE) + ": %s (%s)\r\n", 
                         m_nRecord, m_aColumn[i].m_sNameAttr, m_aColumn[i].m_sText);                  
                 pAttr->SetBoolean(FALSE);             
             }                                            
         }     
      };
   };
   return TRUE;
}

///////////////////////////////////////////////////////////////////////////////

BOOL CImportTable::DetermineNumber(CImportDB& importDB, CAttrArray& aAttr)
{
   for (int i = 0; i < m_aColumn.GetSize(); i++)
   {
      // Determine attribute for each column

      if (m_aColumn[i].m_bAttr)
      {
         CAttribute* pAttr = NULL;
         int iAttr = GetAttr(aAttr, m_aColumn[i].m_sNameAttr);
         ASSERT(iAttr != Undefined);         
         if (iAttr == Undefined) return FALSE;

         for (int j = 0; j < aAttr.GetSize(); j++)
         {
            if (aAttr.GetAt(j)->GetAttrId() == LOWORD(iAttr))
            {
               pAttr = aAttr.GetAt(j);
            }
         }

         // For each link, search for the feature

         if (pAttr->GetDataType() == BDNUMBER)
         {             
             if (!pAttr->AsAttr(m_aColumn[i].m_sText))
             {
                fprintf(importDB.m_pLogFile, "%i: " + BDString(IDS_INVALIDVALUE) + ": %s (%s)\r\n", 
                         m_nRecord, m_aColumn[i].m_sNameAttr, m_aColumn[i].m_sText);                       
                pAttr->SetDouble(AFX_RFX_DOUBLE_PSEUDO_NULL);
             }                                                      
         }     
      };
   };
   return TRUE;
}

///////////////////////////////////////////////////////////////////////////////

BOOL CImportTable::DetermineLatLon(CImportDB& importDB, CAttrArray& aAttr)
{
   double dLat, dLon;
   double *pX = NULL, *pY = NULL;

   for (int i = 0; i < m_aColumn.GetSize(); i++)
   {
      // Determine attribute for each column

      if (m_aColumn[i].m_bAttr)
      {
         CAttribute* pAttr = NULL;
         int iAttr = GetAttr(aAttr, m_aColumn[i].m_sNameAttr);
         ASSERT(iAttr != Undefined);
         if (iAttr == Undefined) return FALSE;

         for (int j = 0; j < aAttr.GetSize(); j++)
         {
            if (aAttr.GetAt(j)->GetAttrId() == LOWORD(iAttr))
            {
               pAttr = aAttr.GetAt(j);
            }
         }

         // For each link, search for the feature

         WORD wFlag = HIWORD(iAttr);

         if (pAttr->GetDataType() == BDCOORD)            
         {  
            // Latitude

            if (wFlag == LAT_COORD)
            {  
               pX = &pAttr->GetCoord()->x;
               if (!BDProjection()->StringAsLatLong(m_aColumn[i].m_sText, &dLat, CProjection::latitude | CProjection::import))              

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -