Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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();
}
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
add comment

1 Answer

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
add comment

Your Answer

 
discard

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.