1 接口及帮助
1.1 IWorkspace
1.2 IFeatureDataset
示例代码(获取数据集下的要素类名称):
IWorkspaceFactory worFact =new FileGDBWorkspaceFactory(); IWorkspace workspace = worFact.OpenFromFile("路径", 0); IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace; IFeatureDataset ipFeaterDataset = featureWorkspace.OpenFeatureDataset(checkLayerName); IEnumDataset ped = ipFeaterDataset.Subsets;
1.3 IFeatureClassContainer
示例代码(获取数据集下的要素类及其别名):
IFeatureDataset ipFeaterDataset = featureWorkspace.OpenFeatureDataset(checkLayerName); IFeatureContainer pfc = ipFeaterDataset as IFeatureContainer; IEnumFeatureClass pefc = pfc.Classes;
2 具体功能函数
2.1 根据名称删除FeatureClass
这个函数的缺点是,不能删除FeatureSet中的FeatureClass
public void DeleteFeatureClass(string featueClassName) { //打开GDB文件 IWorkspaceFactory worFact =new FileGDBWorkspaceFactory(); IWorkspace workspace = worFact.OpenFromFile("路径", 0); IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace; //根据名称删除数据库要素 IFeatureWorkspaceManage featureWorkspaceMange = (IFeatureWorkspaceManage)featureWorkspace; IEnumDatasetName enumDatasetName = workspace.get_DatasetNames(esriDatasetType.esriDTFeatureClass); IDatasetName datasetName = null; while ((datasetName = enumDatasetName.Next()) != null) { if (datasetName.Name.Equals(featueClassName)) { featureWorkspaceMange.DeleteByName(datasetName);//删除指定要素类 break; } } }
2.2 删除GDB下的指定数据集
public void DeleteFeatureDataset(string featueClassName, string featueDatasetName) { //打开GDB文件 IWorkspaceFactory worFact =new FileGDBWorkspaceFactory(); IWorkspace workspace = worFact.OpenFromFile("路径", 0); IFeatureWorkspace featureWorkspace = workspace as IFeatureWorkspace; //根据名称删除数据库要素 IFeatureWorkspaceManage featureWorkspaceMange = (IFeatureWorkspaceManage)featureWorkspace; IEnumDatasetName enumDatasetName = workspace.get_DatasetNames(esriDatasetType.esriDTFeatureDataset); IDatasetName datasetName = null; while ((datasetName = enumDatasetName.Next()) != null) { if (datasetName.Name.Equals(featueDatasetName)) { featureWorkspaceMange.DeleteByName(featueDatasetName);//删除指定要素类 break; } } }
3 获取给定数据集下的所有要素层的别名、名称
3.1 获取一个Dataset下的所有FeatureClass的别名
public List<string> getAllFeaClsNamesInDS(IFeatureDataset m_FeatureDataset) { List<string> liFeaClsNames = new List<string>(); //利用IFeatureClassContainer对象遍历IFeatureDataset IFeatureClassContainer m_FeatureClassContainer = (IFeatureClassContainer)m_FeatureDataset; IEnumFeatureClass m_EnumFC = m_FeatureClassContainer.Classes; IFeatureClass m_FeatureClass = m_EnumFC.Next(); while (m_FeatureClass != null) { liFeaClsNames.Add(m_FeatureClass.AliasName); m_FeatureClass = m_EnumFC.Next(); } return liFeaClsNames; }
3.2 获取所有要素类及要素下的FeatureClass名称
/// <summary> /// 获取GDB MDB工作空间的要素类 /// </summary> /// <param name="workspace"></param> /// <param name="workspaceType"></param> /// <returns></returns> public List<string> GetFeatureClassByWorkspace(IWorkspace workspace) { List<string> listFeaClass = new List<string>(); try { //遍历工作空间下的featureclass IFeatureWorkspace pFeatureWorkspace = workspace as IFeatureWorkspace; IEnumDataset pEnumDatasets = workspace.get_Datasets(esriDatasetType.esriDTAny) as IEnumDataset; IDataset pDataset = pEnumDatasets.Next(); while (pDataset != null) { if (pDataset.Type == esriDatasetType.esriDTFeatureClass) { //string aliasName = pFeatureWorkspace.OpenFeatureClass(pDataset.Name).AliasName; listFeaClass.Add(pDataset.Name); } else if (pDataset.Type == esriDatasetType.esriDTFeatureDataset) { IEnumDataset pESubDataset = pDataset.Subsets; IDataset pSubDataset = pESubDataset.Next(); while (pSubDataset != null) { listFeaClass.Add(pSubDataset.Name); pSubDataset = pESubDataset.Next(); } } pDataset = pEnumDatasets.Next(); } } catch (Exception ex) { return null; } return listFeaClass; }