Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/rasterstats/io.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import json
import math
import operator
import warnings
from collections.abc import Iterable, Mapping
from json import JSONDecodeError
Expand All @@ -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
Expand Down Expand Up @@ -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


Expand Down Expand Up @@ -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):
Expand Down
4 changes: 2 additions & 2 deletions src/rasterstats/point.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand All @@ -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
Expand Down
18 changes: 18 additions & 0 deletions tests/test_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand Down Expand Up @@ -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) == (
Expand Down