|
| 1 | +"""Regression test for issue #1734. |
| 2 | +
|
| 3 | +Under the lenient default (``XRSPATIAL_GEOTIFF_STRICT`` unset), |
| 4 | +``read_vrt`` warns once per unreadable source and continues, producing |
| 5 | +a mosaic with zero-filled holes for integer VRTs that downstream code |
| 6 | +cannot distinguish from real data. The warning is easy to miss in a |
| 7 | +pipeline that ignores ``UserWarning``s. |
| 8 | +
|
| 9 | +This module pins the new behaviour: the returned DataArray now carries |
| 10 | +an ``attrs['vrt_holes']`` list describing each skipped source so |
| 11 | +callers can detect a partial mosaic with a single attribute lookup. |
| 12 | +Strict mode is unchanged and still raises. |
| 13 | +""" |
| 14 | +from __future__ import annotations |
| 15 | + |
| 16 | +import warnings |
| 17 | + |
| 18 | +import pytest |
| 19 | + |
| 20 | +from xrspatial.geotiff import GeoTIFFFallbackWarning, read_vrt |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture |
| 24 | +def clear_strict_env(monkeypatch): |
| 25 | + monkeypatch.delenv('XRSPATIAL_GEOTIFF_STRICT', raising=False) |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture |
| 29 | +def set_strict_env(monkeypatch): |
| 30 | + monkeypatch.setenv('XRSPATIAL_GEOTIFF_STRICT', '1') |
| 31 | + |
| 32 | + |
| 33 | +def _write_vrt_with_missing_source(vrt_path, missing_src) -> None: |
| 34 | + vrt_path.write_text( |
| 35 | + '<VRTDataset rasterXSize="4" rasterYSize="4">\n' |
| 36 | + ' <SRS></SRS>\n' |
| 37 | + ' <GeoTransform>0, 1, 0, 0, 0, -1</GeoTransform>\n' |
| 38 | + ' <VRTRasterBand dataType="Float32" band="1">\n' |
| 39 | + ' <NoDataValue>-9999</NoDataValue>\n' |
| 40 | + ' <SimpleSource>\n' |
| 41 | + f' <SourceFilename relativeToVRT="0">{missing_src}' |
| 42 | + '</SourceFilename>\n' |
| 43 | + ' <SourceBand>1</SourceBand>\n' |
| 44 | + ' <SrcRect xOff="0" yOff="0" xSize="4" ySize="4"/>\n' |
| 45 | + ' <DstRect xOff="0" yOff="0" xSize="4" ySize="4"/>\n' |
| 46 | + ' </SimpleSource>\n' |
| 47 | + ' </VRTRasterBand>\n' |
| 48 | + '</VRTDataset>\n' |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +def test_skipped_source_records_vrt_holes_attr( |
| 53 | + clear_strict_env, tmp_path, |
| 54 | +): |
| 55 | + """A VRT with a missing source returns a DataArray whose attrs |
| 56 | + carry a ``vrt_holes`` entry naming the source, band, dst_rect, |
| 57 | + and underlying error.""" |
| 58 | + vrt_path = tmp_path / 'mosaic_1734_missing.vrt' |
| 59 | + missing_src = f'{tmp_path}/does_not_exist_1734.tif' |
| 60 | + _write_vrt_with_missing_source(vrt_path, missing_src) |
| 61 | + |
| 62 | + with warnings.catch_warnings(): |
| 63 | + warnings.simplefilter('ignore', GeoTIFFFallbackWarning) |
| 64 | + da = read_vrt(str(vrt_path)) |
| 65 | + |
| 66 | + assert 'vrt_holes' in da.attrs |
| 67 | + holes = da.attrs['vrt_holes'] |
| 68 | + assert isinstance(holes, list) |
| 69 | + assert len(holes) == 1 |
| 70 | + h = holes[0] |
| 71 | + assert h['source'].endswith('does_not_exist_1734.tif') |
| 72 | + assert h['band'] == 1 |
| 73 | + assert h['dst_rect'] == (0, 0, 4, 4) |
| 74 | + assert 'error' in h |
| 75 | + assert h['error'] # non-empty |
| 76 | + |
| 77 | + |
| 78 | +def test_no_holes_attr_when_all_sources_read(clear_strict_env, tmp_path): |
| 79 | + """A successful VRT read does not advertise an empty ``vrt_holes`` |
| 80 | + attr; the key is omitted entirely so ``"vrt_holes" in attrs`` is a |
| 81 | + cheap completeness check.""" |
| 82 | + import numpy as np |
| 83 | + import xarray as xr |
| 84 | + from xrspatial.geotiff import to_geotiff |
| 85 | + |
| 86 | + # Write a real source the VRT can reference. |
| 87 | + src_path = tmp_path / 'src_1734.tif' |
| 88 | + arr = np.arange(16, dtype=np.float32).reshape(4, 4) |
| 89 | + da_src = xr.DataArray( |
| 90 | + arr, dims=['y', 'x'], |
| 91 | + coords={'y': np.linspace(3.5, 0.5, 4), |
| 92 | + 'x': np.linspace(0.5, 3.5, 4)}, |
| 93 | + attrs={'crs': 4326}, |
| 94 | + ) |
| 95 | + to_geotiff(da_src, str(src_path), compression='none') |
| 96 | + |
| 97 | + vrt_path = tmp_path / 'mosaic_1734_ok.vrt' |
| 98 | + vrt_path.write_text( |
| 99 | + '<VRTDataset rasterXSize="4" rasterYSize="4">\n' |
| 100 | + ' <SRS></SRS>\n' |
| 101 | + ' <GeoTransform>0, 1, 0, 0, 0, -1</GeoTransform>\n' |
| 102 | + ' <VRTRasterBand dataType="Float32" band="1">\n' |
| 103 | + ' <SimpleSource>\n' |
| 104 | + f' <SourceFilename relativeToVRT="0">{src_path}' |
| 105 | + '</SourceFilename>\n' |
| 106 | + ' <SourceBand>1</SourceBand>\n' |
| 107 | + ' <SrcRect xOff="0" yOff="0" xSize="4" ySize="4"/>\n' |
| 108 | + ' <DstRect xOff="0" yOff="0" xSize="4" ySize="4"/>\n' |
| 109 | + ' </SimpleSource>\n' |
| 110 | + ' </VRTRasterBand>\n' |
| 111 | + '</VRTDataset>\n' |
| 112 | + ) |
| 113 | + |
| 114 | + with warnings.catch_warnings(): |
| 115 | + warnings.simplefilter('error', GeoTIFFFallbackWarning) |
| 116 | + da = read_vrt(str(vrt_path)) |
| 117 | + |
| 118 | + assert 'vrt_holes' not in da.attrs |
| 119 | + |
| 120 | + |
| 121 | +def test_strict_mode_still_raises(set_strict_env, tmp_path): |
| 122 | + """Strict mode is unchanged: the missing source raises and no |
| 123 | + DataArray is returned.""" |
| 124 | + vrt_path = tmp_path / 'mosaic_1734_strict.vrt' |
| 125 | + missing_src = f'{tmp_path}/does_not_exist_1734_strict.tif' |
| 126 | + _write_vrt_with_missing_source(vrt_path, missing_src) |
| 127 | + |
| 128 | + with pytest.raises(Exception): |
| 129 | + read_vrt(str(vrt_path)) |
| 130 | + |
| 131 | + |
| 132 | +def test_warning_mentions_how_to_detect_holes(clear_strict_env, tmp_path): |
| 133 | + """The fallback warning now points callers at the attr or the |
| 134 | + strict env var so the recovery path is discoverable from a single |
| 135 | + captured warning.""" |
| 136 | + vrt_path = tmp_path / 'mosaic_1734_msg.vrt' |
| 137 | + missing_src = f'{tmp_path}/does_not_exist_1734_msg.tif' |
| 138 | + _write_vrt_with_missing_source(vrt_path, missing_src) |
| 139 | + |
| 140 | + with warnings.catch_warnings(record=True) as w: |
| 141 | + warnings.simplefilter('always') |
| 142 | + read_vrt(str(vrt_path)) |
| 143 | + |
| 144 | + fallback = [ |
| 145 | + x for x in w if issubclass(x.category, GeoTIFFFallbackWarning) |
| 146 | + ] |
| 147 | + assert fallback, "expected at least one GeoTIFFFallbackWarning" |
| 148 | + msg = ' '.join(str(x.message) for x in fallback) |
| 149 | + assert 'vrt_holes' in msg or 'XRSPATIAL_GEOTIFF_STRICT' in msg |
0 commit comments