Source code for energy_data_retrieval.pv_catalogue.cesena

"""Stage geometry-free Valle del Savio ground-PV inventory features for review."""

from __future__ import annotations

import json
import math
import re
from typing import TypedDict

from .sam import verify_sha256

LAYER = "Cesena:OD_VG_FOTOVOLTAICO_SDO_preview"
FIELDS = {"OBJECTID", "TIPO_AUTO", "POSIZ", "ANNO", "POTENZA", "U_M_POT", "COMUNE"}


[docs] class CesenaGroundPvObservation(TypedDict): """Source feature fields with the reported kW basis left unspecified.""" object_id: int source_type: str municipality_code: str reported_year: int | None power_native: str | None reported_kw_ac_dc_unspecified: float | None
[docs] def read_cesena_ground_pv_page( content: bytes, *, sha256: str, offset: int, limit: int, ) -> tuple[int, list[CesenaGroundPvObservation]]: """Read a bounded WFS 2.0 page of the published ground-PV layer projection. Project FIELDS, sort by OBJECTID ascending and filter POSIZ to A TERRA. Source feature IDs and years do not establish plant identity or operation. The service includes municipality codes outside Cesena. Keep them literal. """ if len(content) > 1_000_000 or offset < 0 or not 1 <= limit <= 100: raise ValueError("Outside bounded Cesena page scope") verify_sha256(content, sha256) page = json.loads(content) if not isinstance(page, dict) or page.get("type") != "FeatureCollection": raise ValueError("Invalid Cesena feature collection") total = page.get("numberMatched") returned = page.get("numberReturned") features = page.get("features") if ( type(total) is not int or total < 0 or page.get("totalFeatures") != total or type(returned) is not int or not isinstance(features, list) or returned != len(features) or returned != min(limit, max(0, total - offset)) ): raise ValueError("Cesena page counts do not match scope") rows: list[CesenaGroundPvObservation] = [] previous = 0 for feature in features: if ( not isinstance(feature, dict) or feature.get("type") != "Feature" or "geometry" not in feature or feature["geometry"] is not None ): raise ValueError("Geometry-free Cesena feature required") row = feature.get("properties") if not isinstance(row, dict) or row.keys() != FIELDS: raise ValueError("Cesena projected fields changed") identifier = row["OBJECTID"] if ( type(identifier) is not int or identifier <= previous or feature.get("id") != f"{LAYER.split(':')[1]}.{identifier}" ): raise ValueError("Invalid, unsorted or duplicate Cesena feature ID") previous = identifier source_type = row["TIPO_AUTO"] code = row["COMUNE"] year = row["ANNO"] if ( row["POSIZ"] != "A TERRA" or source_type not in ("UFFICIO", "SITONLINE", "CATASTO") or not isinstance(code, str) or re.fullmatch(r"[A-Z][0-9]{3}", code) is None or (year is not None and (type(year) is not int or not 1900 <= year <= 2100)) or row["U_M_POT"] != "KW" ): raise ValueError("Cesena technology-layer attributes or units changed") native = row["POTENZA"] power = None if native is not None: if ( not isinstance(native, str) or re.fullmatch(r"[0-9]+(?:,[0-9]+)?", native.strip()) is None ): raise ValueError("Ambiguous Cesena power literal") power = float(native.strip().replace(",", ".")) if not math.isfinite(power): raise ValueError("Nonfinite Cesena power") rows.append( { "object_id": identifier, "source_type": source_type, "municipality_code": code, "reported_year": year, "power_native": native, "reported_kw_ac_dc_unspecified": power, } ) return total, rows