Home | Research

picogeojson

conscientious and compliant GeoJSON

GeoJSON is a specification for encoding geographical features in JSON. GeoJSON is simple, reasonably flexible, and can be read using any stdlib JSON package.

picogeojson is a pure-Python package that ensures that the GeoJSON you create adheres to the 2016 GeoJSON specification, taking care of details such as polygon closing, winding order, and antimeridian-cutting. It also handles unmarshaling GeoJSON strings from unknown sources, allowing you to skip a lot of boilerplate and key-checking.

Installation

pip install -U picogeojson

Usage

Create and serialize a Geometry:

pt = picogeojson.Point((1.0, 3.0))
json_str = picogeojson.tostring(pt)

Or, load from a JSON string:

pt_from_json = picogeojson.fromstring('{"type": "Point", "coordinates": [1.0, 3.0]}')

Skip boilerplate error-checking using GeoJSONResult and extractor methods:

result = picogeojson.result_fromstring(api_response.decode("utf-8"))

# Expecting one or more points or multipoints
for geom in result.points:
    # do something with points
    # ...

for geom in result.multilinestrings:
    # do something with multilinestrings
    # ...

Extractor also work with Feature objects and can filter by .properties

result = picogeojson.result_fromstring(api_response.decode("utf-8"))

for feature in result.features("Polygon", {"type": "Lake", "state": "Oregon"}):
    # do something with lakes in Oregon
    # ...

Compose and deconstruct GeoJSON objects with merge() and burst():

points = [picogeojson.Point((1, 2)),
          picogeojson.Point((3, 4)),
          picogeojson.Point((5, 6))]

merged_points = picogeojson.merge(points)
original_points = picogeojson.burst(merged_points)

For a more full-featured Python geospatial package, take a look at Karta.

Source

github.com/fortyninemaps/picogeojson