C#+ArcEngine 不同格式数据打开,对象创建,导入导出

一:打开类

  1. public class clsOpenClass
  2. {
  3. public static void OpenFeatureClass(AxMapControl MapControl,
  4. IFeatureClassName pFcName, ListView listview1)
  5. {
  6. try
  7. {
  8. MapControl.Map.ClearLayers();
  9. MapControl.SpatialReference = null;
  10. IName pName = pFcName as IName;
  11. IFeatureClass pFc = pName.Open() as IFeatureClass;
  12. listview1.Items.Clear();
  13. listview1.Columns.Clear();
  14. LoadListView(pFc, listview1);
  15. IFeatureCursor pCursor = pFc.Search(null, false);
  16. IFeature pfea = pCursor.NextFeature();
  17. int j = 0;
  18. while (pfea != null)
  19. {
  20. ListViewItem lv = new ListViewItem();
  21. for (int i = 0; i < pfea.Fields.FieldCount; i++)
  22. {
  23. string sFieldName = pfea.Fields.get_Field(i).Name;
  24. lv.SubItems.Add(FeatureHelper.GetFeatureValue(pfea, sFieldName).ToString());
  25. }
  26. lv.Tag = pfea;
  27. if (j % 2 == 0)
  28. {
  29. lv.BackColor = System.Drawing.Color.GreenYellow;
  30. }
  31. listview1.Items.Add(lv);
  32. pfea = pCursor.NextFeature();
  33. j++;
  34. }
  35. LSGISHelper.OtherHelper.ReleaseObject(pCursor);
  36. //最后加载图形数据
  37. if (pFcName.FeatureType == esriFeatureType.esriFTRasterCatalogItem)
  38. {
  39. ESRI.ArcGIS.Carto.IGdbRasterCatalogLayer pGdbRCLayer = new ESRI.ArcGIS.Carto.GdbRasterCatalogLayerClass();
  40. pGdbRCLayer.Setup(pFc as ITable);
  41. MapControl.Map.AddLayer(pGdbRCLayer as ILayer);
  42. }
  43. else if( (pFcName.FeatureType == esriFeatureType.esriFTSimple)||
  44. (pFcName.FeatureType == esriFeatureType.esriFTComplexEdge)||
  45. (pFcName.FeatureType == esriFeatureType.esriFTComplexJunction)||
  46. (pFcName.FeatureType == esriFeatureType.esriFTSimpleEdge)||
  47. (pFcName.FeatureType == esriFeatureType.esriFTSimpleJunction))
  48. {
  49. IFeatureLayer pLayer = new FeatureLayerClass();
  50. pLayer.FeatureClass = pFc;
  51. pLayer.Name = (pFc as IDataset).Name;
  52. MapControl.Map.AddLayer(pLayer as ILayer);
  53. }
  54. else if (pFcName.FeatureType == esriFeatureType.esriFTAnnotation)
  55. {
  56. ILayer pLayer = OpenAnnotationLayer(pFc);
  57. pLayer.Name = (pFc as IDataset).Name;
  58. MapControl.Map.AddLayer(pLayer as ILayer);
  59. }
  60. MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
  61. }
  62. catch (Exception ex)
  63. { }
  64. }
  65. public static void OpenRasterDataset(AxMapControl MapControl,
  66. IRasterDatasetName pRdName, ListView listview1)
  67. {
  68. MapControl.ClearLayers();
  69. MapControl.SpatialReference = null;
  70. listview1.Items.Clear();
  71. listview1.Columns.Clear();
  72. IDatasetName pDsName = pRdName as IDatasetName;
  73. string sName = pDsName.Name;
  74. IName pName = pRdName as IName;
  75. IRasterDataset pRds = pName.Open() as IRasterDataset;
  76. IRasterLayer pRL = new RasterLayerClass();
  77. pRL.CreateFromDataset(pRds);
  78. pRL.Name = sName;
  79. MapControl.AddLayer(pRL as ILayer);
  80. MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
  81. }
  82. public static void OpenMosaicDataset(AxMapControl MapControl,
  83. IMosaicDatasetName pMdName, ListView listview1)
  84. {
  85. MapControl.ClearLayers();
  86. MapControl.SpatialReference = null;
  87. listview1.Items.Clear();
  88. listview1.Columns.Clear();
  89. IDatasetName pDsName = pMdName as IDatasetName;
  90. string sName = pDsName.Name;
  91. IName pName = pMdName as IName;
  92. IMosaicDataset pMds = pName.Open() as IMosaicDataset;
  93. IFeatureClass pFc = pMds.Catalog;
  94. listview1.Items.Clear();
  95. listview1.Columns.Clear();
  96. LoadListView(pFc, listview1);
  97. IFeatureCursor pCursor = pFc.Search(null, false);
  98. IFeature pfea = pCursor.NextFeature();
  99. int j = 0;
  100. while (pfea != null)
  101. {
  102. ListViewItem lv = new ListViewItem();
  103. for (int i = 0; i < pfea.Fields.FieldCount; i++)
  104. {
  105. string sFieldName = pfea.Fields.get_Field(i).Name;
  106. lv.SubItems.Add(FeatureHelper.GetFeatureValue(pfea, sFieldName).ToString());
  107. }
  108. lv.Tag = pfea;
  109. if (j % 2 == 0)
  110. {
  111. lv.BackColor = System.Drawing.Color.GreenYellow;
  112. }
  113. listview1.Items.Add(lv);
  114. pfea = pCursor.NextFeature();
  115. j++;
  116. }
  117. LSGISHelper.OtherHelper.ReleaseObject(pCursor);
  118. IMosaicLayer pML = new MosaicLayerClass();
  119. pML.CreateFromMosaicDataset(pMds);
  120. MapControl.AddLayer(pML as ILayer);
  121. MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewGeography, null, null);
  122. }
  123. public static void OpenTable(AxMapControl MapControl,
  124. ITableName pTName, ListView listview1)
  125. {
  126. try
  127. {
  128. MapControl.Map.ClearLayers();
  129. MapControl.SpatialReference = null;
  130. MapControl.ActiveView.PartialRefresh(esriViewDrawPhase.esriViewAll, null, null);
  131. IName pName = pTName as IName;
  132. ITable pFc = pName.Open() as ITable;
  133. listview1.Items.Clear();
  134. listview1.Columns.Clear();
  135. LoadListView(pFc, listview1);
  136. ICursor pCursor = pFc.Search(null, false);
  137. IRow pfea = pCursor.NextRow();
  138. int j = 0;
  139. while (pfea != null)
  140. {
  141. ListViewItem lv = new ListViewItem();
  142. for (int i = 0; i < pfea.Fields.FieldCount; i++)
  143. {
  144. string sFieldName = pfea.Fields.get_Field(i).Name;
  145. lv.SubItems.Add(FeatureHelper.GetRowValue(pfea, sFieldName).ToString());
  146. }
  147. lv.Tag = pfea;
  148. if (j % 2 == 0)
  149. {
  150. lv.BackColor = System.Drawing.Color.GreenYellow;
  151. }
  152. listview1.Items.Add(lv);
  153. pfea = pCursor.NextRow();
  154. j++;
  155. }
  156. LSGISHelper.OtherHelper.ReleaseObject(pCursor);
  157. }
  158. catch { }
  159. }
  160. public static void LoadListView(IFeatureClass pFC, ListView listView1)
  161. {
  162. try
  163. {
  164. listView1.Columns.Clear();
  165. //添加一个空
  166. ColumnHeader columnHeader = new ColumnHeader();
  167. listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
  168. columnHeader
  169. });
  170. columnHeader.Text = "";
  171. for (int i = 0; i < pFC.Fields.FieldCount; i++)
  172. {
  173. ColumnHeader columnHeader1 = new ColumnHeader();
  174. listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
  175. columnHeader1
  176. });
  177. IFields pFields = pFC.Fields;
  178. IField pField = pFields.get_Field(i);
  179. columnHeader1.Text = pField.AliasName;
  180. }
  181. }
  182. catch (Exception ex)
  183. { }
  184. }
  185. public static void LoadListView(ITable pFC, ListView listView1)
  186. {
  187. try
  188. {
  189. listView1.Columns.Clear();
  190. //添加一个空
  191. ColumnHeader columnHeader = new ColumnHeader();
  192. listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
  193. columnHeader
  194. });
  195. columnHeader.Text = "";
  196. for (int i = 0; i < pFC.Fields.FieldCount; i++)
  197. {
  198. ColumnHeader columnHeader1 = new ColumnHeader();
  199. listView1.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
  200. columnHeader1
  201. });
  202. IFields pFields = pFC.Fields;
  203. IField pField = pFields.get_Field(i);
  204. columnHeader1.Text = pField.AliasName;
  205. }
  206. }
  207. catch (Exception ex)
  208. { }
  209. }
  210. public static ILayer OpenAnnotationLayer(IFeatureClass pfc)
  211. {
  212. IFDOGraphicsLayerFactory pfdof = new FDOGraphicsLayerFactoryClass();
  213. IFeatureDataset pFDS=pfc.FeatureDataset;
  214. IWorkspace pWS = pFDS.Workspace;
  215. IFeatureWorkspace pFWS=pWS as IFeatureWorkspace;
  216. ILayer pLayer = pfdof.OpenGraphicsLayer(pFWS, pFDS, (pfc as IDataset).Name );
  217. return pLayer;
  218. }
  219. }

