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

📄 importodbc.cpp

📁 一个英国人写的GIS查看/编辑工具。支持标准的shapefile地图文件格式和coverage地图文件格式。同时可以编辑相应的dbf文件。
💻 CPP
📖 第 1 页 / 共 4 页
字号:
      wFlag = LAT_COORD;
   };
   if (sAttr.Right(strlen(LON_COORDS)) == LON_COORDS)
   {
      sAttr = sAttr.Left(sAttr.GetLength()-strlen(LON_COORDS));
      wFlag = LON_COORD;
   }
   if (sAttr.Right(strlen(IMP_COORDS)) == IMP_COORDS)
   {
      sAttr = sAttr.Left(sAttr.GetLength()-strlen(IMP_COORDS));  
      wFlag = IMP_COORD;
   }   
   if (sAttr.Right(strlen(IMP_MAPLINES)) == IMP_MAPLINES)
   {
      sAttr = sAttr.Left(sAttr.GetLength() - strlen(IMP_MAPLINES));
      wFlag = IMP_MAPLINE;
   }   

   // Return matching attribute

   for (int i = 0; i < aAttr.GetSize(); i++)
   {
      CAttribute* pAttr = aAttr.GetAt(i);
      if (pAttr->GetDesc() == sAttr) return MAKELONG(pAttr->GetAttrId(), wFlag);
   }
   return Undefined;
}

///////////////////////////////////////////////////////////////////////////////
//
// Returns the id of a feature, if it exists and is not an empty string.  If
// not the user is prompted with a dialog
//

long CImportTable::DetermineFeature(long lFType, CString sFeature, LPCSTR sFeatureParent, 
                                    LPCSTR sFeatureGrandParent, LPCSTR sFeatureGreatGrandParent, BOOL bParent)
{
   CFeature feature;   
   CFeatureType ftype;
   CArray <CFeature,CFeature> aFeatures;   
   feature.m_sName = sFeature;
   long lFeature = 0;
   long lParent = 0;
   BOOL bFound = FALSE;
   int nRet = 0;

   // Strip leading and trailing spaces

   sFeature.TrimLeft();
   sFeature.TrimRight();

   // Determine parent feature
  
   if (sFeatureParent[0] != '\0')
   {
	   long lPFType = GetFTypeParentI(lFType);
	   if (lPFType != 0)
	   {
		  lParent = DetermineFeature(lPFType, sFeatureParent, sFeatureGrandParent, sFeatureGreatGrandParent, "", TRUE);
	   }

      // Cancel
      if (lParent == -1) return -1;
   }

   if (sFeature.IsEmpty()) sFeature = BDString(IDS_UNDEFINED);

   // If one to one then determine the inherited feature type

   lFType = GetFTypeI(lFType);
   
   // Selected feature for all

   for (int i = 0; i < m_aImportFeature.GetSize(); i++)
   {
      if (m_aImportFeature[i].m_lFType == lFType && 
          m_aImportFeature[i].m_sFeature == sFeature+sFeatureParent+sFeatureGrandParent+sFeatureGreatGrandParent)
      {
         return m_aImportFeature[i].m_lFeature;
      };
   }   

   // Search the database   
 
   if (!sFeature.IsEmpty())
   {           
      feature.m_lFeatureTypeId = lFType;
      feature.m_sName = sFeature;
      bFound = BDFeature(BDHandle(), &feature, BDSELECT3);
      while (bFound)
      {
         if (feature.m_lParentFeature == lParent || lParent == 0)
         {
            aFeatures.Add(feature);
         };
         bFound = BDGetNext(BDHandle());
      }
      BDEnd(BDHandle());
                     
      // If one unique feature then use it
      
      if (aFeatures.GetSize() == 1)
      {
         lFeature = aFeatures[0].m_lId;

         // Add for autoselection to speed up, especially for parent feature

         CImportFeature feature;
         feature.m_lFeature = lFeature;
         feature.m_lFType = lFType;
         feature.m_sFeature = sFeature + sFeatureParent + sFeatureGrandParent + sFeatureGreatGrandParent; 
         m_aImportFeature.Add(feature);         
      }

   };
   
   // If not found then display a dialog

   if (lFeature == 0)
   {    
      CDlgImportFeature dlg(lFType, "", sFeature, sFeatureParent, lParent); 

      for (int i = 0; i < m_alCreateAll.GetSize(); i++)
      {
         if (m_alCreateAll[i] == lFType)
         {
            nRet = IDC_CREATEALL;
         }
      }

      // For auto import then create all

      if (m_bAutoCreate) nRet = IDC_CREATEALL;

      if (nRet != IDC_CREATEALL)
      {         
         // Sound alert incase import is occuring in the background      

         MessageBeep(MB_ICONQUESTION);

         nRet = dlg.DoModal();         
      }

      // If create all is selected then create automatically

      if (nRet == IDC_CREATEALL)
      {
         // Add new feature type

         for (int i = 0; i < m_alCreateAll.GetSize(); i++)
         {
             if (m_alCreateAll[i] == lFType) break;
         }
         if (i == m_alCreateAll.GetSize()) m_alCreateAll.Add(lFType);         
         
         // Create feature

         if (!sFeature.IsEmpty())
         {
            lFeature = CreateFeature(lFType, sFeature, sFeatureParent, sFeatureGrandParent, sFeatureGreatGrandParent, lParent);
                              
            if (lFeature == 0)
            {
               // If create feature failed then display create dialog

               CDlgEditFeature dlg(lFType, sFeature, sFeatureParent);
               nRet = dlg.DoModal();

               if (nRet == IDOK)
               {      
                  lFeature = dlg.GetId();            
               };
            } else if (lFeature == -1)
            {
               return -1;
            }
         } else
         {  
            fprintf(m_pImportDB->m_pLogFile, "%i: No name supplied for feature, not imported\r\n", 
                    m_nRecord);                                 

         };
      }

      // For select then return feature

      else if (nRet == IDC_SELECT || nRet == IDC_SELECTALL)
      {
         lFeature = dlg.GetFeature();
      }

      // For select all, add to the list, per feature type

      if (nRet == IDC_SELECTALL)
      {         
         CImportFeature feature;
         feature.m_lFeature = lFeature;
         feature.m_lFType = lFType;
         feature.m_sFeature = sFeature + sFeatureParent; 
         m_aImportFeature.Add(feature);         
      }
      if (nRet == IDCANCEL)
      {
         return -1;
      }
   } else if (sFeature[0] == '\0')
   {
      fprintf(m_pImportDB->m_pLogFile, "%i: " + BDString(IDS_NONAME) + "\r\n", m_nRecord);                                 
   }

   return lFeature;
}

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

