Skip to content

Commit

Permalink
Add test for out of range nodata value (#3075)
Browse files Browse the repository at this point in the history
* Make np.can_cast usage compatible with numpy 2.

* Add test for out of range nodata value.
  • Loading branch information
groutr committed Apr 17, 2024
1 parent fe48b1e commit c9e29eb
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
5 changes: 3 additions & 2 deletions rasterio/merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -300,13 +300,14 @@ def nullcontext(obj):
if cmath.isfinite(nodataval):
info = np.finfo(dt)
inrange = info.min <= nodataval <= info.max
inrange = inrange
nodata_dt = np.min_scalar_type(nodataval)
inrange = inrange & np.can_cast(nodata_dt, dt)
else:
inrange = True

if not inrange:
warnings.warn(
"The nodata value, %s, cannot safely be represented "
"Ignoring nodata value. The nodata value, %s, cannot safely be represented "
"in the chosen data type, %s. Consider overriding it "
"using the --nodata option for better results." % (nodataval, dt)
)
Expand Down
6 changes: 6 additions & 0 deletions tests/test_merge.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,9 @@ def test_complex_nodata(test_data_complex):

result, _ = merge(inputs, nodata=0-1j)
assert numpy.all(result[:, 2] == 0-1j)


def test_complex_outrange_nodata_():
with rasterio.open("tests/data/float_raster_with_nodata.tif") as src:
with pytest.warns(UserWarning, match="Ignoring nodata value"):
res, _ = merge([src], nodata=1+1j, dtype='float64')

0 comments on commit c9e29eb

Please sign in to comment.