Skip to main content

Fix GeoJSON geometries that cross the antimeridian

Project description

antimeridian

CI Status PyPI Docs Status 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]'

We use pytest for test:

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.1.post1.tar.gz (11.0 MB view details)

Uploaded Source

Built Distribution

antimeridian-0.0.1.post1-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file antimeridian-0.0.1.post1.tar.gz.

File metadata

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

File hashes

Hashes for antimeridian-0.0.1.post1.tar.gz
Algorithm Hash digest
SHA256 cb40506d3d5ae4373a337ecffe6be443c29136f3e424b7ce98aaeb79cdd6b9ac
MD5 3a14ed4232923e8f3bb4424d143b8e42
BLAKE2b-256 c9c83576e15fa5cdce5bcd3efd782c942170334e4679cf3e2aae5b8b175c86b6

See more details on using hashes here.

Provenance

File details

Details for the file antimeridian-0.0.1.post1-py3-none-any.whl.

File metadata

File hashes

Hashes for antimeridian-0.0.1.post1-py3-none-any.whl
Algorithm Hash digest
SHA256 fa26ea12c003d1fb6a3926fc0a3d91ddcb5ec57931704cdea595b26d8666fbb8
MD5 4cb1ca5ee80701676fcb695af7a61e1e
BLAKE2b-256 f80086a29ea7060dad7d6a3a53078b46da2092edcd7be195c86a28dd548444d5

See more details on using hashes here.

Provenance

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