C# +AE加载shp、栅格、地图文档数据

步骤:

1) 创建工作空间工厂

2) 打开shapefile工作空间

3) 打开要素类

4) 创建要素图层

5) 关联图层和要素类

6) 添加到地图空间

具体代码(后面数字为对应的步骤):

              IWorkspaceFactory pWorkspaceFactory=new ShapefileWorkspaceFactory (); // 1

            openFileDialog1.Filter="shaperfile(*.shp)|*.shp";

            openFileDialog1.InitialDirectory=@"E:\test\文档和数据\Data";

            openFileDialog1.Multiselect=false;

            DialogResult pDialogResult=openFileDialog1.ShowDialog ();

            if(pDialogResult !=DialogResult.OK)

                return;

            string pPath=openFileDialog1 .FileName;

            string pFolder=Path.GetDirectoryName (pPath);

            string pFileName=Path.GetFileName(pPath);

            IWorkspace pWorkspace=pWorkspaceFactory .OpenFromFile(pFolder ,0); // 2

            IFeatureWorkspace pFeatureWorkspace =pWorkspace as IFeatureWorkspace ;

            IFeatureClass pFC=pFeatureWorkspace .OpenFeatureClass (pFileName ); //3

            IFeatureLayer pFLayer=new FeatureLayerClass (); // 4

            pFLayer.FeatureClass =pFC;

            pFLayer.Name =pFC.AliasName ; // 5

            ILayer pLayer=pFLayer as ILayer ;

            IMap pMap=axMapControl1.Map ;

            pMap.AddLayer(pLayer); // 6

            axMapControl1.ActiveView .Refresh ();

-----------------------------------------------------------------------------------------------------------

//添加ArcGIS命名空间

using ESRI.ArcGIS.Carto;

using ESRI.ArcGIS.Geometry;

using ESRI.ArcGIS.Geodatabase;

using ESRI.ArcGIS.DataSourcesFile;

using ESRI.ArcGIS.DataSourcesRaster;

//存储打开文件的全路径

         string fullFilePath;

//设置OpenFileDialog的属性,使其能打开多种类型文件

             OpenFileDialog openFile = new OpenFileDialog();

             openFile.Filter = "shape文件(*.shp)|*.shp|";

openFile.Filter +="栅格数据(*.jpg,*.bmp,*.tiff)|*.jpg;*.bmp;*.tiff|";

openFile.Filter +="地图文档(*.mxd,*.mxt,*.jmf)|*.mxd;*.mxt;*.jmf";

             openFile.Title = "打开文件";

          

             try

             {

                 if (openFile.ShowDialog() == DialogResult.OK)

                 {

                  

                     fullFilePath = openFile.FileName;

                     //获得文件路径

                     int index = fullFilePath.LastIndexOf("\\");

                     string filePath = fullFilePath.Substring(0, index);

                     //获得文件名称

                     string fileNam = fullFilePath.Substring(index + 1);

                     //加载shape文件

                     if (openFile.FilterIndex == 1)

                     {

                         //打开工作空间工厂

                         IWorkspaceFactory workspcFac = new ShapefileWorkspaceFactory();

                         IFeatureWorkspace featureWorkspc;

                         IFeatureLayer featureLay = new FeatureLayerClass();

                         //打开路径

                 featureWorkspc = workspcFac.OpenFromFile(filePath, 0) as IFeatureWorkspace;

                         //打开类要素

                         featureLay.FeatureClass = featureWorkspc.OpenFeatureClass(fileNam);

                         //清空图层

                         axMapControl1.ClearLayers();

                         //添加图层

                         axMapControl1.AddLayer(featureLay);

                         axMapControl1.Refresh();

                     }

                         //加载栅格图像

                     else if (openFile.FilterIndex == 2)

                     {

                         IWorkspaceFactory workspcFac = new RasterWorkspaceFactory();

                         IRasterWorkspace rasterWorkspc;

                         IRasterDataset rasterDatst = new RasterDatasetClass();

                         IRasterLayer rasterLay = new RasterLayerClass();

                 rasterWorkspc = workspcFac.OpenFromFile(filePath, 0) as IRasterWorkspace;

                         rasterDatst = rasterWorkspc.OpenRasterDataset(fileNam );

                         rasterLay.CreateFromDataset(rasterDatst);

                         axMapControl1.ClearLayers();

                         axMapControl1.AddLayer(rasterLay);

                         axMapControl1.Refresh();

                     }

                     //加载地图文档

                     else if (openFile.FilterIndex == 3)

                     {

                         IMapDocument mapDoc = new MapDocumentClass();

                         mapDoc.Open(filePath ,"");

                         axMapControl1.ClearLayers();

                         for (int i = 0; i < mapDoc.MapCount - 1; i++)

                         {

                             axMapControl1.Map =mapDoc.get_Map (i);

                         }

                         IActiveView activeViw = axMapControl1.Map as IActiveView;

                         activeViw.Extent = axMapControl1.FullExtent;

                         axMapControl1.Refresh();

                     }

                 }

             }

             catch (Exception ex)

             {

                 MessageBox.Show(ex.Message.ToString ());

             }

         }

     }

你可能感兴趣的:(AE开发积累)