Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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;
    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

share|improve this question
Is fc an in-memory featureclass? – Kirk Kuykendall Oct 5 '11 at 17:50
@KirkKuykendall, yes. – Keith G Oct 5 '11 at 17:52
up vote 3 down vote accepted

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().

share|improve this answer
This was just enough to get me thinking about how to do it the right way. – Keith G Oct 6 '11 at 14:29

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.