Calculate area of polygon having WGS coordinates using DotSpatial?

1.4k views Asked by At

Calculating area:

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

Calculation is right, because it's happen in orthogonal coordinate system.

But how to mark that coordinates are in WGS?

2

There are 2 answers

0
Anton Pavelyev On

It seems that task is more complicated that I've expected. I found this useful discussion on google groups

Firstly we need to found projection system, that is most suitable for our region where we need to compute area. For example you can take one of 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 mutates 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
Mapster On

You can get the area directly from IGeometry or from Feature.Geometry. Also You need to repeat the first coordinate to close your polygon.

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;