ArcGIS Engine开发Geodatabase代码(二)——DataAccess and Creation

时间: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:创建FileGeodatabase

2:创建要素类

3:创建域对象

4:创建子类

5:创建要素


  1. using System;  
  2. using System.IO;  
  3. using System.Runtime.InteropServices;  
  4. using ESRI.ArcGIS.DataSourcesGDB;  
  5. using ESRI.ArcGIS.esriSystem;  
  6. using ESRI.ArcGIS.Geodatabase;  
  7. using ESRI.ArcGIS.Geometry;  
  8.   
  9. namespace DataAccessDemo  
  10. {  
  11.     public class DataAccessDemo  
  12.     {  
  13.         public static void Main(string[] args)  
  14.         {  
  15.             #region Licensing  
  16.             // Set up the licencing. NOTE: This sample assumes that you are using ArcInfo Desktop.  
  17.             // You will need to adjust this code if using ArcEngine or ArcEditor.  
  18.             IAoInitialize aoInitialize = new AoInitializeClass();  
  19.             esriLicenseStatus licenseStatus = aoInitialize.Initialize(esriLicenseProductCode.esriLicenseProductCodeArcInfo);  
  20.             if (licenseStatus != esriLicenseStatus.esriLicenseCheckedOut)  
  21.             {  
  22.                 Console.WriteLine("Unable to check-out an ArcInfo license, error code is {0}", licenseStatus);  
  23.                 return;  
  24.             }  
  25.             #endregion  
  26.   
  27.             // If existing data is leftover from a previous run, delete it.  
  28.             if (Directory.Exists("DataAccessDemo.gdb"))  
  29.             {  
  30.                 Directory.Delete("DataAccessDemo.gdb"true);  
  31.             }  
  32.   
  33.             // The first demo, CreateFileGdbDemo, will create a new File geodatabase and return  
  34.             // an IWorkspace reference for the second demo to use.  
  35.             IWorkspace workspace = CreateFileGdbDemo();  
  36.   
  37.             // The second demo, CreateFeatureClassDemo, will create and return a new feature class.  
  38.             IFeatureClass featureClass = CreateFeatureClassDemo(workspace);  
  39.   
  40.             // The third demo, CreateDomainsDemo, will create several domains in the workspace.  
  41.             CreateDomainsDemo(workspace);  
  42.   
  43.             // The fourth demo, CreateSubtypesDemo, will create subtypes in the feature class created earlier.  
  44.             IWorkspaceDomains workspaceDomains = (IWorkspaceDomains)workspace;  
  45.             CreateSubtypesDemo(workspaceDomains, featureClass);  
  46.   
  47.             // The fifth demo, CreateFeatureDemo, will create a single feature in the feature class.  
  48.             CreateFeatureDemo(workspace, featureClass);  
  49.   
  50.             // Shutdown the application licensing.  
  51.             aoInitialize.Shutdown();  
  52.         }  
  53.   
  54.         /// <summary>  
  55.         /// This sample creates a new File Geodatabase in the working directory.  
  56.         /// </summary>  
  57.         /// <returns>An IWorkspace reference to the newly-created geodatabase.</returns>  
  58.         private static IWorkspace CreateFileGdbDemo()  
  59.         {  
  60.             // Instantiate a File GDB workspace factory and use it to create a new File GDB in the  
  61.             // section's Data directory.  
  62.             Type factoryType = Type.GetTypeFromProgID("esriDataSourcesGDB.FileGDBWorkspaceFactory");  
  63.             IWorkspaceFactory workspaceFactory = (IWorkspaceFactory)Activator.CreateInstance(factoryType);  
  64.             IWorkspaceName workspaceName = workspaceFactory.Create(Environment.CurrentDirectory, "DataAccessDemo"null, 0);  
  65.   
  66.             // We can open the workspace using the name object.  
  67.             IName name = (IName)workspaceName;  
  68.             IWorkspace workspace = (IWorkspace)name.Open();  
  69.   
  70.             // Display the path of the new workspace.  
  71.             Console.WriteLine("Path of the new File GDB: {0}", workspace.PathName);  
  72.   
  73.             return workspace;  
  74.         }  
  75.   
  76.         /// <summary>  
  77.         /// This sample creates a new feature dataset and feature class.  
  78.         /// </summary>  
  79.         /// <param name="workspace">The workspace to create the feature dataset and feature class in.</param>  
  80.         /// <returns>An IFeatureClass reference to the newly-created feature class.</returns>  
  81.         private static IFeatureClass CreateFeatureClassDemo(IWorkspace workspace)  
  82.         {  
  83.             // To create new datasets in a workspace, the IFeatureWorkspace interface is required.  
  84.             IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspace;  
  85.  
  86.             #region Create a spatial reference for the feature dataset.  
  87.             // Create a spatial reference for the feature dataset.  
  88.             ISpatialReferenceFactory spatialRefFactory = new SpatialReferenceEnvironmentClass();  
  89.             ISpatialReference spatialReference = spatialRefFactory.CreateProjectedCoordinateSystem  
  90.                 ((int)esriSRProjCSType.esriSRProjCS_NAD1983UTM_20N);  
  91.   
  92.             // Determine whether the workspace supports high-precision storage.  
  93.             Boolean supportsHighPrecision = false;  
  94.             IWorkspaceProperties workspaceProperties = (IWorkspaceProperties)workspace;  
  95.             IWorkspaceProperty workspaceProperty = workspaceProperties.get_Property  
  96.                 (esriWorkspacePropertyGroupType.esriWorkspacePropertyGroup,  
  97.                 (int)esriWorkspacePropertyType.esriWorkspacePropSupportsHighPrecisionStorage);  
  98.             if (workspaceProperty.IsSupported)  
  99.             {  
  100.                 supportsHighPrecision = Convert.ToBoolean(workspaceProperty.PropertyValue);  
  101.             }  
  102.   
  103.             // Set the precision of the spatial reference.  
  104.             IControlPrecision2 controlPrecision = (IControlPrecision2)spatialReference;  
  105.             controlPrecision.IsHighPrecision = supportsHighPrecision;  
  106.               
  107.             // Set the resolution and tolerance of the spatial reference.  
  108.             ISpatialReferenceResolution spatialRefResolution = (ISpatialReferenceResolution)spatialReference;  
  109.             spatialRefResolution.ConstructFromHorizon();  
  110.             spatialRefResolution.SetDefaultXYResolution();  
  111.             ISpatialReferenceTolerance spatialRefTolerance = (ISpatialReferenceTolerance)spatialReference;  
  112.             spatialRefTolerance.SetDefaultXYTolerance();  
  113.             #endregion  
  114.   
  115.             // Create the feature dataset.  
  116.             IFeatureDataset featureDataset = featureWorkspace.CreateFeatureDataset("DemoDataset", spatialReference);  
  117.   
  118.             // Use a feature class description object to get the required fields for a  
  119.             // new feature class.  
  120.             IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass();  
  121.             IObjectClassDescription ocDescription = (IObjectClassDescription)fcDescription;  
  122.             IFields fields = ocDescription.RequiredFields;  
  123.             IFieldsEdit fieldsEdit = (IFieldsEdit)fields;  
  124.   
  125.             // By default, the GeometryDef provided by a feature class description specifies that  
  126.             // a feature class should contain polygon features. Change it to polylines.  
  127.             int shapeFieldIndex = fields.FindField(fcDescription.ShapeFieldName);  
  128.             IField shapeField = fields.get_Field(shapeFieldIndex);  
  129.             IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)shapeField.GeometryDef;  
  130.             geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;  
  131.   
  132.             // If the workspace doesn't support high-precision storage, change the precision of the  
  133.             // GeometryDef's spatial reference.  
  134.             if (!supportsHighPrecision)  
  135.             {  
  136.                 IControlPrecision2 geoDefPrecision = (IControlPrecision2)geometryDefEdit.SpatialReference;  
  137.                 geoDefPrecision.IsHighPrecision = false;  
  138.             }  
  139.   
  140.             // Add a small integer field called "PipeType" to the fields collection.  
  141.             IField field = new FieldClass();  
  142.             IFieldEdit fieldEdit = (IFieldEdit)field;  
  143.             fieldEdit.Name_2 = "PipeType";  
  144.             fieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger;  
  145.             fieldsEdit.AddField(field);  
  146.  
  147.             #region Add some more fields...  
  148.             // Add a small integer field called "Material" to the fields collection.  
  149.             field = new FieldClass();  
  150.             fieldEdit = (IFieldEdit)field;  
  151.             fieldEdit.Name_2 = "Material";  
  152.             fieldEdit.AliasName_2 = "Pipe Material";  
  153.             fieldEdit.Type_2 = esriFieldType.esriFieldTypeSmallInteger;  
  154.             fieldEdit.IsNullable_2 = true;  
  155.             fieldsEdit.AddField(field);  
  156.   
  157.             // Add a small integer field called "Diameter" to the fields collection.  
  158.             field = new FieldClass();  
  159.             fieldEdit = (IFieldEdit)field;  
  160.             fieldEdit.Name_2 = "Diameter";  
  161.             fieldEdit.AliasName_2 = "Pipe Diameter";  
  162.             fieldEdit.Type_2 = esriFieldType.esriFieldTypeDouble;  
  163.             fieldEdit.IsNullable_2 = true;  
  164.             fieldsEdit.AddField(field);  
  165.   
  166.             // Add a string field called "InstBy" to the fields collection.  
  167.             field = new FieldClass();  
  168.             fieldEdit = (IFieldEdit)field;  
  169.             fieldEdit.Name_2 = "InstBy";  
  170.             fieldEdit.AliasName_2 = "Installed By";  
  171.             fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;  
  172.             fieldEdit.Length_2 = 75;  
  173.             fieldEdit.IsNullable_2 = true;  
  174.             fieldEdit.DefaultValue_2 = "K. Johnston";  
  175.             fieldsEdit.AddField(field);  
  176.             #endregion  
  177.   
  178.             // Create the feature class using the fields collection that was just created.  
  179.             IFeatureClass featureClass = featureDataset.CreateFeatureClass("DemoClass", fields,  
  180.                 ocDescription.InstanceCLSID, null, esriFeatureType.esriFTSimple, fcDescription.ShapeFieldName, String.Empty);  
  181.   
  182.             // Return the newly-created feature class.  
  183.             return featureClass;  
  184.         }  
  185.   
  186.         /// <summary>  
  187.         /// This sample creates several domains in the specified workspace.  
  188.         /// </summary>  
  189.         /// <param name="workspace">The workspace to create the domains in.</param>  
  190.         private static void CreateDomainsDemo(IWorkspace workspace)  
  191.         {  
  192.             // The IWorkspaceDomains interface is required to add domains to a workspace.  
  193.             IWorkspaceDomains workspaceDomains = (IWorkspaceDomains)workspace;  
  194.   
  195.             // Create a new coded value domain for primary pipe materials.  
  196.             ICodedValueDomain primaryMaterialCVDomain = new CodedValueDomainClass();  
  197.             IDomain primaryMaterialDomain = (IDomain)primaryMaterialCVDomain;  
  198.             primaryMaterialCVDomain.AddCode(1, "Copper");  
  199.             primaryMaterialCVDomain.AddCode(2, "Steel");  
  200.             primaryMaterialCVDomain.AddCode(3, "Concrete");  
  201.             primaryMaterialDomain.Name = "PrimMats";  
  202.             primaryMaterialDomain.Description = "Valid materials for primary pipes.";  
  203.             primaryMaterialDomain.FieldType = esriFieldType.esriFieldTypeSmallInteger;  
  204.             primaryMaterialDomain.MergePolicy = esriMergePolicyType.esriMPTDefaultValue;  
  205.             primaryMaterialDomain.SplitPolicy = esriSplitPolicyType.esriSPTDuplicate;  
  206.             workspaceDomains.AddDomain(primaryMaterialDomain);  
  207.   
  208.             // Create a new range domain for primary pipe diameters.  
  209.             IRangeDomain primaryDiameterRangeDomain = new RangeDomainClass();  
  210.             IDomain primaryDiameterDomain = (IDomain)primaryDiameterRangeDomain;  
  211.             primaryDiameterRangeDomain.MaxValue = 10;  
  212.             primaryDiameterRangeDomain.MinValue = 2.5;  
  213.             primaryDiameterDomain.Name = "PrimDiam";  
  214.             primaryDiameterDomain.Description = "Valid diameters (in inches) for primary pipes.";  
  215.             primaryDiameterDomain.FieldType = esriFieldType.esriFieldTypeDouble;  
  216.             primaryDiameterDomain.MergePolicy = esriMergePolicyType.esriMPTDefaultValue;  
  217.             primaryDiameterDomain.SplitPolicy = esriSplitPolicyType.esriSPTDuplicate;  
  218.             workspaceDomains.AddDomain(primaryDiameterDomain);  
  219.  
  220.             #region Add secondary pipe domains...  
  221.             // Create a new coded value domain for secondary pipe materials.  
  222.             ICodedValueDomain secondaryMaterialCVDomain = new CodedValueDomainClass();  
  223.             IDomain secondaryMaterialDomain = (IDomain)secondaryMaterialCVDomain;  
  224.             secondaryMaterialCVDomain.AddCode(1, "Copper");  
  225.             secondaryMaterialCVDomain.AddCode(2, "Steel");  
  226.             secondaryMaterialCVDomain.AddCode(3, "PVC");  
  227.             secondaryMaterialDomain.Name = "SecMats";  
  228.             secondaryMaterialDomain.Description = "Valid materials for secondary pipes.";  
  229.             secondaryMaterialDomain.FieldType = esriFieldType.esriFieldTypeSmallInteger;  
  230.             secondaryMaterialDomain.MergePolicy = esriMergePolicyType.esriMPTDefaultValue;  
  231.             secondaryMaterialDomain.SplitPolicy = esriSplitPolicyType.esriSPTDuplicate;  
  232.             workspaceDomains.AddDomain(secondaryMaterialDomain);  
  233.   
  234.             // Create a new range domain for secondary pipe diameters.  
  235.             IRangeDomain secondaryDiameterRangeDomain = new RangeDomainClass();  
  236.             IDomain secondaryDiameterDomain = (IDomain)secondaryDiameterRangeDomain;  
  237.             secondaryDiameterRangeDomain.MaxValue = 7.5;  
  238.             secondaryDiameterRangeDomain.MinValue = 1;  
  239.             secondaryDiameterDomain.Name = "SecDiam";  
  240.             secondaryDiameterDomain.Description = "Valid diameters (in inches) for secondary pipes.";  
  241.             secondaryDiameterDomain.FieldType = esriFieldType.esriFieldTypeDouble;  
  242.             secondaryDiameterDomain.MergePolicy = esriMergePolicyType.esriMPTDefaultValue;  
  243.             secondaryDiameterDomain.SplitPolicy = esriSplitPolicyType.esriSPTDuplicate;  
  244.             workspaceDomains.AddDomain(secondaryDiameterDomain);  
  245.             #endregion  
  246.         }  
  247.   
  248.         /// <summary>  
  249.         /// This sample creates subtypes in the specified feature class. The fields of the subtypes are  
  250.         /// assigned domains from the workspace.  
  251.         /// </summary>  
  252.         /// <param name="workspaceDomains">The workspace of the feature class.</param>  
  253.         /// <param name="featureClass">The feature class to create subtypes in.</param>  
  254.         private static void CreateSubtypesDemo(IWorkspaceDomains workspaceDomains, IFeatureClass featureClass)  
  255.         {  
  256.             // Cast the feature class to the ISchemaLock interface, because we are making a  
  257.             // schema change in an existing dataset.  
  258.             ISchemaLock schemaLock = (ISchemaLock)featureClass;  
  259.             try  
  260.             {  
  261.                 // Attempt to acquire an exclusive lock on the feature class. This will raise an  
  262.                 // exception if it fails.  
  263.                 schemaLock.ChangeSchemaLock(esriSchemaLock.esriExclusiveSchemaLock);  
  264.   
  265.                 // Cast the feature class to the ISubtypes interface and setup the subtype field.  
  266.                 ISubtypes subtypes = (ISubtypes)featureClass;  
  267.                 subtypes.SubtypeFieldName = "PipeType";  
  268.                 subtypes.AddSubtype(1, "Primary");  
  269.                 subtypes.AddSubtype(2, "Secondary");  
  270.                 subtypes.DefaultSubtypeCode = 1;  
  271.   
  272.                 // Setup the default values and domains for primary pipes.  
  273.                 subtypes.set_DefaultValue(1, "Diameter", 10);  
  274.                 subtypes.set_DefaultValue(1, "Material", 2);  
  275.                 subtypes.set_Domain(1, "Diameter", workspaceDomains.get_DomainByName("PrimDiam"));  
  276.                 subtypes.set_Domain(1, "Material", workspaceDomains.get_DomainByName("PrimMats"));  
  277.   
  278.                 // Setup the default values and domains for secondary pipes.  
  279.                 subtypes.set_DefaultValue(2, "Diameter", 7.5);  
  280.                 subtypes.set_DefaultValue(2, "Material", 2);  
  281.                 subtypes.set_Domain(2, "Diameter", workspaceDomains.get_DomainByName("SecDiam"));  
  282.                 subtypes.set_Domain(2, "Material", workspaceDomains.get_DomainByName("SecMats"));  
  283.             }  
  284.             catch (COMException comExc)  
  285.             {  
  286.                 Console.WriteLine("An error occurred while attempting to add subtypes to the feature class.");  
  287.                 Console.WriteLine("{0} ({1})", comExc.Message, comExc.ErrorCode);  
  288.             }  
  289.             finally  
  290.             {  
  291.                 // Regardless of what happened, make sure the schema lock is set to a shared lock.  
  292.                 schemaLock.ChangeSchemaLock(esriSchemaLock.esriSharedSchemaLock);  
  293.             }  
  294.         }  
  295.   
  296.         /// <summary>  
  297.         /// This sample creates a new feature in the provided feature class, setting the subtype field  
  298.         /// and the subtype's default values.  
  299.         /// </summary>  
  300.         /// <param name="workspace">The workspace containing the feature class.</param>  
  301.         /// <param name="featureClass">The feature class to create the feature in.</param>  
  302.         public static void CreateFeatureDemo(IWorkspace workspace, IFeatureClass featureClass)  
  303.         {  
  304.             // Create a shape for the new feature.  
  305.             IPoint fromPoint = new PointClass { X = 500000, Y = 0 };  
  306.             IPoint toPoint = new PointClass { X = 500050, Y = -50 };  
  307.             IPolyline polyline = new PolylineClass { FromPoint = fromPoint, ToPoint = toPoint };  
  308.   
  309.             // Get the indexes of the fields to edit in the feature class.  
  310.             int pipeTypeIndex = featureClass.FindField("PipeType");  
  311.             int materialIndex = featureClass.FindField("Material");  
  312.             int diameterIndex = featureClass.FindField("Diameter");  
  313.             int instByIndex = featureClass.FindField("InstBy");  
  314.   
  315.             // Start an edit session and an edit operation.  
  316.             IWorkspaceEdit workspaceEdit = (IWorkspaceEdit)workspace;  
  317.             workspaceEdit.StartEditing(true);  
  318.             workspaceEdit.StartEditOperation();  
  319.   
  320.             // Create a new feature and set its shape.  
  321.             IFeature feature = featureClass.CreateFeature();  
  322.             feature.Shape = polyline;  
  323.   
  324.             // Use the IRowSubtypes interface to set the subtype code and the subtype default values.  
  325.             IRowSubtypes rowSubtypes = (IRowSubtypes)feature;  
  326.             rowSubtypes.SubtypeCode = 1;  
  327.   
  328.             // Display the attribute values of the feature at this point. Note that the  
  329.             // subtype defaults have not been applied by setting the subtype code.  
  330.             Console.WriteLine("OID: {0}, PipeType: {1}, Material: {2}, Diameter: {3}, InstBy: {4}",  
  331.                 feature.OID, feature.get_Value(pipeTypeIndex), feature.get_Value(materialIndex),  
  332.                 feature.get_Value(diameterIndex), feature.get_Value(instByIndex));  
  333.   
  334.             // Apply the default subtype values.  
  335.             rowSubtypes.InitDefaultValues();  
  336.   
  337.             // Display the feature's values, now set to the subtype defaults.  
  338.             Console.WriteLine("OID: {0}, PipeType: {1}, Material: {2}, Diameter: {3}, InstBy: {4}",  
  339.                 feature.OID, feature.get_Value(pipeTypeIndex), feature.get_Value(materialIndex),  
  340.                 feature.get_Value(diameterIndex), feature.get_Value(instByIndex));  
  341.   
  342.             // Set the attribute values that need to be changed.  
  343.             feature.set_Value(instByIndex, "B. Pierce");  
  344.   
  345.             // Display the feature's new values.  
  346.             Console.WriteLine("OID: {0}, PipeType: {1}, Material: {2}, Diameter: {3}, InstBy: {4}",  
  347.                 feature.OID, feature.get_Value(pipeTypeIndex), feature.get_Value(materialIndex),  
  348.                 feature.get_Value(diameterIndex), feature.get_Value(instByIndex));  
  349.   
  350.             // Store the feature.  
  351.             feature.Store();  
  352.   
  353.             // Stop the edit operation and edit session.  
  354.             workspaceEdit.StopEditOperation();  
  355.             workspaceEdit.StopEditing(true);  
  356.         }  
  357.     }  
  358. }  

标签: arcgis二次开发 arcgis Geodatabase

WRITTEN BY

avatar