Fix GeoJSON geometries that cross the antimeridian
Project description
antimeridian
A Python package to correct GeoJSON shapes that cross the antimeridian.
Usage
pip install antimeridian
Then, in your code:
import antimeridian
fixed = antimeridian.fix_geojson(geojson)
There's a command-line entrypoint, too:
fix-antimeridian input.json > output.json
Background
What's the antimeridian?
Also known as the 180th meridian, the antimeridian is the line of longitude on the opposite side of the world from the prime meridian. It can be either 180° east or west.
What's the problem?
The GeoJSON specification recommends cutting geometries at the antimeridian. Many real-world geometries, however, don't follow this recommendation. It's very common to create a geometry in a projected coordinate system, then reproject that geometry to WGS84 to use it in GeoJSON. The reprojection process usually does not split the output geometry across the antimeridian, leading to invalid geometries. Here's a simple example, taken from a real-world Landsat STAC item:
{
"type": "Polygon",
"coordinates": [
[
[
-179.70358951407547,
52.750507455036264
],
[
179.96672360880183,
52.00163609753924
],
[
-177.89334479610974,
50.62805205289558
],
[
-179.9847165338706,
51.002602948712465
],
[
-179.70358951407547,
52.750507455036264
]
]
]
}
As you can see, a tiny corner of the polygon crosses the antimeridian, leading to an invalid item:
The issue also arises when geometries cross over a pole.
How do we fix it?
We use a relatively simple algorithm that splits the input polygon into segments. Each segment is defined by jumps of greater than 180° longitude -- it's not a perfect heuristic, but tends to work for most real-world geometries we've encountered. Segments are then joined along the antimeridian. Segments that enclose the poles are constructed by adding points at the top of the antimeridian at both the east and the west longitudes.
Here's before and after pictures of some Sentinel 5p data. These are swath data that enclose both poles. In the before picture, you can see the strange artifacts created by the invalid geometry:
After correction, it's more clear that the data covers both poles:
Our library also handles splitting complex polygons that cross the antimeridian:
Developing
Clone and install in editable mode with the development optional dependencies:
git clone https://github.com/gadomski/antimeridian
pip install -e '.[dev,docs]'
We use pytest for tests:
pytest
We use Sphinx for docs:
make -C docs html
Contributing
Github issues and pull requests, please and thank you!
License
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file antimeridian-0.1.0.tar.gz
.
File metadata
- Download URL: antimeridian-0.1.0.tar.gz
- Upload date:
- Size: 10.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 16b9b27f177fa441ae4ac72d8eac62173878bb3bdc6f65c17df2b5c4dafba35e |
|
MD5 | 0d7ba3cbdb81c7028550c827fd929a8a |
|
BLAKE2b-256 | 162485797a06d6944fed92ebe703d29c193b7b8bf4a5b821b9a7e1fc830d25f0 |
File details
Details for the file antimeridian-0.1.0-py3-none-any.whl
.
File metadata
- Download URL: antimeridian-0.1.0-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.11.3
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 66a49d07e8a199a1e2f3a8ee4a8121a93a6965473d2c0f89c5d3b657969293c2 |
|
MD5 | ed31641bc3a2224be3aea59f8a161602 |
|
BLAKE2b-256 | 6a4ca0baae2b8d709fdc372883972d4c6a7f6f9b2eab05de4827fd2f72680bdc |