diff --git a/src/rasterstats/io.py b/src/rasterstats/io.py index a07d698..490e0f2 100644 --- a/src/rasterstats/io.py +++ b/src/rasterstats/io.py @@ -1,5 +1,6 @@ import json import math +import operator import warnings from collections.abc import Iterable, Mapping from json import JSONDecodeError @@ -15,6 +16,9 @@ from rasterio.transform import guard_transform from shapely import wkb, wkt +# Affine 3 uses @ for coordinates. Affine 2 only supports *. +_transform_point = operator.matmul if hasattr(Affine, "__matmul__") else operator.mul + try: from shapely.errors import ShapelyError except ImportError: # pragma: no cover @@ -255,8 +259,8 @@ def bounds_window(bounds, affine): def window_bounds(window, affine): (row_start, row_stop), (col_start, col_stop) = window - w, s = affine * (col_start, row_stop) - e, n = affine * (col_stop, row_start) + w, s = _transform_point(affine, (col_start, row_stop)) + e, n = _transform_point(affine, (col_stop, row_start)) return w, s, e, n @@ -378,7 +382,7 @@ def __init__(self, raster, affine=None, nodata=None, band=1): def index(self, x, y): """Given (x, y) in crs, return the (row, column) on the raster""" - col, row = (math.floor(a) for a in (~self.affine * (x, y))) + col, row = (math.floor(a) for a in _transform_point(~self.affine, (x, y))) return row, col def read(self, bounds=None, window=None, masked=False, boundless=True): diff --git a/src/rasterstats/point.py b/src/rasterstats/point.py index aa2e20d..3eb01b9 100644 --- a/src/rasterstats/point.py +++ b/src/rasterstats/point.py @@ -2,7 +2,7 @@ from shapely.geometry import shape from shapely.ops import transform -from rasterstats.io import DEFAULT_CHUNK_SIZE, Raster, read_features +from rasterstats.io import DEFAULT_CHUNK_SIZE, Raster, _transform_point, read_features def point_window_unitxy(x, y, affine): @@ -14,7 +14,7 @@ def point_window_unitxy(x, y, affine): ((row1, row2), (col1, col2)), (unitx, unity) """ - fcol, frow = ~affine * (x, y) + fcol, frow = _transform_point(~affine, (x, y)) r, c = round(frow), round(fcol) # The new source window for our 2x2 array diff --git a/tests/test_io.py b/tests/test_io.py index e2327aa..21281fe 100644 --- a/tests/test_io.py +++ b/tests/test_io.py @@ -5,6 +5,7 @@ import pyogrio import pytest import rasterio +from affine import Affine from shapely.geometry import shape from rasterstats.io import ( # todo parse_feature @@ -18,6 +19,7 @@ rowcol, window_bounds, ) +from rasterstats.point import point_window_unitxy data_dir = Path(__file__).parent / "data" polygons = data_dir / "polygons.shp" @@ -249,6 +251,22 @@ def test_window_bounds(): assert src.window_bounds(win) == window_bounds(win, src.transform) +@pytest.mark.filterwarnings("error:Use .*matmul:PendingDeprecationWarning") +def test_affine_point_operations(): + transform = Affine(2, 0, 10, 0, -3, 20) + assert window_bounds(((1, 3), (1, 4)), transform) == (12, 11, 18, 17) + + with Raster(np.arange(16).reshape(4, 4), affine=transform, nodata=-1) as raster: + assert raster.index(14.5, 16.25) == (1, 2) + window = raster.read(window=((1, 3), (1, 4))) + np.testing.assert_array_equal(window.array, [[5, 6, 7], [9, 10, 11]]) + assert (window.affine.c, window.affine.f) == (12, 17) + + window, unitxy = point_window_unitxy(14.5, 16.25, transform) + assert window == ((0, 2), (1, 3)) + assert unitxy == pytest.approx((0.75, 0.25)) + + def test_bounds_window(): with rasterio.open(raster) as src: assert bounds_window(src.bounds, src.transform) == (