我正在使用 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?
我的问题是,如何设置该多边形的字段值?
您可以创建一个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);
由于
fs.AddFeature(new Polygon(new LinearRing(coord)));
将创建的要素作为 IFeature 对象返回,您可以将此对象分配给变量,然后使用其 DataRow 属性来分配您的“肥料值”
代码应如下所示:
// 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.
IFeature f = fs.AddFeature(new Polygon(new LinearRing(coord)));
// Modify the data row like any DataTable DataRow object.
f.DataRow["Fertilizer Value"] = 100;