Skip to main content

Fix GeoJSON geometries that cross the antimeridian

Project description

antimeridian

CI Status PyPI Read the Docs Test PyPI Status GitHub Contributor Covenant

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.

The antimeridian, from Wikipedia

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:

Landsat problem

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:

Sentinel 5p before

After correction, it's more clear that the data covers both poles:

Sentinel 5p after

Our library also handles splitting complex polygons that cross the antimeridian:

Complex split

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

Apache-2.0

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

antimeridian-0.0.2.post0.tar.gz (11.0 MB view details)

Uploaded Source

Built Distribution

antimeridian-0.0.2.post0-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file antimeridian-0.0.2.post0.tar.gz.

File metadata

  • Download URL: antimeridian-0.0.2.post0.tar.gz
  • Upload date:
  • Size: 11.0 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.11.3

File hashes

Hashes for antimeridian-0.0.2.post0.tar.gz
Algorithm Hash digest
SHA256 4333eabe6a5c7b039e5e3c95e0308c728a289e1a1e34dbd78cb4fcb93ac4d45c
MD5 8ffb9a408ffa7288b692707afd88d043
BLAKE2b-256 2184c75eb3c931129e8ddee0e85074a12d85e1e09224aff484d544d87d9957ca

See more details on using hashes here.

File details

Details for the file antimeridian-0.0.2.post0-py3-none-any.whl.

File metadata

File hashes

Hashes for antimeridian-0.0.2.post0-py3-none-any.whl
Algorithm Hash digest
SHA256 1f595ae4fbe5c1c4575b9e02e956831cb426767f8377f69db0bd5fb330959771
MD5 39ea2f5791d8f890b8156f1d412251da
BLAKE2b-256 a0bfcb2bbb60e70eb0741898f8539789c13b6b926458d5a9eaef8e08009b731c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page