3

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();
}
2
  • Is fc an in-memory featureclass? Commented Oct 5, 2011 at 17:50
  • @KirkKuykendall, yes.
    – Keith G
    Commented Oct 5, 2011 at 17:52

1 Answer 1

3

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

1
  • This was just enough to get me thinking about how to do it the right way.
    – Keith G
    Commented Oct 6, 2011 at 14:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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