Handle VMM WCS edge grid rounding
This commit is contained in:
@@ -57,6 +57,10 @@ class FloodHazardAcquisitionService:
|
||||
WCS_REQUEST_INTERVAL_SECONDS = 1.0
|
||||
WCS_RETRY_DELAY_SECONDS = 3.0
|
||||
WCS_TRANSIENT_STATUS_CODES = frozenset({400, 429, 502, 503, 504})
|
||||
# The VMM WCS rounds the grid size of partial edge tiles to an integer
|
||||
# number of cells. Keep that provider artefact bounded and auditable.
|
||||
WCS_EDGE_RESOLUTION_REL_TOLERANCE = 0.05
|
||||
WCS_EDGE_RESOLUTION_ABS_TOLERANCE_M = 0.25
|
||||
SERVICE_CATALOG_URL = "https://www.vlaanderen.be/datavindplaats/catalogus/publieke-inspire-coverage-service-van-ogrk"
|
||||
LIMITATION = (
|
||||
"Gemodelleerde maximale overstromingsdiepte voor een vast kans- en klimaatscenario. "
|
||||
@@ -319,7 +323,11 @@ class FloodHazardAcquisitionService:
|
||||
)
|
||||
|
||||
@staticmethod
|
||||
def _mosaic_geotiffs(coverages: list[bytes], expected_resolution_m: float) -> bytes:
|
||||
def _mosaic_geotiffs(
|
||||
coverages: list[bytes],
|
||||
expected_resolution_m: float,
|
||||
diagnostics: dict[str, Any] | None = None,
|
||||
) -> bytes:
|
||||
if len(coverages) == 1:
|
||||
return coverages[0]
|
||||
try:
|
||||
@@ -331,11 +339,63 @@ class FloodHazardAcquisitionService:
|
||||
sources = []
|
||||
try:
|
||||
sources = [memory.open() for memory in memories]
|
||||
for source in sources:
|
||||
if source.crs is None or source.crs.to_epsg() != 31370 or source.count != 1:
|
||||
raise AppError(code="FLOOD_HAZARD_TILE_MISMATCH", message="VMM coverage tiles do not share the governed CRS and band layout", status_code=502)
|
||||
if not all(math.isclose(abs(float(value)), expected_resolution_m, rel_tol=0.02, abs_tol=0.05) for value in source.res):
|
||||
raise AppError(code="FLOOD_HAZARD_TILE_MISMATCH", message="VMM coverage tile resolution differs from the governed request", status_code=502)
|
||||
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": tile_resolutions[index],
|
||||
}
|
||||
for index, source in enumerate(sources)
|
||||
if not all(
|
||||
math.isclose(
|
||||
abs(float(value)),
|
||||
expected_resolution_m,
|
||||
rel_tol=FloodHazardAcquisitionService.WCS_EDGE_RESOLUTION_REL_TOLERANCE,
|
||||
abs_tol=FloodHazardAcquisitionService.WCS_EDGE_RESOLUTION_ABS_TOLERANCE_M,
|
||||
)
|
||||
for value in source.res
|
||||
)
|
||||
]
|
||||
if invalid_crs or invalid_bands or invalid_resolution:
|
||||
raise AppError(
|
||||
code="FLOOD_HAZARD_TILE_MISMATCH",
|
||||
message="VMM coverage tiles do not match the governed CRS, band layout and resolution",
|
||||
details={
|
||||
"invalid_crs_tile_indexes": invalid_crs,
|
||||
"invalid_band_tile_indexes": invalid_bands,
|
||||
"invalid_resolution_tiles": invalid_resolution,
|
||||
"expected_resolution_m": expected_resolution_m,
|
||||
},
|
||||
status_code=502,
|
||||
)
|
||||
harmonized_tile_indexes = [
|
||||
index
|
||||
for index, resolution in enumerate(tile_resolutions)
|
||||
if not all(
|
||||
math.isclose(value, expected_resolution_m, 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": expected_resolution_m,
|
||||
"harmonized_tile_indexes": harmonized_tile_indexes,
|
||||
"harmonization_method": (
|
||||
"rasterio_merge_target_resolution" if harmonized_tile_indexes else None
|
||||
),
|
||||
}
|
||||
)
|
||||
mosaic, transform = merge(sources, res=(expected_resolution_m, expected_resolution_m), nodata=0.0, dtype="float32")
|
||||
profile = sources[0].profile.copy()
|
||||
profile.pop("blockxsize", None)
|
||||
@@ -378,16 +438,25 @@ class FloodHazardAcquisitionService:
|
||||
time.sleep(FloodHazardAcquisitionService.WCS_RETRY_DELAY_SECONDS)
|
||||
raw_content, content_type = FloodHazardAcquisitionService._fetch(request_url, settings, opener)
|
||||
coverage = FloodHazardAcquisitionService._extract_geotiff(raw_content, content_type)
|
||||
raw_hash.update(len(raw_content).to_bytes(8, "big")); raw_hash.update(raw_content)
|
||||
coverage_hash.update(len(coverage).to_bytes(8, "big")); coverage_hash.update(coverage)
|
||||
raw_hash.update(len(raw_content).to_bytes(8, "big"))
|
||||
raw_hash.update(raw_content)
|
||||
coverage_hash.update(len(coverage).to_bytes(8, "big"))
|
||||
coverage_hash.update(coverage)
|
||||
content_types.append(content_type)
|
||||
coverages.append(coverage)
|
||||
return FloodHazardAcquisitionService._mosaic_geotiffs(coverages, prepared["resolution_m"]), {
|
||||
mosaic_diagnostics: dict[str, Any] = {}
|
||||
mosaic = FloodHazardAcquisitionService._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
|
||||
@@ -417,7 +486,8 @@ class FloodHazardAcquisitionService:
|
||||
normalized_m[positive] = raw_cm[positive] / 100.0
|
||||
valid_values = normalized_m[positive].astype("float64")
|
||||
profile = source.profile.copy()
|
||||
profile.pop("blockxsize", None); profile.pop("blockysize", None)
|
||||
profile.pop("blockxsize", None)
|
||||
profile.pop("blockysize", None)
|
||||
profile.update(driver="GTiff", width=normalized_m.shape[1], height=normalized_m.shape[0], count=1, dtype="float32", crs=FloodHazardAcquisitionService.SOURCE_CRS, transform=transform, nodata=FloodHazardAcquisitionService.NODATA, compress="deflate", predictor=3)
|
||||
with MemoryFile() as output_memory:
|
||||
with output_memory.open(**profile) as output:
|
||||
@@ -536,6 +606,7 @@ class FloodHazardAcquisitionService:
|
||||
"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).hexdigest(),
|
||||
"bbox_epsg4326": prepared["bbox_epsg4326"],
|
||||
"bbox_epsg31370": prepared["bbox_epsg31370"],
|
||||
|
||||
Reference in New Issue
Block a user