Skip to main content

High performance rasterization tool for Python build in Rust

Project description

rusterize

High performance rasterization tool for python built in Rust. This repository is heavily based on the fasterize package built in C++ for R. This version ports it to Python with a Rust backend.

Functionally, it takes an input geopandas dataframes and returns a xarray. It tighly mirrors the processing routine of fasterize, so it works only on (multi)polygon geometries at the moment.

Installation

Install the current version with pip:

pip install rusterize

Contributing

Any contribution is welcome! You can install rusterize directly from this repo using maturin as an editable package. For this to work, you’ll need to have Rust and cargo installed.

# Clone repo
git clone https://github.com/<username>/rusterize.git
cd rusterize

# Install the Rust nightly toolchain
rustup toolchain install nightly-2025-01-05

 # Install maturin
pip install maturin

# Install editable version with optmized code
maturin develop --profile dist-release

API

This function has a simple API:

from rusterize.core import rusterize

# gdf = <import datasets as needed>

# rusterize
rusterize(gdf,
          (30, 30),
          "field",
          "by",
          "sum",
          0) 
  • gdf: geopandas dataframe to rasterize
  • res: tuple of (xres, yres) for final resolution
  • field: field to rasterize. Default is None (a value of 1 is rasterized).
  • by: column to rasterize. Assigns each group to a band in the stack. Values are taken from field. Default is None
  • fun: pixel function to use when multiple values overlap. Default is last. Available options are sum, first, last, min, max, count, or any
  • background: background value in final raster. Default is None (NaN)

Usage

rusterize consists of a single function rusterize(). The Rust implementation returns an array that is then converted to a xarray on the Python side for simpliicty.

from rusterize.core import rusterize
import geopandas as gpd
from shapely import wkt
import matplotlib.pyplot as plt

# example from fasterize
polygons = [
    "POLYGON ((-180 -20, -140 55, 10 0, -140 -60, -180 -20), (-150 -20, -100 -10, -110 20, -150 -20))",
    "POLYGON ((-10 0, 140 60, 160 0, 140 -55, -10 0))",
    "POLYGON ((-125 0, 0 60, 40 5, 15 -45, -125 0))"
]

# Convert WKT strings to Shapely geometries
geometries = [wkt.loads(polygon) for polygon in polygons]

# Create a GeoDataFrame
gdf = gpd.GeoDataFrame({'value': range(1, len(polygons) + 1)}, geometry=geometries, crs='EPSG:32619')

# rusterize
output = rusterize(
    gdf,
    res=(1, 1),
    field="value",
    fun="sum"
).squeeze()

# plot it
fig, ax = plt.subplots(figsize=(12, 6))
output.plot.imshow(ax=ax)
plt.show()

Benchmarks

fasterize is fast and so is rusterize! Let’s try it on small and large datasets.

from rusterize.core import rusterize
import geopandas as gpd
import requests
import zipfile
from io import BytesIO

# large dataset (~380 MB)
url = "https://s3.amazonaws.com/hp3-shapefiles/Mammals_Terrestrial.zip"
response = requests.get(url)

# unzip
with zipfile.ZipFile(BytesIO(response.content), 'r') as zip_ref:
    zip_ref.extractall()
    
# read
gdf_large = gpd.read_file("Mammals_Terrestrial/Mammals_Terrestrial.shp")

# small dataset (first 1000 rows)
gdf_small = gdf_large.iloc[:1000, :]

# rusterize at 1/6 degree resolution
def test_large(benchmark):
  benchmark(rusterize, gdf_large, (1/6, 1/6), fun="sum")
   
def test_small(benchmark):
  benchmark(rusterize, gdf_small, (1/6, 1/6), fun="sum")  

Then you can run it with pytest and pytest-benchmark:

pytest <python file> --benchmark-min-rounds=20 --benchmark-time-unit='s'

--------------------------------------------- benchmark: 1 tests --------------------------------------------
Name (time in s)         Min      Max     Mean  StdDev   Median     IQR  Outliers     OPS  Rounds  Iterations
-------------------------------------------------------------------------------------------------------------
test_large           10.5870  11.2302  10.8633  0.1508  10.8417  0.1594       4;1  0.0921      20           1
test_small            0.5083   0.6416   0.5265  0.0393   0.5120  0.0108       2;2  1.8995      20           1
-------------------------------------------------------------------------------------------------------------

And fasterize:

