Select to view content in your preferred language

insert features with attributes

794
3
04-15-2013 03:15 PM
InsuHong1
Emerging Contributor
Hi,

How can I insert (or copy, move) features from one featureclass to another with original attributes?

I have two featureclasses, and both of them have same fields.

I want to select part of Featureclass A, then move those selected features to FeatureClass B, with their attributes.

I can do move operations, but it loose all attributes.


How can I do this?


Thank you


Insu
0 Kudos
3 Replies
sameerpuppal
Deactivated User
Hi,

private void copyfeatures(IFeatureLayer FromFeatureLayer, IFeatureLayer ToFeatureLayer)
        {
            //take selected features
            IFeatureSelection pFeatureSelection = FromFeatureLayer as IFeatureSelection;
            ICursor pFromCursor = null;
            if (pFeatureSelection.SelectionSet.Count > 1)
            {
                //get selected features to cursor
                pFeatureSelection.SelectionSet.Search(null, false, out pFromCursor);
                IFeatureCursor pFromFCursor = pFromCursor as IFeatureCursor;
                IFeature pFeatureFrom = null;
                //loop selected features
                while ((pFeatureFrom = pFromFCursor.NextFeature())!=null)
                {
                    //create new feature
                    IFeature pFeatureToNew = ToFeatureLayer.FeatureClass.CreateFeature();
                    //assign shape + attributes (Schema should be exactly same)
                    pFeatureToNew = pFeatureFrom;
                    //store it.
                    pFeatureToNew.Store();
                   
                }
            }           
        }

Please do not forget to start and stop editing to use this code.

Let me know if this doesnt work.

Regards,
Sameer Puppal
GIS Developer
Mahindra Satyam, India








Hi,

How can I insert (or copy, move) features from one featureclass to another with original attributes?

I have two featureclasses, and both of them have same fields.

I want to select part of Featureclass A, then move those selected features to FeatureClass B, with their attributes.

I can do move operations, but it loose all attributes.


How can I do this?


Thank you


Insu
0 Kudos
DrewDowling
Frequent Contributor
You might also accomplish this with the append tool in modelbuilder or script it out in python.
0 Kudos
sameerpuppal
Deactivated User
private void copyfeatures(IFeatureLayer FromFeatureLayer, IFeatureLayer ToFeatureLayer)
        {
            //take selected features
            IFeatureSelection pFeatureSelection = FromFeatureLayer as IFeatureSelection;
            ICursor pFromCursor = null;
            if (pFeatureSelection.SelectionSet.Count > 1)
            {
                //get selected features to cursor
                pFeatureSelection.SelectionSet.Search(null, false, out pFromCursor);
                IFeatureCursor pFromFCursor = pFromCursor as IFeatureCursor;
                IFeature pFeatureFrom = null;
                //loop selected features
                while ((pFeatureFrom = pFromFCursor.NextFeature())!=null)
                {
                    //create new feature
                    IFeature pFeatureToNew = ToFeatureLayer.FeatureClass.CreateFeature();
                    //assign shape + attributes (Schema should be exactly same)
                     pFeatureNew.Shape = pFeatureFrom.Shape;
                    for (int i = 0; i < pFeatureNew.Fields.FieldCount; i++)
                    {
                        if (pFeatureNew.Fields.get_Field(i).Editable)
                        {
                            pFeatureNew.set_Value(i, pFeatureFrom.get_Value(i));
                        }
                    }
                    //store it.
                    pFeatureToNew.Store();
                    
                }
            }            
        }


Little corrections in the code
0 Kudos