Set field value in a Featureset Dotspatial

1.3k views Asked by At

I am creating a shapefile using DotSpatial library in Microsoft Visual Studio and C# programming language. The shapefile consists of a Polygon layer. Each polygon in the layer needs to have a specific fertilizer value. As far as my understanding goes, I have to first create a field (say "Fertilizer value") and then add corresponding fertilizer value to it for each polygon created. I have created a field and have created a Polygon. However, I am still struggling to find the right way to add a fields value in the corresponding Polygon. The code is as below:

// Create a feature set of type Polygon and set the projection
FeatureSet fs = new FeatureSet(FeatureType.Polygon);
fs.Projection = ProjectionInfo.FromAuthorityCode("EPSG", 3857);

// Get the DataTable and set the fertilizer field
DataTable table = fs.DataTable;
DataColumn FertilizerField = table.Columns.Add("Fertilizer Value", typeof(double));

// Adding a Polygon feature to the layer 
Coordinate[] coord = new Coordinate[]
{
    new Coordinate(0.0, 0.0),
    new Coordinate(1.0, 0.0),
    new Coordinate(1.0, 1.0),
    new Coordinate(0.0, 1.0),
    new Coordinate(0.0, 0.0)
};
fs.AddFeature(new Polygon(new LinearRing(coord)));

// TODO: HOW TO ADD FERTILIZER VALUE OF 100 TO THE FERTILIZER FIELD OF THIS POLYGON?

My question is, how do I set the fields value for this polygon?

2

There are 2 answers

1
Ted On BEST ANSWER

You can create a Feature variable and then update the content on the DataRow property. You will have to then add the feature to the Features collection directly, rather than using the AddFeature shortcut method.

        // Create a feature set of type Polygon and set the projection
        FeatureSet fs = new FeatureSet(FeatureType.Polygon);
        fs.Projection = ProjectionInfo.FromAuthorityCode("EPSG", 3857);

        // Get the DataTable and set the fertilizer field
        DataTable table = fs.DataTable;
        DataColumn FertilizerField = table.Columns.Add("Fertilizer Value", typeof(double));

        // Adding a Polygon feature to the layer 
        Coordinate[] coord = new Coordinate[]
        {
            new Coordinate(0.0, 0.0),
            new Coordinate(1.0, 0.0),
            new Coordinate(1.0, 1.0),
            new Coordinate(0.0, 1.0),
            new Coordinate(0.0, 0.0)
        };
        // Create a Feature Variable to update the shape with attribute content.
        Feature f = new Feature(new Polygon(new LinearRing(coord)));
        // Modify the data row like any DataTable DataRow object.
        f.DataRow["Fertilizer Value"] = 100;
        // Add the fully created feature to the list of features directly.
        fs.Features.Add(f);
0
Tymofii Tarasenko On

As fs.AddFeature(new Polygon(new LinearRing(coord))); returns the created feature as an IFeature object, you can assign this object to a variable and then use its DataRow property to assign your "Fertilizer Value"

The code should look something like this:

        // Create a feature set of type Polygon and set the projection
        FeatureSet fs = new FeatureSet(FeatureType.Polygon);
        fs.Projection = ProjectionInfo.FromAuthorityCode("EPSG", 3857);

        // Get the DataTable and set the fertilizer field
        DataTable table = fs.DataTable;
        DataColumn FertilizerField = table.Columns.Add("Fertilizer Value", typeof(double));

        // Adding a Polygon feature to the layer 
        Coordinate[] coord = new Coordinate[]
        {
            new Coordinate(0.0, 0.0),
            new Coordinate(1.0, 0.0),
            new Coordinate(1.0, 1.0),
            new Coordinate(0.0, 1.0),
            new Coordinate(0.0, 0.0)
        };
        IFeature f = fs.AddFeature(new Polygon(new LinearRing(coord)));
        // Assign Fertilizer Value
        f.DataRow["Fertilizer Value"] = 100;