Source code for energy_data_retrieval.pv_catalogue.dera

"""Stage explicit PV installation observations from Andalucía's DERA solar layer."""

from __future__ import annotations

import json
import math
import re
from typing import TypedDict

from .sam import verify_sha256

FIELDS = {
    "id_dera",
    "codigo",
    "nombre",
    "potenc_kW",
    "tecnologia",
    "cod_mun",
    "municipio",
    "provincia",
}


[docs] class DeraSolarObservation(TypedDict): """Native solar-layer attributes, before canonical project identity review.""" id_dera: int codigo: int nombre: str potenc_kW: int | float tecnologia: str cod_mun: str municipio: str provincia: str
[docs] def read_dera_pv_page( content: bytes, *, sha256: str, province: str, min_kw: int, start_index: int, count: int, ) -> tuple[int, list[DeraSolarObservation]]: """Read a projected, sorted WFS 2.0 page with exact PV, province and kW filters. Use g10_03_PlantaSolar with only the attributes in FIELDS and ascending id_dera. The IDs identify GIS features, and kW has no specified AC/DC basis. Neither dates of operation nor whole-project scope follow from this layer. """ if not 1 <= count <= 100 or start_index < 0 or min_kw < 100 or not province: raise ValueError("Invalid DERA page scope") verify_sha256(content, sha256) if len(content) > 1_000_000: raise ValueError("DERA page exceeds reviewed size limit") document = json.loads(content) if not isinstance(document, dict) or document.get("type") != "FeatureCollection": raise ValueError("DERA FeatureCollection required") total = document.get("numberMatched") returned = document.get("numberReturned") features = document.get("features") if ( type(total) is not int or total < 0 or type(returned) is not int or not isinstance(features, list) or document.get("totalFeatures") != total or returned != len(features) or returned != min(count, max(total - start_index, 0)) ): raise ValueError("DERA page counts do not match requested bounds") rows: list[DeraSolarObservation] = [] previous_id = 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("DERA geometry-free feature required") row = feature.get("properties") if not isinstance(row, dict) or row.keys() != FIELDS: raise ValueError("DERA projected fields changed") identifier = row["id_dera"] if ( type(identifier) is not int or identifier <= previous_id or feature.get("id") != f"g10_03_PlantaSolar.{identifier}" or type(row["codigo"]) is not int or row["codigo"] <= 0 ): raise ValueError("Invalid, unsorted or duplicated DERA feature ID") previous_id = identifier kw = row["potenc_kW"] if ( row["tecnologia"] != "Fotovoltaica" or row["provincia"] != province or type(kw) not in (int, float) or not math.isfinite(kw) or kw < min_kw ): raise ValueError("DERA result outside explicit PV, province or power scope") if ( any( not isinstance(row[key], str) or not row[key].strip() for key in ("nombre", "municipio") ) or not isinstance(row["cod_mun"], str) or re.fullmatch(r"[0-9]{5}", row["cod_mun"]) is None ): raise ValueError("DERA public name or municipality missing or invalid") rows.append(row) return total, rows