Skip to content

Commit f188583

Browse files
authored
Fix resampling not being passed to stack_rasters and add tests (#601)
1 parent b5a0d2d commit f188583

File tree

2 files changed

+12
-1
lines changed

2 files changed

+12
-1
lines changed

geoutils/raster/multiraster.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -181,6 +181,7 @@ def stack_rasters(
181181
crs=reference_raster.crs,
182182
dtype=reference_raster.data.dtype,
183183
nodata=reference_raster.nodata,
184+
resampling=resampling_method,
184185
silent=True,
185186
)
186187
reprojected_raster.set_nodata(nodata)

tests/test_raster/test_multiraster.py

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ def __init__(
5252
inplace=True,
5353
)
5454
if different_crs:
55-
self.img2 = self.img2.reproject(crs=different_crs)
55+
self.img2 = self.img2.reproject(crs=different_crs, resampling="nearest")
5656

5757
# To check that use_ref_bounds work - create a img that do not cover the whole extent
5858
self.img3 = img.copy()
@@ -160,6 +160,16 @@ def test_stack_rasters(self, rasters) -> None: # type: ignore
160160
stacked_img2 = gu.raster.stack_rasters([rasters.img1, rasters.img3], reference=rasters.img, use_ref_bounds=True)
161161
assert stacked_img2.bounds == rasters.img.bounds
162162

163+
# This case should preserve unique data values through "nearest" resampling
164+
rasters.img1[:] = 5
165+
rasters.img1[0:5, 0:5] = 1
166+
rasters.img2 = rasters.img1.translate(0.5, 0.5, distance_unit="pixel")
167+
stacked_img = gu.raster.stack_rasters([rasters.img1, rasters.img2], resampling_method="nearest")
168+
assert np.array_equal(np.unique(stacked_img.data.compressed()), np.array([1, 5]))
169+
# But not this case with a shifted raster resampled with "bilinear"
170+
stacked_img = gu.raster.stack_rasters([rasters.img1, rasters.img2], resampling_method="bilinear")
171+
assert not np.array_equal(np.unique(stacked_img.data.compressed()), np.array([1, 5]))
172+
163173
@pytest.mark.parametrize(
164174
"rasters",
165175
[

0 commit comments

Comments
 (0)