Skip to content

Instantly share code, notes, and snippets.

@kiichi
Last active November 16, 2023 18:50
Show Gist options
  • Save kiichi/c44dd9f65b954ade7d1f to your computer and use it in GitHub Desktop.
Save kiichi/c44dd9f65b954ade7d1f to your computer and use it in GitHub Desktop.
C# GIS Polygon and Distance / Area Example using NetTopologySuite
using System;
using NetTopologySuite.Geometries;
using GeoAPI.Geometries;
/*
this looks wrong. SRID is not used?
Edge (West-East) of Central Park: 0.00906435667878181
Margin Distance of Central Park: 0.0983743244869002
Central Park Area: 0.000354214827500022
*/
namespace NetTopologySuiteTest {
class MainClass {
public static void Main (string[] args) {
GeometryFactory factory = new GeometryFactory (new PrecisionModel(), 4326);
// Edge of Central Park
Point p1 = (Point)factory.CreatePoint(new Coordinate (-73.957986, 40.800566));
Point p2 = (Point)factory.CreatePoint(new Coordinate (-73.949575,40.797187));
double dist = p1.Distance (p2);
Console.WriteLine ("Edge (West-East) of Central Park: " + dist);
// Around Central Park
Coordinate[] coords = new Coordinate[]{
new Coordinate(-73.957986,40.800566),
new Coordinate(-73.949575,40.797187),
new Coordinate(-73.972921,40.764951),
new Coordinate(-73.981676,40.768071),
new Coordinate(-73.957986,40.800566) // Close this polygon!
};
LineString lineStr = (LineString)factory.CreateLineString (coords);
double marginDist = lineStr.Length;
Console.WriteLine ("Margin Distance of Central Park: " + marginDist);
Polygon poly = (Polygon)factory.CreatePolygon(new LinearRing(coords));
Console.WriteLine ("Central Park Area: " + poly.Area);
}
}
}
@philreindl
Copy link

Were you able to find a solution to this? I'm struggling with the same issue.

@salesHgabriel
Copy link

Were you able to find a solution to this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment