54 lines
1.4 KiB
Python
54 lines
1.4 KiB
Python
from pathlib import Path
|
|
|
|
import numpy as np
|
|
import pytest
|
|
import rasterio
|
|
from rasterio.transform import from_origin
|
|
|
|
from app.core.dem import DemNotConfiguredError, elevation_at, elevations_along
|
|
from app.core.geo import PathPoint
|
|
|
|
|
|
def write_test_dem(path: Path) -> None:
|
|
data = np.array([[10.0, 11.0], [20.0, 21.0]], dtype="float32")
|
|
transform = from_origin(24.0, 61.0, 1.0, 1.0)
|
|
with rasterio.open(
|
|
path,
|
|
"w",
|
|
driver="GTiff",
|
|
height=2,
|
|
width=2,
|
|
count=1,
|
|
dtype=data.dtype,
|
|
crs="EPSG:4326",
|
|
transform=transform,
|
|
nodata=-9999.0,
|
|
) as dataset:
|
|
dataset.write(data, 1)
|
|
|
|
|
|
def test_elevation_at_samples_geotiff(tmp_path: Path) -> None:
|
|
write_test_dem(tmp_path / "test_dem.tif")
|
|
|
|
assert elevation_at(60.5, 24.5, dem_path=tmp_path) == 10.0
|
|
assert elevation_at(59.5, 25.5, dem_path=tmp_path) == 21.0
|
|
|
|
|
|
def test_elevations_along_samples_all_points(tmp_path: Path) -> None:
|
|
write_test_dem(tmp_path / "test_dem.tif")
|
|
points = [
|
|
PathPoint(lat=60.5, lon=24.5, distance_m=0),
|
|
PathPoint(lat=59.5, lon=25.5, distance_m=1),
|
|
]
|
|
|
|
result = elevations_along(points, dem_path=tmp_path)
|
|
|
|
assert result.tolist() == [10.0, 21.0]
|
|
|
|
|
|
def test_elevation_at_errors_without_covering_tile(tmp_path: Path) -> None:
|
|
write_test_dem(tmp_path / "test_dem.tif")
|
|
|
|
with pytest.raises(DemNotConfiguredError):
|
|
elevation_at(10.0, 10.0, dem_path=tmp_path)
|