Skip to content

feat(alluvione): acqua EMS contro il PAI e registro per comune (#713) - #740

Merged
gzileni merged 2 commits into
mainfrom
feat/ems-pai-registro-713
Nov 27, 2026
Merged

gzileni merged 2 commits into
mainfrom
feat/ems-pai-registro-713

Conversation

@gzileni

@gzileni gzileni commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Parte di #713.

Due analisi sull'acqua osservata Copernicus EMS che il curatore di copernicus-ems ha deciso stiano in Limen. Solo misura e lettura: nessun cambio a punteggio, YAML, migrazioni.

A. limen ems-confronto-pai

Per attivazione (--attivazioni o LIMEN_EMS_PAI_ATTIVAZIONI, nessuna ⇒ tutte): acqua = unione dei prodotti non di monitoraggio (DEL0, GRA0, FEP0; mai la somma dei DEL1..n), divisa fra P3/P2/P1 del mosaico idraulico ISPRA in sorgenti (classi esclusive, vince la più alta) e il fuori da ogni zona, grezzo e filtrato (oltre --tolleranza-m 50, macchie da --patch-minima-ha 5). Aree in EPSG:3035. Report in LIMEN_REPORTS_DIR con provenienza.scrivi_report.

Misurato sul DB di produzione di questa macchina (sola lettura, default_transaction_read_only=on), mosaico ISPRA 2020 (mosaicatura_ispra_2020_aree_pericolosita_idraulica_{hph,mph,lph}):

Attivazione Acqua P3 P2 P1 Fuori grezzo Fuori ≥5 ha oltre 50 m Riferimento libreria
EMSR659 49,05 km² 45 % 52 % 0 % 2 % 0,10 km² (2 macchie) 49,1 · 45/52/0/2 · 0,10 — uguale
EMSR664 280,25 km² 36 % 63 % 0 % 1 % 0,73 km² (8 macchie) 280,3 · 36/63/0/1 · 0,73 — uguale
EMSR705 14,14 km² 71 % 25 % 3 % 1 % 0 km² 16,2 · 73/23/3/1 · 0

EMSR705 differisce per un buco dell'ingest, non della misura: flood_events non ha EMSR705-aoi02-DEL0. Le sue 85 feature hanno notation = "Not Applicable", e l'ingest tiene solo FLOOD_NOTATIONS. Va deciso a parte se includerle: cambierebbe la verità del backtest.

Il mosaico ISPRA ha poligoni da 3,5 milioni di vertici: si tagliano al riquadro dell'attivazione e a pezzi da 256 vertici (ST_ClipByBox2D + ST_Subdivide) prima di uscire dal DB, e l'acqua si taglia in tessere da 2 km in shapely. Fatta tutta in SQL, la stessa misura non finiva in dieci minuti.

B. Registro per comune

  • GET /api/ems/attivazioni — attivazioni in flood_events, con l'attribuzione per anno.
  • GET /api/ems/attivazioni/{codice}/comuni (JSON) e ?formato=csv (download) — comuni ISTAT (comuni) toccati dall'acqua, km² per comune (EPSG:3035, unione non somma), inizio evento, osservato dal/al, prodotti EMS, attribuzione.

«Contains modified Copernicus Emergency Management Service information, {anno}» sta nel JSON e su ogni riga del CSV; il JSON porta un'avvertenza: acqua osservata dopo, limite inferiore, non un'allerta né una previsione. Endpoint → core/ems_registro.py → flood_events_repo; niente logica nella route. Calcolo al volo (EMSR664: 35 comuni, 279,96 km², ~50 s su questa macchina carica), in memoria 6 h; i codici inesistenti non si memorizzano. Nessuna migrazione.

Saltato

  • Strumento MCP: limen-ops non espone flood_events oggi, non era banale.
  • SPA: nessun link.
  • Test di integrazione: solo unit. Le query sono state provate sul DB reale (numeri qui sopra).

Gate

  • ruff check, ruff format --check src tests: puliti.
  • mypy --strict src: pulito tranne pyarrow mancante (gruppo ml, non installabile su questo Mac x86).
  • pytest tests/unit: 2166 passati, 3 falliti preesistenti per l'ambiente (test_radice_dati mlflow); test_pluviometri_trentino escluso (pyarrow).

🤖 Generated with Claude Code

https://claude.ai/code/session_01VFEUmbtBQgrNQpKGm9kXCu

@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 27, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-27T15:12:57.629982Z 779bad4 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@chatgpt-codex-connector chatgpt-codex-connector Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

💡 Codex Review

Here are some automated review suggestions for this pull request.

Reviewed commit: 779bad41d2

ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review".

If Codex has suggestions, it will comment; otherwise it will react with 👍.

Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".

Comment on lines +413 to +414
LATERAL ST_Subdivide(ST_ClipByBox2D(z.geom, r.b), 256) s
WHERE z.geom && r.b

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Expand the zone query bounds by the configured tolerance

When a PAI zone lies just outside the activation's water bounding box but within tolleranza_m of the water, this query discards it before ripartisci can apply the distance filter. For example, water beginning 10 m beyond a zone boundary should have its first 40 m excluded with the default 50 m tolerance, but the zone is never loaded and that strip remains in fuori_filtrato_m2, potentially also changing which patches exceed the minimum area. Expand the query and clipping envelope by the configured metric tolerance before fetching the zones.

Useful? React with 👍 / 👎.

Comment on lines +104 to +106
toccati = [acqua[i] for i in idx]
pezzi = shapely.intersection(confine, shapely.union_all(geom_acqua[idx]))
if pezzi.area <= 0:

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

P2 Badge Exclude boundary-only polygons from municipality metadata

When one water polygon overlaps a municipality and another only touches its boundary, predicate="intersects" includes both in toccati. The subsequent area check tests their combined intersection, so the overlapping polygon lets the boundary-only polygon contribute its product and observation timestamp to the municipality's JSON and CSV. This can falsely extend osservato_dal/osservato_al and attribute products that observed no flooded area there. Filter individual polygon intersections to positive area before deriving the metadata.

Useful? React with 👍 / 👎.

gzileni and others added 2 commits November 27, 2026 16:33
`limen ems-confronto-pai`: per attivazione, l'acqua osservata (unione dei
prodotti non di monitoraggio) divisa fra P3/P2/P1 del mosaico idraulico
ISPRA in `sorgenti`, vince la classe più alta, e il fuori da ogni zona
grezzo e filtrato (oltre 50 m, macchie da 5 ha). Aree in EPSG:3035, sola
lettura, report con provenienza.

`GET /api/ems/attivazioni` e `/api/ems/attivazioni/{codice}/comuni`
(JSON o `?formato=csv`): comuni ISTAT toccati, km² per comune, inizio
evento, osservazione, prodotti, attribuzione CEMS su ogni riga. Calcolo al
volo in shapely, in memoria sei ore; nessuna migrazione.

Il mosaico si taglia al riquadro e a pezzi da 256 vertici prima di uscire
dal database: poligoni da 3,5 milioni di vertici non finivano in dieci
minuti.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01VFEUmbtBQgrNQpKGm9kXCu
@gzileni
gzileni force-pushed the feat/ems-pai-registro-713 branch from e7a770b to fef0306 Compare November 27, 2026 15:33
@gzileni
gzileni merged commit 3389eee into main Nov 27, 2026
7 checks passed
@gzileni
gzileni deleted the feat/ems-pai-registro-713 branch November 27, 2026 15:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant