Handle DHMV WCS edge grid rounding
This commit is contained in:
@@ -51,6 +51,8 @@ class DhmvAcquisitionService:
|
||||
WCS_REQUEST_INTERVAL_SECONDS = 2.0
|
||||
WCS_RETRY_DELAY_SECONDS = 4.0
|
||||
WCS_TRANSIENT_STATUS_CODES = frozenset({400, 429, 502, 503, 504})
|
||||
WCS_EDGE_RESOLUTION_REL_TOLERANCE = 0.05
|
||||
WCS_EDGE_RESOLUTION_ABS_TOLERANCE_M = 0.25
|
||||
DTM_CATALOG_URL = (
|
||||
"https://www.vlaanderen.be/datavindplaats/catalogus/"
|
||||
"digitaal-hoogtemodel-vlaanderen-ii-dtm-raster-1-m"
|
||||
@@ -349,11 +351,14 @@ class DhmvAcquisitionService:
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _mosaic_geotiffs(coverages: list[bytes], expected_resolution_m: float | None = None) -> bytes:
|
||||
def _mosaic_geotiffs(
|
||||
coverages: list[bytes],
|
||||
expected_resolution_m: float | None = None,
|
||||
diagnostics: dict[str, Any] | None = None,
|
||||
) -> bytes:
|
||||
if len(coverages) == 1:
|
||||
return coverages[0]
|
||||
try:
|
||||
import rasterio
|
||||
from rasterio.io import MemoryFile
|
||||
from rasterio.merge import merge
|
||||
except ImportError as exc:
|
||||
@@ -370,14 +375,23 @@ class DhmvAcquisitionService:
|
||||
target_resolution = float(expected_resolution_m or abs(float(sources[0].res[0])))
|
||||
invalid_crs = [index for index, source in enumerate(sources) if source.crs is None or source.crs.to_epsg() != 31370]
|
||||
invalid_bands = [index for index, source in enumerate(sources) if source.count != 1]
|
||||
tile_resolutions = [
|
||||
[abs(float(source.res[0])), abs(float(source.res[1]))]
|
||||
for source in sources
|
||||
]
|
||||
invalid_resolution = [
|
||||
{
|
||||
"tile_index": index,
|
||||
"resolution": [abs(float(source.res[0])), abs(float(source.res[1]))],
|
||||
"resolution": tile_resolutions[index],
|
||||
}
|
||||
for index, source in enumerate(sources)
|
||||
if not all(
|
||||
math.isclose(abs(float(value)), target_resolution, rel_tol=0.02, abs_tol=0.05)
|
||||
math.isclose(
|
||||
abs(float(value)),
|
||||
target_resolution,
|
||||
rel_tol=DhmvAcquisitionService.WCS_EDGE_RESOLUTION_REL_TOLERANCE,
|
||||
abs_tol=DhmvAcquisitionService.WCS_EDGE_RESOLUTION_ABS_TOLERANCE_M,
|
||||
)
|
||||
for value in source.res
|
||||
)
|
||||
]
|
||||
@@ -393,6 +407,23 @@ class DhmvAcquisitionService:
|
||||
},
|
||||
status_code=502,
|
||||
)
|
||||
harmonized_tile_indexes = [
|
||||
index
|
||||
for index, resolution in enumerate(tile_resolutions)
|
||||
if not all(
|
||||
math.isclose(value, target_resolution, rel_tol=0.02, abs_tol=0.05)
|
||||
for value in resolution
|
||||
)
|
||||
]
|
||||
if diagnostics is not None:
|
||||
diagnostics.update(
|
||||
{
|
||||
"source_tile_resolutions_m": tile_resolutions,
|
||||
"target_resolution_m": target_resolution,
|
||||
"harmonized_tile_indexes": harmonized_tile_indexes,
|
||||
"harmonization_method": "rasterio_merge_target_resolution" if harmonized_tile_indexes else None,
|
||||
}
|
||||
)
|
||||
mosaic, transform = merge(
|
||||
sources,
|
||||
res=(target_resolution, target_resolution),
|
||||
@@ -471,19 +502,25 @@ class DhmvAcquisitionService:
|
||||
coverage_hash.update(coverage_content)
|
||||
content_types.append(content_type)
|
||||
coverages.append(coverage_content)
|
||||
return DhmvAcquisitionService._mosaic_geotiffs(coverages, prepared["resolution_m"]), {
|
||||
mosaic_diagnostics: dict[str, Any] = {}
|
||||
mosaic = DhmvAcquisitionService._mosaic_geotiffs(
|
||||
coverages,
|
||||
prepared["resolution_m"],
|
||||
diagnostics=mosaic_diagnostics,
|
||||
)
|
||||
return mosaic, {
|
||||
"tile_count": len(request_urls),
|
||||
"request_urls": request_urls,
|
||||
"response_content_types": content_types,
|
||||
"response_sha256": raw_hash.hexdigest(),
|
||||
"coverage_sha256": coverage_hash.hexdigest(),
|
||||
"grid_harmonization": mosaic_diagnostics,
|
||||
}
|
||||
|
||||
@staticmethod
|
||||
def _normalize_raster(content: bytes, scope_geometry_4326, prepared: dict[str, Any]) -> tuple[bytes, dict[str, Any]]:
|
||||
try:
|
||||
import numpy as np
|
||||
import rasterio
|
||||
from rasterio.io import MemoryFile
|
||||
from rasterio.mask import mask
|
||||
except ImportError as exc:
|
||||
@@ -656,6 +693,7 @@ class DhmvAcquisitionService:
|
||||
"response_content_types": transfer["response_content_types"],
|
||||
"response_sha256": transfer["response_sha256"],
|
||||
"coverage_sha256": transfer["coverage_sha256"],
|
||||
"grid_harmonization": transfer["grid_harmonization"],
|
||||
"normalized_sha256": hashlib.sha256(normalized_content).hexdigest(),
|
||||
"bbox_epsg4326": prepared["bbox_epsg4326"],
|
||||
"bbox_epsg31370": prepared["bbox_epsg31370"],
|
||||
|
||||
Reference in New Issue
Block a user