Skip to main content

EWKB and TWKB parsing and conversion to GeoJSON

Project description

wkbparse

wkbparse is a Python module written in Rust for parsing EWKB and TWKB geometries into GeoJSON strings or GeoJSON-like Python dictionaries in a performant fashion.

Original EWKB/TWKB encoding code forked from https://github.com/Mortal/rust-ewkb which in turn originates from https://github.com/andelf/rust-postgis.

wkbparse is developed mainly for usage with PostGIS and has been tested with geometries generated with one. However, it has no explicit dependencies towards PostgreSQL or PostGIS and can be used with EWKB/TWKB geometries originating from any system. In such case it is advisable to validate results carefully before using for anything serious.

It supports reading and writing ZM geometries as well, even though GeoJSON specification doesn't really recognize the M coordinate. The M coordinate is simply output as the fourth coordinate in a vertex. Respectively, input GeoJSON dictionaries with four coordinates in a vertex are treated as ZM geometries.

Motivation

The main rationale behind this library is to offload compute related to geometry encoding from the database to the application and to minimize data transfer between them. This can be achieved by favoring native EWKB geometries or better-yet the transfer-optimized TWKB-geometries instead of making the database encode the data in some text-based format such as WKT or GeoJSON and sending that over the wire.

The benefits may be especially noticeable when dealing with large geometries with lots of vertices. E.g. the size of a 300 000 vertex multipolygon as EWKB is ~10 MB while as TWKB (1 cm precision) it is ~2 MB. Letting the database encode such geometry as GeoJSON and transferring it over the wire takes a long time (anecdotally way longer than a typical API timeout). Deserializing such MultiPolygon using wkbparse takes ~150 ms on an AMD Ryzen 4900 HS laptop and the transfer of TWKB is much quicker than of the other formats.

Installation

Pre-built wheels are available for the following platforms and python versions:

Python versions: [3.9, 3.10, 3.11, 3.12, 3.13, 3.14]

Platforms: Linux [x86_64, x86, aarch64, armv7, s390x, ppc64le], Windows: [x64, x86], MacOS: [x86_64, aarch64]

Install by saying pip install wkbparse.

Supported python version is >=3.9.

Tested on Python versions 3.12, 3.13, 3.14 on Linux x86_64.

Reprojection

Install wkbparse-proj instead of wkbparse to enable coordinate reprojection using the Proj project.

Using the reprojection-enabled package allows one to pass in from_srid and to_srid as integers corresponding to EPSG-codes to many of the functions. The from_srid argument may be omitted if the source EWKB bytes or GeoJSON-dictionary data already contains the SRID. TWKB data never contains the SRID.

Pre-installed proj library must be present on the system for this feature to work. See Proj installation.

Using wkbparse-proj bumps up the package size from ~250 kilobytes to ~10 megabytes due to rather large size of the Proj C++-dependency.

NOTE: Separate package is used instead of python "extras" within a single package due to extras not interacting nicely with wheels built with different Rust feature flags. This approach allows us to have a single codebase and a surefire way of selecting the appropriate package to avoid downloading redundant large dependencies.

Usage

This module implements the following functionalities:

  • TWKB to GeoJSON dictionary: twkb_to_geojson
  • TWKB to EWKB: twkb_to_ewkb
  • EWKB to GeoJSON dictionary: ewkb_to_geojson
  • GeoJSON dictionary to EWKB: geojson_to_ewkb
  • Reproject geojson reproject_geojson (only with wkbparse-proj)

The following is not currently implemented:

  • Support for GeometryCollection types
  • Encoding any data in TWKB

Example:

import wkbparse

twkb_bytes = bytes.fromhex("610805d00fa01f50")
geometry = wkbparse.twkb_to_geojson(twkb_bytes)
print(geometry)

The result dict has the following shape:

{
    type: str                # GeoJSON geometry type
    crs: Optional[int]       # Spatial reference system identifier
    coordinates: list[float] # nesting depth depending on geometry type
}

