If I need to generate a polygon that crosses the anti-meridian, does it exists a Python library that treat such cases (partitioning such polygon and generate a multipart polygon)?
3 Answers
I would suggest looking into Xarray here is a thread xarray slicing across the antimeridian that covers a use for perhaps what you are looking for.
This thread isn't answered but provides another example Shapely polygons crossing the antimeridian
Here is a blog entry How to correct a linestring for the antimeridian (180 longitude) to provide additional context.
-
Nice suggestions. I've found another question on the same line using
gdal.VectorTranslate
. Have you tried to use this method? (I'm having issues to installgdal
2.1.x on my mac, so I haven't tested) Commented Sep 12, 2017 at 18:42 -
In a context of GeoJSON files (in which I was inserted) I've preferred to use a solution that uses gdal.VectorTranslate
method, because it provides compliance to the RFC 7946 standard, that also provides a rationale for antimeridian cases.
Splitting a polygon along the antimeridian can potentially much more involved than expected. In Python, we can use the Shapely library to accomplish this with the following steps
- Taking a given polygon's coordinates (in GeoJSON format), then detecting whether there are antimeridian crossings by checking if the absolute value of the longitudinal difference between two consecutive vertices of the polygon is greater than a certain threshold. Practically, 180 degrees could be a sensible threshold value.
- If there are antimeridian crossing, you will next need to determine the direction (e.g. is it going from +179.9 to -179.9 or vice versa). Once you know the direction, you can shift certain vertices of the polygon into a new Cartesian space. For example, consider the case of the previous vertex at +179.9 the the current vertex at -179.9:
- we can shift the current vertex to +180.1 in our new Cartesian space
- we note down that the antimeridian crossing is a line at +180.0 of longitude
- Once the entire polygon's vertices have been appropriately shifted into the new Cartesian space, we can then leverage Shapely's Cartesian geometry operations (particularly the
shapely.ops.split
function) to split the polygon safely. - Finally, for each of the resultant polygons from the
split
operation, you'll need to shift their longitude values to appropriate geographic coordinates (+/- 360.0)
Here's the full tutorial + code snippets using Shapely.