This is the most straightforward method for creating a new feature. After instantiating the feature and assigning its geometry and attribute values, you must explicitly call the Store method to commit the changes to the database. This approach is generally preferred for its reliability; the Store method ensures that the transaction is immediately written to the database, minimizing conflicts with other complex operations or event triggers.
The following example demonstrates connecting to an SDE geodatabase and creating a feature:
IAoInitialize licenseInitializer = new AoInitializeClass();
esriLicenseStatus licenseStatus = licenseInitializer.Initialize(esriLicenseProductCode.esriLicenseProductCodeEngineGeoDB);
IPropertySet connectionProperties = new PropertySetClass();
connectionProperties.SetProperty("SERVER", "192.168.1.143");
connectionProperties.SetProperty("INSTANCE", "5151");
connectionProperties.SetProperty("USER", "myTestUser");
connectionProperties.SetProperty("PASSWORD", "123456");
connectionProperties.SetProperty("VERSION", "SDE.DEFAULT");
IWorkspaceFactory workspaceFactory = new SdeWorkspaceFactoryClass();
IFeatureWorkspace featureWorkspace = (IFeatureWorkspace)workspaceFactory.Open(connectionProperties, 0);
IFeatureClass targetFeatureClass = featureWorkspace.OpenFeatureClass("TargetFeatureClassName");
IFeature newFeature = targetFeatureClass.CreateFeature();
newFeature.Shape = geometryObject; // IGeometry (IPolygon, IPolyline, IPoint)
int fieldIndex = newFeature.Fields.FindField("FieldName");
if (fieldIndex != -1)
{
newFeature.set_Value(fieldIndex, "FieldValue");
}
newFeature.Store();
Using IFeatureClass.CreateFeatureBuffer
This method utilizes an Insert Cursor (IFeatureCursor) in conjunction with a Feature Buffer. It offers performance benefits when inserting large volumes of simple data. However, according to ESRI documentation, using InsertFeature might not guarantee the triggering of complex custom events or behavior associated with the feature class.
In parctice, this approach can sometimes result in latency where data takes time to appear in the database, or in some cases, fails to appear with out throwing an error. Additionally, when working with an SDE geodatabase, this method is strictly limited to datasets or feature classes that are not registered as versioned.
IFeatureCursor insertCursor = targetFeatureClass.Insert(true);
IFeatureBuffer featureBuffer = targetFeatureClass.CreateFeatureBuffer();
featureBuffer.Shape = geometryObject;
int fieldIndex = featureBuffer.Fields.FindField("FieldName");
if (fieldIndex >= 0)
{
featureBuffer.set_Value(fieldIndex, "FieldValue");
}
insertCursor.InsertFeature(featureBuffer);
// Optionally call insertCursor.Flush() to force immediate write
Using the IFeatureClassWrite Interface
The IFeatureClassWrite interface provides a mechanism to write features directly. This is often used within an editing session to manage transactions explictily. You create the feature using the standard method, but write it using the interface implementation.
public void CreatePointFeature(ILayer layer, double xCoord, double yCoord, IWorkspaceEdit workspaceEdit)
{
IFeatureLayer featureLayer = layer as IFeatureLayer;
IFeatureClass featureClass = featureLayer.FeatureClass;
IFeatureClassWrite featureWriter = featureClass as IFeatureClassWrite;
workspaceEdit.StartEditOperation();
try
{
IFeature featureInstance = featureClass.CreateFeature();
IPoint pointGeometry = new PointClass();
pointGeometry.PutCoords(xCoord, yCoord);
featureInstance.Shape = pointGeometry;
featureWriter.WriteFeature(featureInstance);
workspaceEdit.StopEditOperation();
}
catch
{
workspaceEdit.AbortEditOperation();
throw;
}
}