Skip to main content

GeoJSON AOI Parser

HOT

Parse and normalize a GeoJSON area of interest, using using PostGIS.

Publish Docs Publish Test Package version Downloads License


📖 Documentation: https://hotosm.github.io/geojson-aoi-parser/

🖥️ Source Code: https://github.com/hotosm/geojson-aoi-parser


Why do we need this?

  • We generally need an Area of Interest (AOI) specified for software to run on a geospatial area.
  • GeoJSON is a simple exchange format to communicate this AOI.
  • We only care about Polygon data types, but GeoJSON data can be quite variable, with many options for presenting data.
  • The goal of this package is to receive GeoJSON data in various forms, then produce a normalised output that can be used for further processing.

Priorities

  • Flexible data input: file bytes, dict, string JSON.
  • Flexible geometry input:
    • Polygon
    • MultiPolygons
    • GeometryCollection
    • Feature
    • FeatureCollection
  • Splits multipolygons into featcol of individual polygons.
  • Handle geometries nested inside GeometryCollection.
  • Remove any z-dimension coordinates.
  • Warn user if CRS is provided, in a coordinate system other than EPSG:4326.
  • Normalised output: FeatureCollection containing Polygon geoms.

Capturing The Warnings

If the GeoJSON has an invalid CRS, or coordinates seem off, a warning will be raised.

To install:

pip install geojson-aoi-parser

Basic example:

from geojson_aoi import parse_aoi

polygon_geojson = {
        "type": "Polygon",
        "coordinates": [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]],
    }

# Where 'db' is some upstream database connection.
feat_col = parse_aoi(db, polygon_geojson)

print(feat_col)
# {
#   'type': 'FeatureCollection',
#   'features': [
#       {
#           'type': 'Feature', 
#           'geometry': 
#               {
#                   'type': 'Polygon', 
#                   'coordinates': [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]
#                }
#        }
#     ]
# }

Basic async example:

from geojson_aoi import parse_aoi_async

polygon_geojson = {
        "type": "Polygon",
        "coordinates": [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]],
    }

# Where 'db' is some upstream database connection.
feat_col = parse_aoi_async(db, polygon_geojson)

print(feat_col)
# {
#   'type': 'FeatureCollection',
#   'features': [
#       {
#           'type': 'Feature', 
#           'geometry': 
#               {
#                   'type': 'Polygon', 
#                   'coordinates': [[[0, 0], [0, 1], [1, 1], [1, 0], [0, 0]]]
#                }
#        }
#     ]
# }

To halt execution when a warning is raised and act on it:

try:
    featcol = parse_aoi(db, raw_geojson)
except UserWarning as warning:
    log.error(warning.message)
    msg = "Using a valid CRS is mandatory!"
    log.error(msg)
    raise HTTPException(HTTPStatus.BAD_REQUEST, detail=msg)

To record warnings, but allow execution to continue:

import warnings

with warnings.catch_warnings(record=True) as recorded_warnings:
    featcol = parse_aoi(db, raw_geojson)

if recorded_warnings:
    for warning in recorded_warnings:
        if isinstance(warning.message, UserWarning)
            # do stuff with warning
            logger.warning(f"A warning was encountered: {warning.message}")

Create a New DB Connection

  • If your app upstream already has a psycopg connection, this can be passed through.
  • If you require a new database connection, the connection parameters can be defined as DbConfig object variables:
from geojson_aoi import parse_aoi, DbConfig

db = DbConfig(
    dbname="db1",
    user="user1",
    password="pass1",
    host="localhost",
    port="5432",
)

featcol = parse_aoi(db, raw_geojson)
  • Or alternatively as variables from your system environment:
GEOJSON_AOI_DB_NAME=aoi
GEOJSON_AOI_DB_USER=aoi
GEOJSON_AOI_DB_PASSWORD=pass
GEOJSON_AOI_DB_HOST=localhost
GEOJSON_AOI_DB_PORT=5432

then

from geojson_aoi import parse_aoi

featcol = parse_aoi(db, raw_geojson)

((All credit for DbConfig goes to https://github.com/hotosm/pg-nearest-city/))

History

  • Initially I tried to write a pure-Python implementation of this, no dependencies.
  • I underestimated the amount of work that is! It could be possible to reverse engineer C++ Geos or georust/geos, but it's more hassle than it's worth.
  • As all of the target install candidates for this package use a db driver anyway, I thought it wisest (and most time efficient) to use the PostGIS Geos implementation (specifically for the unary_union and convex_hull algorithms).
  • An additional advantage is the potential to port this to PGLite when the PostGIS extension is available, meaning AOI processing easily in the browser.

Metadata

Release files for geojson-aoi-parser 0.3.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for geojson-aoi-parser 0.3.5
File Size Uploaded Provenance Actions
geojson_aoi_parser-0.3.5.tar.gz 76.2 kB Without provenance Details for geojson_aoi_parser-0.3.5.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for geojson-aoi-parser 0.3.5
File Interpreter ABI Platform Actions
geojson_aoi_parser-0.3.5-py3-none-any.whl Python 3 none any Details for geojson_aoi_parser-0.3.5-py3-none-any.whl

Total release size: 105.4 kB

Release files / geojson_aoi_parser-0.3.5.tar.gz

Download URL geojson_aoi_parser-0.3.5.tar.gz
Size 76.2 kB
Tags Source
SHA-256 checksum
How to use checksums
4005883ab0eab4b2f8273ac19277e75e521e4aeafcbabcd106a01019f826e783
BLAKE2b-256 checksum
How to use checksums
6a7223ea59fc5d8408e1b20de9c7d45b7977b8979e1ea2d50278c44082182bfa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.9.3 CPython/3.12.3

Release files / geojson_aoi_parser-0.3.5-py3-none-any.whl

Download URL geojson_aoi_parser-0.3.5-py3-none-any.whl
Size 29.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
37d73f096c34ba12dba653198fd52369edf6edc713a3591284ececdb4093296e
BLAKE2b-256 checksum
How to use checksums
96a373b1d460a243939221815e3d65710cd13fce0f26c0621a44cb7c43ca8813
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via pdm/2.9.3 CPython/3.12.3

Security for geojson-aoi-parser 0.3.5

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page