2

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

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

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

4

1 回答 1

1

您可以创建一个 Feature 变量,然后更新 DataRow 属性上的内容。然后,您必须直接将该功能添加到 Features 集合中,而不是使用 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);
于 2018-08-14T21:11:50.360 回答