二:创建类

  1. public class clsCreateClass
  2. {
  3. public IFeatureDataset CreateDataset(IWorkspace pWorkspace)
  4. {
  5. try
  6. {
  7. if (pWorkspace == null) return null;
  8. IFeatureWorkspace aFeaWorkspace = pWorkspace as IFeatureWorkspace;
  9. if (aFeaWorkspace == null) return null;
  10. DatasetPropertiesForm aForm = new DatasetPropertiesForm();
  11. aForm.HignPrecision = LSGISHelper.WorkspaceHelper.HighPrecision(pWorkspace);
  12. if (aForm.ShowDialog() == DialogResult.OK)
  13. {
  14. string dsName = aForm.FeatureDatasetName;
  15. ISpatialReference aSR = aForm.SpatialReference;
  16. IFeatureDataset aDS = aFeaWorkspace.CreateFeatureDataset(dsName, aSR);
  17. return aDS;
  18. }
  19. }
  20. catch (Exception ex) { }
  21. return null;
  22. }
  23. public IRasterDataset CreateRasterDataset(IWorkspace pWorkspace,string sName
  24. )
  25. {
  26. try
  27. {
  28. IRasterWorkspaceEx pRWEx = pWorkspace as IRasterWorkspaceEx;
  29. IGeometryDef pGDef=new GeometryDefClass ();
  30. IRasterDataset pRD = pRWEx.CreateRasterDataset(
  31. sName, 3, rstPixelType.PT_CHAR, null, null, null, null);
  32. }
  33. catch { }
  34. return null;
  35. }
  36. public IFeatureClass CreateFeatureClass(IWorkspace pWorkspace)
  37. {
  38. if (pWorkspace == null) return null;
  39. IFeatureWorkspace aFeaWorkspace = pWorkspace as IFeatureWorkspace;
  40. if (aFeaWorkspace == null) return null;
  41. IFeatureClass aClass = null;
  42. FeatureClassWizard aForm = new FeatureClassWizard();
  43. aForm.Workspace = pWorkspace;
  44. if (aForm.ShowDialog() == DialogResult.OK)
  45. {
  46. while (true)
  47. {
  48. string className = aForm.FeatureClassName;
  49. string aliasName = aForm.FeatureClassAliasName;
  50. IFields flds = aForm.Fields;
  51. try
  52. {
  53. aClass = aFeaWorkspace.CreateFeatureClass(className, flds
  54. , null, null, esriFeatureType.esriFTSimple, "SHAPE", null);
  55. if (!aliasName.Equals(""))
  56. {
  57. IClassSchemaEdit aClassEdit = aClass as IClassSchemaEdit;
  58. if (aClassEdit != null) aClassEdit.AlterAliasName(aliasName);
  59. }
  60. break;
  61. }
  62. catch (Exception ex)
  63. {
  64. //MessageBox.Show ("错误:\n"+ex.Message ,"新建特性类",
  65. // MessageBoxButtons.OK ,MessageBoxIcon.Error );
  66. LSCommonHelper.MessageBoxHelper.ShowErrorMessageBox(ex, "");
  67. }
  68. aForm = new FeatureClassWizard();
  69. aForm.Workspace = pWorkspace;
  70. aForm.FeatureClassName = className;
  71. aForm.FeatureClassAliasName = aliasName;
  72. aForm.Fields = flds;
  73. if (aForm.ShowDialog() == DialogResult.Cancel) break;
  74. }
  75. }
  76. return aClass;
  77. }
  78. public IFeatureClass CreateFeatureClass(IFeatureDataset pDS)
  79. {
  80. if (pDS == null) return null;
  81. IFeatureClass aClass = null;
  82. FeatureClassWizard aForm = new FeatureClassWizard();
  83. aForm.Workspace = (pDS as IDataset).Workspace;
  84. IGeoDataset pGDS = pDS as IGeoDataset;
  85. if (pGDS != null)
  86. {
  87. aForm.SpatialReference = pGDS.SpatialReference;
  88. }
  89. if (aForm.ShowDialog() == DialogResult.OK)
  90. {
  91. while (true)
  92. {
  93. string className = aForm.FeatureClassName;
  94. string aliasName = aForm.FeatureClassAliasName;
  95. IFields flds = aForm.Fields;
  96. try
  97. {
  98. aClass = pDS.CreateFeatureClass(className, flds
  99. , null, null, esriFeatureType.esriFTSimple, "SHAPE", null);
  100. if (!aliasName.Equals(""))
  101. {
  102. IClassSchemaEdit aClassEdit = aClass as IClassSchemaEdit;
  103. if (aClassEdit != null) aClassEdit.AlterAliasName(aliasName);
  104. }
  105. break;
  106. }
  107. catch (Exception ex)
  108. {
  109. LSCommonHelper.MessageBoxHelper.ShowErrorMessageBox(ex, "请选择高精度坐标系");
  110. }
  111. aForm = new FeatureClassWizard();
  112. aForm.Workspace = (pDS as IDataset).Workspace;
  113. aForm.FeatureClassName = className;
  114. aForm.FeatureClassAliasName = aliasName;
  115. aForm.Fields = flds;
  116. if (aForm.ShowDialog() == DialogResult.Cancel) break;
  117. }
  118. }
  119. return aClass;
  120. }
  121. public ITable CreateTable(IWorkspace pWorkspace)
  122. {
  123. if (pWorkspace == null) return null;
  124. IFeatureWorkspace aFeaWorkspace = pWorkspace as IFeatureWorkspace;
  125. if (aFeaWorkspace == null) return null;
  126. ITable aTable = null;
  127. DataTableWizard aWizard = new DataTableWizard();
  128. aWizard.Workspace = pWorkspace;
  129. if (aWizard.ShowDialog() == DialogResult.OK)
  130. {
  131. while (true)
  132. {
  133. string tableName = aWizard.TableName;
  134. string aliasName = aWizard.TableAliasName;
  135. IFields flds = aWizard.Fields;
  136. try
  137. {
  138. aTable = aFeaWorkspace.CreateTable(tableName, flds
  139. , null, null, null);
  140. if (!aliasName.Equals(""))
  141. {
  142. IClassSchemaEdit aClassEdit = aTable as IClassSchemaEdit;
  143. aClassEdit.RegisterAsObjectClass("OBJECTID", null);
  144. if (aClassEdit != null) aClassEdit.AlterAliasName(aliasName);
  145. }
  146. break;
  147. }
  148. catch (Exception ex)
  149. {
  150. //MessageBox.Show ("错误:\n"+ex.Message ,"新建表",
  151. // MessageBoxButtons.OK ,MessageBoxIcon.Error );
  152. LSCommonHelper.MessageBoxHelper.ShowErrorMessageBox(ex, "");
  153. }
  154. aWizard = new DataTableWizard();
  155. aWizard.Workspace = pWorkspace;
  156. aWizard.TableName = tableName;
  157. aWizard.TableAliasName = aliasName;
  158. aWizard.Fields = flds;
  159. if (aWizard.ShowDialog() == DialogResult.Cancel) break;
  160. }
  161. }
  162. return aTable;
  163. }
  164. }

