1

I am using ArcObject DLL to create shapefile. I am successfully able to create the shapefile but I am having some issues with populating the shapefile data into .dbf file. At present the .dbf file just contains the column names but the dataset is empty.

Excel screenshot

I adding the data in db file as follows

 public IFields CreateFieldsCollectionForFeatureClass(ISpatialReference spatialReference, string ViewType, DataSet ds)
    {
        //IFeatureLayer featureLayer = (IFeatureLayer)annotationLayer;
        IFeatureClassDescription fcDesc = new FeatureClassDescriptionClass();
        IObjectClassDescription ocDesc = (IObjectClassDescription)fcDesc;

        // Create the fields using the required fields method.
        IFields fields = ocDesc.RequiredFields;

        // Locate the shape field with the name from the feature class description.
        int shapeFieldIndex = fields.FindField(fcDesc.ShapeFieldName);
        IField shapeField = fields.get_Field(shapeFieldIndex);

        // Modify the GeometryDef object before using the fields collection to create a new feature class.
        IGeometryDef geometryDef = shapeField.GeometryDef;
        IGeometryDefEdit geometryDefEdit = (IGeometryDefEdit)geometryDef;

        // Alter the feature class geometry type to lines (default is polygons).
        if (ViewType == ViewsType.Powerplant.ToString())
        {
            geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryMultipoint;
        }
        else if (ViewType == ViewsType.Pipeline.ToString())
        {
            geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryPolyline;
        }
        else if (ViewType == ViewsType.Facility.ToString())
        {
            geometryDefEdit.GeometryType_2 = esriGeometryType.esriGeometryMultipoint;
        }
        geometryDefEdit.HasM_2 = true;
        geometryDefEdit.GridCount_2 = 1;

        // Set the first grid size to zero and allow ArcGIS to determine a valid grid size.
        geometryDefEdit.set_GridSize(0, 0);
        geometryDefEdit.SpatialReference_2 = spatialReference;

        // Because the fields collection already exists, the AddField method on the IFieldsEdit interface
        // will be used to add a field that is not required to the fields collection.
        IFieldsEdit fieldsEdit = (IFieldsEdit)fields;
        foreach (DataColumn col in ds.Tables[0].Columns)
        {
            IField filed = new FieldClass();
            IFieldEdit editFiled = (IFieldEdit)filed;
            editFiled.Type_2 = esriFieldType.esriFieldTypeString;
            editFiled.Name_2 = col.ColumnName;
            editFiled.DefaultValue_2 = "test";//ds.Tables[0].Rows;
            //foreach (DataRow d in ds.Tables[0].Rows)
            //{
            //    editFiled.DefaultValue_2 = d["ID"];
            //}

            //editFiled.DefaultValue_2 = ds.Tables[0].Rows[0].ItemArray[0].ToString();
            editFiled.Length_2 = 50;
            fieldsEdit.AddField(editFiled);
        }
        //fields = (ESRI.ArcGIS.Geodatabase.IFields)fieldsEdit;

        return fields;
    }

How can I get the data of that field in .dbf file.

1 Answer 1

1

There are two ways to get features into a shapefile, One is with IFeatureClass.CreateFeature() followed by IFeature.store() and the other is with IFeatureCursor.InsertFeature() on a IFeatureBuffer created from the IFeatureClass using CreateFeatureBuffer

The fastest way is with IFeatureCursor:

IFeatureCursor pCur = pFeatureClass.InsertCursor; //pFeatureClass is an IFeatureClass
IFeatBuff pFtBuff = pFeatureClass.CreateFeatureBuffer();
// A feature buffer has the same properties as a feature
pFtBuff.shape = pYourGeometry; // a geometry you've created
pFtBuff.set_Value(indx) = pSomeValue; // indx is the index of the field
// do that a few times..
// fields are from 0 to fieldcount - 1
pCur.InsertFeature(pFtBuff);

If you have a lot of features to store then it's a good idea to call the Flush() on the cursor from time to time.

After you've finished with the cursor and buffer get rid of them using ReleaseComObject to do the final flush and release the locks on the file.

6
  • Thank you Michael for your reply. I didn't understand that so can you please explain me in detail if possible.? Jun 28, 2014 at 11:33
  • Values in the dbf file belongs to features - a row with a geometry and ID. You can't have values without meeting that condition. The code is how to create, set the attributes and then store a feature (row) in the shapefile. Jun 28, 2014 at 11:36
  • Hi Michael Thanks for your reply I tried the solution you mentioned, I'm getting a error can you please look into it , the error is here webashlar.com/Demos/images/Untitled.png Jun 30, 2014 at 11:10
  • It's hard to tell from that error message, it's fairly generic. What is field 1? The value is a variant so if field 1 is not a string then remove the quotes and set it as a number, if it's a geometry don't set it that way, geometries are set by pFtBuff.Shape = Geometry. You will also get that error if the field is read-only. Jun 30, 2014 at 21:27
  • I have tried this and i am successfully added data in the dbf but now the problem is that in the dbf the first row is blank so thats why when i selects one geometry its select all geometry so for that any solution my code is. IGeometry pol = GetMultipointGeometry(ds,LogfilePath,ViewName) /* */ pFeature.Shape = pol; this is for Geometry feature. then after i am inserting data in dbf file code: IFeatureBuffer pFtBuff = FC.CreateFeatureBuffer(); int t = pFtBuff.Fields.FindField(dc.ColumnName); pFtBuff.set_Value(t, value);j++;}}pFtBuff.Shape = g; pCur.InsertFeature(pFtBuff);pCur.Flush();} Sep 23, 2014 at 6:07

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.