|
| 1 | +"""Regression tests for issue #1752. |
| 2 | +
|
| 3 | +Two public geotiff entry points used to accept size parameters without |
| 4 | +checking they were positive: |
| 5 | +
|
| 6 | +* ``to_geotiff(..., tiled=True, tile_size=0)`` reached the tiled writer |
| 7 | + where ``math.ceil(width / tile_size)`` raised ``ZeroDivisionError``, |
| 8 | + with a traceback that did not name ``tile_size`` as the bad input. |
| 9 | +* ``read_geotiff_dask(chunks=0)`` (or ``chunks=(0, N)``) propagated zero |
| 10 | + into dask's chunk math and surfaced as a confusing ``range()`` / |
| 11 | + empty-chunks error. |
| 12 | +
|
| 13 | +Both entry points now validate the size arguments up front and raise |
| 14 | +``ValueError`` naming the parameter and the invalid value. |
| 15 | +""" |
| 16 | +from __future__ import annotations |
| 17 | + |
| 18 | +import os |
| 19 | + |
| 20 | +import numpy as np |
| 21 | +import pytest |
| 22 | +import xarray as xr |
| 23 | + |
| 24 | +from xrspatial.geotiff import read_geotiff_dask, to_geotiff |
| 25 | + |
| 26 | + |
| 27 | +def _make_raster(tmp_path: str) -> str: |
| 28 | + arr = np.arange(100, dtype=np.float32).reshape(10, 10) |
| 29 | + da = xr.DataArray( |
| 30 | + arr, dims=['y', 'x'], |
| 31 | + coords={'y': np.arange(10), 'x': np.arange(10)}, |
| 32 | + attrs={'transform': (1.0, 0.0, 0.0, 0.0, -1.0, 10.0)}, |
| 33 | + ) |
| 34 | + path = os.path.join(tmp_path, 'raster.tif') |
| 35 | + to_geotiff(da, path) |
| 36 | + return path |
| 37 | + |
| 38 | + |
| 39 | +# -- to_geotiff tile_size --------------------------------------------------- |
| 40 | + |
| 41 | + |
| 42 | +def test_to_geotiff_tile_size_zero_raises(tmp_path): |
| 43 | + arr = np.arange(100, dtype=np.float32).reshape(10, 10) |
| 44 | + da = xr.DataArray(arr, dims=['y', 'x']) |
| 45 | + out = os.path.join(str(tmp_path), 'out.tif') |
| 46 | + with pytest.raises(ValueError, match='tile_size'): |
| 47 | + to_geotiff(da, out, tiled=True, tile_size=0) |
| 48 | + |
| 49 | + |
| 50 | +def test_to_geotiff_tile_size_negative_raises(tmp_path): |
| 51 | + arr = np.arange(100, dtype=np.float32).reshape(10, 10) |
| 52 | + da = xr.DataArray(arr, dims=['y', 'x']) |
| 53 | + out = os.path.join(str(tmp_path), 'out.tif') |
| 54 | + with pytest.raises(ValueError, match='tile_size'): |
| 55 | + to_geotiff(da, out, tiled=True, tile_size=-1) |
| 56 | + |
| 57 | + |
| 58 | +def test_to_geotiff_tile_size_non_int_raises(tmp_path): |
| 59 | + arr = np.arange(100, dtype=np.float32).reshape(10, 10) |
| 60 | + da = xr.DataArray(arr, dims=['y', 'x']) |
| 61 | + out = os.path.join(str(tmp_path), 'out.tif') |
| 62 | + with pytest.raises(ValueError, match='tile_size'): |
| 63 | + to_geotiff(da, out, tiled=True, tile_size=256.0) |
| 64 | + |
| 65 | + |
| 66 | +def test_to_geotiff_tile_size_one_still_writes(tmp_path): |
| 67 | + # tile_size=1 is silly but technically valid TIFF; do not reject it. |
| 68 | + arr = np.arange(16, dtype=np.float32).reshape(4, 4) |
| 69 | + da = xr.DataArray(arr, dims=['y', 'x']) |
| 70 | + out = os.path.join(str(tmp_path), 'out.tif') |
| 71 | + to_geotiff(da, out, tiled=True, tile_size=1) |
| 72 | + assert os.path.exists(out) |
| 73 | + |
| 74 | + |
| 75 | +# -- read_geotiff_dask chunks ---------------------------------------------- |
| 76 | + |
| 77 | + |
| 78 | +def test_read_geotiff_dask_chunks_zero_raises(tmp_path): |
| 79 | + path = _make_raster(str(tmp_path)) |
| 80 | + with pytest.raises(ValueError, match='chunks'): |
| 81 | + read_geotiff_dask(path, chunks=0) |
| 82 | + |
| 83 | + |
| 84 | +def test_read_geotiff_dask_chunks_negative_raises(tmp_path): |
| 85 | + path = _make_raster(str(tmp_path)) |
| 86 | + with pytest.raises(ValueError, match='chunks'): |
| 87 | + read_geotiff_dask(path, chunks=-1) |
| 88 | + |
| 89 | + |
| 90 | +def test_read_geotiff_dask_chunks_tuple_zero_row_raises(tmp_path): |
| 91 | + path = _make_raster(str(tmp_path)) |
| 92 | + with pytest.raises(ValueError, match='chunks'): |
| 93 | + read_geotiff_dask(path, chunks=(0, 256)) |
| 94 | + |
| 95 | + |
| 96 | +def test_read_geotiff_dask_chunks_tuple_negative_col_raises(tmp_path): |
| 97 | + path = _make_raster(str(tmp_path)) |
| 98 | + with pytest.raises(ValueError, match='chunks'): |
| 99 | + read_geotiff_dask(path, chunks=(256, -1)) |
| 100 | + |
| 101 | + |
| 102 | +def test_read_geotiff_dask_chunks_tuple_wrong_length_raises(tmp_path): |
| 103 | + path = _make_raster(str(tmp_path)) |
| 104 | + with pytest.raises(ValueError, match='chunks'): |
| 105 | + read_geotiff_dask(path, chunks=(64, 64, 64)) |
| 106 | + |
| 107 | + |
| 108 | +def test_read_geotiff_dask_positive_int_chunks_works(tmp_path): |
| 109 | + path = _make_raster(str(tmp_path)) |
| 110 | + arr = read_geotiff_dask(path, chunks=256) |
| 111 | + assert arr.shape == (10, 10) |
| 112 | + # Materialise to confirm the lazy graph is well-formed. |
| 113 | + np.asarray(arr) |
| 114 | + |
| 115 | + |
| 116 | +def test_read_geotiff_dask_positive_tuple_chunks_works(tmp_path): |
| 117 | + path = _make_raster(str(tmp_path)) |
| 118 | + arr = read_geotiff_dask(path, chunks=(4, 8)) |
| 119 | + assert arr.shape == (10, 10) |
| 120 | + np.asarray(arr) |
0 commit comments