三:导出类

  1. public class clsExportClass
  2. {
  3. ///
  4. /// 导出FeatureClass到Shapefile文件
  5. ///
  6. ///
  7. public static bool ExportFeatureClassToShp(string sPath,IFeatureClass apFeatureClass)
  8. {
  9. try
  10. {
  11. string ExportFileShortName = System.IO.Path.GetFileNameWithoutExtension(sPath);
  12. if (ExportFileShortName == "")
  13. {
  14. ExportFileShortName =LSCommonHelper.OtherHelper.GetRightName( (apFeatureClass as IDataset).Name,".");
  15. }
  16. string ExportFilePath = System.IO.Path.GetDirectoryName(sPath);
  17. if (ExportFilePath == null)
  18. {
  19. ExportFilePath = sPath;
  20. }
  21. //设置导出要素类的参数
  22. IFeatureClassName pOutFeatureClassName = new FeatureClassNameClass();
  23. IDataset pOutDataset = (IDataset)apFeatureClass;
  24. pOutFeatureClassName = (IFeatureClassName)pOutDataset.FullName;
  25. //创建一个输出shp文件的工作空间
  26. IWorkspaceFactory pShpWorkspaceFactory = new ShapefileWorkspaceFactoryClass();
  27. IWorkspaceName pInWorkspaceName = new WorkspaceNameClass();
  28. pInWorkspaceName = pShpWorkspaceFactory.Create(ExportFilePath, ExportFileShortName, null, 0);
  29. //创建一个要素集合
  30. IFeatureDatasetName pInFeatureDatasetName = null;
  31. //创建一个要素类
  32. IFeatureClassName pInFeatureClassName = new FeatureClassNameClass();
  33. IDatasetName pInDatasetClassName;
  34. pInDatasetClassName = (IDatasetName)pInFeatureClassName;
  35. pInDatasetClassName.Name = ExportFileShortName;//作为输出参数
  36. pInDatasetClassName.WorkspaceName = pInWorkspaceName;
  37. //通过FIELDCHECKER检查字段的合法性,为输出SHP获得字段集合
  38. long iCounter;
  39. IFields pOutFields, pInFields;
  40. IField pGeoField;
  41. IEnumFieldError pEnumFieldError = null;
  42. pInFields = apFeatureClass.Fields;
  43. IFieldChecker pFieldChecker = new FieldChecker();
  44. pFieldChecker.Validate(pInFields, out pEnumFieldError, out pOutFields);
  45. //通过循环查找几何字段
  46. pGeoField = null;
  47. for (iCounter = 0; iCounter < pOutFields.FieldCount; iCounter++)
  48. {
  49. if (pOutFields.get_Field((int)iCounter).Type == esriFieldType.esriFieldTypeGeometry)
  50. {
  51. pGeoField = pOutFields.get_Field((int)iCounter);
  52. break;
  53. }
  54. }
  55. //得到几何字段的几何定义
  56. IGeometryDef pOutGeometryDef;
  57. IGeometryDefEdit pOutGeometryDefEdit;
  58. pOutGeometryDef = pGeoField.GeometryDef;
  59. //设置几何字段的空间参考和网格
  60. pOutGeometryDefEdit = (IGeometryDefEdit)pOutGeometryDef;
  61. pOutGeometryDefEdit.GridCount_2 = 1;
  62. pOutGeometryDefEdit.set_GridSize(0, 1500000);
  63. //开始导入
  64. IFeatureDataConverter pShpToClsConverter = new FeatureDataConverterClass();
  65. pShpToClsConverter.ConvertFeatureClass(pOutFeatureClassName, null, pInFeatureDatasetName, pInFeatureClassName, pOutGeometryDef, pOutFields, "", 1000, 0);
  66. return true;
  67. }
  68. catch (Exception ex)
  69. {
  70. return false;
  71. }
  72. }
  73. public static void ExportFeatureClass2Shapefile(IFeatureClassName pFcName)
  74. {
  75. IName pName = pFcName as IName;
  76. IFeatureClass pFc = pName.Open() as IFeatureClass;
  77. SaveFileDialog ofd = new SaveFileDialog();
  78. ofd.Filter = "Shapefile文件(.shp)|*.shp";
  79. if (ofd.ShowDialog() == DialogResult.OK)
  80. {
  81. string sPath = ofd.FileName;
  82. if (ExportFeatureClassToShp(sPath, pFc))
  83. {
  84. LSCommonHelper.MessageBoxHelper.ShowMessageBox("导出成功");
  85. }
  86. LSCommonHelper.MessageBoxHelper.ShowMessageBox("导出失败");
  87. }
  88. }
  89. public static bool ConvertFeatureDataset(IWorkspace sourceWorkspace, IWorkspace targetWorkspace,
  90. string nameOfSourceFeatureDataset, string nameOfTargetFeatureDataset)
  91. {
  92. try
  93. {
  94. //create source workspace name
  95. IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;
  96. IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName;
  97. //create source dataset name
  98. IFeatureDatasetName sourceFeatureDatasetName = new FeatureDatasetNameClass();
  99. IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureDatasetName;
  100. sourceDatasetName.WorkspaceName = sourceWorkspaceName;
  101. sourceDatasetName.Name = nameOfSourceFeatureDataset;
  102. //create target workspace name
  103. IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
  104. IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName;
  105. //create target dataset name
  106. IFeatureDatasetName targetFeatureDatasetName = new FeatureDatasetNameClass();
  107. IDatasetName targetDatasetName = (IDatasetName)targetFeatureDatasetName;
  108. targetDatasetName.WorkspaceName = targetWorkspaceName;
  109. targetDatasetName.Name = nameOfTargetFeatureDataset;
  110. //Convert feature dataset
  111. IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass();
  112. featureDataConverter.ConvertFeatureDataset(sourceFeatureDatasetName, targetFeatureDatasetName, null, "", 1000, 0);
  113. return true;
  114. }
  115. catch (Exception ex)
  116. { return false; }
  117. }
  118. public static void ExportFeatureDataset2GDB(IDatasetName pDSName,int flag)
  119. {
  120. FolderBrowserDialog fbd = new FolderBrowserDialog();
  121. fbd.Description = "选择保存路径";
  122. if (fbd.ShowDialog() == DialogResult.OK)
  123. {
  124. string sPath = fbd.SelectedPath;
  125. string sTemplate = "";
  126. if (flag == 0)
  127. {
  128. sTemplate = Application.StartupPath + @"\template\pgdb.mdb";
  129. File.Copy(sTemplate, sPath + "\\pgdb.mdb");
  130. }
  131. else
  132. {
  133. sTemplate = Application.StartupPath + @"\template\fgdb.gdb";
  134. FileHelper.CopyDir(sTemplate, sPath+"\\fgdb.gdb");
  135. }
  136. IName pName = pDSName as IName;
  137. string sSrcDSName = pDSName.Name;
  138. sSrcDSName = LSCommonHelper.OtherHelper.GetRightName(sSrcDSName, ".");
  139. IDataset pDS = pName.Open() as IDataset;
  140. IWorkspace pSrcWS = pDS.Workspace;
  141. IWorkspace pDesWS = null;
  142. if (flag == 0)
  143. {
  144. pDesWS = LSGISHelper.WorkspaceHelper.GetAccessWorkspace(sPath + "\\pgdb.mdb");
  145. }
  146. else
  147. {
  148. pDesWS = LSGISHelper.WorkspaceHelper.GetFGDBWorkspace(sPath + "\\fgdb.gdb");
  149. }
  150. if (ConvertFeatureDataset(pSrcWS, pDesWS, sSrcDSName, sSrcDSName))
  151. {
  152. LSCommonHelper.MessageBoxHelper.ShowMessageBox("导出成功");
  153. }
  154. else
  155. {
  156. LSCommonHelper.MessageBoxHelper.ShowMessageBox("导出失败");
  157. }
  158. }
  159. }
  160. public static void ExportFeatureDataset2Shapefile(IDatasetName pDSName,
  161. TaskMonitor mTaskMonitor)
  162. {
  163. FolderBrowserDialog fbd = new FolderBrowserDialog();
  164. fbd.Description = "选择保存路径";
  165. if (fbd.ShowDialog() == DialogResult.OK)
  166. {
  167. string sPath = fbd.SelectedPath;
  168. IName pName = pDSName as IName;
  169. IDataset pDS = pName.Open() as IDataset;
  170. IFeatureDataset pFDS = pDS as IFeatureDataset;
  171. IFeatureClassContainer pFCC = pFDS as IFeatureClassContainer;
  172. IFeatureClass pfc = null;
  173. mTaskMonitor.EnterWaitState();
  174. for (int i = 0; i < pFCC.ClassCount; i++)
  175. {
  176. pfc = pFCC.get_Class(i);
  177. mTaskMonitor.TaskCaption = "正在导出第"+(i+1)+"个"+pfc.AliasName+"图层,共"+pFCC.ClassCount+"个";
  178. mTaskMonitor.TaskProgress = LSCommonHelper.MathHelper.Precent(
  179. 0, pFCC.ClassCount, i);
  180. if (ExportFeatureClassToShp(sPath, pfc))
  181. { }
  182. }
  183. mTaskMonitor.ExitWaitState();
  184. LSCommonHelper.MessageBoxHelper.ShowMessageBox("导出完毕");
  185. }
  186. }
  187. }

