Skip to main content

msgspec GeoJSON model

Project description

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.

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

geojson_msgspec-1.0.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

geojson_msgspec-1.0.0-py3-none-any.whl (4.1 kB view details)

Uploaded Python 3

File details

Details for the file geojson_msgspec-1.0.0.tar.gz.

File metadata

  • Download URL: geojson_msgspec-1.0.0.tar.gz
  • Upload date:
  • Size: 8.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: uv/0.9.5

File hashes

Hashes for geojson_msgspec-1.0.0.tar.gz
Algorithm Hash digest
SHA256 85314c11470c77d7a742aeeaf0a84fbb5a1be6a15f706a5aa9123d72c0fe15d7
MD5 56e294bba9763f339ca4983caa0883ed
BLAKE2b-256 db13d97b5a4da0f89b5a13542c41d2a80cff9a440b92500018dbe47b45a3e420

See more details on using hashes here.

File details

Details for the file geojson_msgspec-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for geojson_msgspec-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9f9bcf8d406b85b5706b51f5cd705b85f86791c92cf3fd16ee268ab86d4a09c7
MD5 b9284b3874b520c5eb9ee7f829be8823
BLAKE2b-256 710b28a44b14fb8daafdffd5f308f26cb0fed83698b9695e548087eaaf90dfa5

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page