Wednesday 1 February 2017

geojson - Fixing orientation in Net Topology Suite



I have a MultiPolygon that was created by reading from a GeoJSON string.


string geoJson = getGeoJson();
var rdr = new GeoJsonReader();
var multiPolygon = rdr.Read(geoJson);

My problem - GeoJSON recommends but does not require that the outer ring be counter-clockwise and the inner rings be clockwise.


It looks to me as if GeoJsonReader, when handed a polygon with a clockwise outside ring constructs a MultiPolygon with a clockwise outer ring.


How can I detect, in Net Topology Suite, that a ring is clockwise or counter clockwise?



Answer



In NTS, every object that inherits ILinarRing interface has the IsCCW property to test if it is counter clockwise.



In case of a MultiPolygon object, you have to test the shell of every internal polygon. Like this:


IMultiPolygon multiPol = rdr.Read(geoJson);
foreach (IGeometry geom in multiPol.Geometries)
{
IPolygon pol = (IPolygon)geom;
if (pol.Shell.IsCCW)
{
// Shell is counter clockwise
}
else

{
// Shell is clockwise
IGeometry ccwShell = pol.Shell.Reverse();
}
}

PD: Old question, maybe usefull for someone...


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...