E.g.

{'type': 'Point', 'crs': None, 'coordinates': [1.0, 2.0, 4.0]}

To reproject data when using wkbparse-proj we can additionally pass in from_srid and to_srid

import wkbparse

twkb_bytes = bytes.fromhex("610805d00fa01f50")
geometry = wkbparse.twkb_to_geojson(twkb_bytes, from_srid=4326, to_srid=3857)
print(geometry)
{'type': 'Point', 'crs': 3857, 'coordinates': [111319.49079327357, 222684.20850554405, 4.0]}

If we already have a dictionary as above, we can reproject it with reproject_geojson:

import wkbparse

d = {"type": "Point", "crs": 3857, "coordinates": [111319.49079327357, 222684.20850554405, 4.0]}

reprojected = wkbparse.reproject_geojson(d, to_srid=4326)
print(reprojected)
{'type': 'Point', 'crs': 4326, 'coordinates': [0.9999999999999998, 1.9999999999999996, 4.0]}

Note that from_srid was omitted in this case as the input geometry already had the crs field. One may provide it anyway to override the crs.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

wkbparse_proj-0.2.2-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded PyPymanylinux: glibc 2.17+ x86-64

wkbparse_proj-0.2.2-cp315-cp315-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.15manylinux: glibc 2.17+ x86-64

wkbparse_proj-0.2.2-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.17+ x86-64

wkbparse_proj-0.2.2-cp314-cp314-macosx_11_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.14macOS 11.0+ ARM64

wkbparse_proj-0.2.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.17+ x86-64

wkbparse_proj-0.2.2-cp313-cp313-macosx_11_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.13macOS 11.0+ ARM64

wkbparse_proj-0.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.17+ x86-64

wkbparse_proj-0.2.2-cp312-cp312-macosx_11_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.12macOS 11.0+ ARM64

wkbparse_proj-0.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.17+ x86-64

wkbparse_proj-0.2.2-cp311-cp311-macosx_11_0_arm64.whl (2.2 MB view details)

Uploaded CPython 3.11macOS 11.0+ ARM64

wkbparse_proj-0.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.10manylinux: glibc 2.17+ x86-64

wkbparse_proj-0.2.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.7 MB view details)

Uploaded CPython 3.9manylinux: glibc 2.17+ x86-64

File details

Details for the file wkbparse_proj-0.2.2-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for wkbparse_proj-0.2.2-pp311-pypy311_pp73-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 90930ce934633bb2867ed1c7b39a204eae9fefb433b101f76cef58ee5551c17b
MD5 1561f7b227914261d9fb4c891f2d9e0c
BLAKE2b-256 d4a877b93cf4774bd4191d9138f801d1b9eb85594755159cfcd348e292109abe

See more details on using hashes here.

File details

Details for the file wkbparse_proj-0.2.2-cp315-cp315-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for wkbparse_proj-0.2.2-cp315-cp315-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 c234a3c6475f075de201e034f7661e805768422ba43ee750d6c31b9a04fceba6
MD5 21e901d349cfe02ec55320b950afb627
BLAKE2b-256 cd5cd046811cee1e0074328bcba09ee5f8479c429b0643b35031e8eebc597563

See more details on using hashes here.

File details

Details for the file wkbparse_proj-0.2.2-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for wkbparse_proj-0.2.2-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 5612213aab396b720b51810d8062308a8e8509ebfb0079b2d96e4aacb89e41d4
MD5 84ff6ed02b041c46ca749ad2f60d5d30
BLAKE2b-256 2609bc80cb093e68db1df4d0f3289176b65c25055d95cec050d7ba216e876de6

See more details on using hashes here.

File details

