Complete regional raster exploration
This commit is contained in:
@@ -14,8 +14,15 @@ from shapely.ops import transform as shapely_transform
|
||||
from app.core.config import Settings, get_settings
|
||||
from app.core.errors import AppError
|
||||
from app.models import Area, Dataset
|
||||
from app.schemas.dhmv import TerrainMetric, TerrainSelectionRequest, TerrainSelectionResponse, TerrainSelectionSummary
|
||||
from app.schemas.dhmv import (
|
||||
TerrainMetric,
|
||||
TerrainPartitionSelectionRequest,
|
||||
TerrainSelectionRequest,
|
||||
TerrainSelectionResponse,
|
||||
TerrainSelectionSummary,
|
||||
)
|
||||
from app.services.dhmv_acquisition_service import DhmvAcquisitionService
|
||||
from app.services.raster_partition_analysis_service import RasterPartitionAnalysisService
|
||||
|
||||
|
||||
class TerrainAnalysisService:
|
||||
@@ -177,6 +184,8 @@ class TerrainAnalysisService:
|
||||
selected_cell_count = int(selected_cells.sum())
|
||||
response = TerrainSelectionResponse(
|
||||
dataset_id=dataset.id,
|
||||
dataset_ids=[dataset.id],
|
||||
partition_count=1,
|
||||
product_key=product_key,
|
||||
surface_model=surface_model,
|
||||
selection_bbox=payload.bbox,
|
||||
@@ -200,6 +209,139 @@ class TerrainAnalysisService:
|
||||
)
|
||||
return response.model_dump(mode="json")
|
||||
|
||||
@staticmethod
|
||||
def analyze_partitions(
|
||||
db,
|
||||
project_id: UUID,
|
||||
payload: TerrainPartitionSelectionRequest,
|
||||
*,
|
||||
settings: Settings | None = None,
|
||||
) -> dict:
|
||||
resolved_settings = settings or get_settings()
|
||||
product = DhmvAcquisitionService._products().get(payload.product_key.strip().lower())
|
||||
if product is None:
|
||||
raise AppError(
|
||||
code="DHMV_PRODUCT_NOT_SUPPORTED",
|
||||
message="Select a governed DHMV terrain or surface product",
|
||||
details={"product_key": payload.product_key},
|
||||
status_code=422,
|
||||
)
|
||||
selection_4326 = TerrainAnalysisService._selection_geometry(db, project_id, payload)
|
||||
partition = RasterPartitionAnalysisService.select(
|
||||
db,
|
||||
project_id,
|
||||
source_name=DhmvAcquisitionService.PROVIDER,
|
||||
product_key=product.key,
|
||||
selection_geometry_4326=selection_4326,
|
||||
nodata=DhmvAcquisitionService.NODATA,
|
||||
max_pixels=resolved_settings.dhmv_max_pixels,
|
||||
)
|
||||
surface_models = {
|
||||
str((dataset.source_metadata or {}).get("surface_model") or "")
|
||||
for dataset in partition.datasets
|
||||
}
|
||||
if surface_models != {product.surface_model}:
|
||||
raise AppError(
|
||||
code="INVALID_TERRAIN_METADATA",
|
||||
message="DHMV partition provenance is incomplete",
|
||||
details={"surface_models": sorted(surface_models)},
|
||||
status_code=409,
|
||||
)
|
||||
|
||||
try:
|
||||
import numpy as np
|
||||
except ImportError as exc:
|
||||
raise AppError(
|
||||
code="RASTER_PROCESSING_UNAVAILABLE",
|
||||
message="Numpy is required for partitioned terrain analysis",
|
||||
status_code=503,
|
||||
) from exc
|
||||
|
||||
raw = partition.values
|
||||
invalid = ~np.isfinite(raw) | (raw == DhmvAcquisitionService.NODATA)
|
||||
valid_mask = partition.selected_cells & ~invalid
|
||||
values = raw[valid_mask]
|
||||
if values.size == 0:
|
||||
raise AppError(
|
||||
code="TERRAIN_NO_VALID_DATA",
|
||||
message="No valid DHMV height cells occur in this selection",
|
||||
status_code=422,
|
||||
)
|
||||
slope_values = np.asarray([], dtype="float64")
|
||||
if raw.shape[0] >= 2 and raw.shape[1] >= 2:
|
||||
surface = np.where(valid_mask, raw, np.nan)
|
||||
gradient_y, gradient_x = np.gradient(
|
||||
surface,
|
||||
partition.resolution_y,
|
||||
partition.resolution_x,
|
||||
)
|
||||
slope = np.degrees(np.arctan(np.hypot(gradient_x, gradient_y)))
|
||||
slope_values = slope[np.isfinite(slope) & valid_mask]
|
||||
|
||||
def metric(key: str, label: str, value: float, unit: str, method: str) -> TerrainMetric:
|
||||
return TerrainMetric(
|
||||
metric_key=key,
|
||||
metric_label=label,
|
||||
metric_value=round(float(value), 4),
|
||||
metric_unit=unit,
|
||||
aggregation_method=method,
|
||||
)
|
||||
|
||||
prefix = "terrain" if product.surface_model == "terrain" else "surface"
|
||||
elevation_label = (
|
||||
"Gemiddelde maaiveldhoogte"
|
||||
if product.surface_model == "terrain"
|
||||
else "Gemiddelde oppervlaktehoogte"
|
||||
)
|
||||
metrics = [
|
||||
metric(f"{prefix}_elevation_mean_m", elevation_label, values.mean(), "m TAW", "mean_valid_cells"),
|
||||
metric(f"{prefix}_elevation_min_m", "Laagste hoogte", values.min(), "m TAW", "minimum_valid_cells"),
|
||||
metric(f"{prefix}_elevation_max_m", "Hoogste hoogte", values.max(), "m TAW", "maximum_valid_cells"),
|
||||
metric(f"{prefix}_elevation_p10_m", "10e percentiel hoogte", np.percentile(values, 10), "m TAW", "percentile_10_valid_cells"),
|
||||
metric(f"{prefix}_elevation_p90_m", "90e percentiel hoogte", np.percentile(values, 90), "m TAW", "percentile_90_valid_cells"),
|
||||
metric("relief_m", "Reliëfverschil", values.max() - values.min(), "m", "maximum_minus_minimum"),
|
||||
]
|
||||
if slope_values.size:
|
||||
metrics.extend(
|
||||
[
|
||||
metric("slope_mean_deg", "Gemiddelde helling", slope_values.mean(), "°", "mean_finite_gradient"),
|
||||
metric("slope_p90_deg", "90e percentiel helling", np.percentile(slope_values, 90), "°", "percentile_90_finite_gradient"),
|
||||
metric("slope_max_deg", "Steilste helling", slope_values.max(), "°", "maximum_finite_gradient"),
|
||||
]
|
||||
)
|
||||
primary = metrics[0]
|
||||
selected_cell_count = int(partition.selected_cells.sum())
|
||||
first_dataset = partition.datasets[0]
|
||||
response = TerrainSelectionResponse(
|
||||
dataset_id=first_dataset.id,
|
||||
dataset_ids=[dataset.id for dataset in partition.datasets],
|
||||
partition_count=len(partition.datasets),
|
||||
product_key=product.key,
|
||||
surface_model=product.surface_model,
|
||||
selection_bbox=payload.bbox,
|
||||
selection_area_id=payload.area_id,
|
||||
sample_count=int(values.size),
|
||||
slope_sample_count=int(slope_values.size),
|
||||
coverage_ratio=round(float(values.size / max(1, selected_cell_count)), 6),
|
||||
resolution_m=round(max(partition.resolution_x, partition.resolution_y), 4),
|
||||
vertical_reference=DhmvAcquisitionService.VERTICAL_REFERENCE,
|
||||
summary=TerrainSelectionSummary(
|
||||
metric_label=primary.metric_label,
|
||||
metric_value=primary.metric_value,
|
||||
metric_unit=primary.metric_unit,
|
||||
aggregation_method=primary.aggregation_method,
|
||||
primary_metric_key=primary.metric_key,
|
||||
metrics=metrics,
|
||||
),
|
||||
unsupported_metrics=TerrainAnalysisService.UNSUPPORTED_METRICS,
|
||||
limitation_message=(
|
||||
f"{TerrainAnalysisService.LIMITATION} De selectie werd exact berekend over "
|
||||
f"{len(partition.datasets)} persistente gemeentelijke rasterpartities."
|
||||
),
|
||||
generated_at=datetime.now(UTC).isoformat(),
|
||||
)
|
||||
return response.model_dump(mode="json")
|
||||
|
||||
@staticmethod
|
||||
def render_png(db, project_id: UUID, dataset_id: UUID, *, max_dimension: int = 1800) -> bytes:
|
||||
dataset = TerrainAnalysisService._load_dataset(db, project_id, dataset_id)
|
||||
|
||||
Reference in New Issue
Block a user