Skip to main content

wkp (Python binding)

Python package with a minimal Shapely-first API on top of the WKP Rust core.

Architecture

  • src/lib.rs: the wkp._core extension, written with pyo3 and rust-numpy over wkp (core/)
  • python/wkp/: the pure-Python package (Shapely conversion, GeometryFrame, contexts)
  • Built with maturin as abi3 wheels: one wheel per platform works on every CPython from 3.10 up (including 3.14)
  • The package version comes from the Cargo workspace (Cargo.toml at the repo root); a runtime check enforces core compatibility
  • Large inputs are processed with the GIL released

Dependencies

  • Python >= 3.10
  • build: Rust (stable) and maturin (pulled in automatically by pip install)
  • runtime: numpy, shapely
  • dev/test: pytest, polyline

Install (dev)

From repo root:

pip install -e "bindings/python[dev]"     # or: cd bindings/python && maturin develop --release

Re-run after changing Rust code (Python changes are picked up directly).

Build

cd bindings/python
maturin build --release      # wheel for this platform, in ../../target/wheels
maturin sdist                # source distribution

Test

pytest bindings/python/tests

API

Geometry encode / decode

encode(geom, precision, *, ctx=None) -> str
decode(encoded, *, ctx=None) -> DecodedGeometry
decode_header(encoded) -> Header  # NamedTuple: (version, precision, dimensions, geometry_type)

encoded can be bytes, str (e.g. read from a text column or JSON) or any other bytes-like object. Encoders return str (use .encode("ascii") if you need bytes).

Supported geometries are Point, LineString (and LinearRing, which decodes as a LineString), Polygon, MultiPoint, MultiLineString and MultiPolygon, in 2D or 3D (XYZ). Geometries with M values raise ValueError: Shapely cannot rebuild them when decoding. Data with more than 3 dimensions can be encoded with encode_frame and decoded with decode_frame.

Limits

Precision is an integer from 0 to 63, and every |value| × 10^precision must be below 2^58 (e.g. ±2.8 × 10^12 at precision 5). Geometries and float streams have 1 to 16 dimensions, and values must be finite. Geometries must have the structure their type requires: one point for a Point, at least two per linestring, no empty parts.

Errors

Inputs wkp cannot encode or decode raise wkp.WkpError, a ValueError whose code is INVALID_ARGUMENT or MALFORMED_INPUT (the encoded data is not valid WKP). Arguments of the wrong type raise TypeError.

GeometryFrame — low-level flat representation

decode_frame(encoded, *, ctx=None) -> GeometryFrame
encode_frame(frame, *, ctx=None) -> str

GeometryFrame fields: version, precision, dimensions, geometry_type, coords (numpy float64 array, shape (n_points, dimensions)), segment_point_counts, group_segment_counts.

Methods: to_geometry() -> Shapely geometry, to_buffer() -> bytes, GeometryFrame.from_buffer(buf) -> GeometryFrame.

Float helpers

encode_floats(floats, precisions, *, ctx=None) -> str
decode_floats_array(encoded, precisions, *, ctx=None) -> np.ndarray  # flat (N*dims,) float64
decode_floats(encoded, precisions, *, ctx=None) -> np.ndarray        # shaped (N, dims) float64

decode_floats_array returns a flat 1D float64 array of interleaved values [x0, y0, x1, y1, ...] — a single allocation with no reshape. Use arr.tobytes() to treat it as a raw byte buffer for shipping over sockets or shared memory.

decode_floats wraps decode_floats_array and returns a (N, dims) 2D view (zero-copy reshape). Use .tolist() on the result if you need plain Python lists.

Context

Context()

A Context owns reusable native buffers. Buffers grow to fit the largest geometry processed, then stay allocated and get reused on subsequent calls — avoiding repeated allocations.

Thread-local (default) — one context per thread, created lazily, lives for the thread's lifetime:

encoded = encode(geom, precision)   # ctx=None uses the thread-local pool
decoded = decode(encoded)

Explicit reuse — create one context and pass it to a batch of calls; the pool is freed when the object is garbage-collected or explicitly deleted:

ctx = wkp.Context()
for geom in large_collection:
    encoded = encode(geom, precision, ctx=ctx)
    ...
del ctx  # in CPython, releases the native buffers once nothing else refers to ctx

A Context belongs to one thread at a time: using the same one from two threads concurrently raises RuntimeError: Already borrowed (use the default thread-local contexts, or one Context per thread). Inputs of 16k+ values are processed with the GIL released, so threads encoding or decoding large geometries run in parallel.

Per-call (no persistent memory) — create a fresh context for each call; buffers are freed as soon as the call returns:

encoded = encode(geom, precision, ctx=wkp.Context())

This costs one allocation/free per call but guarantees no native memory persists between calls — useful when peak RSS matters more than throughput.

Example

from shapely.geometry import LineString
from wkp import GeometryFrame, decode, decode_frame, encode

geom = LineString([
    (174.776, -41.289),
    (174.777, -41.290),
    (174.778, -41.291),
])

encoded = encode(geom, precision=6)
decoded = decode(encoded)

