/******************************************/
偶然间整理电脑的文件夹,发现在Esri官网上曾经下载过关于Geodatabase开发的相关代码示例,赶紧跟各位共享一下
开发环境:
说明:该代码适用于ArcGIS Engine初学者,或者对Geodatabase开发感兴趣的朋友,如果你的Engine版本高于9.3.1,可能相关的接口会发生变化,这个需要用户自己来进行修改,但是聪明的用户不会局限于代码的是否允许,也许学习一下人家的接口使用方法,开发模式才是最重要的。
关于版本的接口差别参考:http://help.arcgis.com/en/sdk/10.0/arcobjects_net/conceptualhelp/index.html#/Type_changes_between_9_3_and_10/000100000408000000/
以下代码主要通过两种方法来进行数据加载
1:直接调用GP工具的Copy Feature
2:使用IFeatureDataConverter转换
说明:大家在使用ArcCatalog对数据导入导出应该使用过两种方法
1:Copy /Paste
该方法其实就是调用Copy Feature,这种方法可以直接将数据集里面包含高级对象(拓扑、几何网络、关系类等)都一块进行paste,而且该方法不会对ObjectID进行重排
更正一下
该方法其实就是调用Copy ,这种方法可以直接将数据集里面包含高级对象(拓扑、几何网络、关系类等)都一块进行paste,而且该方法不会对ObjectID进行重排
Copy工具是对整个数据集拷贝
Copy Feature 工具是对要素类拷贝
2:Import/Export
该方法之对要素类进行导入导出,那么就意味着高级对象不会被导入导出,而且ObjectID会重排
所以在使用过程中可以选择不同的方法
第一种方法:
using System; using System.IO; using ESRI.ArcGIS.DataManagementTools; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.Geoprocessor; namespace CopyFeaturesDemo { public class CopyFeaturesDemo { public static void Main(string[] args) { #region Licensing // Set up the licencing. NOTE: This sample assumes that you are using ArcInfo Desktop. // You will need to adjust this code if using ArcEngine or ArcEditor. IAoInitialize AoInitialize = new AoInitializeClass(); esriLicenseStatus licenseStatus = AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo); if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut) { Console.WriteLine("Unable to check-out an ArcInfo license, error code is {0}.", licenseStatus); return; } #endregion #region Data Setup // If any GDB test data is leftover from previous runs, delete it. if (Directory.Exists("LoadTarget.gdb")) { Directory.Delete("LoadTarget.gdb", true); } // Create a new File GDB as a target for the copy. Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"); IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType); workspaceFactory.Create(Environment.CurrentDirectory, "LoadTarget", null, 0); #endregion // Intialize the Geoprocessor. Geoprocessor geoprocessor = new Geoprocessor(); // Set the overwrite output option to true. geoprocessor.OverwriteOutput = true; // Intialize the CopyFeatures tool. CopyFeatures copyFeatures = new CopyFeatures { in_features = Path.Combine(Environment.CurrentDirectory, @"..\..\..\Data\Buildings.shp"), out_feature_class = Path.Combine(Environment.CurrentDirectory, @"LoadTarget.gdb\Buildings_GP") }; // Run the tool. try { geoprocessor.Execute(copyFeatures, null); Console.WriteLine("CopyFeatures completed."); } catch (Exception exc) { Console.WriteLine("CopyFeatures failed."); Console.WriteLine("Exception message: {0}", exc.Message); } finally { // Display the geoprocessor's output. for (int i = 0; i < geoprocessor.MessageCount; i++) { Console.WriteLine(geoprocessor.GetMessage(i)); } } } } }
using System; using System.Runtime.InteropServices; using ESRI.ArcGIS.esriSystem; using ESRI.ArcGIS.Geodatabase; using ESRI.ArcGIS.DataSourcesFile; using ESRI.ArcGIS.DataSourcesGDB; namespace FDConverterDemo { public class FDConverterDemo { public static void Main(string[] args) { #region Licensing // Set up the licencing. NOTE: This sample assumes that you are using ArcInfo Desktop. // You will need to adjust this code if using ArcEngine or ArcEditor. IAoInitialize aoInitialize = new AoInitializeClass(); esriLicenseStatus licenseStatus = aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo); if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut) { Console.WriteLine("Unable to check-out an ArcInfo license, error code is {0}", licenseStatus); return; } #endregion try { // Create a name object for the source workspace. IWorkspaceName sourceWorkspaceName = new WorkspaceNameClass { PathName = @"..\..\..\Data", WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory" }; // Create and open a new Geodatabase for the data. Type targetFactoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory"); IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(targetFactoryType); IWorkspaceName targetWorkspaceName = workspaceFactory.Create(Environment.CurrentDirectory, "LoadTarget", null, 0); // Create a name object for the source feature class. IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass(); IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName; sourceDatasetName.WorkspaceName = sourceWorkspaceName; sourceDatasetName.Name = "Buildings.shp"; // Create a name object for the feature class to be created. IFeatureClassName targetFeatureClassName = new FeatureClassNameClass(); IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName; targetDatasetName.WorkspaceName = targetWorkspaceName; targetDatasetName.Name = "Buildings_FDC"; // Open the source feature class to get field definitions. IName sourceName = (IName)sourceDatasetName; IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open(); // Open the two workspaces for the field validator. IName sourceIName = (IName)sourceWorkspaceName; IName targetIName = (IName)targetWorkspaceName; IWorkspace sourceWorkspace = (IWorkspace)sourceIName.Open(); IWorkspace targetWorkspace = (IWorkspace)targetIName.Open(); // Use a field checker for field validation. IFieldChecker fieldChecker = new FieldCheckerClass(); IFields sourceFields = sourceFeatureClass.Fields; fieldChecker.InputWorkspace = sourceWorkspace; fieldChecker.ValidateWorkspace = targetWorkspace; IEnumFieldError enumFieldError = null; IFields outputFields = null; fieldChecker.Validate(sourceFields, out enumFieldError, out outputFields); // If any field validation errors occurred, they can be viewed at this point. // Get the GeometryDef from the source feature class and modify it. // Note that this only modifies the object in memory, and will not effect the source data. int shapeFieldIndex = sourceFeatureClass.FindField(sourceFeatureClass.ShapeFieldName); IField shapeField = sourceFields.get_Field(shapeFieldIndex); IGeometryDef geometryDef = shapeField.GeometryDef; IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef; geometryDefEdit.GridCount_2 = 1; geometryDefEdit.set_GridSize(0, 20); // Load the feature class. IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass(); IEnumInvalidObject enumInvalidObject = featureDataConverter.ConvertFeatureClass(sourceFeatureClassName, null, null, targetFeatureClassName, geometryDef, outputFields, "", 1000, 0); // If any invalid features were encountered during conversion, they can be // displayed by iterating through the enumInvalidObject enumerator. } catch (COMException comExc) { Console.WriteLine("An error occurred ({0}): {1}", comExc.ErrorCode, comExc.Message); } catch (Exception exc) { Console.WriteLine("An error occurred: {0}", exc.Message); } finally { Console.WriteLine("Done."); } } } }