|
| 1 | +"""Regression tests for issue #1751. |
| 2 | +
|
| 3 | +``read_vrt`` parses ``<ResampleAlg>`` from a ``ComplexSource`` but always |
| 4 | +calls ``_resample_nearest`` during placement, regardless of the parsed |
| 5 | +algorithm. A VRT declaring ``Bilinear`` / ``Cubic`` / ``Average`` / |
| 6 | +``Mode`` therefore receives nearest-sampled pixels mislabelled as the |
| 7 | +requested algorithm -- silently wrong analytics. |
| 8 | +
|
| 9 | +The fix raises ``NotImplementedError`` at the resample call site when |
| 10 | +the source declares an unsupported algorithm and the SrcRect/DstRect |
| 11 | +sizes actually differ (a 1:1 placement is nearest-equivalent and stays |
| 12 | +permissive). Nearest, NearestNeighbour, NEAR, empty, and an absent |
| 13 | +``<ResampleAlg>`` element are all accepted. |
| 14 | +""" |
| 15 | +from __future__ import annotations |
| 16 | + |
| 17 | +import numpy as np |
| 18 | +import pytest |
| 19 | + |
| 20 | +from xrspatial.geotiff._vrt import read_vrt |
| 21 | +from xrspatial.geotiff._writer import write |
| 22 | + |
| 23 | + |
| 24 | +def _write_src(tmp_path) -> str: |
| 25 | + """Write a 4x4 uint16 source TIFF and return its path.""" |
| 26 | + src = np.arange(16, dtype=np.uint16).reshape(4, 4) |
| 27 | + src_path = str(tmp_path / 'src.tif') |
| 28 | + write(src, src_path, compression='none', tiled=False) |
| 29 | + return src_path |
| 30 | + |
| 31 | + |
| 32 | +def _write_vrt(tmp_path, xml: str, name: str = 'test.vrt') -> str: |
| 33 | + p = str(tmp_path / name) |
| 34 | + with open(p, 'w') as f: |
| 35 | + f.write(xml) |
| 36 | + return p |
| 37 | + |
| 38 | + |
| 39 | +def _vrt_xml(src_path: str, *, alg_elem: str, |
| 40 | + dst_x: int = 2, dst_y: int = 2) -> str: |
| 41 | + """Render a VRT XML with a 4x4 SrcRect and configurable DstRect+Alg. |
| 42 | +
|
| 43 | + ``alg_elem`` is the raw ``<ResampleAlg>...</ResampleAlg>`` element |
| 44 | + to splice into the ``<ComplexSource>``, or the empty string to |
| 45 | + omit it entirely. |
| 46 | + """ |
| 47 | + return f"""<VRTDataset rasterXSize="{dst_x}" rasterYSize="{dst_y}"> |
| 48 | + <GeoTransform>0.0, 2.0, 0.0, 0.0, 0.0, -2.0</GeoTransform> |
| 49 | + <VRTRasterBand dataType="UInt16" band="1"> |
| 50 | + <ComplexSource> |
| 51 | + <SourceFilename relativeToVRT="0">{src_path}</SourceFilename> |
| 52 | + <SourceBand>1</SourceBand> |
| 53 | + <SrcRect xOff="0" yOff="0" xSize="4" ySize="4"/> |
| 54 | + <DstRect xOff="0" yOff="0" xSize="{dst_x}" ySize="{dst_y}"/> |
| 55 | + {alg_elem} |
| 56 | + </ComplexSource> |
| 57 | + </VRTRasterBand> |
| 58 | +</VRTDataset>""" |
| 59 | + |
| 60 | + |
| 61 | +@pytest.mark.parametrize('alg', ['Bilinear', 'Cubic', 'CubicSpline', |
| 62 | + 'Lanczos', 'Average', 'Mode']) |
| 63 | +def test_unsupported_resample_alg_raises(tmp_path, alg): |
| 64 | + """A ComplexSource declaring any non-nearest algorithm with a size |
| 65 | + change must raise ``NotImplementedError`` rather than return |
| 66 | + silently nearest-sampled pixels.""" |
| 67 | + src_path = _write_src(tmp_path) |
| 68 | + xml = _vrt_xml(src_path, |
| 69 | + alg_elem=f'<ResampleAlg>{alg}</ResampleAlg>') |
| 70 | + vrt_path = _write_vrt(tmp_path, xml, f'{alg.lower()}.vrt') |
| 71 | + |
| 72 | + with pytest.raises(NotImplementedError) as excinfo: |
| 73 | + read_vrt(vrt_path) |
| 74 | + msg = str(excinfo.value) |
| 75 | + assert alg in msg |
| 76 | + assert '1751' in msg |
| 77 | + |
| 78 | + |
| 79 | +def test_unsupported_resample_alg_case_insensitive(tmp_path): |
| 80 | + """Algorithm names are matched case-insensitively: ``bilinear`` |
| 81 | + (lowercase) is the same unsupported request as ``Bilinear``.""" |
| 82 | + src_path = _write_src(tmp_path) |
| 83 | + xml = _vrt_xml(src_path, |
| 84 | + alg_elem='<ResampleAlg>bilinear</ResampleAlg>') |
| 85 | + vrt_path = _write_vrt(tmp_path, xml, 'lower.vrt') |
| 86 | + |
| 87 | + with pytest.raises(NotImplementedError, match='bilinear'): |
| 88 | + read_vrt(vrt_path) |
| 89 | + |
| 90 | + |
| 91 | +@pytest.mark.parametrize('alg', ['Nearest', 'NearestNeighbour', |
| 92 | + 'NearestNeighbor', 'NEAR', 'nearest', |
| 93 | + 'NEAREST', '']) |
| 94 | +def test_nearest_variants_accepted(tmp_path, alg): |
| 95 | + """Nearest (and its case / spelling variants, plus empty text) is |
| 96 | + the implemented algorithm and must round-trip without raising.""" |
| 97 | + src_path = _write_src(tmp_path) |
| 98 | + xml = _vrt_xml(src_path, |
| 99 | + alg_elem=f'<ResampleAlg>{alg}</ResampleAlg>') |
| 100 | + vrt_path = _write_vrt(tmp_path, xml, f'near_{alg or "empty"}.vrt') |
| 101 | + |
| 102 | + arr, _ = read_vrt(vrt_path) |
| 103 | + assert arr.shape == (2, 2) |
| 104 | + |
| 105 | + |
| 106 | +def test_missing_resample_alg_accepted(tmp_path): |
| 107 | + """Absent ``<ResampleAlg>`` (GDAL's nearest default) must still |
| 108 | + round-trip without raising.""" |
| 109 | + src_path = _write_src(tmp_path) |
| 110 | + xml = _vrt_xml(src_path, alg_elem='') |
| 111 | + vrt_path = _write_vrt(tmp_path, xml, 'absent.vrt') |
| 112 | + |
| 113 | + arr, _ = read_vrt(vrt_path) |
| 114 | + assert arr.shape == (2, 2) |
| 115 | + |
| 116 | + |
| 117 | +def test_bilinear_at_same_size_does_not_raise(tmp_path): |
| 118 | + """A ``Bilinear`` declaration with matching SrcRect/DstRect sizes |
| 119 | + is nearest-equivalent (no resample step runs) so the read is |
| 120 | + accepted. This pins down the resample-site placement of the |
| 121 | + check -- a parse-time check would have rejected this case too.""" |
| 122 | + src_path = _write_src(tmp_path) |
| 123 | + # SrcRect 4x4, DstRect 4x4: needs_resample is False, no kernel |
| 124 | + # call, no silent wrongness. |
| 125 | + xml = _vrt_xml(src_path, |
| 126 | + alg_elem='<ResampleAlg>Bilinear</ResampleAlg>', |
| 127 | + dst_x=4, dst_y=4) |
| 128 | + vrt_path = _write_vrt(tmp_path, xml, 'bilinear_1to1.vrt') |
| 129 | + |
| 130 | + arr, _ = read_vrt(vrt_path) |
| 131 | + assert arr.shape == (4, 4) |
0 commit comments