Skip to content

Commit

Permalink
Fix issue where mapping to null did not set a null value
Browse files Browse the repository at this point in the history
  • Loading branch information
fbunt committed May 16, 2024
1 parent 2126d2b commit 0de3aa7
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 5 deletions.
3 changes: 2 additions & 1 deletion raster_tools/general.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,7 +1104,8 @@ def remap_range(raster, mapping, inclusivity="left"):
dtype=data.dtype,
meta=np.array((), dtype=data.dtype),
)
outrs.xmask.data = outrs.data == nv
if raster._masked or None in map_news:
outrs = outrs.set_null_value(nv)
if f16_workaround:
outrs = outrs.astype(F16)
return outrs
Expand Down
11 changes: 7 additions & 4 deletions tests/test_general.py
Original file line number Diff line number Diff line change
Expand Up @@ -715,18 +715,21 @@ def test_remap_range_f16():


def test_remap_range_null_mapping():
raster = testdata.raster.dem_small
raster = testdata.raster.dem_small.set_null_value(None)
assert raster.null_value is None
nv = get_default_null_value(raster.dtype)
mapping = (0, 1500, None)
truth_rast = xr.where(raster.xdata > 1500, raster.xdata, raster.null_value)
truth_mask = truth_rast == raster.null_value
truth_rast = xr.where(raster.xdata > 1500, raster.xdata, nv)
truth_mask = truth_rast == nv

result = general.remap_range(raster, mapping)
assert_valid_raster(result)
assert result.dtype == raster.dtype
assert result.null_value == raster.null_value
assert result.null_value == get_default_null_value(result.dtype)
assert np.allclose(result.xdata, truth_rast)
assert np.allclose(result.xmask, truth_mask)

raster = raster.set_null_value(-999)
mapping = [(0, 1500, None), (1500, 2000, 1)]
truth_rast = xr.where(raster.xdata > 1500, raster.xdata, raster.null_value)
truth_rast = xr.where(
Expand Down

0 comments on commit 0de3aa7

Please sign in to comment.