ArcObjects Library Reference

Create FeatureClass Snippet

Simple helper to create a featureclass in a geodatabase.

[C#]

///<summary>Simple helper to create a featureclass in a geodatabase.</summary>
/// 
///<param name="workspace">An IWorkspace2 interface</param>
///<param name="featureDataset">An IFeatureDataset interface or Nothing</param>
///<param name="featureClassName">A System.String that contains the name of the feature class to open or create. Example: "states"</param>
///<param name="fields">An IFields interface</param>
///<param name="CLSID">A UID value or Nothing. Example "esriGeoDatabase.Feature" or Nothing</param>
///<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>
///<param name="strConfigKeyword">An empty System.String or RDBMS table string for ArcSDE. Example: "myTable" or ""</param>
///  
///<returns>An IFeatureClass interface or a Nothing</returns>
///  
///<remarks>
///  (1) If a 'featureClassName' already exists in the workspace a reference to that feature class 
///      object will be returned.
///  (2) If an IFeatureDataset is passed in for the 'featureDataset' argument the feature class
///      will be created in the dataset. If a Nothing is passed in for the 'featureDataset'
///      argument the feature class will be created in the workspace.
///  (3) When creating a feature class in a dataset the spatial reference is inherited 
///      from the dataset object.
///  (4) If an IFields interface is supplied for the 'fields' collection it will be used to create the
///      table. If a Nothing value is supplied for the 'fields' collection, a table will be created using 
///      default values in the method.
///  (5) The 'strConfigurationKeyword' parameter allows the application to control the physical layout 
///      for this table in the underlying RDBMSfor example, in the case of an Oracle database, the 
///      configuration keyword controls the tablespace in which the table is created, the initial and 
///     next extents, and other properties. The 'strConfigurationKeywords' for an ArcSDE instance are 
///      set up by the ArcSDE data administrator, the list of available keywords supported by a workspace 
///      may be obtained using the IWorkspaceConfiguration interface. For more information on configuration 
///      keywords, refer to the ArcSDE documentation. When not using an ArcSDE table use an empty 
///      string (ex: "").
///</remarks>
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)
{
  if (featureClassName == "") return null; // name was not passed in 

  ESRI.ArcGIS.Geodatabase.IFeatureClass featureClass;
  ESRI.ArcGIS.Geodatabase.IFeatureWorkspace featureWorkspace = (ESRI.ArcGIS.Geodatabase.IFeatureWorkspace)workspace; // Explicit Cast

  if (workspace.get_NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass, featureClassName)) //feature class with that name already exists 
  {
    featureClass = featureWorkspace.OpenFeatureClass(featureClassName);
    return featureClass;
  }

  // assign the class id value if not assigned
  if (CLSID == null)
  {
    CLSID = new ESRI.ArcGIS.esriSystem.UIDClass();
    CLSID.Value = "esriGeoDatabase.Feature";
  }

  ESRI.ArcGIS.Geodatabase.IObjectClassDescription objectClassDescription = new ESRI.ArcGIS.Geodatabase.FeatureClassDescriptionClass();

  // if a fields collection is not passed in then supply our own
  if (fields == null)
  {
    // create the fields using the required fields method
    fields = objectClassDescription.RequiredFields;
    ESRI.ArcGIS.Geodatabase.IFieldsEdit fieldsEdit = (ESRI.ArcGIS.Geodatabase.IFieldsEdit)fields; // Explicit Cast
    ESRI.ArcGIS.Geodatabase.IField field = new ESRI.ArcGIS.Geodatabase.FieldClass();

    // create a user defined text field
    ESRI.ArcGIS.Geodatabase.IFieldEdit fieldEdit = (ESRI.ArcGIS.Geodatabase.IFieldEdit)field; // Explicit Cast

    // setup field properties
    fieldEdit.Name_2 = "SampleField";
    fieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString;
    fieldEdit.IsNullable_2 = true;
    fieldEdit.AliasName_2 = "Sample Field Column";
    fieldEdit.DefaultValue_2 = "test";
    fieldEdit.Editable_2 = true;
    fieldEdit.Length_2 = 100;

    // add field to field collection
    fieldsEdit.AddField(field);
    fields = (ESRI.ArcGIS.Geodatabase.IFields)fieldsEdit; // Explicit Cast
  }

  System.String strShapeField = "";

  // locate the shape field
  for (int j = 0; j < fields.FieldCount; j++)
  {
    if (fields.get_Field(j).Type == ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry)
    {
      strShapeField = fields.get_Field(j).Name;
    }
  }

  // Use IFieldChecker to create a validated fields collection.
  ESRI.ArcGIS.Geodatabase.IFieldChecker fieldChecker = new ESRI.ArcGIS.Geodatabase.FieldCheckerClass();
  ESRI.ArcGIS.Geodatabase.IEnumFieldError enumFieldError = null;
  ESRI.ArcGIS.Geodatabase.IFields validatedFields = null;
  fieldChecker.ValidateWorkspace = (ESRI.ArcGIS.Geodatabase.IWorkspace)workspace;
  fieldChecker.Validate(fields, out enumFieldError, out validatedFields);

  // The enumFieldError enumerator can be inspected at this point to determine 
  // which fields were modified during validation.


  // finally create and return the feature class
  if (featureDataset == null)// if no feature dataset passed in, create at the workspace level
  {
      featureClass = featureWorkspace.CreateFeatureClass(featureClassName, validatedFields, CLSID, CLSEXT, ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword);
  }
  else
          {
      featureClass = featureDataset.CreateFeatureClass(featureClassName, validatedFields, CLSID, CLSEXT, ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword);
  }
  return featureClass;
}
[Visual Basic .NET]

