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 ());

             }

         }

     }

  • 0
    点赞
  • 12
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值