Source code for wolfhece._builtin_plugins.dtm_wallonia_1m.tests.test_companion

"""Integration tests for the dtm_wallonia_1m built-in plugin.

These tests make real HTTP requests to download the tile-index shapefile and,
optionally, individual 1 m DTM tiles.  Run them with::

    pytest -m network wolfhece/_builtin_plugins/dtm_wallonia_1m/tests/ -v

They are skipped in offline / CI environments unless the ``network`` mark is
explicitly requested.

Geographic notes
----------------
All coordinates are in **EPSG:31370 (Belgian Lambert 1972)**.
The approximate geographic centre of Wallonia is around
    x = 214 500 m,  y = 98 000 m  (Condroz plateau, near Ciney).
The 500 m × 500 m test window is centred there.
"""
from __future__ import annotations

import numpy as np
import pytest

# ---------------------------------------------------------------------------
# Shared geometry helpers
# ---------------------------------------------------------------------------

[docs] def _make_bounds_vec(xmin: float, ymin: float, xmax: float, ymax: float): """Return a closed :class:`~wolfhece.PyVertexvectors.vector` bounding rectangle.""" from wolfhece.PyVertexvectors import vector v = vector() v.add_vertices_from_array( np.array([[xmin, ymin], [xmax, ymin], [xmax, ymax], [xmin, ymax]]) ) v.force_to_close() return v
# Centre of Wallonia (Lambert 1972), 500 m × 500 m window _CX, _CY = 214_500.0, 98_000.0
[docs] _HALF = 250.0
[docs] _XMIN = _CX - _HALF
[docs] _XMAX = _CX + _HALF
[docs] _YMIN = _CY - _HALF
[docs] _YMAX = _CY + _HALF
# --------------------------------------------------------------------------- # Tests # --------------------------------------------------------------------------- @pytest.mark.network
[docs] class TestDtmWalloniaTiles: """Tests for the internal :class:`_DtmWalloniaTiles` tile manager."""
[docs] def test_download_index(self): """The tile-index shapefile is downloaded (or retrieved from cache).""" from wolfhece._builtin_plugins.dtm_wallonia_1m.companion import _DtmWalloniaTiles tiles = _DtmWalloniaTiles() assert tiles.idx_path.exists(), ( f"Tile-index shapefile not found at {tiles.idx_path}" ) assert tiles.idx_path.suffix == '.shp' # The Tiles object must expose at least one zone with vectors. assert tiles._tiles.myzones, "Tile index is empty — no zones loaded." total = sum(len(z.myvectors) for z in tiles._tiles.myzones) assert total > 0, "Tile index has no tile entries."
[docs] def test_extract_center_wallonia_500m(self): """A 500 m × 500 m crop around the centre of Wallonia returns a valid WolfArray.""" from wolfhece._builtin_plugins.dtm_wallonia_1m.companion import _DtmWalloniaTiles from wolfhece.wolf_array import WolfArray tiles = _DtmWalloniaTiles() bounds = _make_bounds_vec(_XMIN, _YMIN, _XMAX, _YMAX) dtm = tiles.extract(bounds) assert isinstance(dtm, WolfArray), ( f"Expected WolfArray, got {type(dtm)}" ) assert dtm.array is not None, "WolfArray.array is None — data not loaded." # At 1 m resolution the array should be roughly 500 × 500 cells. assert dtm.dx == pytest.approx(1.0, abs=0.0), ( f"Expected 1 m cell size, got dx={dtm.dx}" ) assert dtm.dy == pytest.approx(1.0, abs=0.0), ( f"Expected 1 m cell size, got dy={dtm.dy}" ) assert dtm.nbx == pytest.approx(500, abs=0), ( f"Expected ~500 columns, got {dtm.nbx}" ) assert dtm.nby == pytest.approx(500, abs=0), ( f"Expected ~500 rows, got {dtm.nby}" ) # The terrain in Wallonia is never at 0 m or below sea level in this area. unmasked = dtm.array[~dtm.array.mask] if dtm.array.mask.any() else dtm.array.data assert unmasked.size > 0, "All pixels are masked — nothing was extracted." assert unmasked.min() > 0.0, ( f"Unexpected elevation value ≤ 0 in centre of Wallonia: min={unmasked.min()}" )
@pytest.mark.network
[docs] class TestDtmWallonia1mCompanion: """Tests for the public API of :class:`DtmWallonia1mCompanion`."""
[docs] def test_get_dtm_returns_wolf_array(self): """``get_dtm_1m(vector)`` returns a valid WolfArray without a viewer.""" from unittest.mock import MagicMock from wolfhece._builtin_plugins.dtm_wallonia_1m.companion import ( DtmWallonia1mCompanion, ) from wolfhece.wolf_array import WolfArray viewer = MagicMock() companion = DtmWallonia1mCompanion() companion.proxy.attach(viewer) companion._dtm_service = None bounds = _make_bounds_vec(_XMIN, _YMIN, _XMAX, _YMAX) dtm = companion.get_dtm_1m(bounds) assert isinstance(dtm, WolfArray) assert dtm.array is not None