'''<summary>Simple helper to create a featureclass in a geodatabase.</summary>
''' 
'''<param name="workspace">An IWorkspace2 interface</param>
'''<param name="featureDataset">An IFeatureDataset interface or Nothing</param>
'''<param name="featureClassName">A System.String that contains the name of the feature class to open or create. Example: "states"</param>
'''<param name="fields">An IFields interface</param>
'''<param name="CLSID">A UID value or Nothing. Example "esriGeoDatabase.Feature" or Nothing</param>
'''<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>
'''<param name="strConfigKeyword">An empty System.String or RDBMS table string for ArcSDE. Example: "myTable" or ""</param>
'''  
'''<returns>An IFeatureClass interface or a Nothing</returns>
'''  
'''<remarks>
'''  (1) If a 'featureClassName' already exists in the workspace a reference to that feature class 
'''      object will be returned.
'''  (2) If an IFeatureDataset is passed in for the 'featureDataset' argument the feature class
'''      will be created in the dataset. If a Nothing is passed in for the 'featureDataset'
'''      argument the feature class will be created in the workspace.
'''  (3) When creating a feature class in a dataset the spatial reference is inherited 
'''      from the dataset object.
'''  (4) If an IFields interface is supplied for the 'fields' collection it will be used to create the
'''      table. If a Nothing value is supplied for the 'fields' collection, a table will be created using 
'''      default values in the method.
'''  (5) The 'strConfigurationKeyword' parameter allows the application to control the physical layout 
'''      for this table in the underlying RDBMSfor example, in the case of an Oracle database, the 
'''      configuration keyword controls the tablespace in which the table is created, the initial and 
'''     next extents, and other properties. The 'strConfigurationKeywords' for an ArcSDE instance are 
'''      set up by the ArcSDE data administrator, the list of available keywords supported by a workspace 
'''      may be obtained using the IWorkspaceConfiguration interface. For more information on configuration 
'''      keywords, refer to the ArcSDE documentation. When not using an ArcSDE table use an empty 
'''      string (ex: "").
'''</remarks>
Public Function CreateFeatureClass(ByVal workspace As ESRI.ArcGIS.Geodatabase.IWorkspace2, ByVal featureDataset As ESRI.ArcGIS.Geodatabase.IFeatureDataset, ByVal featureClassName As System.String, ByVal fields As ESRI.ArcGIS.Geodatabase.IFields, ByVal CLSID As ESRI.ArcGIS.esriSystem.UID, ByVal CLSEXT As ESRI.ArcGIS.esriSystem.UID, ByVal strConfigKeyword As System.String) As ESRI.ArcGIS.Geodatabase.IFeatureClass

  If featureClassName = "" Then
    Return Nothing ' name was not passed in
  End If

  Dim featureClass As ESRI.ArcGIS.Geodatabase.IFeatureClass
  Dim featureWorkspace As ESRI.ArcGIS.Geodatabase.IFeatureWorkspace = CType(workspace, ESRI.ArcGIS.Geodatabase.IFeatureWorkspace) ' Explicit Cast

  If workspace.NameExists(ESRI.ArcGIS.Geodatabase.esriDatasetType.esriDTFeatureClass, featureClassName) Then
    featureClass = featureWorkspace.OpenFeatureClass(featureClassName) ' feature class with that name already exists
    Return featureClass
  End If

  ' assign the class id value if not assigned
  If CLSID Is Nothing Then
    CLSID = New ESRI.ArcGIS.esriSystem.UIDClass
    CLSID.Value = "esriGeoDatabase.Feature"
  End If

  Dim objectClassDescription As ESRI.ArcGIS.Geodatabase.IObjectClassDescription = New ESRI.ArcGIS.Geodatabase.FeatureClassDescriptionClass

  ' if a fields collection is not passed in then supply our own
  If fields Is Nothing Then

    ' create the fields using the required fields method
    fields = objectClassDescription.RequiredFields
    Dim fieldsEdit As ESRI.ArcGIS.Geodatabase.IFieldsEdit = CType(fields, ESRI.ArcGIS.Geodatabase.IFieldsEdit) ' Explict Cast
    Dim field As ESRI.ArcGIS.Geodatabase.IField = New ESRI.ArcGIS.Geodatabase.FieldClass

    ' create a user defined text field
    Dim fieldEdit As ESRI.ArcGIS.Geodatabase.IFieldEdit = CType(Field, ESRI.ArcGIS.Geodatabase.IFieldEdit) ' Explict Cast

    ' setup field properties
    fieldEdit.Name_2 = "SampleField"
    fieldEdit.Type_2 = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeString
    fieldEdit.IsNullable_2 = True
    fieldEdit.AliasName_2 = "Sample Field Column"
    fieldEdit.DefaultValue_2 = "test"
    fieldEdit.Editable_2 = True
    fieldEdit.Length_2 = 100

    ' add field to field collection
    fieldsEdit.AddField(Field)
    fields = CType(fieldsEdit, ESRI.ArcGIS.Geodatabase.IFields) ' Explicit Cast

  End If

  Dim strShapeField As System.String = ""

  ' locate the shape field
  Dim j As System.Int32
  For j = 0 To fields.FieldCount
    If fields.Field(j).Type = ESRI.ArcGIS.Geodatabase.esriFieldType.esriFieldTypeGeometry Then
      strShapeField = fields.Field(j).Name
    End If
  Next j

  ' Use IFieldChecker to create a validated fields collection.
  Dim fieldChecker As ESRI.ArcGIS.Geodatabase.IFieldChecker = New ESRI.ArcGIS.Geodatabase.FieldCheckerClass()
  Dim enumFieldError As ESRI.ArcGIS.Geodatabase.IEnumFieldError = Nothing
  Dim validatedFields As ESRI.ArcGIS.Geodatabase.IFields = Nothing
  fieldChecker.ValidateWorkspace = CType(workspace, ESRI.ArcGIS.Geodatabase.IWorkspace)
  fieldChecker.Validate(fields, enumFieldError, validatedFields)

  ' The enumFieldError enumerator can be inspected at this point to determine 
  ' which fields were modified during validation.


  ' finally create and return the feature class
  If featureDataset Is Nothing Then
    ' if no feature dataset passed in, create at the workspace level
    featureClass = featureWorkspace.CreateFeatureClass(featureClassName, validatedFields, CLSID, CLSEXT, ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword)
  Else
    featureClass = featureDataset.CreateFeatureClass(featureClassName, validatedFields, CLSID, CLSEXT, ESRI.ArcGIS.Geodatabase.esriFeatureType.esriFTSimple, strShapeField, strConfigKeyword)
  End If

  Return featureClass

End Function


Additional Requirements
  • The code in this document requires the following References added to the Visual Studio project:
  • ESRI.ArcGIS.Geodatabase
  • ESRI.ArcGIS.Geometry
  • ESRI.ArcGIS.System
  • System