Skip to main content

A Python package to rasterize GeoDataFrames

Project description

Rasterizer

rasterizer is a lightweight Python package for rasterizing geopandas GeoDataFrames.

Features

  • Rasterize lines into a binary (presence/absence) or length-based grid.
  • Rasterize polygons into a binary (presence/absence) or area-based grid.
  • Hybrid polygon rasterization for large polygon bounding boxes: exact clipping on boundary cells, faster scanline filling for interior 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.

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")

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 now uses two internal strategies. Small polygon bounding boxes are handled with exact per-cell clipping. Larger ones switch to a hybrid path that still clips boundary cells exactly, but fills interior spans with a scanline pass to reduce the amount of geometric clipping required. The resulting area and binary outputs stay exact at cell boundaries while scaling better on large polygons.

Binary Mode Area Mode
Polygons - Binary Polygons - Area

Installation

You can install the package directly from PyPI:

pip install rasterizer

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 GDAL-based solutions. 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.

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

rasterizer-0.3.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

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

rasterizer-0.3-py3-none-any.whl (10.7 kB view details)

Uploaded Python 3

File details

Details for the file rasterizer-0.3.tar.gz.

File metadata

  • Download URL: rasterizer-0.3.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for rasterizer-0.3.tar.gz
Algorithm Hash digest
SHA256 1f22239a9a9d2670c1e240eb60c6739b9d0b8cbe9024ef96b87bcae34a83b598
MD5 5c4bc0640dde6d86a193d8cab6e89914
BLAKE2b-256 b4c35e872d718e45289d815423c432ac0378835e377d1448fc6f1c4f730585ce

See more details on using hashes here.

File details

Details for the file rasterizer-0.3-py3-none-any.whl.

File metadata

  • Download URL: rasterizer-0.3-py3-none-any.whl
  • Upload date:
  • Size: 10.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for rasterizer-0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3b99002179f2c72412644cbf71c2947b8eb4f20e33b4fc2d4663665af7916e06
MD5 3b76ac16c0c40c2e0b63567f7474d5a2
BLAKE2b-256 91d9a1826e9f16975430c1c4bfad127f0ee73e59ec98645d65bfca66ca1ed251

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