Skip to main content

This project contains code published as an example in the msgspec project <https://jcristharif.com/msgspec/> backported to python 3.8.

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 msgspec 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 msgspec.Struct objects representing GeoJSON types

  • Will 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 json module (as well as most other json implementations in Python).

This example makes use msgspec.Struct types to define the different GeoJSON types, and struct-tagged-unions to differentiate between them. See the relevant docs for more information.

The original source code can be found here.

Here we use the loads method defined above to read some example GeoJSON.

In [1]: import msgspec_geojson

In [2]: with open("canada.json", "rb") as f:
   ...:     data = f.read()

In [3]: canada = msgspec_geojson.loads(data)

In [4]: type(canada)  # loaded as high-level, validated object
Out[4]: msgspec_geojson.FeatureCollection

In [5]: canada.features[0].properties
Out[5]: {'name': 'Canada'}

Comparing performance to:

In [6]: %timeit msgspec_geojson.loads(data)  # benchmark msgspec
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 msgspec 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 msgspec was 15.3x faster.

Release files for geojson-msgspec 1.1.0

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-msgspec 1.1.0
File Size Uploaded
geojson_msgspec-1.1.0.tar.gz 9.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for geojson-msgspec 1.1.0
File Interpreter ABI Platform
geojson_msgspec-1.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 13.8 kB

Release files / geojson_msgspec-1.1.0.tar.gz

Download URL geojson_msgspec-1.1.0.tar.gz
Size 9.7 kB
Tags Source
SHA-256 checksum
How to use checksums
0d60380afeb6c16cf8418d17236c0ff288c73f52da637071350746214032fb10
BLAKE2b-256 checksum
How to use checksums
3487ea9f5abc32d791dec17eef6f53253333e56d672cd3d8c1c63635ae78413a
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / geojson_msgspec-1.1.0-py3-none-any.whl

Download URL geojson_msgspec-1.1.0-py3-none-any.whl
Size 4.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
ed5c8150d4a7cd0d5a94ce9553c61b732e042a0111d3a579f147ed974d29ab96
BLAKE2b-256 checksum
How to use checksums
e63b47046e7c124b5b470bb2f1b36b88423cde566aab87b317b4eab5cb099d69
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.11.28 {"installer":{"name":"uv","version":"0.11.28","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

1.1.0 This release

2 release files

1.0.0

2 release files

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