GeoJSON¶
GeoJSON is a popular format for encoding geographic
data. Its specification describes nine different types a message may take
(seven “geometry” types, plus two “feature” types). Here we provide one way of
implementing that specification using structtype to handle the parsing and
validation.
The loads and dumps methods defined below work similar to the
standard library’s json.loads / json.dumps, but:
Will result in high-level
structtype.Structobjects representing GeoJSON typesWill error nicely if a field is missing or the wrong type
Will fill in default values for optional fields
Decodes and encodes significantly faster than the
jsonmodule (as well as most otherjsonimplementations in Python).
This example makes use structtype.Struct types to define the different GeoJSON
types, and Tagged Unions to differentiate between them. See the
relevant docs for more information.
The full example source can be found here.
import structtype
Position = tuple[float, float]
# Define the 7 standard Geometry types.
# All types set `tag=True`, meaning that they'll make use of a `type` field to
# disambiguate between types when decoding.
class Point(structtype.Struct, tag=True):
coordinates: Position
class MultiPoint(structtype.Struct, tag=True):
coordinates: list[Position]
class LineString(structtype.Struct, tag=True):
coordinates: list[Position]
class MultiLineString(structtype.Struct, tag=True):
coordinates: list[list[Position]]
class Polygon(structtype.Struct, tag=True):
coordinates: list[list[Position]]
class MultiPolygon(structtype.Struct, tag=True):
coordinates: list[list[list[Position]]]
class GeometryCollection(structtype.Struct, tag=True):
geometries: list[Geometry]
Geometry = (
Point
| MultiPoint
| LineString
| MultiLineString
| Polygon
| MultiPolygon
| GeometryCollection
)
# Define the two Feature types
class Feature(structtype.Struct, tag=True):
geometry: Geometry | None = None
properties: dict | None = None
id: str | int | None = None
class FeatureCollection(structtype.Struct, tag=True):
features: list[Feature]
# A union of all 9 GeoJSON types
GeoJSON = Geometry | Feature | FeatureCollection
# Create a decoder and an encoder to use for decoding & encoding GeoJSON types
loads = structtype.json.Decoder(GeoJSON).decode
dumps = structtype.json.Encoder().encode
Here we use the loads method defined above to read some example GeoJSON.
In [1]: import structtype_geojson
In [2]: with open("canada.json", "rb") as f:
...: data = f.read()
In [3]: canada = structtype_geojson.loads(data)
In [4]: type(canada) # loaded as high-level, validated object
Out[4]: structtype_geojson.FeatureCollection
In [5]: canada.features[0].properties
Out[5]: {'name': 'Canada'}
Comparing performance to:
In [6]: %timeit structtype_geojson.loads(data) # benchmark structtype
6.15 ms ± 13.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [7]: %timeit orjson.loads(data) # benchmark orjson
8.67 ms ± 20.8 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
In [8]: %timeit json.loads(data) # benchmark json
27.6 ms ± 102 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
In [9]: %timeit geojson.loads(data) # benchmark geojson
93.9 ms ± 88.1 µs per loop (mean ± std. dev. of 7 runs, 10 loops each)
This shows that the readable structtype implementation above is 1.4x faster
than orjson (on this data), while also ensuring the loaded data is valid
GeoJSON. Compared to geojson (another validating geojson library for python),
loading the data using structtype was 15.3x faster.