Skip to main content

A Python package to rasterize GeoDataFrames

Project description

Rasterizer

rasterizer is a lightweight Python package for rasterizing geopandas GeoDataFrames containing LineString, MultiLineString, Polygon, and MultiPolygon geometries. It is designed to be a simple, dependency-light alternative to gdal.RasterizeLayer, relying on numpy and xarray for grid manipulation.

Features

  • Rasterize lines into a binary (presence/absence) or length-based grid.
  • Rasterize polygons into a binary (presence/absence) or area-based grid.
  • Works with geopandas GeoDataFrames.
  • Outputs an xarray.DataArray for easy integration with other scientific Python libraries.
  • No GDAL dependency for the rasterization algorithm itself.

Installation

You can install the package directly from the source code:

pip install .

Make sure you have the required dependencies installed: geopandas, xarray, numpy, shapely, rioxarray.

Usage

Here is a basic example of how to use rasterizer:

import numpy as np
import geopandas as gpd
from shapely.geometry import LineString, Polygon
from rasterizer import rasterize_lines, rasterize_polygons

# 1. Define the output grid
crs = "EPSG:32631"  # A metric CRS (UTM 31N)
x_coords = np.arange(0.5, 100.5, 1.0)
y_coords = np.arange(0.5, 100.5, 1.0)

# 2. Create some line data
line = LineString([(10, 10), (90, 90)])
gdf_lines = gpd.GeoDataFrame([1], geometry=[line], crs=crs)

# 3. Rasterize the lines
# Get a raster where cell values represent the length of the line within them
length_raster = rasterize_lines(gdf_lines, x=x_coords, y=y_coords, crs=crs)

# Get a binary raster (True where cells are intersected)
binary_raster_lines = rasterize_lines(gdf_lines, x=x_coords, y=y_coords, crs=crs, mode='binary')

print("Length Raster:\n", length_raster)
print("\nBinary Raster (Lines):\n", binary_raster_lines)

# 4. Create some polygon data
poly = Polygon([(20, 30), (80, 30), (80, 70), (20, 70)])
gdf_polygons = gpd.GeoDataFrame([1], geometry=[poly], crs=crs)

# 5. Rasterize the polygons
# Get a raster where cell values represent the area of the polygon within them
area_raster = rasterize_polygons(gdf_polygons, x=x_coords, y=y_coords, crs=crs)

# Get a binary raster (True where cells are covered)
binary_raster_polygons = rasterize_polygons(gdf_polygons, x=x_coords, y=y_coords, crs=crs, mode='binary')

print("\nArea Raster:\n", area_raster)
print("\nBinary Raster (Polygons):\n", binary_raster_polygons)


# The result is an xarray.DataArray
# You can plot it easily
# length_raster.plot()
# area_raster.plot()

How it Works

The core of the package are the rasterize_lines and rasterize_polygons functions. For each geometry in the input GeoDataFrame, it identifies the grid cells that the geometry's bounding box overlaps. Then, for each of these candidate cells, it uses a clipping algorithm to determine the portion of the geometry that lies strictly inside the cell. The length or area of this clipped portion is then used to update the cell's value in the output raster.

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.1.0.tar.gz (11.1 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.1.0-py3-none-any.whl (8.3 kB view details)

Uploaded Python 3

File details

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

File metadata

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

File hashes

Hashes for rasterizer-0.1.0.tar.gz
Algorithm Hash digest
SHA256 8b39b020d2b10ef77e68defa1efbb7757b2103961bb64e1f463d021a3b887af1
MD5 9af9a3bba16c1d4acf30a7f4e5de8364
BLAKE2b-256 3cf2d4f8d0d0d43320ce773995b1530247a50f13c2e302e220c449ae32e2160f

See more details on using hashes here.

File details

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

File metadata

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

File hashes

Hashes for rasterizer-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1d47dd4f0c413182653e8a0d042536f59f7b7ac75f69d2fa28ed0f604064a5c3
MD5 5ac3da43966c9dff4a3b4f852c5df7b7
BLAKE2b-256 2c71efd13ef29ab944671f683b8acc600f61ae17fc52a3a25e066df913815f3e

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