large <- st_read("Mammals_Terrestrial/Mammals_Terrestrial.shp", quiet = TRUE)
small <- large[1:1000, ]
fn <- function(v) {
  r <- raster(v, res = 1/6)
  return(fasterize(v, r, fun = "sum"))
}
microbenchmark(
  fasterize_large = f <- fn(large),
  fasterize_small = f <- fn(small),
  times=20L,
  unit='s'
)
Unit: seconds
      expr             min        lq      mean    median        uq       max  neval
 fasterize_large  9.565781  9.815375  10.02838  9.984965  10.18532  10.66656     20
 fasterize_small  0.469389  0.500616  0.571851  0.558818  0.613419  0.795159     20

And on even larger datasets? This is a benchmark with 350K+ geometries rasterized at 30 meters (20 rounds) with no field value and pixel function sum.

# rusterize
--------------------------------------------- benchmark: 1 tests --------------------------------------------
Name (time in s)         Min      Max     Mean  StdDev   Median     IQR  Outliers     OPS  Rounds  Iterations
-------------------------------------------------------------------------------------------------------------
test_sbw             46.5711  49.0212  48.4340  0.5504  48.5812  0.5054       3;1  0.0206      20           1
-------------------------------------------------------------------------------------------------------------

# fasterize
Unit: seconds
      expr      min       lq     mean   median       uq      max neval
 fasterize 62.12409 72.13832 74.53424 75.12375 77.72899 84.77415    20

Comparison with other tools

While rusterize is fast, there are other very fast solutions out there, including

  • GDAL
  • rasterio
  • geocube

However, rusterize allows for a seamless, Rust-native processing with similar or lower memory footprint that doesn't require you to leave Python, and returns the geoinformation you need for downstream processing.

The following is a time comparison run on a dataset with 340K+ geometries, rasterized at 2m resolution.

rusterize:   24 sec
fasterize:   47 sec
GDAL (cli):  40 sec (read from fast drive, write to fast drive)
rasterio:    20 sec (but no spatial information)
geocube:     42 sec (larger memory footprint)

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

rusterize-0.1.0.tar.gz (41.6 kB view details)

Uploaded Source

Built Distributions

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

rusterize-0.1.0-cp310-abi3-win_amd64.whl (11.3 MB view details)

Uploaded CPython 3.10+Windows x86-64

