|
| 1 | +"""Refuse non-numeric ``nodata=`` / ``attrs['_FillValue']`` (#1973). |
| 2 | +
|
| 3 | +The writer compares the resolved nodata against pixel values via |
| 4 | +``np.isnan`` and casts it to the array dtype. A non-numeric value |
| 5 | +used to fall through ``_resolve_nodata_attr`` (returned verbatim) or |
| 6 | +the ``nodata=`` kwarg path and then crash inside NumPy with |
| 7 | +``ufunc 'isnan' not supported``. Both the entry point and the attr |
| 8 | +resolution path now refuse non-numeric values up front with a clear |
| 9 | +error. |
| 10 | +""" |
| 11 | +from __future__ import annotations |
| 12 | + |
| 13 | +import io |
| 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._attrs import _resolve_nodata_attr |
| 21 | +from xrspatial.geotiff._validation import _validate_nodata_arg |
| 22 | + |
| 23 | + |
| 24 | +def _nan_square(): |
| 25 | + return xr.DataArray( |
| 26 | + np.full((4, 4), np.nan, dtype=np.float32), |
| 27 | + coords={'y': np.arange(4.0), 'x': np.arange(4.0)}, |
| 28 | + dims=('y', 'x'), |
| 29 | + ) |
| 30 | + |
| 31 | + |
| 32 | +@pytest.mark.parametrize("bad", ['missing', object(), [1, 2]]) |
| 33 | +def test_validate_nodata_arg_rejects_non_numeric(bad): |
| 34 | + with pytest.raises(ValueError, match="nodata must be numeric"): |
| 35 | + _validate_nodata_arg(bad) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize("ok", [None, 0, -9999, 1.5, np.float32(-1), np.int64(0)]) |
| 39 | +def test_validate_nodata_arg_accepts_numeric_and_none(ok): |
| 40 | + _validate_nodata_arg(ok) |
| 41 | + |
| 42 | + |
| 43 | +def test_resolve_nodata_attr_rejects_non_numeric_fillvalue(): |
| 44 | + with pytest.raises(ValueError, match="_FillValue"): |
| 45 | + _resolve_nodata_attr({'_FillValue': 'missing'}) |
| 46 | + |
| 47 | + |
| 48 | +def test_resolve_nodata_attr_rejects_non_numeric_nodata_attr(): |
| 49 | + with pytest.raises(ValueError, match=r"attrs\['nodata'\]"): |
| 50 | + _resolve_nodata_attr({'nodata': 'missing'}) |
| 51 | + |
| 52 | + |
| 53 | +def test_resolve_nodata_attr_skips_non_numeric_in_nodatavals(): |
| 54 | + # nodatavals (rioxarray's per-band tuple) keeps its skip-on-non-numeric |
| 55 | + # behaviour: those values often come from arbitrary upstream pipelines |
| 56 | + # and a single bad entry should not block writing. |
| 57 | + assert _resolve_nodata_attr({'nodatavals': ('NaN ', -9999.0)}) == -9999.0 |
| 58 | + |
| 59 | + |
| 60 | +def test_resolve_nodata_attr_still_accepts_numeric_fillvalue(): |
| 61 | + assert _resolve_nodata_attr({'_FillValue': -9999}) == -9999 |
| 62 | + |
| 63 | + |
| 64 | +def test_resolve_nodata_attr_returns_none_for_nan_fillvalue(): |
| 65 | + assert _resolve_nodata_attr({'_FillValue': float('nan')}) is None |
| 66 | + |
| 67 | + |
| 68 | +def test_to_geotiff_rejects_non_numeric_nodata_kwarg(): |
| 69 | + buf = io.BytesIO() |
| 70 | + with pytest.raises(ValueError, match="nodata must be numeric"): |
| 71 | + to_geotiff(_nan_square(), buf, nodata='missing') |
| 72 | + |
| 73 | + |
| 74 | +def test_to_geotiff_rejects_non_numeric_fillvalue_attr(): |
| 75 | + da = _nan_square() |
| 76 | + da.attrs['_FillValue'] = 'missing' |
| 77 | + buf = io.BytesIO() |
| 78 | + with pytest.raises(ValueError, match="_FillValue"): |
| 79 | + to_geotiff(da, buf) |
| 80 | + |
| 81 | + |
| 82 | +def test_to_geotiff_vrt_path_rejects_non_numeric_nodata(tmp_path): |
| 83 | + vrt_path = str(tmp_path / "tmp_1973_vrt.vrt") |
| 84 | + with pytest.raises(ValueError, match="nodata must be numeric"): |
| 85 | + to_geotiff(_nan_square(), vrt_path, nodata='missing') |
| 86 | + |
| 87 | + |
| 88 | +def test_to_geotiff_accepts_numeric_nodata_kwarg(): |
| 89 | + buf = io.BytesIO() |
| 90 | + to_geotiff(_nan_square(), buf, nodata=-9999) |
| 91 | + assert buf.getbuffer().nbytes > 0 |
0 commit comments