使用DotSpatial计算具有WGS坐标的多边形面积?

时间:2017-09-11 15:42:27

标签: c# area dotspatial

计算面积:

var coordinates = new List<Coordinate> {
    new Coordinate(55, 35),
    new Coordinate(55, 35.1),
    new Coordinate(55.1, 35.1),
    new Coordinate(55.1, 35),
};
Console.WriteLine(new Polygon(coordinates).Area); // ~0.01

计算是正确的,因为它发生在正交坐标系中。

但是如何标记坐标是在WGS中?

2 个答案:

答案 0 :(得分:1)

我认为任务似乎更复杂。我在google groups

上找到了这个有用的讨论

首先,我们需要找到最适合我们需要计算面积的区域的投影系统。例如,您可以选择UTM zones

之一
using DotSpatial.Projections;
using DotSpatial.Topology;
public static double CalculateArea(IEnumerable<double> latLonPoints)
{
    // source projection is WGS1984
    var projFrom = KnownCoordinateSystems.Geographic.World.WGS1984;
    // most complicated problem - you have to find most suitable projection
    var projTo = KnownCoordinateSystems.Projected.UtmWgs1984.WGS1984UTMZone37N;

    // prepare for ReprojectPoints (it's mutate array)
    var z = new double[latLonPoints.Count() / 2];
    var pointsArray = latLonPoints.ToArray();

    Reproject.ReprojectPoints(pointsArray, z, projFrom, projTo, 0, pointsArray.Length / 2);

    // assemblying new points array to create polygon
    var points = new List<Coordinate>(pointsArray.Length / 2);
    for (int i = 0; i < pointsArray.Length / 2; i++)
        points.Add(new Coordinate(pointsArray[i * 2], pointsArray[i * 2 + 1]));

    var poly = new Polygon(points);
    return poly.Area;
}

答案 1 :(得分:0)

您可以直接从IGeometry或Feature.Geometry获取区域。另外,您需要重复第一个坐标以关闭多边形。

FeatureSet fs = new FeatureSet(FeatureType.Polygon);

Coordinate[] coord = new Coordinate[]
{
    new Coordinate(55, 35),
    new Coordinate(55, 35.1),
    new Coordinate(55.1, 35.1),
    new Coordinate(55.1, 35),
    new Coordinate(55, 35)
};

fs.AddFeature(new Polygon(new LinearRing(coord)));

var area = fs.Features.First().Geometry.Area;