Optimize regional BWK municipality selection
This commit is contained in:
@@ -37,6 +37,10 @@ SEMANTIC_METRICS_DISABLED_OPERATOR_TOOLS = {
|
||||
"provision_regional_historical_landuse.py",
|
||||
}
|
||||
|
||||
PRECLIPPED_MUNICIPALITY_PARTITION_OPERATOR_TOOLS = {
|
||||
"provision_regional_bwk_natura2000.py",
|
||||
}
|
||||
|
||||
|
||||
SEMANTIC_SELECTION_METRICS: dict[str, tuple[dict[str, Any], ...]] = {
|
||||
"buildings": (
|
||||
@@ -181,6 +185,30 @@ class VectorFeatureService:
|
||||
provenance = dataset.provenance_metadata if isinstance(dataset.provenance_metadata, dict) else {}
|
||||
return provenance.get("operator_tool") in FULL_AREA_CLIPPED_OPERATOR_TOOLS
|
||||
|
||||
@staticmethod
|
||||
def preclipped_partition_filter(dataset: Dataset, selection_area_name: str | None) -> tuple[str, str] | None:
|
||||
provenance = dataset.provenance_metadata if isinstance(dataset.provenance_metadata, dict) else {}
|
||||
if provenance.get("operator_tool") not in PRECLIPPED_MUNICIPALITY_PARTITION_OPERATOR_TOOLS:
|
||||
return None
|
||||
source_metadata = dataset.source_metadata if isinstance(dataset.source_metadata, dict) else {}
|
||||
if (
|
||||
source_metadata.get("partitioned_source_audit") is not True
|
||||
or source_metadata.get("geometry_clipped_to_area") is not True
|
||||
):
|
||||
return None
|
||||
normalized_name = str(selection_area_name or "").strip()
|
||||
prefix = "Gemeente "
|
||||
suffixes = (" - officiële grens", " - officiele grens")
|
||||
if not normalized_name.startswith(prefix):
|
||||
return None
|
||||
municipality = normalized_name[len(prefix):]
|
||||
for suffix in suffixes:
|
||||
if municipality.endswith(suffix):
|
||||
municipality = municipality[: -len(suffix)]
|
||||
break
|
||||
municipality = municipality.strip()
|
||||
return ("municipality", municipality) if municipality else None
|
||||
|
||||
@staticmethod
|
||||
def _feature_row(dataset_id: UUID, feature: dict[str, Any], index: int, feature_class: str | None) -> VectorFeature | None:
|
||||
geometry_payload = feature.get("geometry")
|
||||
@@ -289,6 +317,7 @@ class VectorFeatureService:
|
||||
selection_geometry: Any | None = None,
|
||||
selection_area_id: UUID | None = None,
|
||||
full_dataset_area: bool = False,
|
||||
preclipped_partition_filter: tuple[str, str] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
normalized_bbox = VectorFeatureService._normalize_selection_bbox(bbox)
|
||||
safe_limit = max(1, min(int(limit), 1000))
|
||||
@@ -303,7 +332,10 @@ class VectorFeatureService:
|
||||
)
|
||||
|
||||
query = db.query(VectorFeature).filter(VectorFeature.dataset_id == dataset_id)
|
||||
if not full_dataset_area:
|
||||
if preclipped_partition_filter is not None:
|
||||
partition_property, partition_value = preclipped_partition_filter
|
||||
query = query.filter(VectorFeature.properties_json.op("->>")(partition_property) == partition_value)
|
||||
elif not full_dataset_area:
|
||||
query = query.filter(ST_Intersects(VectorFeature.geometry, selection_shape))
|
||||
if hasattr(query, "count"):
|
||||
total_feature_count = int(query.count())
|
||||
@@ -327,6 +359,7 @@ class VectorFeatureService:
|
||||
total_feature_count=total_feature_count,
|
||||
selection_geometry=selection_geometry,
|
||||
full_dataset_area=full_dataset_area,
|
||||
preclipped_partition_filter=preclipped_partition_filter,
|
||||
)
|
||||
|
||||
result = {
|
||||
@@ -354,6 +387,7 @@ class VectorFeatureService:
|
||||
total_feature_count: int | None = None,
|
||||
selection_geometry: Any | None = None,
|
||||
full_dataset_area: bool = False,
|
||||
preclipped_partition_filter: tuple[str, str] | None = None,
|
||||
) -> dict[str, Any]:
|
||||
normalized_bbox = VectorFeatureService._normalize_selection_bbox(bbox)
|
||||
selection_shape = selection_geometry
|
||||
@@ -366,8 +400,14 @@ class VectorFeatureService:
|
||||
4326,
|
||||
)
|
||||
selection_filter = (VectorFeature.dataset_id == dataset.id,)
|
||||
if not full_dataset_area:
|
||||
if preclipped_partition_filter is not None:
|
||||
partition_property, partition_value = preclipped_partition_filter
|
||||
selection_filter += (
|
||||
VectorFeature.properties_json.op("->>")(partition_property) == partition_value,
|
||||
)
|
||||
elif not full_dataset_area:
|
||||
selection_filter += (ST_Intersects(VectorFeature.geometry, selection_shape),)
|
||||
selection_is_preclipped = full_dataset_area or preclipped_partition_filter is not None
|
||||
feature_count = total_feature_count
|
||||
if feature_count is None:
|
||||
feature_count = int(db.query(func.count(VectorFeature.id)).filter(*selection_filter).scalar() or 0)
|
||||
@@ -438,7 +478,7 @@ class VectorFeatureService:
|
||||
selection_filter=selection_filter,
|
||||
selection_shape=selection_shape,
|
||||
feature_count=feature_count,
|
||||
full_dataset_area=full_dataset_area,
|
||||
full_dataset_area=selection_is_preclipped,
|
||||
)
|
||||
for metric_config in metric_configs
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user