Skip to content

Treat cells outside a nodata-less raster file as nodata - #330

Merged
perrygeo merged 1 commit into
perrygeo:masterfrom
kevincommits:fix/boundless-fill-without-nodata
Dec 5, 2026
Merged

perrygeo merged 1 commit into
perrygeo:masterfrom
kevincommits:fix/boundless-fill-without-nodata

Conversation

@kevincommits

Copy link
Copy Markdown
Contributor

The boundless docs say cells outside the dataset extent are treated as nodata. For a raster file that has no nodata value, they are instead counted as valid zeros, so a polygon that hangs off the edge gets a higher count and a min of 0. A numpy array with the same values and affine gives the right answer.

# 3x3 float32 GeoTIFF, values 1..9, Affine(1, 0, 0, 0, -1, 3), no nodata set
polygon = box(2, 0, 4, 3)  # last column (3, 6, 9) plus one column off the raster
zonal_stats(polygon, "no_nodata.tif", stats="count min sum")
# actual:   [{'count': 6, 'min': 0.0, 'sum': 18.0}]
# expected: [{'count': 3, 'min': 3.0, 'sum': 18.0}]  (same as the ndarray input)

Raster.read falls back to nodata -999 and masks that value, but the rasterio boundless read fills off-extent cells with 0 because the file has no nodata. The fix passes fill_value=nodata to src.read, so the fill matches the value that is masked. Files that do have a nodata value fill with the same value as before.

The new test runs the case above against both a numpy array and a GeoTIFF written in tmp_path; the file case fails on master.

What I ran (Python 3.13, rasterio 1.5.2, numpy 2.5.5): pytest (138 passed) and ruff check src tests/test_zonal.py, ruff format --check ..

What I did not run: Python 3.12, 3.14 and 3.15.

@perrygeo perrygeo left a comment

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kevincommits Thanks for the bug fix!

Running the CI now so we'll have feedback on the other Python versions. Looks good.

Comment thread tests/test_zonal.py
}


@pytest.mark.parametrize("use_file", [False, True])

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice touch - the behavior can actually change based on the tiff metadata so it's good to check!

Comment thread tests/test_zonal.py

# Covers the last raster column (3, 6, 9) and one column beyond the extent.
polygon = Polygon([(2, 0), (4, 0), (4, 3), (2, 3)])
stats = zonal_stats(polygon, source, affine=transform, stats="count min sum")

Copy link
Copy Markdown
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should add nodata to the stats as well. I'll add this on my end after the merge.

@perrygeo
perrygeo merged commit 72c004e into perrygeo:master Dec 5, 2026
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants