进行数据的访问(mxd、shapefile、gdb、mdb、栅格图像)

/// <summary>
        /// 打开地图文档
        /// </summary>

        public string OpenMXD()
        {
            string MxdPath = “”;
            OpenFileDialog OpenMXD = new OpenFileDialog();
            OpenMXD.Title = “打开地图文档”;
            OpenMXD.Filter = “Map Documents (*.mxd)|*.mxd”;
            if (OpenMXD.ShowDialog() == DialogResult.OK)
            {
                MxdPath = OpenMXD.FileName;
                Maplayer.LoadMxFile(MxdPath);
            }
            return MxdPath;
        }

        private void openmxd_Click(object sender, EventArgs e)
        {
            OpenMXD();
        }

        /// <summary>
        /// 添加数据shpfile
        /// </summary>

        private void AddShapeFile()
        {
        
                IWorkspaceFactory pWorkspaceFactory;
                IFeatureWorkspace pFeatureWorkspace;
                IFeatureLayer pFeatureLayer;

                //获取当前路径和文件名
                OpenFileDialog AddShapeFile = new OpenFileDialog();
                AddShapeFile.Title = “打开Shape文件”;
                AddShapeFile.Filter = “Shape文件(*.shp)|*.shp”;
                AddShapeFile.ShowDialog();
                string strFullPath = AddShapeFile.FileName;
                if (strFullPath == “”) return;
                int Index = strFullPath.LastIndexOf(“\”);
                string filePath = strFullPath.Substring(0, Index);
                string fileName = strFullPath.Substring(Index + 1);

                //打开工作空间并添加shp文件
                pWorkspaceFactory = new ShapefileWorkspaceFactory();
              
                pFeatureWorkspace = (IFeatureWorkspace)pWorkspaceFactory.OpenFromFile(filePath, 0);
               
                pFeatureLayer = new FeatureLayer();
              
                pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(fileName);
                pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
                Maplayer.Map.AddLayer(pFeatureLayer);
               
            AddShapeFile.Dispose();
        
        }

        private void openshp_Click(object sender, EventArgs e)
        {
            AddShapeFile();

        }

 

 private void menuAddShp_Click(object sender, EventArgs e)
 {
  //前两行代码用来定义一个工作空间,可以简单理解为将文件输入输出定位到该文件夹下
  IWorkspaceFactory pWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
  IWorkspace pWorkspace = pWorkspaceFactory.OpenFromFile(@”D:\GIS-Data”, 0);
  //定义特征工作空间(其实和工作空间的差别只是AE类的组织的原因,你甚至可以理解成它们是一样的),将工作空间类型强制转化到特征工作空间
  IFeatureWorkspace pFeatureWorkspace = pWorkspace as IFeatureWorkspace;
  //打开特征集,其实特征集仍然只是AE对象组织的一种定义,可以简单理解为就是shape文件中空间和属性数据的转化形式
  IFeatureClass pFC = pFeatureWorkspace.OpenFeatureClass(“continent.shp”);
  IFeatureLayer pFLayer = new FeatureLayerClass();//定义特征图层,就是GIS里面图层的概念
  pFLayer.FeatureClass = pFC; //设置图层的特征集为刚才的特征集,这样就可以将特征集中的数据加载到特征图层对象中
  pFLayer.Name = pFC.AliasName; //设置图层名
  ILayer pLayer = pFLayer as ILayer; //定义图层,并将刚才的特征图层强制转化为图层变量
  //下面两行代码是在地图控件中加载图层
  IMap pMap = axMapControl1.Map;
  pMap.AddLayer(pLayer);
  axMapControl1.ActiveView.Refresh(); //刷新视图,显示加载图层后的结果

 }
        ///// <summary>
        ///// 添加数据gdb
        ///// </summary>

        private void Addgdb()
        {

            IFeatureWorkspace pFeatureWorkspace;
            IFeatureLayer pFeatureLayer;
            IFeatureDataset pFeatureDataset;


            //获取当前路径和文件名
            FolderBrowserDialog addgdb = new FolderBrowserDialog();
            addgdb.SelectedPath = “”;
            addgdb.ShowDialog();
          
            //打开personGeodatabase,并添加图层
            IWorkspaceFactory pFileGDBWorkspaceFactory = new FileGDBWorkspaceFactory();


            //打开工作空间并遍历数据集
            IWorkspace pWorkspace = pFileGDBWorkspaceFactory.OpenFromFile(addgdb.SelectedPath, 0);
            IFeatureWorkspace pFeatWorkspace = pWorkspace as IFeatureWorkspace;
           
            IEnumDataset pEnumDataset = pWorkspace.get_Datasets(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTAny);
            pEnumDataset.Reset();
            IDataset pDataset = pEnumDataset.Next();
          
            //如果数据集是IFeatureDataset,则遍历它下面的子类
            if (pDataset is IFeatureDataset)
            {
                pFeatureWorkspace = (IFeatureWorkspace)pFileGDBWorkspaceFactory.OpenFromFile(addgdb.SelectedPath, 0);
                pFeatureDataset = pFeatureWorkspace.OpenFeatureDataset(pDataset.Name);
                IEnumDataset pEnumDataset1 = pFeatureDataset.Subsets;
                pEnumDataset1.Reset();
                IDataset pDataset1 = pEnumDataset1.Next();

                //如果子类是FeatureClass,则添加到Maplayer中
                if (pDataset1 is IFeatureClass)
                {
                    pFeatureLayer = new FeatureLayerClass();
                    pFeatureLayer.FeatureClass = pFeatureWorkspace.OpenFeatureClass(pDataset1.Name);
                    pFeatureLayer.Name = pFeatureLayer.FeatureClass.AliasName;
                    Maplayer.Map.AddLayer(pFeatureLayer);

                }

                else
                {

                }
            }
            addgdb.Dispose();
   }

       
        private void addgdb_Click(object sender, EventArgs e)
        {
            Addgdb();
        }
       

         ///// <summary>
        ///// 添加栅格数据
        ///// </summary>

        private void Addgrid()
        {
            IWorkspaceFactory pWorkspaceFactory;
            IRasterWorkspace pRasterWorkspace;

            OpenFileDialog addgrid = new OpenFileDialog();
            addgrid.Filter = “栅格数据(*.lyr)|*.jpg;*.bmp;*.tiff”;
            addgrid.Title = “打开栅格数据”;
            addgrid.ShowDialog();

            //获取当前路径和文件名
            string strFullPath = addgrid.FileName;
            if (strFullPath == “”) return;
            int Index = strFullPath.LastIndexOf(“\”);
            string fileName = strFullPath.Substring(Index + 1);
            string filePath = strFullPath.Substring(0, Index);

            pWorkspaceFactory = new RasterWorkspaceFactoryClass();
            pRasterWorkspace = (IRasterWorkspace)pWorkspaceFactory.OpenFromFile(filePath, 0);
            IRasterDataset pRasterDataset = (IRasterDataset)pRasterWorkspace.OpenRasterDataset(fileName);
            IRasterLayer pRasterLayer = new RasterLayerClass();
            pRasterLayer.CreateFromDataset(pRasterDataset);
            Maplayer.Map.AddLayer(pRasterLayer);
          addgrid.Dispose();

        }
        private void sgrid_Click(object sender, EventArgs e)
        {
           Addgrid();
        }
       
        ///// <summary>
        ///// 添加数据mdb
        ///// </summary>

        private void addmdb_Click(object sender, EventArgs e)
        {
            try
            {
                OpenFileDialog Openmdb = new OpenFileDialog();
                Openmdb.Filter = “数据库(MDB)|*.mdb”;
                DialogResult DialogR = Openmdb.ShowDialog();

                if (DialogR == DialogResult.OK)
                {
                    // textBox1.Text = OpenFile.FileName;

                    TreeNode fristNode = treeView1.Nodes.Add(Openmdb.FileName);

                    IWorkspaceFactory pWsFt = new AccessWorkspaceFactoryClass();
                    IWorkspace pWs = pWsFt.OpenFromFile(Openmdb.FileName, 0);
                    IFeatureWorkspace pFWS = pWs as IFeatureWorkspace;
                    IEnumDataset pEDataset = pWs.get_Datasets(esriDatasetType.esriDTAny);
                    IDataset pDataset = pEDataset.Next();
                    while (pDataset != null)
                    {

                        LoadFeatureClass(pDataset, fristNode);
                        pDataset = pEDataset.Next();
                    }
                    fristNode.Expand();
                }

                Openmdb.Dispose();
            }

            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);


            }
          
        }
        private void LoadFeatureClass(IDataset pSubDataset, TreeNode CurrentNode)
        {

            if (pSubDataset.Type == esriDatasetType.esriDTFeatureDataset)
            {

                IFeatureDataset pFDataSet = (IFeatureDataset)pSubDataset;

                TreeNode SubNode = CurrentNode.Nodes.Add(pSubDataset.Name);

                IEnumDataset pEDataset = pFDataSet.Subsets;

                IDataset pDataset = pEDataset.Next();

                while (pDataset != null)
                {

                    LoadFeatureClass(pDataset, SubNode);

                    pDataset = pEDataset.Next();

                }

            }

            else
            {
                CurrentNode.Nodes.Add(pSubDataset.Name);

            }

        }

转载自:https://blog.csdn.net/niusi1288/article/details/53456077

You may also like...