Skip to main content

Rasterizer

PyPI - Version Documentation Status Pytest

rasterizer is a lightweight Python package that speeds up rasterization of geopandas GeoDataFrames by specializing in regular, axis-aligned rectangular grids.

Features

  • Rasterize lines into a binary (presence/absence) or length-based grid.
  • Rasterize polygons into a binary (presence/absence) or area-based grid.
  • Fast because it targets regular rectilinear grids described by 1D x and y cell-center coordinates with constant spacing.
  • Fast large-polygon rasterization: exact per-cell clipping for tiny bounding boxes, signed per-edge area accumulation for everything larger, so cost scales with edge-cell crossings instead of vertices times boundary cells.
  • Weighted rasterization: Rasterize geometries while weighting the output by a numerical column in the GeoDataFrame.
  • Works with geopandas GeoDataFrames.
  • Outputs an xarray.DataArray for easy integration with other scientific Python libraries.
  • No GDAL dependency for the rasterization algorithm itself.

For detailed usage and API documentation, please see the full documentation.

Installation

You can install the package directly from PyPI:

pip install rasterizer

Usage

Here are some examples of what you can do with rasterizer.

import geopandas as gpd
from rasterizer import rasterize_polygons

polys = gpd.read_file("polygons.gpkg")
area_raster = rasterize_polygons(polys, your_x_grid, your_y_grid, polys.crs, mode="area")

# Enable a tqdm progress bar when processing large geometry collections.
area_raster = rasterize_polygons(
    polys,
    your_x_grid,
    your_y_grid,
    polys.crs,
    mode="area",
    progress_bar=True,
)

Rasterizing Lines

You can rasterize lines in either binary or length mode.

Binary Mode Length Mode
Lines - Binary Lines - Length

Rasterizing Polygons

You can rasterize polygons in either binary or area mode.

For polygon workloads, rasterizer picks an internal strategy per polygon. Small polygon bounding boxes are handled with exact per-cell clipping. Larger ones use a signed per-edge area accumulation: each edge distributes exact area and cover contributions over the cells it crosses, and a row sweep turns the running cover into interior fill. Boundary cells keep exact fractional areas while the total cost scales with edge-cell crossings plus bbox size instead of re-clipping the polygon for every boundary cell.

Binary Mode Area Mode
Polygons - Binary Polygons - Area

Large Dataset Showcase

This real-world example uses 606,667 building polygons on a 10 m Lambert-93 grid covering Paris. The area rasterization step completes in 3.9 s on a regular laptop used as the local documentation machine for a 2804 x 1978 grid.

import geopandas as gpd
import numpy as np
from rasterizer import rasterize_polygons

buildings = gpd.read_file(
    "BDT_3-5_GPKG_LAMB93_D075-ED2026-03-15.gpkg",
    layer="batiment",
    columns=[],
)

xmin, ymin, xmax, ymax = buildings.total_bounds
x = np.arange(xmin, xmax, 10.0)
y = np.arange(ymin, ymax, 10.0)

coverage = rasterize_polygons(buildings, x=x, y=y, crs=buildings.crs, mode="area")

Large dataset showcase

The full walkthrough, including the benchmark context and reproduction script, is available in the large dataset showcase documentation.

Why rasterizer

This package provides functionalities that are not present in rasterio.features, such as area and length-based rasterization. It is also lighter and faster than using more general GDAL-based solutions because it is specialized for regular rectilinear grids instead of arbitrary raster layouts. GDAL's rasterization only burns values per pixel; it cannot return exact fractional area or length contributions without an expensive workaround. The common workaround is to rasterize at a much finer resolution and then downsample with averaging, which approximates the true area/length but is not exact and can be slow, e.g.:

gdal_rasterize -burn 1 -tr 1 1 -ot Float32 -of GTiff input.gpkg tmp_fine.tif
gdalwarp -tr 10 10 -r average tmp_fine.tif out_area_approx.tif

Doing this purely in geopandas by generating one polygon per grid cell and overlaying it with the input geometry is also slow because it creates a huge number of tiny geometries, triggers expensive overlay operations, and scales poorly with grid size.

That speed-up comes with a deliberate constraint: rasterizer is built for regular, axis-aligned rectangular grids, not for arbitrary affine transforms or irregular meshes.

Metadata

Release files for rasterizer 0.3.5

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for rasterizer 0.3.5
File Size Uploaded
rasterizer-0.3.5.tar.gz 25.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for rasterizer 0.3.5
File Interpreter ABI Platform
rasterizer-0.3.5-py3-none-any.whl Python 3 none any Details

Total release size: 44.0 kB

Release files / rasterizer-0.3.5.tar.gz

Download URL rasterizer-0.3.5.tar.gz
Size 25.0 kB
Tags Source
SHA-256 checksum
How to use checksums
883adc78f0fd22fb9e78e0af21ebdb11912377ff8cb62990e1df5fa7a38002b0
BLAKE2b-256 checksum
How to use checksums
4808550485f13111bf1c4b73ef584795a0d005c619028fa296892e2306b49082
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release files / rasterizer-0.3.5-py3-none-any.whl

Download URL rasterizer-0.3.5-py3-none-any.whl
Size 19.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
679297df9d4d5315cc1b94126962e8bd668128229e1234733824d055ece53fa8
BLAKE2b-256 checksum
How to use checksums
b97eaa157a5bbd03fa8551e4a2d2301c83989fe4c62eefa6696f0052c1af1025
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.14.6

Release history Release notifications | RSS feed

This release

0.3.5 This release

2 release files

0.3.4

2 release files

0.3.3

2 release files

0.3.2

2 release files

0.3.1

2 release files

0.3

2 release files

0.2.1

2 release files

0.2

2 release files

0.1.0

2 release files

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