From 94cb502ac3619eacfe2a34b6a56a5449ec03289a Mon Sep 17 00:00:00 2001 From: Anton Soloviev Date: Sat, 24 Oct 2026 14:51:15 -0700 Subject: [PATCH 1/2] Mask NaN nodata in NumPy point-query windows --- src/rasterstats/io.py | 3 ++- tests/test_point.py | 19 +++++++++++++++++++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/rasterstats/io.py b/src/rasterstats/io.py index a07d698..de2e21d 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 np.isnan(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..30bb5a1 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,22 @@ 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] + + def test_unitxy_ul(): win, unitxy = point_window_unitxy(245300, 1000073, affine) assert win == ((30, 32), (38, 40)) From a43b621d1af64dac02e1ac7b1fc08368b2770b6b Mon Sep 17 00:00:00 2001 From: Anton Soloviev Date: Sat, 24 Oct 2026 15:50:23 -0700 Subject: [PATCH 2/2] Preserve Python integer nodata values when detecting NaN --- src/rasterstats/io.py | 2 +- tests/test_point.py | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/rasterstats/io.py b/src/rasterstats/io.py index de2e21d..9035994 100644 --- a/src/rasterstats/io.py +++ b/src/rasterstats/io.py @@ -308,7 +308,7 @@ def boundless_array(arr, window, nodata, masked=False): ] if masked: - mask = np.isnan(out) if np.isnan(nodata) else 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 30bb5a1..e04990c 100644 --- a/tests/test_point.py +++ b/tests/test_point.py @@ -31,6 +31,20 @@ def test_point_query_nan_nodata(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))