print(encoded)
print(decoded.geometry.wkt)

# Access the flat frame directly (useful for numpy / bulk processing)
frame = decode_frame(encoded)
print(frame.coords)          # numpy float64 array, shape (3, 2)
buf = frame.to_buffer()      # compact binary blob
frame2 = GeometryFrame.from_buffer(buf)  # round-trip
assert frame2 == frame

Benchmark

Full geometry encode/decode comparison against Shapely WKB/WKT:

python bindings/python/benchmark/benchmark.py --linestring-points=10000 --precisions=5 --max-iterations=1000 --max-duration=1

Flat-array API micro-benchmark (encode/decode/decode_frame/decode_floats_array):

python bindings/python/benchmark/bench_flat_api.py
python bindings/python/benchmark/bench_flat_api.py --points=50000 --iterations=200

Example results (wkp 0.6.0, Python 3.10, WSL2 on an i7-12700H, one core; x86-64, so the SIMD paths are used):

Method Source kb Encode (ms) Decode (ms) Total (ms) Iters
shapely_wkb nz-coast 156.3 0.23 ± 0.03 0.06 ± 0.02 0.29 ± 0.04 2000
shapely_wkt nz-coast 379.7 1.32 ± 0.15 4.37 ± 0.33 5.70 ± 0.35 988
shapely_wkt_5dp nz-coast 202.8 1.38 ± 0.12 2.76 ± 0.20 4.15 ± 0.24 1088
wkp-5p-bytes nz-coast 42.9 0.06 ± 0.02 0.05 ± 0.01 0.11 ± 0.02 2000
wkp-5p-str nz-coast 42.9 0.06 ± 0.01 0.05 ± 0.01 0.11 ± 0.02 2000
wkp-frame-5p nz-coast 42.9 0.07 ± 0.03 0.03 ± 0.01 0.10 ± 0.04 2000
shapely_wkb random 156.3 0.22 ± 0.03 0.06 ± 0.06 0.29 ± 0.07 2000
shapely_wkt random 378.4 1.35 ± 0.35 4.13 ± 0.27 5.53 ± 0.62 983
shapely_wkt_5dp random 163.9 1.49 ± 0.19 2.57 ± 0.25 4.06 ± 0.31 1058
wkp-5p-bytes random 72.2 0.06 ± 0.02 0.07 ± 0.01 0.13 ± 0.02 2000
wkp-5p-str random 72.2 0.06 ± 0.02 0.07 ± 0.03 0.13 ± 0.04 2000
wkp-frame-5p random 72.2 0.06 ± 0.01 0.05 ± 0.01 0.11 ± 0.02 2000

On the same machine, the 0.5.0 (nanobind + C core) build took 0.17 ms to encode and 0.18 ms to decode nz-coast (wkp-5p-str): 0.6.0 is about 2.8x / 3.6x faster.

Packaging / release

  • abi3 wheels (manylinux and musllinux x86_64/aarch64, Windows x64/arm64, macOS x86_64/arm64) and the sdist are built in .github/workflows/python.yml with PyO3/maturin-action, then tested on Python 3.10 and 3.14.
  • Release/publish runs from tag pushes or workflow_dispatch in .github/workflows/python.yml.
  • Tag publish (recommended): push a tag in the exact format pypi-vX.Y.Z (for example pypi-v0.1.0).
  • Publish order is always TestPyPI first, then PyPI.
  • Python releases are binding-specific. Product-level GitHub releases use the separate wkp-vX.Y.Z tag flow.

Manual publish from GitHub

  1. Open Actions -> Build and upload to PyPI.
  2. Run workflow dispatch.
  3. The workflow publishes to TestPyPI and then PyPI automatically.

If you only publish via git tags, manual dispatch is optional and can be removed.

Metadata

Release files for wkp 0.7.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 wkp 0.7.0
File Size Uploaded Provenance
wkp-0.7.0.tar.gz 122.0 kB With provenance Details

Built distributions (wheels)

Table of built distributions (wheels) for wkp 0.7.0
File
wkp-0.7.0-cp310-abi3-win_arm64.whl CPython 3.10 abi3 Windows ARM64 Details
wkp-0.7.0-cp310-abi3-win_amd64.whl CPython 3.10 abi3 Windows x86-64 Details
wkp-0.7.0-cp310-abi3-musllinux_1_2_x86_64.whl CPython 3.10 abi3 Linux musl 1.2+ x86-64 Details
wkp-0.7.0-cp310-abi3-musllinux_1_2_aarch64.whl CPython 3.10 abi3 Linux musl 1.2+ ARM64 Details
wkp-0.7.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl CPython 3.10 abi3 Linux glibc 2.17+ x86-64 Details
wkp-0.7.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl CPython 3.10 abi3 Linux glibc 2.17+ ARM64 Details
wkp-0.7.0-cp310-abi3-macosx_11_0_arm64.whl CPython 3.10 abi3 macOS 11.0+ ARM64 Details
wkp-0.7.0-cp310-abi3-macosx_10_12_x86_64.whl CPython 3.10 abi3 macOS 10.12+ x86-64 Details