long CImportTable::GetFTypeI(long lFType)
{
   CFeatureType ftype;

   for (int i = 0; i < m_aImportFTypeI.GetSize(); i++)
   {
      if (m_aImportFTypeI[i].m_lFType == lFType) 
      {
         return m_aImportFTypeI[i].m_lFTypeI;
      };
   }
   if (BDFTypeI(BDHandle(), lFType, &ftype))
   {      
      CImportFTypeI importftype;
      importftype.m_lFType = lFType;
      importftype.m_lFTypeI = ftype.m_lId;
      m_aImportFTypeI.Add(importftype);
      return ftype.m_lId;
   }

   ASSERT(FALSE);
   return 0;   
}

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

long CImportTable::GetFTypeParentI(long lFType)
{
   CFeatureType ftypeP;

   for (int i = 0; i < m_aImportFTypeParentI.GetSize(); i++)
   {
      if (m_aImportFTypeParentI[i].m_lFType == lFType) 
      {
         return m_aImportFTypeParentI[i].m_lFTypeParentI;
      };
   }

   if (BDFTypeParentI(BDHandle(), lFType, &ftypeP))
   {      
      CImportFTypeParentI importftype;
      importftype.m_lFType = lFType;
      importftype.m_lFTypeParentI = ftypeP.m_lId;
      m_aImportFTypeParentI.Add(importftype);      

      return ftypeP.m_lId;
   }
   
   return 0;   
}

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

long CImportTable::CreateFeature(long lFType, LPCSTR sFeature, LPCSTR sFeatureParent, 
								 LPCSTR sFeatureGrandParent, LPCSTR sFeatureGreatGrandParent, 
								 long lParent)
{
   CFeature feature;
   CFeatureType ftype;    
   BOOL bOK = TRUE;
   CString s;
   
   // Determine feature type

   ftype.m_lId = lFType;
   bOK = BDFeatureType(BDHandle(), &ftype, BDSELECT);
   BDEnd(BDHandle());

   // If no parent then ask for it

   if (lParent == 0)
   {
	   long lPFType = GetFTypeParentI(lFType);
	   if (lPFType != 0)
	   {
		  lParent = DetermineFeature(lPFType, sFeatureParent, sFeatureGrandParent, sFeatureGreatGrandParent, "", TRUE);
	   }
   }

   // Determine the next id

   if (bOK)
   {          
      BDNextId(BDHandle(), BDFEATURE, lFType, &feature.m_lId);
      
      feature.m_sName = sFeature;
      feature.m_lFeatureTypeId = lFType;

      // Set parent feature

      feature.m_lParentFeature = lParent;

      // Create feature 

      bOK = BDFeature(BDHandle(), &feature, BDADD);
	   BDEnd(BDHandle());
   };

   if (bOK)
   {
      return feature.m_lId;
   }
   return 0;
   
};

///////////////////////////////////////////////////////////////////////////////
//
// Determine the file being imported
//

