Friday, 28 August 2015

shapefile - Order of polygon vertices in general GIS: clockwise or counterclockwise



Two days ago I asked a question about the internal storing order for the vertices of a polygon in ESRI shapefiles. That question was answered (Are polygons stored clockwise or counterclockwise in a shapefile?) and it was also answered in an old post (Polygon creation (Clockwise rotation or not))


But now my question is more general, and I don't know if it has an unique answer.


Is the clockwise order only for ESRI shapefiles or for general GIS formats?


And what about the internal representation for a GIS software?


For example, if I use QGIS and I read a *.shp containing polygons I suppose the internal representation of the outer bound is clockwise as in the original shapefile, but what about for all the file formats supported by QGIS?


And for ArcGIS?


And in the case there exists a file format with polygons stored in counterclockwise, if these files are loaded in QGIS, ArcGIS, etc., is the orientation changed internally, so if I read the data using PyQGIS, for example, the polygons are clockwise ordered?



My purpose is to write a plugin for QGIS, but the source of data can be ESRI shapefiles or other formats. As I need to check the angles between consecutive sides of polygons using their azimuths I need to known if the order is clockwise. One solution is computing the area of each polygon and, if I remember correctly, if it is positive the order is clockwise and if negative the order is counterclockwise.


Area computation is not an intensive task, so it will not slow my plugin so much.


But in the special case of QGIS, anybody knows if it stores the polygons clockwise or counterclockwise, regardless the order in the original source?


By now I'm working with ESRI shapefiles and the coordinates in layer.getFeatures().geometry().asPolygon() are stored in clockwise for the outer border and counterclockwise for the holes, i. e. as in the original *.shp.



Answer



In the OGC specification, which can be downloaded [here],(http://www.opengeospatial.org/standards/sfs) they state:



"Polygon rotation is not defined by this standard; actual polygon rotation may be in a clockwise or counter-clockwise direction."



In the Oracle docs, it is clearly stated that exterior ring boundaries are oriented counterclockwise, and interior ring boundaries, clockwise. Likewise, in SQL Server Spatial, the geography datatype follows a counter-clockwise rule for the exterior ring, and clockwise for the interior rings -- see this MicroSoft blog for more details. Postgis seems to allow it either way, for geometries, and has functions that will force a polygon's geometry to follow either a right or left hand rule, see ST_ForceRHR and ForceLHR. JTS/Geos seem to have followed the right-hand rule, ie, a clockwise orientation of the outer ring, so it is all a bit unclear, really.



In general it makes sense for a geography datatype to force orientation, as otherwise it would be impossible to tell if a small polygon was just that or the inner ring of a whole world polygon. With a geometry datatype on a planar surface, this confusion cannot arise, as the outer ring and inner ring follow in order, and if there is only a single ring, it will be enclosing (no matter what the orientation), unlike on a globe, which wraps round.




From a comment by @mxfh: OGC's OpenGIS Simple Features Access (ISO 19125-1) specifies a counter-clockwise direction for outer rings as of version 1.2.1 document [OGC 06-103r4] 6.1.11.1/page 26 from opengeospatial.org/standards/sfa. The change got introduced between version 1.1.0 and 1.2.0 in 2006 latest. The footnote you're citing from hasn't been updated since 2005


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...