|
| 1 | +import numpy as np |
| 2 | +import pytest |
| 3 | +import xarray as xa |
| 4 | + |
| 5 | +from xrspatial.experimental import min_observable_height |
| 6 | + |
| 7 | + |
| 8 | +def _make_raster(data, xs=None, ys=None): |
| 9 | + """Build a DataArray with y/x coords from a 2-D numpy array.""" |
| 10 | + ny, nx = data.shape |
| 11 | + if xs is None: |
| 12 | + xs = np.arange(nx, dtype=float) |
| 13 | + if ys is None: |
| 14 | + ys = np.arange(ny, dtype=float) |
| 15 | + return xa.DataArray( |
| 16 | + data.astype(np.float64), |
| 17 | + coords=dict(x=xs, y=ys), |
| 18 | + dims=["y", "x"], |
| 19 | + ) |
| 20 | + |
| 21 | + |
| 22 | +# ---- input validation --------------------------------------------------- |
| 23 | + |
| 24 | +def test_max_height_positive(): |
| 25 | + r = _make_raster(np.zeros((3, 3))) |
| 26 | + with pytest.raises(ValueError, match="max_height must be positive"): |
| 27 | + min_observable_height(r, x=1.0, y=1.0, max_height=0) |
| 28 | + |
| 29 | + |
| 30 | +def test_precision_positive(): |
| 31 | + r = _make_raster(np.zeros((3, 3))) |
| 32 | + with pytest.raises(ValueError, match="precision must be positive"): |
| 33 | + min_observable_height(r, x=1.0, y=1.0, precision=0) |
| 34 | + |
| 35 | + |
| 36 | +def test_precision_exceeds_max_height(): |
| 37 | + r = _make_raster(np.zeros((3, 3))) |
| 38 | + with pytest.raises(ValueError, match="precision.*must not exceed"): |
| 39 | + min_observable_height(r, x=1.0, y=1.0, max_height=5, precision=10) |
| 40 | + |
| 41 | + |
| 42 | +# ---- flat terrain -------------------------------------------------------- |
| 43 | + |
| 44 | +def test_flat_terrain_all_visible_at_zero(): |
| 45 | + """On flat terrain with any positive observer_elev, all cells are |
| 46 | + visible. min_observable_height should be 0 everywhere.""" |
| 47 | + data = np.full((5, 5), 10.0) |
| 48 | + r = _make_raster(data) |
| 49 | + result = min_observable_height(r, x=2.0, y=2.0, |
| 50 | + max_height=20.0, precision=1.0) |
| 51 | + assert result.shape == r.shape |
| 52 | + # Every cell should be visible at height 0 (observer at terrain level |
| 53 | + # can see all cells at same elevation). |
| 54 | + assert np.all(result.values == 0.0) |
| 55 | + |
| 56 | + |
| 57 | +# ---- obstacle blocking --------------------------------------------------- |
| 58 | + |
| 59 | +def test_wall_requires_height(): |
| 60 | + """A wall between the observer and far cells forces higher observer.""" |
| 61 | + # y=0: 0 0 0 0 0 |
| 62 | + # y=1: 0 0 10 0 0 <- wall at (y=1, x=2) |
| 63 | + # y=2: 0 0 0 0 0 |
| 64 | + # Observer at (x=0, y=1). |
| 65 | + # |
| 66 | + # To see cell (x=3, y=1) over the wall, the line of sight from |
| 67 | + # (0, h) to (3, 0) must clear (2, 10). LOS height at x=2 is h/3, |
| 68 | + # so h >= 30 is required. |
| 69 | + data = np.zeros((3, 5)) |
| 70 | + data[1, 2] = 10.0 # wall |
| 71 | + r = _make_raster(data) |
| 72 | + |
| 73 | + result = min_observable_height(r, x=0.0, y=1.0, |
| 74 | + max_height=50.0, precision=1.0) |
| 75 | + |
| 76 | + # Observer cell itself should be 0. |
| 77 | + assert result.sel(x=0.0, y=1.0).item() == 0.0 |
| 78 | + |
| 79 | + # Cells on same side as observer (x=1) should be visible at 0. |
| 80 | + assert result.sel(x=1.0, y=1.0).item() == 0.0 |
| 81 | + |
| 82 | + # Cell behind the wall (x=3, y=1) needs a significant height. |
| 83 | + behind_wall = result.sel(x=3.0, y=1.0).item() |
| 84 | + assert behind_wall > 0.0, "cells behind wall should need height > 0" |
| 85 | + |
| 86 | + # The wall cell itself is visible from height 0 (it's tall, angles |
| 87 | + # up to observer) or at least from some modest height. |
| 88 | + wall_h = result.sel(x=2.0, y=1.0).item() |
| 89 | + assert wall_h < behind_wall, "wall cell should be easier to see than cells behind it" |
| 90 | + |
| 91 | + |
| 92 | +def test_tall_peak_blocks_cells_behind(): |
| 93 | + """A peak should block cells directly behind it, requiring more height.""" |
| 94 | + # Observer at (x=0, y=1), peak at (x=2, y=1), target at (x=4, y=1). |
| 95 | + # Need at least 2 rows so viewshed can compute ns_res. |
| 96 | + data = np.zeros((3, 5)) |
| 97 | + data[1, 2] = 8.0 # peak |
| 98 | + r = _make_raster(data) |
| 99 | + |
| 100 | + result = min_observable_height(r, x=0.0, y=1.0, |
| 101 | + max_height=40.0, precision=0.5) |
| 102 | + |
| 103 | + h_behind = result.values[1, 4] |
| 104 | + h_front = result.values[1, 1] |
| 105 | + # cell in front of peak should be visible from lower height |
| 106 | + assert h_front < h_behind |
| 107 | + |
| 108 | + |
| 109 | +# ---- NaN for unreachable cells ------------------------------------------- |
| 110 | + |
| 111 | +def test_unreachable_cells_are_nan(): |
| 112 | + """If a cell cannot be seen even at max_height, result should be NaN.""" |
| 113 | + # Very tall wall close to observer, low max_height. |
| 114 | + data = np.zeros((3, 3)) |
| 115 | + data[1, 1] = 1000.0 # enormous wall next to observer |
| 116 | + r = _make_raster(data) |
| 117 | + |
| 118 | + result = min_observable_height(r, x=0.0, y=1.0, |
| 119 | + max_height=5.0, precision=1.0) |
| 120 | + |
| 121 | + # Some cells behind the 1000-unit wall should be NaN at max_height=5. |
| 122 | + # The far corner (x=2, y=0 or y=2) is behind the wall. |
| 123 | + far_val = result.sel(x=2.0, y=0.0).item() |
| 124 | + assert np.isnan(far_val), "cell behind huge wall should be NaN at low max_height" |
| 125 | + |
| 126 | + |
| 127 | +# ---- output shape and coords --------------------------------------------- |
| 128 | + |
| 129 | +def test_output_shape_coords_match_input(): |
| 130 | + data = np.random.default_rng(42).uniform(0, 10, (6, 8)) |
| 131 | + xs = np.linspace(0, 7, 8) |
| 132 | + ys = np.linspace(0, 5, 6) |
| 133 | + r = _make_raster(data, xs=xs, ys=ys) |
| 134 | + |
| 135 | + result = min_observable_height(r, x=3.0, y=2.0, |
| 136 | + max_height=20, precision=2.0) |
| 137 | + assert result.shape == r.shape |
| 138 | + np.testing.assert_array_equal(result.coords['x'].values, xs) |
| 139 | + np.testing.assert_array_equal(result.coords['y'].values, ys) |
| 140 | + assert result.dims == r.dims |
| 141 | + |
| 142 | + |
| 143 | +# ---- monotonicity property ----------------------------------------------- |
| 144 | + |
| 145 | +def test_monotonicity(): |
| 146 | + """Visibility is monotonic: if visible at h, visible at all h' > h. |
| 147 | +
|
| 148 | + This indirectly tests that the binary search is correct: for each |
| 149 | + cell, all heights >= min_observable_height should show it as visible. |
| 150 | + """ |
| 151 | + data = np.zeros((5, 5)) |
| 152 | + data[2, 3] = 8.0 |
| 153 | + r = _make_raster(data) |
| 154 | + |
| 155 | + result = min_observable_height(r, x=0.0, y=2.0, |
| 156 | + max_height=20.0, precision=1.0) |
| 157 | + |
| 158 | + # Pick a cell that requires non-zero height. |
| 159 | + h_needed = result.sel(x=4.0, y=2.0).item() |
| 160 | + if not np.isnan(h_needed) and h_needed > 0: |
| 161 | + from xrspatial import viewshed |
| 162 | + # Should be invisible slightly below threshold. |
| 163 | + vs_below = viewshed(r, x=0.0, y=2.0, observer_elev=max(h_needed - 1.0, 0)) |
| 164 | + vs_above = viewshed(r, x=0.0, y=2.0, observer_elev=h_needed + 1.0) |
| 165 | + # At or above threshold the cell should be visible. |
| 166 | + assert vs_above.sel(x=4.0, y=2.0).item() != -1 |
| 167 | + |
| 168 | + |
| 169 | +# ---- precision affects granularity --------------------------------------- |
| 170 | + |
| 171 | +def test_finer_precision(): |
| 172 | + """With finer precision, results should be at least as good (<=) as |
| 173 | + coarser precision.""" |
| 174 | + data = np.zeros((3, 5)) |
| 175 | + data[1, 2] = 10.0 |
| 176 | + r = _make_raster(data) |
| 177 | + |
| 178 | + coarse = min_observable_height(r, x=0.0, y=1.0, |
| 179 | + max_height=20, precision=5.0) |
| 180 | + fine = min_observable_height(r, x=0.0, y=1.0, |
| 181 | + max_height=20, precision=0.5) |
| 182 | + |
| 183 | + # Fine result should be <= coarse result for all finite cells |
| 184 | + # (finer search can find an equal or lower threshold). |
| 185 | + mask = np.isfinite(fine.values) & np.isfinite(coarse.values) |
| 186 | + assert np.all(fine.values[mask] <= coarse.values[mask] + 1e-10) |
| 187 | + |
| 188 | + |
| 189 | +# ---- accessor integration ------------------------------------------------ |
| 190 | + |
| 191 | +def test_accessor(): |
| 192 | + """min_observable_height is available via .xrs accessor.""" |
| 193 | + import xrspatial # noqa: F401 – registers accessor |
| 194 | + data = np.zeros((3, 3)) |
| 195 | + r = _make_raster(data) |
| 196 | + result = r.xrs.min_observable_height(x=1.0, y=1.0, |
| 197 | + max_height=10, precision=2) |
| 198 | + assert result.shape == r.shape |
0 commit comments