|
| 1 | +"""Coregistered reads through the xarray backend engine (issue #3376). |
| 2 | +
|
| 3 | +The ``xrspatial`` engine forwards to the standalone ``open_geotiff`` by |
| 4 | +default, which has no target grid. Passing ``like=`` (a DataArray or |
| 5 | +Dataset) routes the read through that object's ``.xrs.open_geotiff`` |
| 6 | +accessor instead, so ``coregister`` / ``auto_reproject`` / ``resampling`` |
| 7 | +become available through the standard ``xr.open_dataset`` API. These |
| 8 | +tests pin that routing, the parity with the accessor, the variable |
| 9 | +naming, the ``open_mfdataset`` composition, and the guard that turns the |
| 10 | +opaque ``TypeError`` (from the standalone reader) into a pointed |
| 11 | +``ValueError`` when a coregister kwarg arrives without ``like=``. |
| 12 | +""" |
| 13 | +from __future__ import annotations |
| 14 | + |
| 15 | +import numpy as np |
| 16 | +import pytest |
| 17 | +import xarray as xr |
| 18 | + |
| 19 | +from xrspatial.geotiff import to_geotiff |
| 20 | +from xrspatial.geotiff._xarray_backend import GeoTIFFBackendEntrypoint |
| 21 | + |
| 22 | + |
| 23 | +def _file_4326(tmp_path, name, nodata=None): |
| 24 | + """Write a 30x30 EPSG:4326 GeoTIFF; return its path.""" |
| 25 | + height, width = 30, 30 |
| 26 | + arr = np.arange(height * width, dtype=np.float32).reshape(height, width) |
| 27 | + y = np.linspace(45.5, 44.5, height) |
| 28 | + x = np.linspace(-120.5, -119.5, width) |
| 29 | + attrs = {"crs": 4326} |
| 30 | + if nodata is not None: |
| 31 | + attrs["nodata"] = nodata |
| 32 | + arr[14:17, 14:17] = nodata |
| 33 | + da = xr.DataArray(arr, dims=["y", "x"], |
| 34 | + coords={"y": y, "x": x}, attrs=attrs) |
| 35 | + path = str(tmp_path / name) |
| 36 | + to_geotiff(da, path, compression="none") |
| 37 | + return path |
| 38 | + |
| 39 | + |
| 40 | +def _template_4326(n=5): |
| 41 | + """A coarser, offset same-CRS grid inside the file footprint.""" |
| 42 | + return xr.DataArray( |
| 43 | + np.zeros((n, n), dtype=np.float32), |
| 44 | + dims=["y", "x"], |
| 45 | + coords={"y": np.linspace(45.3, 44.7, n), |
| 46 | + "x": np.linspace(-120.3, -119.7, n)}, |
| 47 | + attrs={"crs": 4326}, |
| 48 | + ) |
| 49 | + |
| 50 | + |
| 51 | +def _template_3857(n=6): |
| 52 | + """A grid in a different CRS overlapping the file footprint.""" |
| 53 | + from pyproj import Transformer |
| 54 | + tr = Transformer.from_crs(4326, 3857, always_xy=True) |
| 55 | + x0, y0 = tr.transform(-120.25, 45.25) |
| 56 | + x1, y1 = tr.transform(-119.75, 44.75) |
| 57 | + return xr.DataArray( |
| 58 | + np.zeros((n, n), dtype=np.float32), |
| 59 | + dims=["y", "x"], |
| 60 | + coords={"y": np.linspace(max(y0, y1), min(y0, y1), n), |
| 61 | + "x": np.linspace(min(x0, x1), max(x0, x1), n)}, |
| 62 | + attrs={"crs": 3857}, |
| 63 | + ) |
| 64 | + |
| 65 | + |
| 66 | +# --------------------------------------------------------------------------- |
| 67 | +# like= routes to the coregister path |
| 68 | +# --------------------------------------------------------------------------- |
| 69 | + |
| 70 | +def test_coregister_via_engine_matches_template_grid(tmp_path): |
| 71 | + path = _file_4326(tmp_path, "cg_3376_same.tif") |
| 72 | + template = _template_4326(5) |
| 73 | + ds = xr.open_dataset( |
| 74 | + path, engine=GeoTIFFBackendEntrypoint, |
| 75 | + backend_kwargs={"like": template, "coregister": True}, |
| 76 | + ) |
| 77 | + assert isinstance(ds, xr.Dataset) |
| 78 | + var = ds[list(ds.data_vars)[0]] |
| 79 | + assert var.shape == template.shape |
| 80 | + assert np.allclose(var.coords["x"].values, template.coords["x"].values) |
| 81 | + assert np.allclose(var.coords["y"].values, template.coords["y"].values) |
| 82 | + |
| 83 | + |
| 84 | +def test_coregister_via_engine_matches_accessor(tmp_path): |
| 85 | + # The engine must produce exactly what the accessor produces; the |
| 86 | + # engine only promotes the DataArray to a one-variable Dataset. |
| 87 | + path = _file_4326(tmp_path, "cg_3376_parity.tif") |
| 88 | + template = _template_4326(5) |
| 89 | + ds = xr.open_dataset( |
| 90 | + path, engine=GeoTIFFBackendEntrypoint, |
| 91 | + backend_kwargs={"like": template, "coregister": True}, |
| 92 | + ) |
| 93 | + accessor_da = template.xrs.open_geotiff(path, coregister=True) |
| 94 | + engine_da = ds[list(ds.data_vars)[0]] |
| 95 | + np.testing.assert_array_equal(engine_da.values, accessor_da.values) |
| 96 | + for coord in accessor_da.coords: |
| 97 | + np.testing.assert_array_equal( |
| 98 | + engine_da[coord].values, accessor_da[coord].values) |
| 99 | + |
| 100 | + |
| 101 | +def test_coregister_via_engine_crs_mismatch(tmp_path): |
| 102 | + path = _file_4326(tmp_path, "cg_3376_mismatch.tif") |
| 103 | + template = _template_3857(6) |
| 104 | + ds = xr.open_dataset( |
| 105 | + path, engine=GeoTIFFBackendEntrypoint, |
| 106 | + backend_kwargs={"like": template, "coregister": True}, |
| 107 | + ) |
| 108 | + var = ds[list(ds.data_vars)[0]] |
| 109 | + assert var.shape == template.shape |
| 110 | + assert np.allclose(var.coords["x"].values, template.coords["x"].values) |
| 111 | + assert np.allclose(var.coords["y"].values, template.coords["y"].values) |
| 112 | + |
| 113 | + |
| 114 | +def test_auto_reproject_via_engine(tmp_path): |
| 115 | + # auto_reproject (no coregister) keeps the file resolution but still |
| 116 | + # needs the target's bbox/CRS, so it routes through like= too. |
| 117 | + path = _file_4326(tmp_path, "ar_3376.tif") |
| 118 | + template = _template_3857(6) |
| 119 | + ds = xr.open_dataset( |
| 120 | + path, engine=GeoTIFFBackendEntrypoint, |
| 121 | + backend_kwargs={"like": template, "auto_reproject": True}, |
| 122 | + ) |
| 123 | + accessor_da = template.xrs.open_geotiff(path, auto_reproject=True) |
| 124 | + engine_da = ds[list(ds.data_vars)[0]] |
| 125 | + np.testing.assert_array_equal(engine_da.values, accessor_da.values) |
| 126 | + |
| 127 | + |
| 128 | +def test_dataset_like_with_var(tmp_path): |
| 129 | + # A Dataset target dispatches to the Dataset accessor, which honours |
| 130 | + # the var= kwarg for backend/CRS inference. |
| 131 | + path = _file_4326(tmp_path, "cg_3376_dsvar.tif") |
| 132 | + template = _template_4326(5).to_dataset(name="elev") |
| 133 | + ds = xr.open_dataset( |
| 134 | + path, engine=GeoTIFFBackendEntrypoint, |
| 135 | + backend_kwargs={"like": template, "coregister": True, "var": "elev"}, |
| 136 | + ) |
| 137 | + accessor_da = template.xrs.open_geotiff(path, coregister=True, var="elev") |
| 138 | + engine_da = ds[list(ds.data_vars)[0]] |
| 139 | + np.testing.assert_array_equal(engine_da.values, accessor_da.values) |
| 140 | + |
| 141 | + |
| 142 | +# --------------------------------------------------------------------------- |
| 143 | +# Variable naming follows the same default_name / stem rule |
| 144 | +# --------------------------------------------------------------------------- |
| 145 | + |
| 146 | +def test_coregister_variable_name_follows_stem(tmp_path): |
| 147 | + path = _file_4326(tmp_path, "cg_3376_stem.tif") |
| 148 | + template = _template_4326(5) |
| 149 | + ds = xr.open_dataset( |
| 150 | + path, engine=GeoTIFFBackendEntrypoint, |
| 151 | + backend_kwargs={"like": template, "coregister": True}, |
| 152 | + ) |
| 153 | + assert "cg_3376_stem" in ds.data_vars |
| 154 | + |
| 155 | + |
| 156 | +def test_coregister_default_name_renames_variable(tmp_path): |
| 157 | + path = _file_4326(tmp_path, "cg_3376_rename.tif") |
| 158 | + template = _template_4326(5) |
| 159 | + ds = xr.open_dataset( |
| 160 | + path, engine=GeoTIFFBackendEntrypoint, |
| 161 | + backend_kwargs={"like": template, "coregister": True, |
| 162 | + "default_name": "elevation"}, |
| 163 | + ) |
| 164 | + assert "elevation" in ds.data_vars |
| 165 | + |
| 166 | + |
| 167 | +# --------------------------------------------------------------------------- |
| 168 | +# open_mfdataset composes onto one shared grid |
| 169 | +# --------------------------------------------------------------------------- |
| 170 | + |
| 171 | +def test_open_mfdataset_coregisters_onto_shared_grid(tmp_path): |
| 172 | + pytest.importorskip("dask") |
| 173 | + template = _template_4326(5) |
| 174 | + paths = [ |
| 175 | + _file_4326(tmp_path, f"mf_3376_{i}.tif") |
| 176 | + for i in range(2) |
| 177 | + ] |
| 178 | + ds = xr.open_mfdataset( |
| 179 | + paths, engine=GeoTIFFBackendEntrypoint, |
| 180 | + combine="nested", concat_dim="tile", |
| 181 | + backend_kwargs={"like": template, "coregister": True, |
| 182 | + "default_name": "band_data"}, |
| 183 | + ) |
| 184 | + assert list(ds.data_vars) == ["band_data"] |
| 185 | + assert ds.sizes["tile"] == 2 |
| 186 | + # Every tile sits on the template grid. |
| 187 | + assert np.allclose(ds.coords["x"].values, template.coords["x"].values) |
| 188 | + assert np.allclose(ds.coords["y"].values, template.coords["y"].values) |
| 189 | + |
| 190 | + |
| 191 | +# --------------------------------------------------------------------------- |
| 192 | +# Guard: coregister kwargs without like= raise a pointed ValueError |
| 193 | +# --------------------------------------------------------------------------- |
| 194 | + |
| 195 | +@pytest.mark.parametrize("kwargs", [ |
| 196 | + {"coregister": True}, |
| 197 | + {"auto_reproject": True}, |
| 198 | + {"resampling": "bilinear"}, |
| 199 | + {"var": "elev"}, |
| 200 | +]) |
| 201 | +def test_coregister_kwarg_without_like_raises(tmp_path, kwargs): |
| 202 | + path = _file_4326(tmp_path, "cg_3376_nolike.tif") |
| 203 | + with pytest.raises(ValueError, match="like="): |
| 204 | + xr.open_dataset(path, engine=GeoTIFFBackendEntrypoint, |
| 205 | + backend_kwargs=kwargs) |
| 206 | + |
| 207 | + |
| 208 | +def test_non_array_like_raises_typeerror(tmp_path): |
| 209 | + # A path or other non-array passed as like= would otherwise blow up on |
| 210 | + # `.xrs` with an opaque AttributeError; the guard names the right type. |
| 211 | + path = _file_4326(tmp_path, "cg_3376_badlike.tif") |
| 212 | + with pytest.raises(TypeError, match="DataArray or Dataset"): |
| 213 | + xr.open_dataset(path, engine=GeoTIFFBackendEntrypoint, |
| 214 | + backend_kwargs={"like": "not_an_array", |
| 215 | + "coregister": True}) |
| 216 | + |
| 217 | + |
| 218 | +# --------------------------------------------------------------------------- |
| 219 | +# GPU / .vrt rejections are inherited from the accessor path |
| 220 | +# --------------------------------------------------------------------------- |
| 221 | + |
| 222 | +def test_coregister_gpu_rejected_through_engine(tmp_path): |
| 223 | + # The unpack-and-reproject pipeline is CPU-only; the accessor raises on |
| 224 | + # gpu=True, and the engine inherits that rejection for free. |
| 225 | + path = _file_4326(tmp_path, "cg_3376_gpu.tif") |
| 226 | + template = _template_4326(5) |
| 227 | + with pytest.raises(ValueError, match="CPU only"): |
| 228 | + xr.open_dataset( |
| 229 | + path, engine=GeoTIFFBackendEntrypoint, |
| 230 | + backend_kwargs={"like": template, "coregister": True, |
| 231 | + "gpu": True}, |
| 232 | + ) |
0 commit comments