Create Feature Class Programmatically Using ArcObject

In ESRI ArcGIS if we want to store spatial object so we need to create a container where we can store spatial data. In ArcGIS, if we want to store spatial as well as non-spatial data together we can create feature class.

The feature class is a spatial object where we can store spatial data with non-spatial properties. For Example, if we want to store ATM information we will create ATM feature class and store the data in that. In ATM feature class there are two things we can store one is the spatial information like which type of geometry we are going to store like Point/Line/Polygon and second is non-spatial property like Bank Name, Bank Address, Bank Type, ATM type.
To Create feature class, we have to create Geodatabase and within Geodatabase we can create feature class either this feature class is normal feature class or Standalone feature class. We can create feature class using Arc Catalog application or if required we can create programmatically using ArcObject. To create programmatically we need required Name of a feature class, spatial reference of geometry, type of geometry, non-spatial fields and its description.

  1. IWorkspaceFactory wsfShp = new ShapefileWorkspaceFactoryClass();  
  2. IWorkspace pws = wsfShp.OpenFromFile(@ "D:\YS\ShapeFiles\Street", 0);  
  3. IFeatureWorkspace pfeatWs = (IFeatureWorkspace) pws;  
  4. IFeatureClassDescription fcDescription = new FeatureClassDescriptionClass();  
  5. IObjectClassDescription ocDescription = (IObjectClassDescription) fcDescription;  
  6. IFields fields = ocDescription.RequiredFields;  
  7. IFieldsEdit fieldsEdit = (IFieldsEdit) fields;  
  8. IField oidField = new FieldClass();  
  9. IFieldEdit oidFieldEdit = (IFieldEdit) oidField;  
  10. oidFieldEdit.Name_2 = "OID";  
  11. oidFieldEdit.Type_2 = esriFieldType.esriFieldTypeOID;  
  12. fieldsEdit.FieldCount_2 = 3;  
  13. fieldsEdit.Field_2[0] = oidField;  
  14. //fieldsEdit.AddField((oidField);  
  15. // Add a text field called "Name" to the required fields.  
  16. IField field = new FieldClass();  
  17. IFieldEdit fieldEdit = (IFieldEdit) field;  
  18. fieldEdit.Name_2 = "Name";  
  19. fieldEdit.Type_2 = esriFieldType.esriFieldTypeString;  
  20. fieldsEdit.Field_2[1] = field;  
  21. //Create geometry field  
  22. IGeometryDef geometryDef = new GeometryDefClass();  
  23. IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit) geometryDef;  
  24. geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPoint;  
  25. ISpatialReferenceFactory spatialReferenceFactory = new SpatialReferenceEnvironment();  
  26. ISpatialReference spatialReference = spatialReferenceFactory.CreateProjectedCoordinateSystem((int) esriSRProjCSType.esriSRProjCS_NAD1983UTM_20N);  
  27. ISpatialReferenceResolution spatialReferenceResolution = (ISpatialReferenceResolution) spatialReference;  
  28. spatialReferenceResolution.ConstructFromHorizon();  
  29. ISpatialReferenceTolerance spatialReferenceTolerance = (ISpatialReferenceTolerance) spatialReference;  
  30. spatialReferenceTolerance.SetDefaultXYTolerance();  
  31. geometryDefEdit.SpatialReference_2 = spatialReference;  
  32. // Add a geometry field to the fields collection. This is where the geometry definition is applied.  
  33. IField geometryField = new FieldClass();  
  34. IFieldEdit geometryFieldEdit = (IFieldEdit) geometryField;  
  35. geometryFieldEdit.Name_2 = "Shape";  
  36. geometryFieldEdit.Type_2 = esriFieldType.esriFieldTypeGeometry;  
  37. geometryFieldEdit.GeometryDef_2 = geometryDef;  
  38. fieldsEdit.Field_2[2] = geometryField;  
  39. // Use IFieldChecker to create a validated fields collection.  
  40. IFieldChecker fieldChecker = new FieldCheckerClass();  
  41. IEnumFieldError enumFieldError = null;  
  42. IFields validatedFields = null;  
  43. fieldChecker.ValidateWorkspace = (IWorkspace) pfeatWs;  
  44. fieldChecker.Validate(fields, out enumFieldError, out validatedFields);  
  45. IFeatureClass featureClass = pfeatWs.CreateFeatureClass("Road", validatedFields, ocDescription.InstanceCLSID, ocDescription.ClassExtensionCLSID, esriFeatureType.esriFTSimple, "Shape""");