Select to view content in your preferred language

Adding fields to a feature class

596
4
05-21-2010 12:07 PM
LuisPenedo
Emerging Contributor
I'm trying to add fields to a layer attribute table using pFeatureLayer.FeatureClass.AddField(pFieldEdit) and it takes like two-three minutes per field.  I try using  layerTable.AddField(pFieldEdit) after converting the layer to an ILayer using ITable layerTable = (ITable)editAttributeLayer and still is very slow.  It takes a fraction of a second doing the same operation in the attribute table of the layer directly.  Somebody knows why it is to slow doing int using arc objects? or is there a trick to do it fast?

I need to add 80+ fields to the layer and even one minute per field would be too long.

Any ideas?

Thanks in advance.
0 Kudos
4 Replies
JamesCrandall
MVP Alum
I'm trying to add fields to a layer attribute table using pFeatureLayer.FeatureClass.AddField(pFieldEdit) and it takes like two-three minutes per field.  I try using  layerTable.AddField(pFieldEdit) after converting the layer to an ILayer using ITable layerTable = (ITable)editAttributeLayer and still is very slow.  It takes a fraction of a second doing the same operation in the attribute table of the layer directly.  Somebody knows why it is to slow doing int using arc objects? or is there a trick to do it fast?

I need to add 80+ fields to the layer and even one minute per field would be too long.

Any ideas?

Thanks in advance.



Adding fields is relatively painless and should not be slow.  But I'm a little confused on what you are attempting to do...  You need to add a field?  or 80 of them?  From where?  How are you assigning the new field parameters to the ITable/Layer?

Maybe something is wrong in the looping of the 80 fields?  Post some code up.
0 Kudos
LuisPenedo
Emerging Contributor
private void btnOK_Click(object sender, EventArgs e)
        {
            //Once user hits OK then add duplicate fields to Layer
            if (editAttributeLayer != null)
            {
                try
                {
                    //Field collection
                    IFieldsEdit pFieldsEdit;
                   
                    //Get FeatureLayer
                    IFeatureLayer pFeatureLayer = editAttributeLayer as IFeatureLayer;
                    this.SetEditLayer = editAttributeLayer;

                    //Get WorkSpace from FeatureLayer
                    IDataset pDataSet = pFeatureLayer.FeatureClass as IDataset;
                    IWorkspace pWorkSpace = pDataSet.Workspace;

                    int numFields = pFeatureLayer.FeatureClass.Fields.FieldCount;

                    for (int k = 0; k < numFields; k++)
                    //for (int k = 0; k < 5; k++)
                    {
                        //Get the Field
                        IField pField = pFeatureLayer.FeatureClass.Fields.get_Field(k);
                        // Needs to process fields which name does not start with "X" only otherwise the program will keep on adding X fields
                        string thisFieldName = pField.Name;
                        bool startsWithX = thisFieldName.StartsWith("X");
                        bool endsWith_1 = thisFieldName.EndsWith("_1");
                        if (pField.Name != "FID" && pField.Name != "SHAPE" && pField.Name != "Shape" && pField.Name != "OBJECTID" && pField.Name != "CHANGE" && pField.Name != "REPO" && startsWithX == false && endsWith_1 == false)
                        {
                            //check whether field already exists before adding
                            string fName = pField.Name.ToString();
                            string xName = "X" + fName;
                            string thisField = "";
                            if (xName.Length > 10)
                                thisField = xName.Substring(0, 10);
                            else
                                thisField = xName;
                            if (FieldExits(thisField, pFeatureLayer) == false)
                            {
                                //Define Field properties
                                IField pNewField = new FieldClass();
                                IFieldEdit pFieldEdit = pNewField as IFieldEdit;
                                pFieldEdit.AliasName_2 = "X" + pField.AliasName.ToString();
                                pFieldEdit.Name_2 = "X" + pField.Name.ToString();
                                pFieldEdit.Type_2 = pField.Type;
                                //Add field
                                try
                                {
                                    int theField = pFeatureLayer.FeatureClass.Fields.FindField("X" + pField.Name.ToString());
                                    if (theField == -1)
                                    {
                                       
                                        pFeatureLayer.FeatureClass.AddField(pFieldEdit);
                                    }
                                }
                                catch (Exception ex)
                                {
                                    MessageBox.Show("Field " + pFieldEdit.Name + " was not added due to an error (" + ex.Message + " )");
                                }
                            }
                        }
                    }
0 Kudos
JamesCrandall
MVP Alum
I'm having a heck of a time with this C# code.  Normally not a problem for me, but I'm just more comfortable in VB.NET and cannot seem to understand a couple of things you are attempting -- maybe someone with more C# exp can chime in.

I don't understand where you are doing here (aside from the obvious of adding fields).  What I mean is you are looping thru the fields of pFeatureLayer, then re-adding all of the existing fields back to it (minus the startswith/endswith)?

Or do you just want to remove fields that match your string test?

The other thing you mention is...

takes a fraction of a second doing the same operation in the attribute table of the layer directly


Can you just not attempt this?  Not sure what the difference would be between the FeatureLayer and setting an ITable to the FLayer and running your code on it.  I am unsure if that'd work or help.
0 Kudos
LuisPenedo
Emerging Contributor
The way they want this tool to behave is adding a copy of all fiends but each new field name will start with an "X" and then the field name.  I'm starting to think that the way arcmap adds a field is making a copy of the data somewhere, then adds the fields and then it puts the data back.  That would explain why the time is not very significant for small datasets (the problem is that the real ones are big).

I am thinking making a copy of the layer, lets say, a temp layer, it would take about one or two minutes for big datasets.  Then add the fields which will take a seconda adding all, and then move the data back to the original layer, delete de temp layer and the whole process would be around 3-4 minutes which is something more acceptable than 90 minutes.

If you know another way, please let me know.
0 Kudos