urbanflow.utils.grid_utils

grid_utils.py

Utilities for combining and reading GeoPandas files, or applying operations on the grid

Functions

cell_to_polygon(row, col, transform)

Convert a raster cell index to its polygon footprint in spatial coordinates.

instantiate_grid_and_transform(cell_size, gdfs)

Instantiate a raster grid and affine transform covering the extent of given geometries.

rasterize_geoms(geoms, value, out, transform)

Rasterize geometries into an existing array (in-place).

urbanflow.utils.grid_utils.cell_to_polygon(row, col, transform)

Convert a raster cell index to its polygon footprint in spatial coordinates.

Parameters:
  • row (int) – Row index of the cell.

  • col (int) – Column index of the cell.

  • transform (affine.Affine) – Affine transform mapping raster indices to spatial coordinates.

Returns:

polygon – Polygon representing the spatial footprint of the cell.

Return type:

shapely.geometry.Polygon

Examples

>>> transform = Affine(1, 0, 0, 0, -1, 2)  # 1x1 cells
>>> cell_to_polygon(0, 0, transform).bounds
(0.0, 1.0, 1.0, 2.0)
urbanflow.utils.grid_utils.instantiate_grid_and_transform(cell_size, gdfs, default_grid_value=0)

Instantiate a raster grid and affine transform covering the extent of given geometries.

Parameters:
  • cell_size (float) – Resolution of grid cells in coordinate units (e.g., meters).

  • gdfs (geopandas.GeoDataFrame or sequence of GeoDataFrame) – One or more GeoDataFrames whose geometries define the spatial extent of the grid.

  • default_grid_value (int, optional) – Initial value used to fill all cells, by default 0.

Returns:

  • grid (ndarray of uint8, shape (rows, cols)) – Raster grid array initialized with default_grid_value.

  • transform (affine.Affine) – Affine transform mapping raster indices (row, col) to spatial coordinates.

Return type:

Tuple[ndarray[tuple[Any, …], dtype[uint8]], Affine]

Notes

  • The grid extent is snapped to whole multiples of cell_size so that the grid completely covers the union of input bounds.

  • The origin of the transform is the upper-left corner.

urbanflow.utils.grid_utils.rasterize_geoms(geoms, value, out, transform)

Rasterize geometries into an existing array (in-place).

Parameters:
  • geoms (iterable of shapely.geometry.BaseGeometry) – Geometries to rasterize.

  • value (int) – Integer value to burn into cells touched by the geometries.

  • out (ndarray) – Target raster array that will be modified in place.

  • transform (affine.Affine) – Affine transform mapping raster indices (row, col) to spatial coordinates.

Notes

  • Uses rasterio.features.rasterize under the hood.

  • A cell is marked if the geometry touches it at all (all_touched=True).