Total release size: 2.4 MB

Release files / wkp-0.7.0.tar.gz

Download URL wkp-0.7.0.tar.gz
Size 122.0 kB
Tags Source
SHA-256 checksum
How to use checksums
895cc1c72e4f38bba57519c9950c2a0f0233d8cb454f58e1d819a69baccb44d6
BLAKE2b-256 checksum
How to use checksums
20075d2f62867cc40bbeb192489141fe70f611267f0556f6c86e5e8d26707de9
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / wkp-0.7.0-cp310-abi3-win_arm64.whl

Download URL wkp-0.7.0-cp310-abi3-win_arm64.whl
Size 173.8 kB
Tags CPython 3.10 Windows ARM64 abi3
SHA-256 checksum
How to use checksums
8ce42f8b0328adee34440df1e925a99899786ba56e4f9878707a6d7f217c927d
BLAKE2b-256 checksum
How to use checksums
fcd09d5c68241865eaed6bacef8407b59f5807562938ac39e1b7504a417f032c
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / wkp-0.7.0-cp310-abi3-win_amd64.whl

Download URL wkp-0.7.0-cp310-abi3-win_amd64.whl
Size 185.0 kB
Tags CPython 3.10 Windows x86-64 abi3
SHA-256 checksum
How to use checksums
375d73c004100380fdf3e2769df8160f19eb26257dd0b0e35905a9765bb6be1b
BLAKE2b-256 checksum
How to use checksums
037afa7ecd5f4abe7c8d911d825ff2a2dc414e79b164e9c645a8501d93f948f0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / wkp-0.7.0-cp310-abi3-musllinux_1_2_x86_64.whl

Download URL wkp-0.7.0-cp310-abi3-musllinux_1_2_x86_64.whl
Size 481.1 kB
Tags CPython 3.10 Linux musl 1.2+ x86-64 abi3
SHA-256 checksum
How to use checksums
d600d8f401f500d58b4297b7b7b067dd6749ef7d919f9496d003faaded970ce3
BLAKE2b-256 checksum
How to use checksums
057ad4dede8cdb452d3fd5bac68d0b90fb8ed1064259780ca3564e7ac1fcb562
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / wkp-0.7.0-cp310-abi3-musllinux_1_2_aarch64.whl

Download URL wkp-0.7.0-cp310-abi3-musllinux_1_2_aarch64.whl
Size 433.0 kB
Tags CPython 3.10 Linux musl 1.2+ ARM64 abi3
SHA-256 checksum
How to use checksums
2f8b75730682370e0f0198316abd4ad42adcff8016688bc0ffef4a5d0640c275
BLAKE2b-256 checksum
How to use checksums
6e05cc076fbfab3f43d46c6da4114d10f1a19c69245c3a717415ca84c471e3fd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / wkp-0.7.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl

Download URL wkp-0.7.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Size 269.4 kB
Tags CPython 3.10 Linux glibc 2.17+ x86-64 abi3
SHA-256 checksum
How to use checksums
f95fe859d9475cab0bc2d694d3e92300a8ca73aa3aa63b683bbddae602060c7b
BLAKE2b-256 checksum
How to use checksums
d839b6e743e44fd760b70faf98151f0184df92f72652c53bc7b38c6d76a337dd
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / wkp-0.7.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl

Download URL wkp-0.7.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Size 254.6 kB
Tags CPython 3.10 Linux glibc 2.17+ ARM64 abi3
SHA-256 checksum
How to use checksums
970fb7f8d108ea59797ed7f993bbe819bb71cf1f35b87f598f65f19bf694946f
BLAKE2b-256 checksum
How to use checksums
9d1821cdd36e3b30a341f41c515f0cd54773957bc462a309f381b8a97e7343e8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / wkp-0.7.0-cp310-abi3-macosx_11_0_arm64.whl

Download URL wkp-0.7.0-cp310-abi3-macosx_11_0_arm64.whl
Size 245.3 kB
Tags CPython 3.10 abi3 macOS 11.0+ ARM64
SHA-256 checksum
How to use checksums
df4489fa7fb533b4863c5fc21439b616ab585165db077132b5b311bca636b4c9
BLAKE2b-256 checksum
How to use checksums
0dbc0bbee0b8de00457fe930ccbdc5c6bcfb3156010b489f567c227b0955e8aa
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Release files / wkp-0.7.0-cp310-abi3-macosx_10_12_x86_64.whl

Download URL wkp-0.7.0-cp310-abi3-macosx_10_12_x86_64.whl
Size 258.2 kB
Tags CPython 3.10 abi3 macOS 10.12+ x86-64
SHA-256 checksum
How to use checksums
b1e849d8cc820ae4cf62b4f73df994fadb2d82ea0b0f8431686a6e2c4eb1d7f9
BLAKE2b-256 checksum
How to use checksums
d548339fea0aae9fe597c83fe4860bc3b4386d70373f5ff0e935d269341c17bf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

Security for wkp 0.7.0

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

All 9 files in this release have been uploaded with provenance metadata.

Select from 9 files

Attestation for wkp-0.7.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Oct 22, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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