Skip to content

Commit

Permalink
Merge back v0p5p2 (#1133)
Browse files Browse the repository at this point in the history
* patch: fix gridline tests for np 2.x (#998)

* patch: fix rasterio 1.4.x regression (#1132)

* patch: fix rasterio 1.4.x regression

* remove broken link (#1065)

* fix upload coverage (#1075)
  • Loading branch information
bjlittle authored Oct 3, 2024
1 parent 8e2604b commit 558bb0a
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 23 deletions.
14 changes: 13 additions & 1 deletion src/geovista/bridge.py
Original file line number Diff line number Diff line change
Expand Up @@ -867,7 +867,19 @@ def from_tiff(
cols, rows = np.meshgrid(
np.arange(src.width), np.arange(src.height), indexing="xy"
)
xs, ys = rio.transform.xy(src.transform, rows, cols)
# rasterio 1.4.0 (regression) expects 1-D arrays, fixed in 1.4.1
# see https://github.com/rasterio/rasterio/issues/3191
xs, ys = rio.transform.xy(src.transform, rows.flatten(), cols.flatten())

# ensure we have arrays, rather than a list of arrays
xs, ys = np.asanyarray(xs), np.asanyarray(ys)

# ensure shape is maintained (rasterio 1.4.1 regression)
if xs.shape != src.shape:
xs = xs.reshape(src.shape)

if ys.shape != src.shape:
ys = ys.reshape(src.shape)

# create the geotiff mesh
mesh = cls.from_2d(
Expand Down
65 changes: 43 additions & 22 deletions tests/bridge/test_from_tiff.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def test_rgb_band_fail(mocker):
_ = Transform.from_tiff(fname, rgb=True)


@pytest.mark.parametrize("rgb", [True])
@pytest.mark.parametrize("rgb", [True, False])
@pytest.mark.parametrize("band", [1, 2, 3])
@pytest.mark.parametrize("unit", ["m", None])
def test_rgb_band(mocker, rgb, band, unit):
Expand All @@ -58,12 +58,14 @@ def test_rgb_band(mocker, rgb, band, unit):
data = mocker.sentinel.data
mocked_read = mocker.MagicMock(return_value=data)
transform = mocker.sentinel.transform
height, width = 2, 3
height, width = pixels_shape = 2, 3
n_pixels = height * width
kwargs = {
"count": band,
"crs": crs,
"height": height,
"read": mocked_read,
"shape": pixels_shape,
"transform": transform,
"units": [unit] * band,
"width": width,
Expand All @@ -77,10 +79,13 @@ def test_rgb_band(mocker, rgb, band, unit):
"numpy.dstack", return_value=mocker.MagicMock(reshape=mocked_reshape)
)

cols, rows = mocker.sentinel.cols, mocker.sentinel.rows
cols_flatten = mocker.sentinel.cols_flatten
rows_flatten = mocker.sentinel.rows_flatten
cols = mocker.MagicMock(flatten=mocker.MagicMock(return_value=cols_flatten))
rows = mocker.MagicMock(flatten=mocker.MagicMock(return_value=rows_flatten))
mocked_meshgrid = mocker.patch("numpy.meshgrid", return_value=(cols, rows))

xs, ys = mocker.sentinel.xs, mocker.sentinel.ys
xs, ys = np.arange(n_pixels), np.arange(n_pixels)
mocked_xy = mocker.patch("rasterio.transform.xy", return_value=(xs, ys))

expected = mocker.sentinel.mesh
Expand All @@ -105,27 +110,35 @@ def test_rgb_band(mocker, rgb, band, unit):
assert len(args) == 2
np.testing.assert_array_equal(args[0], np.arange(width))
np.testing.assert_array_equal(args[1], np.arange(height))
assert mocked_meshgrid.call_args.kwargs == {"indexing": "xy"}

mocked_xy.assert_called_once_with(transform, rows, cols)

kwargs = {"radius": None, "zlevel": None, "zscale": None, "clean": None}
mocked_from_2d.assert_called_once_with(
xs,
ys,
data=data,
name=name.format(units=str(unit)),
crs=crs,
rgb=rgb,
**kwargs,
)
mocked_xy.assert_called_once_with(transform, rows_flatten, cols_flatten)

expected_kwargs = {
"data": data,
"name": name.format(units=str(unit)),
"crs": crs,
"rgb": rgb,
"radius": None,
"zlevel": None,
"zscale": None,
"clean": None,
}
mocked_from_2d.assert_called_once()
args = mocked_from_2d.call_args.args
assert len(args) == 2
assert args[0].shape == pixels_shape
assert args[1].shape == pixels_shape
assert mocked_from_2d.call_args.kwargs == expected_kwargs


@pytest.mark.parametrize("sieve", [True])
@pytest.mark.parametrize("rgb", [False, True])
@pytest.mark.parametrize("masked", [False, True])
def test_extract(mocker, masked, rgb, sieve):
"""Test extract behaviour with and without image masking."""
height, width = 2, 3
pixels_shape = height, width = 2, 3
n_pixels = height * width
band = 3 if rgb else 1
crs = mocker.sentinel.crs
dtypes = ["uint8"] * band
Expand All @@ -148,16 +161,20 @@ def test_extract(mocker, masked, rgb, sieve):
"dtypes": dtypes,
"height": height,
"read": mocked_read,
"shape": pixels_shape,
"transform": transform,
"width": width,
}
dataset = mocker.MagicMock(**kwargs)
mocker.patch("rasterio.open").return_value.__enter__.return_value = dataset

cols, rows = mocker.sentinel.cols, mocker.sentinel.rows
cols_flatten = mocker.sentinel.cols_flatten
rows_flatten = mocker.sentinel.rows_flatten
cols = mocker.MagicMock(flatten=mocker.MagicMock(return_value=cols_flatten))
rows = mocker.MagicMock(flatten=mocker.MagicMock(return_value=rows_flatten))
mocked_meshgrid = mocker.patch("numpy.meshgrid", return_value=(cols, rows))

xs, ys = mocker.sentinel.xs, mocker.sentinel.ys
xs, ys = np.arange(n_pixels), np.arange(n_pixels)
mocked_xy = mocker.patch("rasterio.transform.xy", return_value=(xs, ys))

expected_mesh = mocker.sentinel.mesh
Expand Down Expand Up @@ -189,15 +206,14 @@ def test_extract(mocker, masked, rgb, sieve):
assert len(args) == 2
np.testing.assert_array_equal(args[0], np.arange(width))
np.testing.assert_array_equal(args[1], np.arange(height))
assert mocked_meshgrid.call_args.kwargs == {"indexing": "xy"}

mocked_xy.assert_called_once_with(transform, rows, cols)
mocked_xy.assert_called_once_with(transform, rows_flatten, cols_flatten)

if rgb:
data = np.dstack(data).reshape(-1, band)
mask = mask[0]

mocked_from_2d.assert_called_once()
assert mocked_from_2d.call_args.args == (xs, ys)
expected_kwargs = {
"name": None,
"crs": crs,
Expand All @@ -207,6 +223,11 @@ def test_extract(mocker, masked, rgb, sieve):
"zscale": None,
"clean": None,
}
mocked_from_2d.assert_called_once()
args = mocked_from_2d.call_args.args
assert len(args) == 2
assert args[0].shape == pixels_shape
assert args[1].shape == pixels_shape
kwargs = mocked_from_2d.call_args.kwargs
actual = kwargs.pop("data")
assert kwargs == expected_kwargs
Expand Down

0 comments on commit 558bb0a

Please sign in to comment.