Details for the file wkbparse_proj-0.2.2-cp314-cp314-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for wkbparse_proj-0.2.2-cp314-cp314-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 db101c683b2d1f718f0c297b29214cf6f646a88aee720f91605442767735e24e
MD5 49efa6b8777f3ff2543949a018551015
BLAKE2b-256 0415ee01ffaa467f47ea4f53af2d1b52e25a9f38364ac932cd0ed4c69b943729

See more details on using hashes here.

File details

Details for the file wkbparse_proj-0.2.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for wkbparse_proj-0.2.2-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 2931401c6d072443d24b5f50744e1cf8dce1a918f70f0674e4db07fd5d3d2fc0
MD5 629f8a08ac6b716674739bb0dd66ceab
BLAKE2b-256 8f79a3d04d601909f15a1efa09264dcdfa29a5cf36749810e3ca839ac821d089

See more details on using hashes here.

File details

Details for the file wkbparse_proj-0.2.2-cp313-cp313-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for wkbparse_proj-0.2.2-cp313-cp313-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 a505b11d640758fd656feaa7294bfa1e40782a5eb1e5bade27e89f06d61767ec
MD5 61f5e8c98ad1a215cbeb835255b3d603
BLAKE2b-256 8c828fa2627342543f4233ef1f9c6a0e6c33b0130558b3251d2452ab4009fecb

See more details on using hashes here.

File details

Details for the file wkbparse_proj-0.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for wkbparse_proj-0.2.2-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6efb106586e08d85b0b5d671604ea8d23c94becdd8c5eab5d73f503670bd93ba
MD5 b5f94d542c7a1369688b865f65d990a5
BLAKE2b-256 0636b153fdd83a5691f41ea25ebaf0ecb291c3b9de82183c642e9d3576edd1b8

See more details on using hashes here.

File details

Details for the file wkbparse_proj-0.2.2-cp312-cp312-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for wkbparse_proj-0.2.2-cp312-cp312-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 aab1c0b5ec3815dcf20e709b93c2cc504404728628d021ffbf96cd9be0409741
MD5 ff290f3e05c97d5c29ff584fa4154c26
BLAKE2b-256 4dc3405b40fdbd6771117c842735af8bd2a3351d66636ee511995c557a959965

See more details on using hashes here.

File details

Details for the file wkbparse_proj-0.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for wkbparse_proj-0.2.2-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 00097a96f6f855bfb766529bc7f5b3b53792938235f978d8b6757d03f446a1f1
MD5 0fa17d5606c230f9226f665d0ba7c4ad
BLAKE2b-256 afa71f4f33f0c4aaa9c31ba275665f7462f84460049438cf4d09d1c33a190eaa

See more details on using hashes here.

File details

Details for the file wkbparse_proj-0.2.2-cp311-cp311-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for wkbparse_proj-0.2.2-cp311-cp311-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 c01114b87d23f69de6129f21f0e9979e7a01303d56bffa294e5f5d5c55565a01
MD5 b4a8b9859042647dd36b5b4a5d8c8677
BLAKE2b-256 25b28802fb7557cbb0ed6adf847bb7c6373e97254ba822e9791b3315a9ed1192

See more details on using hashes here.

File details

Details for the file wkbparse_proj-0.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for wkbparse_proj-0.2.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 a784a4c6e99e93bd563972ffc17ee5b318a4bca20231b949bd4d92e3764ac9ef
MD5 6cfb420a5fd93ac3c71cc72d8a050359
BLAKE2b-256 1dffe36e51096699b40581d2d8489d71d27ca45c11e09a20d62d07bb28ced0e7

See more details on using hashes here.

File details

Details for the file wkbparse_proj-0.2.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for wkbparse_proj-0.2.2-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 95f902aa270cec8e4af47b350f2ae5f126ca5bed7825987e5545670268733a84
MD5 3feac840cbbef5450ad72a94678039f1
BLAKE2b-256 df26538a9fcbe2e0a0933b7f87660afdb0161ce3e61346d0e282a6a924c1fe14

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