Files
RadioPropagationApi/api/tests/test_viewshed_and_canopy.py
T
2026-06-24 09:37:09 +03:00

93 lines
2.5 KiB
Python

from __future__ import annotations
import sys
from pathlib import Path
import numpy as np
import rasterio
from rasterio.transform import from_origin
from app.core.geo import GeoPoint
from app.core.viewshed import _find_dem_tiles, _radius_bbox
sys.path.append(str(Path(__file__).resolve().parents[2] / "scripts"))
import bootstrap_canopy # noqa: E402
def _write_dem(path: Path, west: float) -> None:
data = np.ones((10, 10), dtype="float32")
transform = from_origin(west, 60.0, 0.01, 0.01)
with rasterio.open(
path,
"w",
driver="GTiff",
height=data.shape[0],
width=data.shape[1],
count=1,
dtype=data.dtype,
crs="EPSG:4326",
transform=transform,
nodata=-9999.0,
) as dataset:
dataset.write(data, 1)
def test_viewshed_dem_search_uses_radius_bbox(tmp_path: Path) -> None:
_write_dem(tmp_path / "west.tif", 30.0)
_write_dem(tmp_path / "east.tif", 30.1)
bbox = _radius_bbox(GeoPoint(lat=59.95, lon=30.095), 2_000)
tiles = _find_dem_tiles(tmp_path, bbox)
assert {tile.name for tile in tiles} == {"west.tif", "east.tif"}
def test_canopy_selects_intersecting_tiles_from_geojson_index() -> None:
index = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"bbox": [30.0, 59.0, 31.0, 60.0],
"properties": {"filename": "tile_a.tif"},
},
{
"type": "Feature",
"bbox": [40.0, 59.0, 41.0, 60.0],
"properties": {"filename": "tile_b.tif"},
},
],
}
urls = bootstrap_canopy.select_tile_urls(index, (30.2, 59.2, 30.4, 59.4))
assert len(urls) == 1
assert urls[0].endswith("/tile_a.tif")
def test_canopy_parse_socks5_proxy_url() -> None:
proxy = bootstrap_canopy.parse_socks5_proxy(
"socks5://proxy_user:secret@194.33.35.46:38599"
)
assert proxy.host == "194.33.35.46"
assert proxy.port == 38599
assert proxy.username == "proxy_user"
assert proxy.password == "secret"
assert proxy.endpoint == "socks5://194.33.35.46:38599"
def test_canopy_urls_file_ignores_comments(tmp_path: Path) -> None:
urls_file = tmp_path / "urls.txt"
urls_file.write_text(
"# comment\n"
"https://example.com/a.tif\n"
"\n"
"https://example.com/b.tif\n",
encoding="utf-8",
)
assert bootstrap_canopy.urls_from_file(urls_file) == [
"https://example.com/a.tif",
"https://example.com/b.tif",
]