四:导入类

  1. public class clsImportClass
  2. {
  3. public static void ImportGDB2SDE(IWorkspace pDesWS, int flag)
  4. {
  5. IWorkspace pSrcWS = null;
  6. try
  7. {
  8. if (flag == 0)
  9. {
  10. OpenFileDialog ofd = new OpenFileDialog();
  11. ofd.Filter = "PGDB文件(.mdb)|*.mdb";
  12. ofd.Multiselect = false;
  13. if (ofd.ShowDialog() == DialogResult.OK)
  14. {
  15. string sFileName = ofd.FileName;
  16. pSrcWS = LSGISHelper.WorkspaceHelper.GetAccessWorkspace(sFileName);
  17. }
  18. }
  19. else
  20. {
  21. FolderBrowserDialog fdb = new FolderBrowserDialog();
  22. if (fdb.ShowDialog() == DialogResult.OK)
  23. {
  24. string sFileName = fdb.SelectedPath;
  25. pSrcWS = LSGISHelper.WorkspaceHelper.GetFGDBWorkspace(sFileName);
  26. }
  27. }
  28. if (pSrcWS != null)
  29. {
  30. IEnumDatasetName pEnumDSName = pSrcWS.get_DatasetNames(esriDatasetType.esriDTFeatureDataset);
  31. IDatasetName pDName = pEnumDSName.Next();
  32. while (pDName != null)
  33. {
  34. clsExportClass.ConvertFeatureDataset(pSrcWS, pDesWS, pDName.Name, pDName.Name);
  35. pDName = pEnumDSName.Next();
  36. }
  37. LSCommonHelper.MessageBoxHelper.ShowMessageBox("导入成功");
  38. }
  39. }
  40. catch { }
  41. }
  42. public static void ImportShapefile2SDE(IWorkspace pDesWS, TaskMonitor mTaskMonitor,
  43. IFeatureDatasetName pFDN)
  44. {
  45. OpenFileDialog ofd = new OpenFileDialog();
  46. ofd.Title = "打开SHP数据";
  47. ofd.Filter = "SHP数据(*.shp)|*.shp";
  48. ofd.Multiselect = true;
  49. ofd.RestoreDirectory = true;
  50. if (ofd.ShowDialog() == DialogResult.OK)
  51. {
  52. string[] sFileNames = ofd.FileNames;
  53. string sFileName = "";
  54. IFeatureClass pFC = null;
  55. mTaskMonitor.EnterWaitState();
  56. string sName = "";
  57. IWorkspace pSrcWS=null;
  58. for (int i = 0; i < sFileNames.Length; i++)
  59. {
  60. mTaskMonitor.TaskCaption = "共" + sFileNames.Length + "个文件,先处理第" + (i + 1) + "个文件";
  61. mTaskMonitor.TaskProgress = LSCommonHelper.MathHelper.Precent(0, sFileNames.Length, i);
  62. sFileName = sFileNames[i].ToString();
  63. pSrcWS=LSGISHelper.WorkspaceHelper.GetShapefileWorkspace(sFileName);
  64. sFileName = System.IO.Path.GetFileNameWithoutExtension(sFileName);
  65. IFeatureWorkspace pFWS = pSrcWS as IFeatureWorkspace;
  66. pFC = pFWS.OpenFeatureClass(sFileName);
  67. sName = (pFC as IDataset).Name;
  68. if (ConvertFeatureClass2FeatureDataset(pSrcWS, pDesWS, sName, sName, pFDN))
  69. { }
  70. }
  71. mTaskMonitor.ExitWaitState();
  72. LSCommonHelper.MessageBoxHelper.ShowMessageBox("导入成功");
  73. }
  74. }
  75. public static bool ConvertFeatureClass2FeatureDataset(IWorkspace sourceWorkspace,
  76. IWorkspace targetWorkspace, string nameOfSourceFeatureClass,
  77. string nameOfTargetFeatureClass, IFeatureDatasetName pName)
  78. {
  79. try
  80. {
  81. //create source workspace name
  82. IDataset sourceWorkspaceDataset = (IDataset)sourceWorkspace;
  83. IWorkspaceName sourceWorkspaceName = (IWorkspaceName)sourceWorkspaceDataset.FullName;
  84. //create source dataset name
  85. IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();
  86. IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName;
  87. sourceDatasetName.WorkspaceName = sourceWorkspaceName;
  88. sourceDatasetName.Name = nameOfSourceFeatureClass;
  89. //create target workspace name
  90. IDataset targetWorkspaceDataset = (IDataset)targetWorkspace;
  91. IWorkspaceName targetWorkspaceName = (IWorkspaceName)targetWorkspaceDataset.FullName;
  92. //create target dataset name
  93. IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();
  94. IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName;
  95. targetDatasetName.WorkspaceName = targetWorkspaceName;
  96. targetDatasetName.Name = nameOfTargetFeatureClass;
  97. //Open input Featureclass to get field definitions.
  98. ESRI.ArcGIS.esriSystem.IName sourceName = (ESRI.ArcGIS.esriSystem.IName)sourceFeatureClassName;
  99. IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();
  100. //Validate the field names because you are converting between different workspace types.
  101. IFieldChecker fieldChecker = new FieldCheckerClass();
  102. IFields targetFeatureClassFields;
  103. IFields sourceFeatureClassFields = sourceFeatureClass.Fields;
  104. IEnumFieldError enumFieldError;
  105. // Most importantly set the input and validate workspaces!
  106. fieldChecker.InputWorkspace = sourceWorkspace;
  107. fieldChecker.ValidateWorkspace = targetWorkspace;
  108. fieldChecker.Validate(sourceFeatureClassFields, out enumFieldError,
  109. out targetFeatureClassFields);
  110. // Loop through the output fields to find the geomerty field
  111. IField geometryField;
  112. for (int i = 0; i < targetFeatureClassFields.FieldCount; i++)
  113. {
  114. if (targetFeatureClassFields.get_Field(i).Type == esriFieldType.esriFieldTypeGeometry)
  115. {
  116. geometryField = targetFeatureClassFields.get_Field(i);
  117. // Get the geometry field's geometry defenition
  118. IGeometryDef geometryDef = geometryField.GeometryDef;
  119. //Give the geometry definition a spatial index grid count and grid size
  120. IGeometryDefEdit targetFCGeoDefEdit = (IGeometryDefEdit)geometryDef;
  121. targetFCGeoDefEdit.GridCount_2 = 1;
  122. targetFCGeoDefEdit.set_GridSize(0, 0);
  123. //Allow ArcGIS to determine a valid grid size for the data loaded
  124. targetFCGeoDefEdit.SpatialReference_2 = geometryField.GeometryDef.SpatialReference;
  125. // we want to convert all of the features
  126. IQueryFilter queryFilter = new QueryFilterClass();
  127. queryFilter.WhereClause = "";
  128. // Load the feature class
  129. IFeatureDataConverter fctofc = new FeatureDataConverterClass();
  130. IEnumInvalidObject enumErrors = fctofc.ConvertFeatureClass(sourceFeatureClassName,
  131. queryFilter, pName, targetFeatureClassName,
  132. geometryDef, targetFeatureClassFields, "", 1000, 0);
  133. break;
  134. }
  135. }
  136. return true;
  137. }
  138. catch (Exception ex) { return false; }
  139. }
  140. }

你可能感兴趣的:(C#+ArcEngine 不同格式数据打开,对象创建,导入导出)