Source code: Rapid situational mapping from open data in Python: the Valencia floods of 29 October 2024

Runnable Python scripts, requirements and console output for the tutorial “Rapid situational mapping from open data in Python: the Valencia floods of 29…

Last updated

These are the exact scripts used to produce every figure and number in Rapid situational mapping from open data in Python: the Valencia floods of 29 October 2024. Download them, install the requirements into a fresh virtual environment and run them from this folder.

Run it
python -m venv .venv
.venv\Scripts\activate # macOS/Linux: source .venv/bin/activate
pip install -r requirements.txt
python situation_map.py

situation_map.py

situation_map.py
"""Rapid situational mapping from open data in Python: the Valencia DANA floods of 29 October 2024.
Part 1 of "GIS for disaster emergency response". Builds a first situation map for L'Horta Sud from
open data only: the Copernicus EMS Rapid Mapping activation EMSR773 (timeline and first flood
delineation), municipalities from geoBoundaries (IGN), rainfall on 29 October from the ECMWF IFS
9 km archive via Open-Meteo, and critical facilities and bridges from OpenStreetMap as they were
on the morning of the flood (an Overpass "attic" query). Writes a one-page situation map PNG,
an interactive Folium map, a GeoPackage and a "what we know" table.
Run with: python situation_map.py
"""
import json
import platform
import time
import zipfile
from importlib.metadata import version
from pathlib import Path
import folium
import geopandas as gpd
import matplotlib
matplotlib.use("Agg")
import matplotlib.pyplot as plt
import numpy as np
import osmnx as ox
import pandas as pd
import requests
from matplotlib.lines import Line2D
from matplotlib.patches import Patch, Rectangle
from shapely import wkt
from shapely.geometry import LineString, box
HERE = Path(__file__).parent
DATA = HERE / "data"
OUT = HERE / "output"
DATA.mkdir(exist_ok=True)
OUT.mkdir(exist_ok=True)
plt.rcParams.update({"figure.dpi": 150, "savefig.bbox": "tight", "font.size": 9})
pd.set_option("display.width", 140)
pd.set_option("display.max_columns", 20)
UA = {"User-Agent": "TechnicalGIS tutorial (https://technicalgis.com)"}
print("=== versions ===")
print(f"Python {platform.python_version()} | geopandas {version('geopandas')} | osmnx {version('osmnx')} | "
f"folium {version('folium')} | matplotlib {version('matplotlib')}")
# --- Settings -------------------------------------------------------------------------------
BBOX = (-0.60, 39.33, -0.30, 39.50) # lon/lat: L'Horta Sud, from Torrent and Aldaia to the Albufera
CRS = "EPSG:25830" # ETRS89 / UTM zone 30N, metres
ACTIVATION = "EMSR773"
EVENT_UTC = pd.Timestamp("2024-10-29 14:30", tz="UTC")
OSM_DATE = "2024-10-29T00:00:00Z" # OpenStreetMap as it was on the morning of the flood
RAIN_BOX = (-1.0, 39.1, -0.2, 39.7) # wider window: the rain fell upstream, in the Poyo catchment
PLACES = {"Chiva": (-0.718, 39.469), "Turís": (-0.711, 39.389), "Paiporta": (-0.418, 39.428)}
study = gpd.GeoDataFrame(geometry=[box(*BBOX)], crs=4326).to_crs(CRS)
def get(url: str, dest: Path, **params) -> Path:
"""Download once and cache in data/."""
if not dest.exists():
r = requests.get(url, params=params or None, headers=UA, timeout=300)
r.raise_for_status()
dest.write_bytes(r.content)
return dest
# --- Step 1: the Copernicus EMS activation and its timeline ----------------------------------
print("\n=== Step 1: Copernicus EMS activation ===")
api = "https://rapidmapping.emergency.copernicus.eu/backend/dashboard-api/public-activations/"
act = requests.get(api, params={"code": ACTIVATION}, headers=UA, timeout=60).json()["results"][0]
print(f"{act['code']}: {act['name']} | {act['category']}, {len(act['aois'])} areas of interest")
print(f"event {act['eventTime']} UTC, activated {act['activationTime']} UTC by {act['activator'].split('|')[0]}")
rows = []
for aoi in act["aois"]:
footprint = wkt.loads(aoi["extent"])
for p in aoi["products"]:
img = p["images"][0] if p["images"] else {}
rows.append({"aoi": f"AOI{aoi['number']:02d}", "name": aoi["name"], "type": p["type"],
"monit": p.get("monitoringNumber") or 0, "sensor": img.get("sensorName"),
"image_utc": pd.Timestamp(img.get("acquisitionTime"), tz="UTC"),
"delivered_utc": pd.Timestamp(p["version"]["deliveryTime"], tz="UTC").floor("min"),
"zip": p.get("downloadPath"), "in_study_area": footprint.intersects(box(*BBOX))})
products = pd.DataFrame(rows)
products["hours_after_event"] = ((products.delivered_utc - EVENT_UTC).dt.total_seconds() / 3600).round(1)
local = products[products.in_study_area].sort_values("delivered_utc")
print(f"{len(products)} products in total; {len(local)} cover the study area:")
print(local[["aoi", "name", "type", "monit", "sensor", "image_utc", "delivered_utc", "hours_after_event"]]
.to_string(index=False))
# The first delineation for L'Horta Sud (AOI03), and the Sentinel-2 one for the whole province (AOI01)
first = local[(local.type == "DEL") & (local.monit == 0)].set_index("aoi")
def read_ems(aoi: str, layer: str) -> gpd.GeoDataFrame:
url = first.loc[aoi, "zip"]
path = get(url, DATA / Path(url).name)
name = next(n for n in zipfile.ZipFile(path).namelist() if n.endswith(f"_{layer}_v1.shp"))
return gpd.read_file(f"zip://{path.as_posix()}!{name}").to_crs(CRS)
# --- Step 2: read the first flood delineation --------------------------------------------------
print("\n=== Step 2: first flood delineation ===")
flood = read_ems("AOI03", "observedEventA")
aoi03 = read_ems("AOI03", "areaOfInterestA")
s2 = gpd.clip(read_ems("AOI01", "observedEventA"), aoi03)
flood["km2"] = flood.area / 1e6
print(f"AOI03 {first.loc['AOI03', 'name']}: {aoi03.area.sum() / 1e6:.0f} km² mapped from "
f"{first.loc['AOI03', 'sensor']} imagery of {first.loc['AOI03', 'image_utc']:%d %b %Y %H:%M} UTC")
print(flood.groupby(["notation", "det_method"])["km2"].agg(["count", "sum"]).round(2).to_string())
print(f"observed flood extent (flooded area + flood trace): {flood.km2.sum():.1f} km²")
print(f"same area in the AOI01 Sentinel-2 delineation: {s2.area.sum() / 1e6:.1f} km²")
# --- Step 3: municipalities -------------------------------------------------------------------
print("\n=== Step 3: municipalities ===")
gb = get("https://github.com/wmgeolab/geoBoundaries/raw/9469f09/releaseData/gbOpen/ESP/ADM3/"
"geoBoundaries-ESP-ADM3_simplified.geojson", DATA / "geoBoundaries-ESP-ADM3_simplified.geojson")
muni_all = gpd.read_file(gb, bbox=RAIN_BOX).to_crs(CRS)[["shapeName", "geometry"]]
muni_all = muni_all.rename(columns={"shapeName": "municipality"})
muni = muni_all[muni_all.intersects(study.geometry[0])].copy()
muni["mapped_km2"] = muni.intersection(aoi03.union_all()).area / 1e6 # area the EMS mapped
print(f"{len(muni_all)} municipalities in the rainfall window, {len(muni)} touch the study area")
# --- Step 4: rainfall on 29 October from the ECMWF IFS 9 km archive ----------------------------
print("\n=== Step 4: rainfall, 29 October 2024 ===")
ARCHIVE = "https://archive-api.open-meteo.com/v1/archive"
lons = np.round(np.arange(RAIN_BOX[0], RAIN_BOX[2] + 0.01, 0.1), 2)
lats = np.round(np.arange(RAIN_BOX[1], RAIN_BOX[3] + 0.01, 0.1), 2)
LON, LAT = np.meshgrid(lons, lats)
grid = requests.get(ARCHIVE, headers=UA, timeout=120, params={
"latitude": ",".join(map(str, LAT.ravel())), "longitude": ",".join(map(str, LON.ravel())),
"start_date": "2024-10-29", "end_date": "2024-10-29", "daily": "precipitation_sum",
"models": "ecmwf_ifs", "timezone": "Europe/Madrid"}).json()
rain = np.array([g["daily"]["precipitation_sum"][0] for g in grid]).reshape(LAT.shape)
iy, ix = np.unravel_index(np.argmax(rain), rain.shape)
print(f"{rain.size} grid points, 0.1° spacing; 29 Oct total {rain.min():.0f}-{rain.max():.0f} mm, "
f"maximum at {LON[iy, ix]:.1f}°, {LAT[iy, ix]:.1f}°")
pts = requests.get(ARCHIVE, headers=UA, timeout=120, params={
"latitude": ",".join(str(v[1]) for v in PLACES.values()),
"longitude": ",".join(str(v[0]) for v in PLACES.values()),
"start_date": "2024-10-29", "end_date": "2024-10-30", "hourly": "precipitation",
"daily": "precipitation_sum", "models": "ecmwf_ifs,era5", "timezone": "Europe/Madrid"}).json()
hourly = pd.DataFrame({name: p["hourly"]["precipitation_ecmwf_ifs"] for name, p in zip(PLACES, pts)},
index=pd.to_datetime(pts[0]["hourly"]["time"]))
for name, p in zip(PLACES, pts):
peak = hourly[name].idxmax()
print(f"{name:9s} IFS {p['daily']['precipitation_sum_ecmwf_ifs'][0]:5.1f} mm | "
f"ERA5 {p['daily']['precipitation_sum_era5'][0]:5.1f} mm | "
f"wettest hour {peak:%H:%M} local, {hourly[name].max():.1f} mm")
# --- Step 5: critical facilities and bridges from OpenStreetMap, as on 29 October 2024 --------
print("\n=== Step 5: OpenStreetMap features (as of 29 Oct 2024 00:00 UTC) ===")
ox.settings.use_cache = True
ox.settings.cache_folder = str(DATA / "osm_cache")
ox.settings.requests_timeout = 300
ox.settings.overpass_settings = f'[out:json][timeout:{{timeout}}][date:"{OSM_DATE}"]{{maxsize}}'
def osm(tags: dict) -> gpd.GeoDataFrame:
"""features_from_bbox with a polite retry: the public Overpass server is often busy."""
for wait in (30, 60, 120, 240, 0):
try:
return ox.features.features_from_bbox(BBOX, tags).to_crs(CRS)
except Exception as e: # noqa: BLE001 - Overpass returns 429/504 when busy
if not wait:
raise
print(f"Overpass busy ({type(e).__name__}), retrying in {wait} s")
time.sleep(wait)
fac = osm({"amenity": ["hospital", "clinic", "school", "fire_station", "police"],
"healthcare": ["hospital", "centre"], "emergency": ["ambulance_station"]}).reset_index()
def facility_type(r) -> str:
if r.get("amenity") == "hospital" or r.get("healthcare") == "hospital":
return "Hospital"
if r.get("amenity") == "clinic" or r.get("healthcare") == "centre":
return "Health centre"
if r.get("amenity") == "fire_station":
return "Fire station"
if r.get("amenity") == "police":
return "Police"
if r.get("emergency") == "ambulance_station":
return "Ambulance station"
return "School" if r.get("amenity") == "school" else None
# "facility", not "type": GeoDataFrame.type is a built-in property that returns geometry types
fac["facility"] = fac.apply(facility_type, axis=1)
fac = fac.dropna(subset=["facility"])
fac = fac[fac.intersects(study.geometry[0])]
fac = fac[~fac.geometry.geom_type.isin(["LineString", "MultiLineString"])]
fac["name"] = fac["name"].fillna("(no name in OSM)")
print(f"{len(fac)} facilities in the study area: " + ", ".join(f"{k} {v}" for k, v in fac.facility.value_counts().items()))
# Roads, bridges, railways and waterways: one raw Overpass query that returns way geometries directly.
# It is lighter on the server than asking OSMnx for every feature carrying any of these tags.
VEHICLE = "motorway|trunk|primary|secondary|tertiary|unclassified|residential|living_street|service"
s, w, n, e = BBOX[1], BBOX[0], BBOX[3], BBOX[2]
query = f"""[out:json][timeout:600][date:"{OSM_DATE}"];
(way["highway"~"^(motorway|trunk|primary|secondary)(_link)?$"]({s},{w},{n},{e});
way["bridge"~"^(yes|viaduct)$"]["highway"~"^({VEHICLE})(_link)?$"]({s},{w},{n},{e});
way["railway"="rail"]({s},{w},{n},{e});
way["waterway"~"^(river|canal|stream)$"]({s},{w},{n},{e}););
out tags geom;"""
ctx_file = DATA / "osm_context_20241029.json"
for wait in (60, 120, 240, 0):
if ctx_file.exists():
break
r = requests.post("https://overpass-api.de/api/interpreter", data={"data": query}, headers=UA, timeout=700)
if r.ok:
ctx_file.write_bytes(r.content)
elif not wait:
r.raise_for_status()
else:
print(f"Overpass busy (HTTP {r.status_code}), retrying in {wait} s")
time.sleep(wait)
ways = [el for el in json.loads(ctx_file.read_text(encoding="utf-8"))["elements"] if len(el.get("geometry", [])) > 1]
lines = gpd.GeoDataFrame([{"id": el["id"], **el["tags"]} for el in ways], crs=4326,
geometry=[LineString([(p["lon"], p["lat"]) for p in el["geometry"]]) for el in ways])
lines = lines.reindex(columns=["id", "name", "highway", "bridge", "railway", "waterway", "geometry"]).to_crs(CRS)
is_vehicle = lines.highway.fillna("").str.fullmatch(f"({VEHICLE})(_link)?")
bridges = lines[lines.bridge.isin(["yes", "viaduct"]) & is_vehicle].copy()
bridges["name"] = bridges["name"].fillna("(no name in OSM)")
roads = lines[lines.highway.fillna("").str.fullmatch("(motorway|trunk|primary|secondary)(_link)?")]
rail = lines[lines.railway.eq("rail")]
rivers = lines[lines.waterway.isin(["river", "canal", "stream"])]
poyo = rivers[rivers["name"].fillna("").str.contains("Poyo|Poio", case=False)]
print(f"{len(roads)} major road ways, {len(rail)} rail ways, {len(rivers)} waterways, {len(bridges)} road bridges")
print("Poyo ravine in OSM: " + ", ".join(sorted(poyo["name"].unique())) + f", {len(poyo)} way, "
f"{poyo.length.sum() / 1000:.1f} km in the study area")
# --- Step 6: what is in the flood? ------------------------------------------------------------
print("\n=== Step 6: exposure to the first delineation ===")
# Only the area inside AOI03 was mapped. Outside it, "no water" means "not mapped", not "dry".
mapped = aoi03.union_all()
fac = fac[fac.intersects(mapped)].copy()
bridges = bridges[bridges.intersects(mapped)].copy()
print(f"inside AOI03: {len(fac)} facilities and {len(bridges)} road bridges")
extent = flood.dissolve()[["geometry"]]
fac["in_flood"] = fac.intersects(extent.geometry[0])
bridges["in_flood"] = bridges.intersects(extent.geometry[0])
bridges["over_poyo"] = bridges.intersects(poyo.union_all().buffer(5))
fac = gpd.sjoin(fac, muni[["municipality", "geometry"]], how="left", predicate="intersects") \
.drop_duplicates(["element", "id"]).drop(columns="index_right")
pt = bridges.copy()
pt["geometry"] = pt.representative_point()
bridges["municipality"] = gpd.sjoin(pt, muni[["municipality", "geometry"]], how="left")["municipality"] \
.groupby(level=0).first()
by_type = fac.groupby("facility")["in_flood"].agg(total="size", in_flood="sum")
print(by_type.sort_values("total", ascending=False).to_string())
print(f"road bridges in the flood extent: {bridges.in_flood.sum()} of {len(bridges)}; "
f"road bridges over the Poyo ravine: {bridges.over_poyo.sum()}")
print("\nFacilities inside the flood extent (excluding schools):")
hit = fac[fac.in_flood & (fac.facility != "School")].sort_values(["facility", "municipality"])
print(hit[["facility", "name", "municipality"]].to_string(index=False))
# per municipality: flooded area, facilities and bridges
fl_muni = gpd.overlay(flood[["geometry"]], muni[["municipality", "geometry"]], how="intersection")
summary = muni.set_index("municipality")[["mapped_km2"]].copy()
fl_muni["km2"] = fl_muni.area / 1e6
summary["flood_km2"] = fl_muni.groupby("municipality")["km2"].sum()
summary["facilities"] = fac.groupby("municipality").size()
summary["facilities_in_flood"] = fac[fac.in_flood].groupby("municipality").size()
summary["bridges_in_flood"] = bridges[bridges.in_flood].groupby("municipality").size()
summary = summary.fillna(0)
summary["flood_pct"] = 100 * summary.flood_km2 / summary.mapped_km2
summary = summary[summary.flood_km2 >= 0.05].sort_values("flood_km2", ascending=False)
for c in ["facilities", "facilities_in_flood", "bridges_in_flood"]:
summary[c] = summary[c].astype(int)
print(f"\n{len(summary)} municipalities with at least 5 ha in the flood extent (flood_pct = share of the mapped part):")
print(summary.round({"mapped_km2": 1, "flood_km2": 2, "flood_pct": 1}).to_string())
# --- Step 7: the "what we know" table ---------------------------------------------------------
aoi03_row = first.loc["AOI03"]
r_max = f"{rain.max():.0f} mm (ECMWF IFS 9 km, max of {rain.size} grid points)"
know = pd.DataFrame([
("Event", f"Extraordinary rainfall from {EVENT_UTC:%d %b %Y %H:%M} UTC", "Copernicus EMS EMSR773",
f"{pd.Timestamp(act['activationTime']):%d %b %H:%M} UTC"),
("Rainfall, 29 Oct", f"up to {r_max}; Chiva {hourly['Chiva'].loc['2024-10-29'].sum():.0f} mm, "
f"Paiporta {hourly['Paiporta'].loc['2024-10-29'].sum():.0f} mm", "Open-Meteo", "29 Oct (local day)"),
("Flood extent", f"{flood.km2.sum():.1f} km² in L'Horta Sud", "EMS AOI03 first delineation",
f"image {aoi03_row.image_utc:%d %b %H:%M} UTC"),
("Municipalities", f"{len(summary)} with ≥5 ha flooded; most: " + ", ".join(summary.index[:3]),
"geoBoundaries + EMS", f"{aoi03_row.delivered_utc:%d %b %H:%M} UTC"),
("Health facilities", f"{fac[fac.in_flood & fac.facility.isin(['Hospital', 'Health centre'])].shape[0]} of "
f"{fac.facility.isin(['Hospital', 'Health centre']).sum()} in the flood extent", "OSM + EMS", "OSM 29 Oct"),
("Schools", f"{fac[fac.in_flood & (fac.facility == 'School')].shape[0]} of {(fac.facility == 'School').sum()} "
"in the flood extent", "OSM + EMS", "OSM 29 Oct"),
("Emergency services", f"{fac[fac.in_flood & fac.facility.isin(['Fire station', 'Police', 'Ambulance station'])].shape[0]} of "
f"{fac.facility.isin(['Fire station', 'Police', 'Ambulance station']).sum()} stations in the flood extent",
"OSM + EMS", "OSM 29 Oct"),
("Road bridges", f"{bridges.in_flood.sum()} in the flood extent, {bridges.over_poyo.sum()} over the Poyo ravine",
"OSM + EMS", "OSM 29 Oct"),
], columns=["topic", "what we know", "source", "as of"])
print("\n=== Step 7: what we know ===")
print(know.to_string(index=False))
know.to_csv(OUT / "what_we_know.csv", index=False)
summary.round(3).to_csv(OUT / "municipality_summary.csv")
gpkg = OUT / "situation_emsr773.gpkg"
gpkg.unlink(missing_ok=True)
flood[["notation", "det_method", "km2", "geometry"]].to_file(gpkg, layer="flood_extent_ems_aoi03")
fac[["id", "facility", "name", "municipality", "in_flood", "geometry"]].to_file(gpkg, layer="facilities_osm")
bridges[["id", "name", "highway", "municipality", "in_flood", "over_poyo", "geometry"]].to_file(gpkg, layer="road_bridges_osm")
muni.to_file(gpkg, layer="municipalities")
print(f"wrote {gpkg.name} (4 layers) and what_we_know.csv")
# --- Figures ----------------------------------------------------------------------------------
STYLE = {"Hospital": ("P", "#c21f3a", 70), "Health centre": ("P", "#f08080", 34),
"Fire station": ("^", "#e66100", 46), "Ambulance station": ("^", "#f5a623", 40),
"Police": ("s", "#1543b0", 32), "School": ("o", "#6a3d9a", 14)}
SEA = "#dbe9f4"
CREDIT = ("Flood extent © European Union, Copernicus EMS (EMSR773). Municipalities: geoBoundaries (IGN), CC BY 4.0. "
"Facilities, roads, rivers: © OpenStreetMap contributors (ODbL), as of 29 Oct 2024.")
def scalebar(ax, km=5, loc=(0.04, 0.05)):
x0, x1 = ax.get_xlim()
y0, y1 = ax.get_ylim()
x, y = x0 + loc[0] * (x1 - x0), y0 + loc[1] * (y1 - y0)
for i in range(2):
ax.add_patch(Rectangle((x + i * km * 500, y), km * 500, (y1 - y0) * 0.008,
facecolor=["k", "w"][i], edgecolor="k", lw=0.6, zorder=20))
ax.text(x, y + (y1 - y0) * 0.015, "0", ha="center", fontsize=7, zorder=20)
ax.text(x + km * 1000, y + (y1 - y0) * 0.015, f"{km} km", ha="center", fontsize=7, zorder=20)
def north(ax, loc=(0.96, 0.9)):
ax.annotate("N", xy=loc, xytext=(loc[0], loc[1] - 0.07), xycoords="axes fraction", ha="center",
fontsize=10, fontweight="bold", arrowprops={"arrowstyle": "-|>", "color": "k", "lw": 1.2}, zorder=20)
def clean(ax):
ax.set_xlabel("")
ax.set_ylabel("")
def base(ax, bounds):
ax.set_facecolor(SEA)
muni_all.plot(ax=ax, color="#f4f3ee", edgecolor="#9a9a9a", lw=0.5)
ax.set_xlim(bounds[0], bounds[2])
ax.set_ylim(bounds[1], bounds[3])
ax.set_xticks([])
ax.set_yticks([])
sb = study.total_bounds
# Figure 1: rainfall
wide = gpd.GeoDataFrame(geometry=[box(*RAIN_BOX)], crs=4326).to_crs(CRS).total_bounds
fig, (ax, ax2) = plt.subplots(1, 2, figsize=(12.5, 5.4), gridspec_kw={"width_ratios": [1.45, 1]})
ax.set_facecolor(SEA)
cells = gpd.GeoDataFrame({"mm": rain.ravel()}, geometry=[box(x - 0.05, y - 0.05, x + 0.05, y + 0.05)
for x, y in zip(LON.ravel(), LAT.ravel())], crs=4326).to_crs(CRS)
cells.plot(ax=ax, column="mm", cmap="Blues", vmin=0, vmax=200, alpha=0.9, legend=True,
legend_kwds={"label": "mm (ECMWF IFS 9 km)", "shrink": 0.8})
muni_all.boundary.plot(ax=ax, color="#555", lw=0.35)
study.boundary.plot(ax=ax, color="#c21f3a", lw=1.4)
for name, (x, y) in PLACES.items():
p = gpd.GeoSeries(gpd.points_from_xy([x], [y]), crs=4326).to_crs(CRS)[0]
ax.plot(p.x, p.y, "ko", ms=3.5)
ax.annotate(name, (p.x, p.y), xytext=(4, 4), textcoords="offset points", fontsize=8, fontweight="bold")
poyo.to_crs(CRS).plot(ax=ax, color="#0b3d91", lw=1.2)
ax.set_xlim(wide[0], wide[2])
ax.set_ylim(wide[1], wide[3])
ax.set_xticks([])
ax.set_yticks([])
ax.text(0.0, -0.02, "Red box: study area. Dark blue line: Rambla de Poio (OSM).\nRainfall: Open-Meteo, ECMWF IFS 9 km "
"(CC BY 4.0). Municipalities: geoBoundaries (IGN), CC BY 4.0.", transform=ax.transAxes, fontsize=7, va="top")
scalebar(ax, km=10)
north(ax)
clean(ax)
ax.set_title("Modelled rainfall on 29 October 2024 (local day)", loc="left", fontsize=10)
win = hourly.loc["2024-10-29 06:00":"2024-10-30 06:00"]
for name, c in zip(PLACES, ["#0b3d91", "#4f9fd6", "#e66100"]):
ax2.step(win.index, win[name], where="post", color=c, lw=1.6, label=f"{name}: {hourly.loc['2024-10-29', name].sum():.0f} mm on 29 Oct")
ax2.set_ylabel("Rainfall per hour (mm)")
ax2.set_ylim(0, win.max().max() * 1.3)
ax2.set_title("Hourly rainfall (ECMWF IFS 9 km), local time", loc="left", fontsize=10)
ax2.xaxis.set_major_locator(matplotlib.dates.HourLocator(byhour=[0, 6, 12, 18]))
ax2.xaxis.set_major_formatter(matplotlib.dates.DateFormatter("%H:%M\n%d %b"))
ax2.legend(frameon=False, loc="upper left")
ax2.grid(alpha=0.3)
fig.savefig(OUT / "01-rainfall-29-october.png")
plt.close(fig)
# Figure 2: first delineation, VHR (AOI03) vs Sentinel-2 (AOI01) over the same area
ab = aoi03.total_bounds
fig, axes = plt.subplots(1, 2, figsize=(12.5, 5.2))
for ax, (g, title, col) in zip(axes, [(flood, f"AOI03: {aoi03_row.sensor} and GeoEye-1, 0.5–2 m", "#1f78b4"),
(s2, "AOI01: Sentinel-2, 10–20 m", "#33a02c")]):
base(ax, ab)
for note, alpha in [("Flooded area", 0.85), ("Flood trace", 0.45)]:
g[g.notation == note].plot(ax=ax, color=col, alpha=alpha, lw=0)
aoi03.boundary.plot(ax=ax, color="k", lw=0.8, ls="--")
ax.set_title(f"{title}: {g.area.sum() / 1e6:.1f} km²", loc="left", fontsize=10)
ax.legend(handles=[Patch(color=col, alpha=0.85, label="Flooded area"),
Patch(color=col, alpha=0.45, label="Flood trace"),
Line2D([], [], color="k", ls="--", lw=0.8, label="AOI03 boundary")],
loc="upper right", fontsize=7.5, framealpha=0.9)
scalebar(ax)
clean(ax)
axes[0].text(0.0, -0.03, "Both delineations use images of 31 Oct 2024. © European Union, Copernicus Emergency Management Service "
"(EMSR773). Municipalities: geoBoundaries (IGN), CC BY 4.0.", fontsize=7, va="top",
transform=axes[0].transAxes)
fig.savefig(OUT / "02-ems-first-delineation.png")
plt.close(fig)
# Figure 3: the one-page situation map
fig, ax = plt.subplots(figsize=(13.5, 8.3))
base(ax, sb)
flood.plot(ax=ax, color="#3d8fd1", alpha=0.75, lw=0)
rivers.plot(ax=ax, color="#4f9fd6", lw=0.6)
poyo.plot(ax=ax, color="#0b3d91", lw=1.5)
roads.plot(ax=ax, color="#7a6a58", lw=0.7)
rail.plot(ax=ax, color="#333", lw=0.6, ls="--")
aoi03.boundary.plot(ax=ax, color="k", lw=0.8, ls="-.")
bridges[bridges.in_flood].plot(ax=ax, color="#c21f3a", lw=3)
for t, (m, c, s) in STYLE.items():
sub = fac[fac.facility == t].copy()
sub["geometry"] = sub.representative_point()
sub[~sub.in_flood].plot(ax=ax, marker=m, color="white", edgecolor=c, lw=0.9, markersize=s, zorder=8)
sub[sub.in_flood].plot(ax=ax, marker=m, color=c, edgecolor="k", lw=0.5, markersize=s * 1.3, zorder=9)
for name, r in muni.set_index("municipality").iterrows():
p = r.geometry.intersection(study.geometry[0]).representative_point()
big = name in summary.index[:12]
if not big and r.geometry.intersection(study.geometry[0]).area < 2e6:
continue
ax.text(p.x, p.y, name, fontsize=8 if big else 6.5, ha="center", color="#222" if big else "#666",
fontweight="bold" if big else "normal", zorder=12,
bbox={"fc": "white", "ec": "none", "alpha": 0.6, "pad": 0.5} if big else None)
handles = [Patch(color="#3d8fd1", alpha=0.75, label="Flood extent, 31 Oct (EMS)"),
Line2D([], [], color="#0b3d91", lw=1.5, label="Rambla de Poio (named in OSM)"),
Line2D([], [], color="#7a6a58", lw=0.7, label="Major road"),
Line2D([], [], color="#333", lw=0.6, ls="--", label="Railway"),
Line2D([], [], color="#c21f3a", lw=3, label="Road bridge in flood"),
Line2D([], [], color="k", lw=0.8, ls="-.", label="EMS AOI03 boundary")]
handles += [Line2D([], [], marker=m, ls="", markerfacecolor=c, markeredgecolor="k", ms=np.sqrt(s) * 1.1,
label=f"{t}: {int(by_type.loc[t, 'in_flood'])} of {int(by_type.loc[t, 'total'])} in flood")
for t, (m, c, s) in STYLE.items() if t in by_type.index]
handles.append(Line2D([], [], marker="o", ls="", markerfacecolor="white", markeredgecolor="#555", ms=5,
label="Open symbol: outside flood"))
ax.legend(handles=handles, loc="upper left", bbox_to_anchor=(1.01, 1), fontsize=7.5, frameon=False,
title="Facilities and bridges inside AOI03 only", title_fontsize=7.5)
scalebar(ax)
north(ax)
ax.set_title(f"Situation map: L'Horta Sud floods (Valencia), {ACTIVATION}\n"
f"Information as of {aoi03_row.delivered_utc:%d %b %Y %H:%M} UTC (EMS first delineation, "
f"image {aoi03_row.image_utc:%d %b %H:%M} UTC)", loc="left", fontsize=11)
clean(ax)
ax.text(0.0, -0.01, CREDIT + " ETRS89 / UTM 30N.", transform=ax.transAxes, fontsize=6.5, va="top")
fig.savefig(OUT / "03-situation-map.png")
plt.close(fig)
# Figure 4: per-municipality summary
top = summary.head(15).iloc[::-1]
fig, ax = plt.subplots(figsize=(10, 5.6))
ax.barh(top.index, top.flood_km2, color="#3d8fd1")
for i, (n, r) in enumerate(top.iterrows()):
ax.text(r.flood_km2 + 0.08, i, f"{r.flood_pct:.0f}% of mapped area | facilities in flood "
f"{int(r.facilities_in_flood)} of {int(r.facilities)} | road bridges in flood {int(r.bridges_in_flood)}",
va="center", fontsize=7.5)
ax.set_xlabel("Flood extent in municipality (km², EMS AOI03, image of 31 Oct 2024)")
ax.set_xlim(0, top.flood_km2.max() * 1.9)
ax.set_title("Flood extent by municipality, first EMS delineation (AOI03)", loc="left", fontsize=10)
ax.spines[["top", "right"]].set_visible(False)
fig.savefig(OUT / "04-municipality-summary.png")
plt.close(fig)
# --- Step 8: an interactive Folium map --------------------------------------------------------
print("\n=== Step 8: interactive map ===")
centre = study.to_crs(4326).geometry[0].centroid
m = folium.Map(location=[centre.y, centre.x], zoom_start=12, tiles="OpenStreetMap", control_scale=True)
web = flood[["notation", "geometry"]].copy()
web["geometry"] = web.simplify(3)
folium.GeoJson(web.to_crs(4326), name="Flood extent, 31 Oct (Copernicus EMS)",
style_function=lambda f: {"color": "#1f5fa8", "weight": 0, "fillColor": "#3d8fd1",
"fillOpacity": 0.6 if f["properties"]["notation"] == "Flooded area" else 0.35},
tooltip=folium.GeoJsonTooltip(["notation"], labels=False)).add_to(m)
folium.GeoJson(aoi03[["geometry"]].to_crs(4326), name="EMS AOI03 (area mapped)",
style_function=lambda f: {"color": "black", "weight": 2, "dashArray": "6 4", "fillOpacity": 0}).add_to(m)
folium.GeoJson(muni[["municipality", "geometry"]].to_crs(4326), name="Municipalities",
style_function=lambda f: {"color": "#555", "weight": 1, "fillOpacity": 0},
tooltip=folium.GeoJsonTooltip(["municipality"], labels=False)).add_to(m)
br = bridges[bridges.in_flood][["name", "highway", "geometry"]].fillna({"name": "(no name)"})
folium.GeoJson(br.to_crs(4326), name="Road bridges in flood (OSM)",
style_function=lambda f: {"color": "#c21f3a", "weight": 5},
tooltip=folium.GeoJsonTooltip(["name", "highway"], aliases=["Bridge", "Road class"])).add_to(m)
for t, (_, c, _) in STYLE.items():
layer = folium.FeatureGroup(name=f"{t} (OSM)", show=t != "School")
sub = fac[fac.facility == t].to_crs(4326)
for _, r in sub.iterrows():
p = r.geometry.representative_point()
folium.CircleMarker([p.y, p.x], radius=6 if r.in_flood else 4, color="black" if r.in_flood else c,
weight=1.5 if r.in_flood else 1, fill=True, fill_color=c, fill_opacity=0.9,
tooltip=f"{t}: {r['name']} ({r.municipality}){' - IN FLOOD EXTENT' if r.in_flood else ''}"
).add_to(layer)
layer.add_to(m)
folium.LayerControl(collapsed=False).add_to(m)
m.get_root().html.add_child(folium.Element(
'<div style="position:fixed;bottom:22px;left:10px;z-index:9999;background:white;padding:4px 6px;font:11px sans-serif">'
"Flood extent © European Union, Copernicus EMS EMSR773 (image 31 Oct 2024). Facilities and bridges: "
"© OpenStreetMap contributors, as of 29 Oct 2024.</div>"))
html = OUT / "situation_map.html"
m.save(html)
print(f"wrote {html.name}: {html.stat().st_size / 1e6:.2f} MB")