Skip to content

Commit

Permalink
Fix inplace nodata modification
Browse files Browse the repository at this point in the history
  • Loading branch information
rhugonnet committed Sep 28, 2024
1 parent 4ea02f2 commit 785e6be
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 1 deletion.
6 changes: 5 additions & 1 deletion geoutils/raster/multiraster.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,14 @@ def stack_rasters(
res=reference_raster.res,
crs=reference_raster.crs,
dtype=reference_raster.data.dtype,
nodata=reference_raster.nodata,
nodata=nodata,
resampling=resampling_method,
silent=True,
)
# If the georeferenced grid was the same, reproject() will have returned self with a warning (silenced here),
# and we want to copy the raster before modifying its nodata (or it will modify raster inputs of this function)
if reprojected_raster.georeferenced_grid_equal(raster):
reprojected_raster = reprojected_raster.copy()
reprojected_raster.set_nodata(nodata)

# Optionally calculate difference
Expand Down
4 changes: 4 additions & 0 deletions tests/test_raster/test_multiraster.py
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ def test_stack_rasters(self, rasters) -> None: # type: ignore
stacked_img = gu.raster.stack_rasters([rasters.img1, rasters.img2], resampling_method="bilinear")
assert not np.array_equal(np.unique(stacked_img.data.compressed()), np.array([1, 5]))

# Check input nodata is not modified inplace (issue 609)
new_nodata_ref = rasters.img1.nodata
assert nodata_ref == new_nodata_ref

# Check nodata value output is consistent with reference input
if nodata_ref is not None:
assert stacked_img.nodata == nodata_ref
Expand Down

0 comments on commit 785e6be

Please sign in to comment.