|
| 1 | +"""Regression test for issue #1643. |
| 2 | +
|
| 3 | +``_coords_to_transform`` previously used ``dims[-2]`` and ``dims[-1]`` to |
| 4 | +look up y/x coords. On a 3D ``(y, x, band)`` DataArray that picked |
| 5 | +``x`` and ``band``, so ``to_geotiff`` silently wrote a wrong |
| 6 | +GeoTransform when ``attrs['transform']`` was absent. The helper now |
| 7 | +detects the band-like trailing/leading dim and uses the two spatial |
| 8 | +dims regardless of position. |
| 9 | +""" |
| 10 | +from __future__ import annotations |
| 11 | + |
| 12 | +import importlib.util |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +import pytest |
| 16 | +import xarray as xr |
| 17 | + |
| 18 | +from xrspatial.geotiff import _coords_to_transform, open_geotiff, to_geotiff |
| 19 | + |
| 20 | + |
| 21 | +def _gpu_available() -> bool: |
| 22 | + if importlib.util.find_spec("cupy") is None: |
| 23 | + return False |
| 24 | + try: |
| 25 | + import cupy |
| 26 | + return bool(cupy.cuda.is_available()) |
| 27 | + except Exception: |
| 28 | + return False |
| 29 | + |
| 30 | + |
| 31 | +_HAS_GPU = _gpu_available() |
| 32 | + |
| 33 | + |
| 34 | +def _make_geo_da_3d(dims): |
| 35 | + """3D DataArray with georeferenced y/x coords and a band axis.""" |
| 36 | + shape = [] |
| 37 | + for d in dims: |
| 38 | + if d in ('y',): |
| 39 | + shape.append(10) |
| 40 | + elif d in ('x',): |
| 41 | + shape.append(20) |
| 42 | + else: |
| 43 | + shape.append(3) |
| 44 | + arr = np.arange(int(np.prod(shape)), dtype=np.uint8).reshape(shape) |
| 45 | + coords = { |
| 46 | + 'y': np.linspace(100.0, 200.0, 10), |
| 47 | + 'x': np.linspace(500.0, 700.0, 20), |
| 48 | + 'band': np.arange(3), |
| 49 | + } |
| 50 | + return xr.DataArray(arr, dims=list(dims), coords=coords) |
| 51 | + |
| 52 | + |
| 53 | +def test_coords_to_transform_yxband_returns_yx_spacing(): |
| 54 | + """3D (y, x, band) picks y/x spacing rather than (x, band) spacing.""" |
| 55 | + da = _make_geo_da_3d(('y', 'x', 'band')) |
| 56 | + gt = _coords_to_transform(da) |
| 57 | + # y spacing = (200 - 100) / 9, x spacing = (700 - 500) / 19 |
| 58 | + assert gt is not None |
| 59 | + np.testing.assert_allclose(gt.pixel_width, (700.0 - 500.0) / 19) |
| 60 | + np.testing.assert_allclose(gt.pixel_height, (200.0 - 100.0) / 9) |
| 61 | + |
| 62 | + |
| 63 | +def test_coords_to_transform_bandyx_returns_yx_spacing(): |
| 64 | + """3D (band, y, x) also returns the y/x transform.""" |
| 65 | + da = _make_geo_da_3d(('band', 'y', 'x')) |
| 66 | + gt = _coords_to_transform(da) |
| 67 | + assert gt is not None |
| 68 | + np.testing.assert_allclose(gt.pixel_width, (700.0 - 500.0) / 19) |
| 69 | + np.testing.assert_allclose(gt.pixel_height, (200.0 - 100.0) / 9) |
| 70 | + |
| 71 | + |
| 72 | +@pytest.mark.parametrize('band_name', ['band', 'bands', 'channel']) |
| 73 | +def test_coords_to_transform_3d_band_name_variants(band_name): |
| 74 | + """All recognized band-dim names (band, bands, channel) are filtered |
| 75 | + out when picking the y/x spatial dims.""" |
| 76 | + arr = np.zeros((10, 20, 3), dtype=np.uint8) |
| 77 | + da = xr.DataArray( |
| 78 | + arr, |
| 79 | + dims=['y', 'x', band_name], |
| 80 | + coords={ |
| 81 | + 'y': np.linspace(100.0, 200.0, 10), |
| 82 | + 'x': np.linspace(500.0, 700.0, 20), |
| 83 | + band_name: np.arange(3), |
| 84 | + }, |
| 85 | + ) |
| 86 | + gt = _coords_to_transform(da) |
| 87 | + assert gt is not None |
| 88 | + np.testing.assert_allclose(gt.pixel_width, (700.0 - 500.0) / 19) |
| 89 | + np.testing.assert_allclose(gt.pixel_height, (200.0 - 100.0) / 9) |
| 90 | + |
| 91 | + |
| 92 | +def test_coords_to_transform_2d_unchanged(): |
| 93 | + """2D (y, x) keeps its original behaviour.""" |
| 94 | + da = xr.DataArray( |
| 95 | + np.zeros((10, 20), dtype=np.uint8), |
| 96 | + dims=['y', 'x'], |
| 97 | + coords={ |
| 98 | + 'y': np.linspace(100.0, 200.0, 10), |
| 99 | + 'x': np.linspace(500.0, 700.0, 20), |
| 100 | + }, |
| 101 | + ) |
| 102 | + gt = _coords_to_transform(da) |
| 103 | + assert gt is not None |
| 104 | + np.testing.assert_allclose(gt.pixel_width, (700.0 - 500.0) / 19) |
| 105 | + np.testing.assert_allclose(gt.pixel_height, (200.0 - 100.0) / 9) |
| 106 | + |
| 107 | + |
| 108 | +def test_to_geotiff_roundtrip_3d_yxband_no_transform_attr(tmp_path): |
| 109 | + """to_geotiff -> open_geotiff round-trip on 3D arrays preserves coords. |
| 110 | +
|
| 111 | + Before the fix the on-disk transform was derived from (x, band) |
| 112 | + spacing, so the round-tripped y/x coords had wrong pixel size and |
| 113 | + origin. After the fix the 3D output matches the 2D output. |
| 114 | + """ |
| 115 | + da_3d = _make_geo_da_3d(('y', 'x', 'band')) |
| 116 | + da_2d = xr.DataArray( |
| 117 | + np.zeros((10, 20), dtype=np.uint8), |
| 118 | + dims=['y', 'x'], |
| 119 | + coords={ |
| 120 | + 'y': np.linspace(100.0, 200.0, 10), |
| 121 | + 'x': np.linspace(500.0, 700.0, 20), |
| 122 | + }, |
| 123 | + ) |
| 124 | + |
| 125 | + p2 = str(tmp_path / 'roundtrip_1643_2d.tif') |
| 126 | + p3 = str(tmp_path / 'roundtrip_1643_3d.tif') |
| 127 | + to_geotiff(da_2d, p2) |
| 128 | + to_geotiff(da_3d, p3) |
| 129 | + |
| 130 | + rt2 = open_geotiff(p2) |
| 131 | + rt3 = open_geotiff(p3) |
| 132 | + np.testing.assert_allclose(rt3.y.values, rt2.y.values) |
| 133 | + np.testing.assert_allclose(rt3.x.values, rt2.x.values) |
| 134 | + assert rt3.attrs.get('transform') == rt2.attrs.get('transform') |
| 135 | + |
| 136 | + |
| 137 | +def test_to_geotiff_roundtrip_3d_bandyx_no_transform_attr(tmp_path): |
| 138 | + """(band, y, x) input round-trips with the correct transform. |
| 139 | +
|
| 140 | + ``to_geotiff`` remaps a (band, y, x) input to (y, x, band) before |
| 141 | + writing, but ``_coords_to_transform`` runs against the original |
| 142 | + dim order. The fix handles both 3D layouts. |
| 143 | + """ |
| 144 | + da_3d = _make_geo_da_3d(('band', 'y', 'x')) |
| 145 | + da_2d = xr.DataArray( |
| 146 | + np.zeros((10, 20), dtype=np.uint8), |
| 147 | + dims=['y', 'x'], |
| 148 | + coords={ |
| 149 | + 'y': np.linspace(100.0, 200.0, 10), |
| 150 | + 'x': np.linspace(500.0, 700.0, 20), |
| 151 | + }, |
| 152 | + ) |
| 153 | + |
| 154 | + p2 = str(tmp_path / 'roundtrip_1643_2d_b.tif') |
| 155 | + p3 = str(tmp_path / 'roundtrip_1643_3d_bandfirst.tif') |
| 156 | + to_geotiff(da_2d, p2) |
| 157 | + to_geotiff(da_3d, p3) |
| 158 | + |
| 159 | + rt2 = open_geotiff(p2) |
| 160 | + rt3 = open_geotiff(p3) |
| 161 | + np.testing.assert_allclose(rt3.y.values, rt2.y.values) |
| 162 | + np.testing.assert_allclose(rt3.x.values, rt2.x.values) |
| 163 | + |
| 164 | + |
| 165 | +def test_to_geotiff_3d_without_transform_attr_does_not_invent_unit_pixels( |
| 166 | + tmp_path): |
| 167 | + """Regression sanity: the bad transform was pixel_width=1.0 (band |
| 168 | + axis spacing). Assert the round-tripped pixel_width is finite, |
| 169 | + non-unit, and matches the source x spacing. |
| 170 | + """ |
| 171 | + da = _make_geo_da_3d(('y', 'x', 'band')) |
| 172 | + p = str(tmp_path / 'roundtrip_1643_3d_not_unit.tif') |
| 173 | + to_geotiff(da, p) |
| 174 | + rt = open_geotiff(p) |
| 175 | + pw = abs(float(rt.x.values[1] - rt.x.values[0])) |
| 176 | + # Source x spacing is (700-500)/19 = ~10.526. The buggy path would |
| 177 | + # have produced pw=1.0 (the band axis spacing). |
| 178 | + assert pw > 1.5, ( |
| 179 | + f"round-tripped pixel_width={pw} suggests the band-axis spacing " |
| 180 | + f"leaked into the GeoTransform; expected ~10.526") |
| 181 | + |
| 182 | + |
| 183 | +@pytest.mark.skipif(not _HAS_GPU, reason="cupy + CUDA required") |
| 184 | +def test_write_geotiff_gpu_roundtrip_3d_no_transform_attr(tmp_path): |
| 185 | + """GPU writer shares ``_coords_to_transform`` with the CPU writer. |
| 186 | +
|
| 187 | + Same regression on the GPU path: a 3D ``(y, x, band)`` cupy |
| 188 | + DataArray without ``attrs['transform']`` would previously round-trip |
| 189 | + through a unit pixel-width transform. |
| 190 | + """ |
| 191 | + import cupy as cp |
| 192 | + |
| 193 | + from xrspatial.geotiff import write_geotiff_gpu |
| 194 | + |
| 195 | + np_arr = np.arange(10 * 20 * 3, dtype=np.uint8).reshape(10, 20, 3) |
| 196 | + da = xr.DataArray( |
| 197 | + cp.asarray(np_arr), |
| 198 | + dims=['y', 'x', 'band'], |
| 199 | + coords={ |
| 200 | + 'y': np.linspace(100.0, 200.0, 10), |
| 201 | + 'x': np.linspace(500.0, 700.0, 20), |
| 202 | + 'band': np.arange(3), |
| 203 | + }, |
| 204 | + ) |
| 205 | + p = str(tmp_path / 'roundtrip_1643_3d_gpu.tif') |
| 206 | + write_geotiff_gpu(da, p) |
| 207 | + rt = open_geotiff(p) |
| 208 | + pw = abs(float(rt.x.values[1] - rt.x.values[0])) |
| 209 | + assert pw > 1.5, ( |
| 210 | + f"GPU writer round-tripped pixel_width={pw}; expected ~10.526") |
| 211 | + ph = abs(float(rt.y.values[1] - rt.y.values[0])) |
| 212 | + assert ph > 1.5, ( |
| 213 | + f"GPU writer round-tripped pixel_height={ph}; expected ~11.111") |
0 commit comments