CString CImportTable::GetShortFileTitle(CDatabase& database)
{
   CString sPath;
   CString sTable;
	char szDBase[_MAX_PATH];

   // Determine the directory

   int i = database.GetConnect().Find("DefaultDir=");      
   sPath = database.GetConnect().Mid(i+11);
   sPath = sPath.Left(sPath.Find(";"));

   // Create the full path

   if (sPath[sPath.GetLength()-1] != '\\') sPath += '\\';   
   sPath += m_sTableImport + ".dbf";

   // Convert to a short file name
    
   GetShortPathName(sPath, szDBase, sizeof(szDBase));

   // Retrieve the short file name

   sTable = szDBase;
   sTable = sTable.Mid(sTable.ReverseFind('\\')+1);
   sTable = sTable.Left(sTable.ReverseFind('.'));

   return sTable;

   return sPath;
}

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

long CImportTable::GetCountRecords(HDBC hdbc, LPCSTR sTable)
{
   BOOL bOK = TRUE;
   long lRet = 0;   
   SDWORD cbCount;   
   RETCODE nRetCode;
      
   if (bOK)
   {              
      HSTMT hstmt;
      if (SQLAllocStmt(hdbc, &hstmt) == SQL_SUCCESS)
      {         
      
         CString sSQL = "select count (*) from [" + CString(sTable) + "]";
         if (::SQLExecDirect(hstmt, (UCHAR*)sSQL.GetBuffer(0), sSQL.GetLength()) != SQL_SUCCESS)
         { 
            bOK = FALSE;
         };
         
         if (bOK && SQLBindCol(hstmt, 1, SQL_C_LONG, &lRet, sizeof(long),  &cbCount) != SQL_SUCCESS)
         { 
            bOK = FALSE;
         };
             
         if (bOK)
         {
            bOK = SQLFetch(hstmt) == SQL_SUCCESS;        
         };
             
         AFX_SQL_SYNC(SQLFreeStmt(hstmt,SQL_DROP));         
      };      
   };   
   
  if (bOK)
  { 
     return lRet;
  };
  
  return 0;
};

///////////////////////////////////////////////////////////////////////////////
//
// Identify features based on a single attribute rather than feature and parent names
//

long CImportTable::DetermineFeature(long lFType, CString sId, CArray <CImportColumn,CImportColumn>& aColumns)
{
   CFeatureType ftype;
   long lFeature = 0;
   CAttrArray aAttr;
   int iAttr = 0;   
   double d;
   CImportPrimaryKey primarykey;

   // Determine the column to be used for identifying the feature

   if (m_nPrimaryKey == -1)
   {
      AfxGetApp()->BeginWaitCursor();

      if (BDFTypeI(BDHandle(), lFType, &ftype))
      {
         CDlgImportPrimaryKey dlg(ftype.m_lId);
         if (dlg.DoModal())
         {
            m_nPrimaryKey = dlg.GetAttrId();
         } else
         {
            return -1;
         }

         // Retrieve the data for the feature and store the id's and feature ids

         aAttr.m_lFType = ftype.m_lId;
         BOOL bFound = BDAttribute(BDHandle(), &aAttr, BDGETINIT);

         // Determine index of attribute

         if (bFound)
         {
            for (iAttr = 0; iAttr < aAttr.GetSize(); iAttr++)
            {
               if (aAttr[iAttr]->GetAttrId() == m_nPrimaryKey) 
               {
                  m_nDataType = aAttr[iAttr]->GetDataType();
                  break;
               }
            }
            ASSERT(iAttr < aAttr.GetSize());
         };

         while (bFound)
         {
            // Convert to integer
                     
            if (m_nDataType == BDNUMBER)
            {
               primarykey.m_dId = *aAttr[iAttr]->GetDouble();
            } else
            {
               primarykey.m_sId = aAttr[iAttr]->AsString();            
            }
            primarykey.m_sId.TrimLeft();
            primarykey.m_sId.TrimRight();

            primarykey.m_lFeature = aAttr.m_lFeature;
            m_aPrimaryKey.Add(primarykey);            

            bFound = BDGetNext(BDHandle());

         }
         BDEnd(BDHandle());
      };    

      AfxGetApp()->EndWaitCursor();
   };

   // Search for primary key 

   sId.TrimLeft();
   sId.TrimRight();

   if (sId != "")
   {
      if (m_nDataType == BDNUMBER)
      {
         sscanf(sId, "%lf", &d);
      }

      for (int i = 0; i < m_aPrimaryKey.GetSize(); i++)
      {
         if ((m_nDataType == BDNUMBER && fabs(d-m_aPrimaryKey[i].m_dId) < 0.000001) ||                            
             (m_nDataType != BDNUMBER && sId == m_aPrimaryKey[i].m_sId))
         {
            lFeature = m_aPrimaryKey[i].m_lFeature;
         }
      }
   };

   return lFeature;
}

⌨️ 快捷键说明

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