创建FeatureClass

1.在GeoDatabase中创建要素类,GeoDatabase包括Personal GeoDatabase(Microsoft Office Access数据库)和File GeoDatabase
(ESRI基于文件存放的空间数据库)。
  1 ///<summary>Simple helper to create a featureclass in a geodatabase.在GeoDatabase中创建要素类</summary>
  2 /// 
  3 ///<param name="workspace">An IWorkspace2 interface</param>
  4 ///<param name="featureDataset">An IFeatureDataset interface or Nothing.要素集,在该要素集下面创建要素类,没有则在GeoDatabase根目录下面创建要素类</param>
  5 ///<param name="featureClassName">A System.String that contains the name of the feature class to open or create. Example: "states"</param>
  6 ///<param name="fields">An IFields interface.创建的要素类的字段</param>
  7 ///<param name="CLSID">A UID value or Nothing. Example "esriGeoDatabase.Feature" or Nothing</param>
  8 ///<param name="CLSEXT">A UID value or Nothing (this is the class extension if you want to reference a class extension when creating the feature class).</param>
  9 ///<param name="strConfigKeyword">An empty System.String or RDBMS table string for ArcSDE. Example: "myTable" or ""</param>
 10 ///  
 11 ///<returns>An IFeatureClass interface or a Nothing</returns>
 12 ///  
 13 ///<remarks>
 14 ///  (1) If a 'featureClassName' already exists in the workspace a reference to that feature class 
 15 ///      object will be returned.
 16 ///  (2) If an IFeatureDataset is passed in for the 'featureDataset' argument the feature class
 17 ///      will be created in the dataset. If a Nothing is passed in for the 'featureDataset'
 18 ///      argument the feature class will be created in the workspace.
 19 ///  (3) When creating a feature class in a dataset the spatial reference is inherited 
 20 ///      from the dataset object.
 21 ///  (4) If an IFields interface is supplied for the 'fields' collection it will be used to create the
 22 ///      table. If a Nothing value is supplied for the 'fields' collection, a table will be created using 
 23 ///      default values in the method.
 24 ///  (5) The 'strConfigurationKeyword' parameter allows the application to control the physical layout 
 25 ///      for this table in the underlying RDBMSfor example, in the case of an Oracle database, the 
 26 ///      configuration keyword controls the tablespace in which the table is created, the initial and 
 27 ///     next extents, and other properties. The 'strConfigurationKeywords' for an ArcSDE instance are 
 28 ///      set up by the ArcSDE data administrator, the list of available keywords supported by a workspace 
 29 ///      may be obtained using the IWorkspaceConfiguration interface. For more information on configuration 
 30 ///      keywords, refer to the ArcSDE documentation. When not using an ArcSDE table use an empty 
 31 ///      string (ex: "").
 32 ///</remarks>
 33 public ESRI.ArcGIS.Geodatabase.IFeatureClass CreateFeatureClass(ESRI.ArcGIS.Geodatabase.IWorkspace2 workspace, ESRI.ArcGIS.Geodatabase.IFeatureDataset featureDataset, System.String featureClassName, ESRI.ArcGIS.Geodatabase.IFields fields, ESRI.ArcGIS.esriSystem.UID CLSID, ESRI.ArcGIS.esriSystem.UID CLSEXT, System.String strConfigKeyword)
 34 {
 35   if (featureClassName == "") return null; // name was not passed in 
 36 
 37   ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass;
 38   ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explicit Cast
 39 
 40   if (workspace.get_NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass, featureClassName)) //feature class with that name already exists 
 41   {
 42     featureClass = featureWorkspace.OpenFeatureClass(featureClassName);
 43     return featureClass;
 44   }
 45 
 46   // assign the class id value if not assigned
 47   if (CLSID == null)
 48   {
 49     CLSID = new ESRI.ArcGIS.esriSystem.UIDClass();
 50     CLSID.Value = "esriGeoDatabase.Feature";
 51   }
 52 
 53   ESRI.ArcGIS.Geodatabase.IObjectClassDescription objectClassDescription = new ESRI.ArcGIS.Geodatabase.FeatureClassDescriptionClass();
 54 
 55   // if a fields collection is not passed in then supply our own
 56   if (fields == null)
 57   {
 58     // create the fields using the required fields method
 59     fields = objectClassDescription.RequiredFields;
 60     ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields; // Explicit Cast
 61     ESRI.ArcGIS.Geodatabase.IField field = new ESRI.ArcGIS.Geodatabase.FieldClass();
 62 
 63     // create a user defined text field
 64     ESRI.ArcGIS.Geodatabase.IFieldEdit fieldEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit)field; // Explicit Cast
 65 
 66     // setup field properties
 67     fieldEdit.Name_2 = "SampleField";
 68     fieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString;
 69     fieldEdit.IsNullable_2 = true;
 70     fieldEdit.AliasName_2 = "Sample Field Column";
 71     fieldEdit.DefaultValue_2 = "test";
 72     fieldEdit.Editable_2 = true;
 73     fieldEdit.Length_2 = 100;
 74 
 75     // add field to field collection
 76     fieldsEdit.AddField(field);
 77     fields = (ESRI.ArcGIS.Geodatabase.IFields)fieldsEdit; // Explicit Cast
 78   }
 79 
 80   System.String strShapeField = "";
 81 
 82   // locate the shape field
 83   for (int j = 0; j < fields.FieldCount; j++)
 84   {
 85     if (fields.get_Field(j).Type == ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry)
 86     {
 87       strShapeField = fields.get_Field(j).Name;
 88     }
 89   }
 90 
 91   // Use IFieldChecker to create a validated fields collection.
 92   ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker = new ESRI.ArcGIS.Geodatabase.FieldCheckerClass();
 93   ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError = null;
 94   ESRI.ArcGIS.Geodatabase.IFields validatedFields = null;
 95   fieldChecker.ValidateWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)workspace;
 96   fieldChecker.Validate(fields, out enumFieldError, out validatedFields);
 97 
 98   // The enumFieldError enumerator can be inspected at this point to determine 
 99   // which fields were modified during validation.
100   // finally create and return the feature class
101   if (featureDataset == null)// if no feature dataset passed in, create at the workspace level
102   {
103       featureClass = featureWorkspace.CreateFeatureClass(featureClassName, validatedFields, CLSID, CLSEXT, ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword);
104   }
105   else
106   {
107       featureClass = featureDataset.CreateFeatureClass(featureClassName, validatedFields, CLSID, CLSEXT, ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword);
108   }
109   return featureClass;
110 }


源:http://help.arcgis.com/en/sdk/10.0/arcobjects_net/componenthelp/index.html#/Create_FeatureClass_Snippet/0049000000pz000000/


posted @ 2016-09-04 11:29  道义相勖  Views(704)  Comments(0Edit  收藏  举报