Skip to content

Commit

Permalink
fix for float NaN nodata (#691)
Browse files Browse the repository at this point in the history
* fix for float NaN nodata

* update changelog

---------

Co-authored-by: vincentsarago <vincent.sarago@gmail.com>
  • Loading branch information
cerolinx and vincentsarago committed Apr 2, 2024
1 parent 6003057 commit cfc411d
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGES.md
@@ -1,4 +1,8 @@

# 6.4.4 (2024-04-02)

* better handler `NaN` nodata values for masking (author @cerolinx, https://github.com/cogeotiff/rio-tiler/pull/691)

# 6.4.3 (2024-03-22)

* make sure `scale` and `offset` are set in `Info` even when `offset=0.` or `scale=1.0` (https://github.com/cogeotiff/rio-tiler/pull/687)
Expand Down
5 changes: 4 additions & 1 deletion rio_tiler/reader.py
Expand Up @@ -238,7 +238,10 @@ def read(

# if data has Nodata then we simply make sure the mask == the nodata
if nodata is not None:
data.mask = data.data == nodata
if numpy.isnan(nodata):
data.mask = numpy.isnan(data.data)
else:
data.mask = data.data == nodata

stats = []
for ix in indexes:
Expand Down
Binary file added tests/fixtures/cog_nodata_float_nan.tif
Binary file not shown.
11 changes: 11 additions & 0 deletions tests/test_reader.py
Expand Up @@ -34,6 +34,9 @@
COG_SCALE = os.path.join(os.path.dirname(__file__), "fixtures", "cog_scale.tif")
COG_CMAP = os.path.join(os.path.dirname(__file__), "fixtures", "cog_cmap.tif")
COG_NODATA = os.path.join(os.path.dirname(__file__), "fixtures", "cog_nodata.tif")
COG_NODATA_FLOAT_NAN = os.path.join(
os.path.dirname(__file__), "fixtures", "cog_nodata_float_nan.tif"
)


@pytest.fixture(autouse=True)
Expand Down Expand Up @@ -848,3 +851,11 @@ def test_nodata_orverride():
prev = reader.read(src_dst, max_size=100, nodata=2720)
assert prev.mask[0, 0] == 255
assert not numpy.all(prev.mask)


def test_tile_read_nodata_float():
"""Should work as expected when using NaN as nodata value."""
with rasterio.open(COG_NODATA_FLOAT_NAN) as src_dst:
prev = reader.read(src_dst, max_size=100)
assert prev.mask[0, 0] == 0
assert not numpy.all(prev.mask)

0 comments on commit cfc411d

Please sign in to comment.