Wednesday, 23 March 2016

python - Shapely polygons crossing the antimeridian



Consider the following WKT Polygon, crossing the international dateline (antimeridian):



POLYGON((176 49,-65 49,-65 11,176 11,176 49))



Polygon crossing the dateline


And the following points:


POINT(-140 32) # Inside the polygon

POINT(0 32) # Outside the polygon

Points inside and outside the polygon



Shapely considers this polygon to span on the other side of the planet - covering Asia and the Atlantic, rather than the US and the Pacific. Therefore, it fails to calculate its centroid and tell whether points are inside or outside it:


from shapely import wkt

polygon_wkt = 'POLYGON((176 49,-65 49,-65 11,176 11,176 49))'
point_in_polygon_wkt = 'POINT(-140 32)'
point_outside_polygon_wkt = 'POINT(0 32)'


polygon = wkt.loads(polygon_wkt)
point_in_polygon = wkt.loads(point_in_polygon_wkt)
point_outside_polygon = wkt.loads(point_outside_polygon_wkt)


print polygon.centroid # POINT (55.5 30) - Wrong!
print polygon.contains(point_in_polygon) # False - Wrong!
print polygon.contains(point_outside_polygon) # True - Wrong!




  • Using PostGIS - I get the same erroneous results.

  • Playing with Shapely arguments - couldn't manage to "wrap" the polygon to the other side of the planet.

  • Reading The International Date Line wrap around. To be frank, there does not seem to be an answer there (Except for splitting the polygon).



How can I calculate the centroid, bounding box, and inside/outside predicate for a WGS84 polygon optionally crossing the international dateline (longitude 180 / -180)?




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