diff --git a/src/rasterstats/io.py b/src/rasterstats/io.py index a07d698..9035994 100644 --- a/src/rasterstats/io.py +++ b/src/rasterstats/io.py @@ -308,7 +308,8 @@ def boundless_array(arr, window, nodata, masked=False): ] if masked: - out = np.ma.MaskedArray(out, mask=(out == nodata)) + mask = np.isnan(out) if nodata != nodata else out == nodata + out = np.ma.MaskedArray(out, mask=mask) return out diff --git a/tests/test_point.py b/tests/test_point.py index 9a50119..e04990c 100644 --- a/tests/test_point.py +++ b/tests/test_point.py @@ -1,6 +1,9 @@ import os +import numpy as np +import pytest import rasterio +from affine import Affine from rasterstats import point_query from rasterstats.point import bilinear, geom_xys, point_window_unitxy @@ -12,6 +15,36 @@ affine = src.transform +@pytest.mark.parametrize("interpolate", ["nearest", "bilinear"]) +def test_point_query_nan_nodata(interpolate): + data = np.array([[1.0, np.nan], [3.0, 4.0]]) + points = [ + {"type": "Point", "coordinates": xy} + for xy in [(0.75, 1.25), (1.5, 1.5), (-0.25, 1.5)] + ] + assert point_query( + points, + data, + affine=Affine(1, 0, 0, 0, -1, 2), + nodata=np.nan, + interpolate=interpolate, + ) == [1.0, None, None] + + +@pytest.mark.parametrize("nodata", [2**64, 10**20]) +@pytest.mark.parametrize("interpolate", ["nearest", "bilinear"]) +def test_point_query_large_integer_nodata(nodata, interpolate): + data = np.array([[1.0, nodata], [3.0, 4.0]], dtype="float64") + points = [{"type": "Point", "coordinates": xy} for xy in [(0.75, 1.25), (1.5, 1.5)]] + assert point_query( + points, + data, + affine=Affine(1, 0, 0, 0, -1, 2), + nodata=nodata, + interpolate=interpolate, + ) == [1.0, None] + + def test_unitxy_ul(): win, unitxy = point_window_unitxy(245300, 1000073, affine) assert win == ((30, 32), (38, 40))