Repository navigation
Treat cells outside a nodata-less raster file as nodata - #330
Merged
perrygeo merged 1 commit intoDec 5, 2026
Merged
Conversation
perrygeo
approved these changes
Dec 5, 2026
perrygeo
left a comment
Owner
There was a problem hiding this comment.
@kevincommits Thanks for the bug fix!
Running the CI now so we'll have feedback on the other Python versions. Looks good.
| } | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("use_file", [False, True]) |
Owner
There was a problem hiding this comment.
Nice touch - the behavior can actually change based on the tiff metadata so it's good to check!
|
|
||
| # 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") |
Owner
There was a problem hiding this comment.
We should add nodata to the stats as well. I'll add this on my end after the merge.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The
boundlessdocs 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 highercountand aminof 0. A numpy array with the same values and affine gives the right answer.Raster.readfalls 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 passesfill_value=nodatatosrc.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) andruff check src tests/test_zonal.py,ruff format --check ..What I did not run: Python 3.12, 3.14 and 3.15.