ArcGIS Engine开发Geodatabase代码(三)—— Data Loading

时间:2013-4-25    作者:悬浮的青春    分类:


/**/

  ESRI Developer Summit 2009

 
Developer's Guide to the Geodatabase

  Code Samples

 
6 April 2009

/**/

开发环境:

  • ArcGIS Engine9.3/9.3.1
  • VS2008

关于版本的接口差别参考: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 ,这种方法可以直接将数据集里面包含高级对象(拓扑、几何网络、关系类等)都一块进行paste,而且该方法不会对ObjectID进行重排

Copy工具是对整个数据集拷贝

Copy Feature 工具是对要素类拷贝


2:Import/Export

该方法之对要素类进行导入导出,那么就意味着高级对象不会被导入导出,而且ObjectID会重排

所以在使用过程中可以选择不同的方法


第一种方法:


  1. using System;  
  2. using System.IO;  
  3. using ESRI.ArcGIS.DataManagementTools;  
  4. using ESRI.ArcGIS.esriSystem;  
  5. using ESRI.ArcGIS.Geodatabase;  
  6. using ESRI.ArcGIS.Geoprocessor;  
  7.   
  8. namespace CopyFeaturesDemo    
  9. {  
  10.     public class CopyFeaturesDemo  
  11.     {  
  12.         public static void Main(string[] args)  
  13.         {  
  14.             #region Licensing  
  15.       // Set up the licencing. NOTE: This sample assumes that you are using ArcInfo Desktop.  
  16.       // You will need to adjust this code if using ArcEngine or ArcEditor.  
  17.       IAoInitialize AoInitialize = new AoInitializeClass();  
  18.       esriLicenseStatus licenseStatus = AoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);  
  19.       if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)  
  20.       {  
  21.         Console.WriteLine("Unable to check-out an ArcInfo license, error code is {0}.", licenseStatus);  
  22.         return;  
  23.       }  
  24.       #endregion  
  25.  
  26.             #region Data Setup  
  27.             // If any GDB test data is leftover from previous runs, delete it.  
  28.             if (Directory.Exists("LoadTarget.gdb"))  
  29.             {  
  30.                 Directory.Delete("LoadTarget.gdb"true);  
  31.             }  
  32.   
  33.             // Create a new File GDB as a target for the copy.  
  34.             Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");  
  35.             IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);  
  36.             workspaceFactory.Create(Environment.CurrentDirectory, "LoadTarget"null, 0);  
  37.             #endregion  
  38.   
  39.             // Intialize the Geoprocessor.  
  40.             Geoprocessor geoprocessor = new Geoprocessor();  
  41.   
  42.             // Set the overwrite output option to true.  
  43.             geoprocessor.OverwriteOutput = true;  
  44.   
  45.              // Intialize the CopyFeatures tool.  
  46.              CopyFeatures copyFeatures = new CopyFeatures  
  47.             {  
  48.                 in_features = Path.Combine(Environment.CurrentDirectory, @"..\..\..\Data\Buildings.shp"),  
  49.                 out_feature_class = Path.Combine(Environment.CurrentDirectory, @"LoadTarget.gdb\Buildings_GP")  
  50.             };  
  51.   
  52.             // Run the tool.  
  53.             try  
  54.             {  
  55.                 geoprocessor.Execute(copyFeatures, null);  
  56.                 Console.WriteLine("CopyFeatures completed.");  
  57.             }  
  58.             catch (Exception exc)  
  59.             {  
  60.                 Console.WriteLine("CopyFeatures failed.");  
  61.                 Console.WriteLine("Exception message: {0}", exc.Message);  
  62.             }  
  63.             finally  
  64.             {  
  65.                 // Display the geoprocessor's output.  
  66.                 for (int i = 0; i < geoprocessor.MessageCount; i++)  
  67.                 {  
  68.                     Console.WriteLine(geoprocessor.GetMessage(i));  
  69.                 }  
  70.             }  
  71.     }  
  72.     }  
  73. }  




第二种方法


  1. using System;  
  2. using System.Runtime.InteropServices;  
  3. using ESRI.ArcGIS.esriSystem;  
  4. using ESRI.ArcGIS.Geodatabase;  
  5. using ESRI.ArcGIS.DataSourcesFile;  
  6. using ESRI.ArcGIS.DataSourcesGDB;  
  7.   
  8. namespace FDConverterDemo  
  9. {  
  10.     public class FDConverterDemo  
  11.     {  
  12.         public static void Main(string[] args)  
  13.         {  
  14.                     #region Licensing  
  15.                     // Set up the licencing. NOTE: This sample assumes that you are using ArcInfo Desktop.  
  16.                     // You will need to adjust this code if using ArcEngine or ArcEditor.  
  17.                     IAoInitialize aoInitialize = new AoInitializeClass();  
  18.                     esriLicenseStatus licenseStatus = aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);  
  19.                     if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)  
  20.                     {  
  21.                         Console.WriteLine("Unable to check-out an ArcInfo license, error code is {0}", licenseStatus);  
  22.                         return;  
  23.                     }  
  24.                     #endregion  
  25.   
  26.           try  
  27.           {  
  28.                         // Create a name object for the source workspace.  
  29.                         IWorkspaceName sourceWorkspaceName = new WorkspaceNameClass  
  30.                         {  
  31.                             PathName = @"..\..\..\Data",  
  32.                             WorkspaceFactoryProgID = "esriDataSourcesFile.ShapefileWorkspaceFactory"  
  33.                         };  
  34.   
  35.                         // Create and open a new Geodatabase for the data.  
  36.                         Type targetFactoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");  
  37.                         IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(targetFactoryType);  
  38.                         IWorkspaceName targetWorkspaceName = workspaceFactory.Create(Environment.CurrentDirectory, "LoadTarget"null, 0);  
  39.               
  40.                           // Create a name object for the source feature class.  
  41.                         IFeatureClassName sourceFeatureClassName = new FeatureClassNameClass();  
  42.                         IDatasetName sourceDatasetName = (IDatasetName)sourceFeatureClassName;  
  43.                          sourceDatasetName.WorkspaceName = sourceWorkspaceName;  
  44.                          sourceDatasetName.Name = "Buildings.shp";  
  45.   
  46.                         // Create a name object for the feature class to be created.  
  47.                         IFeatureClassName targetFeatureClassName = new FeatureClassNameClass();  
  48.                         IDatasetName targetDatasetName = (IDatasetName)targetFeatureClassName;  
  49.                         targetDatasetName.WorkspaceName = targetWorkspaceName;  
  50.                         targetDatasetName.Name = "Buildings_FDC";  
  51.               
  52.                         // Open the source feature class to get field definitions.  
  53.                         IName sourceName = (IName)sourceDatasetName;  
  54.                         IFeatureClass sourceFeatureClass = (IFeatureClass)sourceName.Open();  
  55.   
  56.                         // Open the two workspaces for the field validator.  
  57.                         IName sourceIName = (IName)sourceWorkspaceName;  
  58.                         IName targetIName = (IName)targetWorkspaceName;  
  59.                         IWorkspace sourceWorkspace = (IWorkspace)sourceIName.Open();  
  60.                         IWorkspace targetWorkspace = (IWorkspace)targetIName.Open();  
  61.   
  62.                         // Use a field checker for field validation.  
  63.                         IFieldChecker fieldChecker = new FieldCheckerClass();  
  64.                         IFields sourceFields = sourceFeatureClass.Fields;  
  65.                         fieldChecker.InputWorkspace = sourceWorkspace;  
  66.                         fieldChecker.ValidateWorkspace = targetWorkspace;  
  67.                         IEnumFieldError enumFieldError = null;  
  68.                         IFields outputFields = null;  
  69.                         fieldChecker.Validate(sourceFields, out enumFieldError, out outputFields);  
  70.   
  71.                         // If any field validation errors occurred, they can be viewed at this point.  
  72.   
  73.                         // Get the GeometryDef from the source feature class and modify it.  
  74.                         // Note that this only modifies the object in memory, and will not effect the source data.  
  75.                         int shapeFieldIndex = sourceFeatureClass.FindField(sourceFeatureClass.ShapeFieldName);  
  76.                         IField shapeField = sourceFields.get_Field(shapeFieldIndex);  
  77.                         IGeometryDef geometryDef = shapeField.GeometryDef;  
  78.                         IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;  
  79.                         geometryDefEdit.GridCount_2 = 1;  
  80.                         geometryDefEdit.set_GridSize(0, 20);  
  81.   
  82.                         // Load the feature class.  
  83.                         IFeatureDataConverter featureDataConverter = new FeatureDataConverterClass();  
  84.                         IEnumInvalidObject enumInvalidObject = featureDataConverter.ConvertFeatureClass(sourceFeatureClassName,   
  85.                             nullnull, targetFeatureClassName, geometryDef, outputFields, "", 1000, 0);  
  86.   
  87.                         // If any invalid features were encountered during conversion, they can be  
  88.                         // displayed by iterating through the enumInvalidObject enumerator.  
  89.           }  
  90.           catch (COMException comExc)  
  91.           {  
  92.                         Console.WriteLine("An error occurred ({0}): {1}", comExc.ErrorCode, comExc.Message);  
  93.           }  
  94.           catch (Exception exc)  
  95.           {  
  96.                         Console.WriteLine("An error occurred: {0}", exc.Message);  
  97.           }  
  98.           finally  
  99.           {  
  100.                         Console.WriteLine("Done.");  
  101.           }  
  102.       }  
  103.     }  
  104. }  

标签: arcgis二次开发 arcgis Geodatabase

WRITTEN BY

avatar