rusterize-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl (11.6 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ x86-64

rusterize-0.1.0-cp310-abi3-musllinux_1_2_armv7l.whl (11.8 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARMv7l

rusterize-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl (10.7 MB view details)

Uploaded CPython 3.10+musllinux: musl 1.2+ ARM64

rusterize-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (11.6 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

rusterize-0.1.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (12.2 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ppc64le

rusterize-0.1.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (11.5 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARMv7l

rusterize-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (10.6 MB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

rusterize-0.1.0-cp310-abi3-macosx_11_0_arm64.whl (10.2 MB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

rusterize-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl (11.3 MB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file rusterize-0.1.0.tar.gz.

File metadata

  • Download URL: rusterize-0.1.0.tar.gz
  • Upload date:
  • Size: 41.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for rusterize-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4354a71a476f32f9798312edce00e48cbcdd094643904d1b69b00fc3982973f6
MD5 dd6de28caed1170f2ed236472aee6c57
BLAKE2b-256 306de2b8185c2cda8aeb9741791e4b93000b24f3b5ac0e7618603097f4c572c7

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusterize-0.1.0.tar.gz:

Publisher: CI.yml on ttrotto/rusterize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rusterize-0.1.0-cp310-abi3-win_amd64.whl.

File metadata

  • Download URL: rusterize-0.1.0-cp310-abi3-win_amd64.whl
  • Upload date:
  • Size: 11.3 MB
  • Tags: CPython 3.10+, Windows x86-64
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.8

File hashes

Hashes for rusterize-0.1.0-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 60b0c2d4b5edfc7c26f555494ea87baf1f6e199bc253d5122bf911755c0af397
MD5 be8c171cba3eaab37eacc117f8e8bdbc
BLAKE2b-256 7163eac721c5bb85089d8bf14857b2a84e2f711d154b4aaef8bccbef8f80bd83

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusterize-0.1.0-cp310-abi3-win_amd64.whl:

Publisher: CI.yml on ttrotto/rusterize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rusterize-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl.

File metadata

File hashes

Hashes for rusterize-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl
Algorithm Hash digest
SHA256 031d59268c100cb16da1a43c2dc8c04ce40461c598444c5af799038982f48ebd
MD5 6f6388916a73b5071bf24786a87386a7
BLAKE2b-256 6942b2c68e815bf5915bdccc20b1cb4dbd4168c725acc8137f87bf579fef1df8

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusterize-0.1.0-cp310-abi3-musllinux_1_2_x86_64.whl:

Publisher: CI.yml on ttrotto/rusterize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rusterize-0.1.0-cp310-abi3-musllinux_1_2_armv7l.whl.

File metadata

File hashes

Hashes for rusterize-0.1.0-cp310-abi3-musllinux_1_2_armv7l.whl
Algorithm Hash digest
SHA256 c927ac1f051a7003a6ca74bc6e4316bbc0b886848f22aea2ae89ef62a38ed6dc
MD5 904310b14ffe852e4a3360337f279976
BLAKE2b-256 a67e922fd4a7dc4cb3f8e0b92713a7fbcfc73d539a766ce1cab5a1bc31c0f918

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusterize-0.1.0-cp310-abi3-musllinux_1_2_armv7l.whl:

Publisher: CI.yml on ttrotto/rusterize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rusterize-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl.

File metadata

File hashes

Hashes for rusterize-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl
Algorithm Hash digest
SHA256 522828e26cb7285f03b72497ce8ba87d00a3e397fd569799beed35736ca83a5a
MD5 56100a69750cb3d04ede319b1ec8f39a
BLAKE2b-256 76316b70a9d2381e5b6e5bf9a961f3bab2fa6751dde7bdc70406bba2b3bd603c

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusterize-0.1.0-cp310-abi3-musllinux_1_2_aarch64.whl:

Publisher: CI.yml on ttrotto/rusterize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rusterize-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for rusterize-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 6c060a56c694a569f610334d7767489bed02b5869cd54e766b53c322c2149347
MD5 fdcc9180c553c5e29c974cd6482e48ab
BLAKE2b-256 e16c829ad3240c8bed1bb76ba1e03b5c1ca48193703d18a1d14ca99ea487312d

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusterize-0.1.0-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl:

Publisher: CI.yml on ttrotto/rusterize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rusterize-0.1.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl.

File metadata

File hashes

Hashes for rusterize-0.1.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl
Algorithm Hash digest
SHA256 3dc929f852089df694be02e13175390148c90025f9843da4cb6e16c1c896ba4c
MD5 fba1a0447e8ab96ffc7e34007e96c9a6
BLAKE2b-256 88c2a0335390138d09fa823c0acdface6aa354abff8859f5053aa227cfb24cf4

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusterize-0.1.0-cp310-abi3-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl:

Publisher: CI.yml on ttrotto/rusterize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rusterize-0.1.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl.

File metadata

File hashes

Hashes for rusterize-0.1.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl
Algorithm Hash digest
SHA256 54f6d32346b726349611c2ad22cef76daaa4eb53c4805b4b2ae1a4148e6a7b64
MD5 b3c5e1fdb5de8eb58587148a7cc50044
BLAKE2b-256 d7a94fdaad70178db2808ccf9c534e36a0460c37d9043d94a73a186f85a2d21c

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusterize-0.1.0-cp310-abi3-manylinux_2_17_armv7l.manylinux2014_armv7l.whl:

Publisher: CI.yml on ttrotto/rusterize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rusterize-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for rusterize-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 062217d0189bac8ed6126c42b7c080962a2772571ab3ce4f9fdb79b3ffaa238b
MD5 b6e737be7cae06aac2138fa7faf83030
BLAKE2b-256 2a1b14d43c75691d7514fc7ed20b68c4bbb0e02b8745a21607b8804ec8de4590

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusterize-0.1.0-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl:

Publisher: CI.yml on ttrotto/rusterize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rusterize-0.1.0-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for rusterize-0.1.0-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 d9a33c6c3076a3c9a366997cfffbeaa96c6479984f0eec05a39c3b0a377d5e61
MD5 df5b06f16e96b4468ca6cbcebbd90e87
BLAKE2b-256 2e45a21b602b0b50b515038d660dde188924669d48da981c637a5afe05996639

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusterize-0.1.0-cp310-abi3-macosx_11_0_arm64.whl:

Publisher: CI.yml on ttrotto/rusterize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file rusterize-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for rusterize-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 41defc2668ed3ba9007ec03afeb1cd3364c62f5eb4a1c053d2ebb3f5a736b91a
MD5 c175c203b3e48f2bbd2b0b850bd54a53
BLAKE2b-256 9880e70f599a796c2764bfab7dcc74adc285e12610fd7cb61fd13bac0d67e238

See more details on using hashes here.

Provenance

The following attestation bundles were made for rusterize-0.1.0-cp310-abi3-macosx_10_12_x86_64.whl:

Publisher: CI.yml on ttrotto/rusterize

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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