在 Featureset Dotspatial 中设置字段值

Set field value in a Featureset Dotspatial

我正在使用 Microsoft Visual Studio 和 C# 编程语言中的 DotSpatial 库创建一个 shapefile。 shapefile 由一个多边形层组成。图层中的每个多边形都需要具有特定的肥料值。就我的理解而言,我必须首先创建一个字段(比如 "Fertilizer value"),然后为创建的每个多边形添加相应的肥料值。我创建了一个字段并创建了一个多边形。但是,我仍在努力寻找在相应的多边形中添加字段值的正确方法。代码如下:

// 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?

我的问题是,如何设置此多边形的字段值?

您可以创建一个 Feature 变量,然后更新 DataRow 上的内容 属性。然后您必须直接将功能添加到功能集合,而不是使用 AddFeature 快捷方式。

        // 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);