contourrs
contourrs converts NumPy rasters into polygons with a Rust core and Python bindings. Use it to trace land-cover classes and segmentation masks, or extract filled contour bands from elevation and probability grids. The Python package requires no GDAL installation.
Documentation · API reference · PyPI · Benchmarks
Install
pip install contourrs
Wheels are available for Python 3.12–3.14 on Linux (x86_64 and ARM64), macOS (Apple Silicon), and Windows (x86_64). NumPy and PyArrow are installed as dependencies.
Polygonize a raster
Each connected region of equal-valued pixels becomes a polygon.
import numpy as np
from contourrs import shapes
labels = np.array([[1, 1, 2], [1, 2, 2], [3, 3, 3]], dtype=np.uint8)
for geometry, value in shapes(labels, connectivity=4):
print(value, geometry["type"])
# 1.0 Polygon
# 2.0 Polygon
# 3.0 Polygon
Use connectivity=8 to join regions that touch diagonally.
Like rasterio.features.shapes, each result pairs a GeoJSON geometry dictionary with its raster value.
shapes() returns a list rather than an iterator.
A 512 × 512 crop of the 2023 USDA Cropland Data Layer for Polk County, Iowa. The right panel shows polygons extracted in tiles and merged across tile boundaries.
Export to Arrow and GeoParquet
The Arrow variants return a pyarrow.Table with a WKB geometry column and a float64 value column.
They avoid constructing a Python geometry dictionary for every polygon and transfer buffers through the Arrow C Data Interface without copying them.
import pyarrow.parquet as pq
from contourrs import shapes_arrow
table = shapes_arrow(labels, connectivity=4)
pq.write_table(table, "polygons.parquet")
The table includes GeoParquet metadata. An affine transform sets output coordinates, but the CRS remains unknown until you assign it; see georeferenced export.
Extract contour bands
contours() interpolates boundaries between raster samples and returns filled polygons between consecutive thresholds.
Each result's value is the lower threshold of its band.
import numpy as np
from contourrs import contours
axis = np.linspace(-2, 2, 128)
x, y = np.meshgrid(axis, axis)
elevation = np.exp(-(x**2 + y**2))
bands = contours(elevation, thresholds=[0.1, 0.3, 0.5, 0.7, 1.0])
This extracts the bands [0.1, 0.3), [0.3, 0.5), [0.5, 0.7), and [0.7, 1.0).
Values outside those intervals are excluded.
Interpolated bands from a synthetic terrain with three peaks. The raster and filled bands share a color scale; white areas fall below the first threshold.
Both operations offer the same two output formats:
| Input | GeoJSON geometry/value pairs | Arrow table |
|---|---|---|
| Categorical raster | shapes() |
shapes_arrow() |
| Continuous raster | contours() |
contours_arrow() |
Masks and map coordinates
All four functions accept mask, nodata, and transform:
from contourrs import shapes_arrow
table = shapes_arrow(
labels,
nodata=0,
transform=(10, 0, 500000, 0, -10, 4500000),
)
Trueentries inmaskinclude samples. Usenodata=0to exclude zeros ornodata=np.nanto exclude NaNs.transformaccepts anaffine.Affineor the six coefficients(a, b, c, d, e, f)used by rasterio.- Inputs and masks must be two-dimensional and C-contiguous. Use
np.ascontiguousarray()after slicing or transposing when needed. - Accepted dtypes are
uint8,uint16,uint32,int16,int32,float32, andfloat64.
Polygonization follows pixel edges; contours interpolate between samples at integer (column, row) coordinates.
See the coordinate conventions when applying a raster transform.
Read raster files with rasterio or another loader, then pass the arrays to contourrs.
Tutorials
- Quickstart covers masks, transforms, and both output formats.
- DEM contours extracts elevation bands from synthetic terrain and a Mount Rainier DEM.
- Tiled CDL polygonization polygonizes land cover in tiles and merges their boundaries.
- TorchGeo field segmentation converts Fields of the World model predictions into field polygons.
Notebook sources are in examples/.
Performance notes describe the benchmark inputs, output costs, and memory measurements.
Development
git clone https://github.com/isaaccorley/contourrs.git
cd contourrs
make install
make test
make check
Run make build after changing Rust code.
The checks include Rust formatting and Clippy, Ruff, ty, Pyrefly, and the Rust test suite.
make test also runs the Python tests, including comparisons against rasterio.
See development and docs setup and the Rust architecture for more.
License
Release files for contourrs 0.8.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| contourrs-0.8.1.tar.gz | 46.3 kB | Details |
Built distributions (wheels)
Total release size: 9.4 MB
Release files / contourrs-0.8.1.tar.gz
| Download URL | contourrs-0.8.1.tar.gz |
|---|---|
| Size | 46.3 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
24cb12498d1a49fd1d074239f9be4323689e01dc8f6414798e47c7a3f859cc99
|
|
BLAKE2b-256 checksum How to use checksums |
7acebd9904273140719050a2d8b1faa171ef93bec6bed4474893c4f14eb6b07a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 25, 2026.
Transparency logRelease files / contourrs-0.8.1-cp314-cp314-win_amd64.whl
| Download URL | contourrs-0.8.1-cp314-cp314-win_amd64.whl |
|---|---|
| Size | 590.2 kB |
| Tags | CPython 3.14 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
7cd024ebe92b170b427ee3ef68449efbbb4dd9e87a9ac4ea87ce70fdaa5a9c91
|
|
BLAKE2b-256 checksum How to use checksums |
6aa628bec541121b22c4bbca21d656ffdab03b441e154840b8e24f1cef88b14e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 25, 2026.
Transparency logRelease files / contourrs-0.8.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | contourrs-0.8.1-cp314-cp314-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.14 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
4419a9cd4eadd427f496f9bd36668901940747a48ff7791d42000cbd4fea680b
|
|
BLAKE2b-256 checksum How to use checksums |
5f902fb38bd1f798594c294a735ed433ff58f13fcc31f0fa8e8c577ddb1e532d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 25, 2026.
Transparency logRelease files / contourrs-0.8.1-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | contourrs-0.8.1-cp314-cp314-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 787.0 kB |
| Tags | CPython 3.14 Linux glibc 2.17+ ARM64 |
|
SHA-256 checksum How to use checksums |
cf08eb8cb66fd16dc0fba28d320a8d8470aee8224acd1f0bfb1e3d08d414c7ec
|
|
BLAKE2b-256 checksum How to use checksums |
a0541564f027502c160f01082a0b323e7c97404e2cd27dadaafdb3b0cafbc574
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 25, 2026.
Transparency logRelease files / contourrs-0.8.1-cp314-cp314-macosx_11_0_arm64.whl
| Download URL | contourrs-0.8.1-cp314-cp314-macosx_11_0_arm64.whl |
|---|---|
| Size | 703.4 kB |
| Tags | CPython 3.14 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
f1dd0c8f693eca8445a7a95dc0f689a6d2c049e91e1e730d851e80a4d092e919
|
|
BLAKE2b-256 checksum How to use checksums |
08fef4ba527f94e2c32c1e0917e13b37f2b4b14aaaf208785d23d4e35121dc19
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 25, 2026.
Transparency logRelease files / contourrs-0.8.1-cp313-cp313-win_amd64.whl
| Download URL | contourrs-0.8.1-cp313-cp313-win_amd64.whl |
|---|---|
| Size | 589.9 kB |
| Tags | CPython 3.13 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
f54bc2e014d89903f3d9a78f2f80ca393bf15d4040586969cd94cdc454656c0e
|
|
BLAKE2b-256 checksum How to use checksums |
2f817fb3c0633c142734e3bcd967577cf1ca169a966ddc6ff3e09d4a6b4c9544
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 25, 2026.
Transparency logRelease files / contourrs-0.8.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | contourrs-0.8.1-cp313-cp313-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.13 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
5815cbbca7fbe5251a9a503f667d9292c018a3a8fe18f10115bed282554c2ffa
|
|
BLAKE2b-256 checksum How to use checksums |
2010240c7f679e9f756951a6e9fad219505d3c227448bc42d69ea2a77e50b244
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 25, 2026.
Transparency logRelease files / contourrs-0.8.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | contourrs-0.8.1-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 785.9 kB |
| Tags | CPython 3.13 Linux glibc 2.17+ ARM64 |
|
SHA-256 checksum How to use checksums |
01264dea34f34d2154a564ff36a492840641f5898bf592b7399bf4e2bb785d98
|
|
BLAKE2b-256 checksum How to use checksums |
bc87f2f9a7533d397d679174e2136f5b0c4254f41ebc92d6929bf1e75da25dfa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 25, 2026.
Transparency logRelease files / contourrs-0.8.1-cp313-cp313-macosx_11_0_arm64.whl
| Download URL | contourrs-0.8.1-cp313-cp313-macosx_11_0_arm64.whl |
|---|---|
| Size | 702.8 kB |
| Tags | CPython 3.13 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
8b42ff1b490f0485d0ac7736c96d4b236f65e139a867377ca3d88103d567648b
|
|
BLAKE2b-256 checksum How to use checksums |
b1b8a6bdec50f5d77c7d08c17f37bd7d302e2907a676fcd557584cb36ed0d6f7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 25, 2026.
Transparency logRelease files / contourrs-0.8.1-cp312-cp312-win_amd64.whl
| Download URL | contourrs-0.8.1-cp312-cp312-win_amd64.whl |
|---|---|
| Size | 590.0 kB |
| Tags | CPython 3.12 Windows x86-64 |
|
SHA-256 checksum How to use checksums |
eea9bfa1eda310c0b1108d17e6a387b253f13703166885518ae6415dd608b6cb
|
|
BLAKE2b-256 checksum How to use checksums |
7a9409488cc9c2e13ac9c72e1df2f40c477b689779de70950d4788ed7496a94a
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 25, 2026.
Transparency logRelease files / contourrs-0.8.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | contourrs-0.8.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 1.1 MB |
| Tags | CPython 3.12 Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
96f90c29bddfef227e41d5c2eb681ce934f18d929ff49ef631d94caa3bd694b4
|
|
BLAKE2b-256 checksum How to use checksums |
9f1f6fb6ab190a9ea711d67675e7bc10f3cfa76c6d811f8e25f089985ad202d6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 25, 2026.
Transparency logRelease files / contourrs-0.8.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
| Download URL | contourrs-0.8.1-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl |
|---|---|
| Size | 786.0 kB |
| Tags | CPython 3.12 Linux glibc 2.17+ ARM64 |
|
SHA-256 checksum How to use checksums |
00968d0d4abbc14274b43fac0535960e32ab47106db2f08168dc96dbfb6259d7
|
|
BLAKE2b-256 checksum How to use checksums |
5b70e317db23f98bba945eb521d3f3a7805f273e14c2888758d8095aa6125558
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 25, 2026.
Transparency logRelease files / contourrs-0.8.1-cp312-cp312-macosx_11_0_arm64.whl
| Download URL | contourrs-0.8.1-cp312-cp312-macosx_11_0_arm64.whl |
|---|---|
| Size | 703.0 kB |
| Tags | CPython 3.12 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
f3d5468d5463f5bbb5c01b482c222c503070965398d87aa4c38553ef138c97e2
|
|
BLAKE2b-256 checksum How to use checksums |
8232413ddb8e7928bf926119b49f6234dac264dbf2d01952fff81d066b28450c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
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 Sep 25, 2026.
Transparency log