(1条消息) 利用Arcengine 直接加载shp文件

 

AE中shp文件的加载

步骤:

1) 创建工作空间工厂

2) 打开shapefile工作空间

3) 打开要素类

4) 创建要素图层

5) 关联图层和要素类

6) 添加到地图空间

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

  1. new ShapefileWorkspaceFactory (); // 1
  2. openFileDialog1.Filter="shaperfile(*.shp)|*.shp";
  3. openFileDialog1.InitialDirectory=@"E:\test\文档和数据\Data";
  4. openFileDialog1.Multiselect=false;
  5. DialogResult pDialogResult=openFileDialog1.ShowDialog ();
  6. if(pDialogResult !=DialogResult.OK)
  7. return;
  8. string pPath=openFileDialog1 .FileName;
  9. string pFolder=Path.GetDirectoryName (pPath);
  10. string pFileName=Path.GetFileName(pPath);
  11. IWorkspace pWorkspace=pWorkspaceFactory .OpenFromFile(pFolder ,0); // 2
  12. IFeatureWorkspace pFeatureWorkspace =pWorkspace as IFeatureWorkspace ;
  13. IFeatureClass pFC=pFeatureWorkspace .OpenFeatureClass (pFileName ); //3
  14. IFeatureLayer pFLayer=new FeatureLayerClass (); // 4
  15. pFLayer.FeatureClass =pFC;
  16. pFLayer.Name =pFC.AliasName ; // 5
  17. ILayer pLayer=pFLayer as ILayer ;
  18. IMap pMap=axMapControl1.Map ;
  19. pMap.AddLayer(pLayer); // 6
  20. axMapControl1.ActiveView .Refresh ();
  21. -----------------------------------------------------------------------------------------------------------
  22. //添加ArcGIS命名空间
  23. using ESRI.ArcGIS.Carto;
  24. using ESRI.ArcGIS.Geometry;
  25. using ESRI.ArcGIS.Geodatabase;
  26. using ESRI.ArcGIS.DataSourcesFile;
  27. using ESRI.ArcGIS.DataSourcesRaster;
  28. //存储打开文件的全路径
  29. string fullFilePath;
  30. //设置OpenFileDialog的属性,使其能打开多种类型文件
  31. OpenFileDialog penFile = new OpenFileDialog();
  32. openFile.Filter = "shape文件(*.shp)|*.shp|";
  33. openFile.Filter +="栅格数据(*.jpg,*.bmp,*.tiff)|*.jpg;*.bmp;*.tiff|";
  34. openFile.Filter +="地图文档(*.mxd,*.mxt,*.jmf)|*.mxd;*.mxt;*.jmf";
  35. openFile.Title = "打开文件";
  36. try
  37. {
  38. if (openFile.ShowDialog() == DialogResult.OK)
  39. {
  40. fullFilePath = openFile.FileName;
  41. //获得文件路径
  42. int index = fullFilePath.LastIndexOf("\");
  43. string filePath = fullFilePath.Substring(0, index);
  44. //获得文件名称
  45. string fileNam = fullFilePath.Substring(index + 1);
  46. //加载shape文件
  47. if (openFile.FilterIndex == 1)
  48. {
  49. //打开工作空间工厂
  50. IWorkspaceFactory workspcFac = new ShapefileWorkspaceFactory();
  51. IFeatureWorkspace featureWorkspc;
  52. IFeatureLayer featureLay = new FeatureLayerClass();
  53. //打开路径
  54. featureWorkspc = workspcFac.OpenFromFile(filePath, 0) as IFeatureWorkspace;
  55. //打开类要素
  56. featureLay.FeatureClass = featureWorkspc.OpenFeatureClass(fileNam);
  57. //清空图层
  58. axMapControl1.ClearLayers();
  59. //添加图层
  60. axMapControl1.AddLayer(featureLay);
  61. axMapControl1.Refresh();
  62. }
  63. //加载栅格图像
  64. else if (openFile.FilterIndex == 2)
  65. {
  66. IWorkspaceFactory workspcFac = new RasterWorkspaceFactory();
  67. IRasterWorkspace rasterWorkspc;
  68. IRasterDataset rasterDatst = new RasterDatasetClass();
  69. IRasterLayer rasterLay = new RasterLayerClass();
  70. rasterWorkspc = workspcFac.OpenFromFile(filePath, 0) as IRasterWorkspace;
  71. rasterDatst = rasterWorkspc.OpenRasterDataset(fileNam );
  72. rasterLay.CreateFromDataset(rasterDatst);
  73. axMapControl1.ClearLayers();
  74. axMapControl1.AddLayer(rasterLay);
  75. axMapControl1.Refresh();
  76. }
  77. //加载地图文档
  78. else if (openFile.FilterIndex == 3)
  79. {
  80. IMapDocument mapDoc = new MapDocumentClass();
  81. mapDoc.Open(filePath ,"");
  82. axMapControl1.ClearLayers();
  83. for (int i = 0; i < mapDoc.MapCount - 1; i++)
  84. {
  85. axMapControl1.Map =mapDoc.get_Map (i);
  86. }
  87. IActiveView activeViw = axMapControl1.Map as IActiveView;
  88. activeViw.Extent = axMapControl1.FullExtent;
  89. axMapControl1.Refresh();
  90. }
  91. }
  92. }
  93. catch (Exception ex)
  94. {
  95. MessageBox.Show(ex.Message.ToString ());
  96. }
  97. }
  98. }
(0)

相关推荐