Skip to content

rasterize: empty geometries leak NaN bounds into inferred extent #2065

@brendancol

Description

@brendancol

Describe the bug

Empty geometries produce NaN bounds, and rasterize() propagates those NaNs into the inferred output extent rather than rejecting them.

Two paths trigger this:

  1. _geometry_bboxes() returns Shapely bounds as-is. Shapely returns (nan, nan, nan, nan) for empty geometries. The caller computes the inferred extent with plain .min()/.max() over the per-geometry bbox array, so a single empty geometry poisons all four bounds. The downstream check xmin < xmax does not catch NaN.

  2. _parse_input() returns tuple(geometries.total_bounds) directly for any GeoDataFrame, including an empty one. GeoPandas returns NaN bounds for an empty frame. This bypasses the "bounds must be provided when geometries are empty" guard.

Reproducer:

from shapely.geometry import Polygon, box
from xrspatial import rasterize

empty = Polygon()
result = rasterize([(empty, 99), (box(0,0,1,1), 1)], width=2, height=2)
# Returns a (2, 2) raster with x = [nan, nan], y = [nan, nan]. Nothing is burned.

Expected behavior

Drop empty geometries from the inferred-bounds calculation. If the result is non-finite or there are no usable geometries, raise the existing "bounds must be provided" error.

Suggested fix

In _geometry_bboxes() filter NaN rows. In _parse_input(), return None for inferred_bounds when total_bounds contains NaN. Add a non-finite check before the existing xmin < xmax test in rasterize() so the failure mode is explicit.

Files: xrspatial/rasterize.py:1457, 1941, 2210-2225

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions