[GIS] How tonsert Feature into Feature Class (in Memory) ESRI

arcgis-10.0arcgis-enginearcobjectscfeatures

I'm looking for suggestions on how to insert a new feature instance into a feature class that is in memory — assuming that I don't have access to the feature workspace. For example, the feature class has 4 "rows", and I want to add another to make the total 5.

So something like this ….

void InsertFeatureInstance(IFeatureClass fc, IFeature feature)
{
     // Do the work here.
}

I have a good idea how to do this when I have the feature workspace, using a feature cursor, calling CreateFeature, etc. But in this case, I want to be able to accomplish it all without touching the underlying geodb.

Working in a stand-alone C# application, ArcGIS 10, and ArcGIS Engine.

EDIT: Here is the solution I came up with. Some of it was copied from other code I got from somewhere else (can't remember where).

public static void InsertFeatureInstance(IFeatureClass targetFeatureClass, IFeature sourceFeature)
{
    IFeature newfeature = targetFeatureClass.CreateFeature();
    var simplifiedFeature = newfeature as IFeatureSimplify;
    IGeometry myGeometry = sourceFeature.ShapeCopy;
    simplifiedFeature.SimplifyGeometry(myGeometry);
    newfeature.Shape = myGeometry;

    for (int i = 1; i < sourceFeature.Fields.FieldCount; i++)
    {
        IFeatureClass sourceFeatureClass = (IFeatureClass) sourceFeature.Class;

        string fieldName = sourceFeature.Fields.Field[i].Name;
        bool bCondition1 = fieldName == sourceFeatureClass.ShapeFieldName;
        bool bCondition2 = (sourceFeatureClass.LengthField != null &&
                            fieldName == sourceFeatureClass.LengthField.Name);
        bool bCondition3 = (sourceFeatureClass.AreaField != null &&
                            fieldName == sourceFeatureClass.AreaField.Name);

        if (!(bCondition1 || bCondition2 || bCondition3))
        // Don't do shape fields
        {
            int myTargetFieldId = targetFeatureClass.FindField(fieldName);
            // Id of field in source feature
            newfeature.Value[myTargetFieldId] = sourceFeature.Value[i]; // Copy value
        }
    }

    newfeature.Store();
}

Best Answer

You can get the featureworkspace by casting the fc to IDataset, then casting IDataset.Workspace to IFeatureWorkspace.

However I don't see why you need this. If you cast feature.Class is not equal to fc, (it's in a different workspace), you need to create a new feature with fc.CreateFeature, copy each field value to the new feature from the feature passed in, then call IFeature.Store().

Related Question