ArcEngine打开GDB,SHP的方法

1.打开gdb,shp,mdb的方法

  1. 1.打开gdb方法
public List<IDataset> addGdbDataset(){
List<IDataset> dataset = new List<IDataset>();
IWorkspaceFactory pFactory = new FileGDBWorkspaceFactory();
string filePath = "G:\\Tasks\\调查成果.gdb";
IWorkspace pWorkspace = pFactory.OpenFromFile(filePath, 0);
IFeatureWorkspace pFeatWorkspace=pWorkspace 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);
                        listDataSet.Add(pDataset);
                    }
                    else if (pDataset.Type == esriDatasetType.esriDTFeatureDataset)
                    {
                        if (pDataset.Name == "自然资源状况分区")
                        {
                            IEnumDataset pESubDataset = pDataset.Subsets;
                            IDataset pSubDataset = pESubDataset.Next();
                            while (pSubDataset != null)
                            {
                                listFeaClass.Add(pSubDataset.Name);
                                listDataSet.Add(pSubDataset);
                                pSubDataset = pESubDataset.Next();
                            }
                        }
                    }

                    pDataset = pEnumDatasets.Next();
                }
 return listDataSet;}

  1.2打开shp文件

public IFeatureLayer GetShpType()
        {
            OpenFileDialog opfd1 = new OpenFileDialog();
            opfd1.Filter = "shapefile(*.shp)|*.shp|allfile(*.*)|*.*";
            opfd1.Multiselect = false;
            opfd1.InitialDirectory = @"G:\Tasks\06NewTask\shp";
            DialogResult diaLres = opfd1.ShowDialog();
            if (diaLres != DialogResult.OK)
                return null;
            string path1 = opfd1.FileName;
            //openfiledialog 常规使用

            string pFolder = Path.GetDirectoryName(path1);
            string pFileName = Path.GetFileName(path1);
            //System.IO.Path

            IWorkspaceFactory pworkspaceFactory = new ShapefileWorkspaceFactory();
            //创建工作空间工厂

            IWorkspace pWorkspace = pworkspaceFactory.OpenFromFile(pFolder, 0);
            //打开shpfile工作空间

            IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
            IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass(pFileName);
            //打开要素类 

            IFeatureLayer pFLayer = new FeatureLayerClass();
            //创建要素图层

            pFLayer.FeatureClass = pFC;
            pFLayer.Name = pFC.AliasName;
            ILayer pLayer = pFLayer as ILayer;
            return pFLayer;
            //以上三行:关联图层和要素类
          
        }

  

上一篇:ArcEngine 释放对象


下一篇:ArcEngine 数据导出Shape的几种方式