Spaces:
Sleeping
Sleeping
Add Folium rendering option with WMS overlay + time selection; update requirements
Browse files- .DS_Store +0 -0
- app.py +147 -2
- requirements.txt +2 -0
.DS_Store
ADDED
|
Binary file (6.15 kB). View file
|
|
|
app.py
CHANGED
|
@@ -1,4 +1,9 @@
|
|
| 1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 2 |
|
| 3 |
|
| 4 |
# Simple Gradio app that renders an interactive Leaflet map in the browser.
|
|
@@ -256,9 +261,149 @@ HTML = r"""
|
|
| 256 |
|
| 257 |
|
| 258 |
with gr.Blocks(fill_height=True, theme=gr.themes.Default()) as demo:
|
| 259 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 260 |
|
| 261 |
|
| 262 |
if __name__ == "__main__":
|
| 263 |
demo.launch()
|
| 264 |
-
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
import folium
|
| 3 |
+
from folium.plugins import FloatImage
|
| 4 |
+
from urllib.request import urlopen
|
| 5 |
+
from urllib.error import URLError, HTTPError
|
| 6 |
+
import xml.etree.ElementTree as ET
|
| 7 |
|
| 8 |
|
| 9 |
# Simple Gradio app that renders an interactive Leaflet map in the browser.
|
|
|
|
| 261 |
|
| 262 |
|
| 263 |
with gr.Blocks(fill_height=True, theme=gr.themes.Default()) as demo:
|
| 264 |
+
# Mapping for Folium rendering
|
| 265 |
+
ACCUM_MAP = {
|
| 266 |
+
"6h": ("dwd:Icon_reg025_fd_sl_TOTPREC06H", "icon_reg025_fd_sl_totprec06h_wmc_isoarea"),
|
| 267 |
+
"24h": ("dwd:Icon_reg025_fd_sl_TOTPREC24H", "icon_reg025_fd_sl_totprec24h_wmc_isoarea"),
|
| 268 |
+
"since_start": ("dwd:Icon_reg025_fd_sl_TOTPREC", "icon_reg025_fd_sl_totprec_wmc_isoarea"),
|
| 269 |
+
}
|
| 270 |
+
|
| 271 |
+
DWD_WMS = "https://maps.dwd.de/geoserver/ows?"
|
| 272 |
+
GIBS_TILES = "https://gibs.earthdata.nasa.gov/wmts/epsg3857/best/BlueMarble_ShadedRelief_Bathymetry/default/2013-12-01/GoogleMapsCompatible_Level8/{z}/{y}/{x}.jpg"
|
| 273 |
+
|
| 274 |
+
def _parse_iso_duration_to_seconds(d):
|
| 275 |
+
# Supports PnDTnHnMnS minimal subset
|
| 276 |
+
import re
|
| 277 |
+
m = re.match(r"^P(?:(\d+)D)?T?(?:(\d+)H)?(?:(\d+)M)?(?:(\d+)S)?$", d)
|
| 278 |
+
if not m:
|
| 279 |
+
return 0
|
| 280 |
+
days = int(m.group(1) or 0)
|
| 281 |
+
hours = int(m.group(2) or 0)
|
| 282 |
+
minutes = int(m.group(3) or 0)
|
| 283 |
+
seconds = int(m.group(4) or 0)
|
| 284 |
+
return (((days * 24 + hours) * 60 + minutes) * 60 + seconds)
|
| 285 |
+
|
| 286 |
+
def _extract_times_from_layer(layer_el):
|
| 287 |
+
ns = {"wms": "http://www.opengis.net/wms"}
|
| 288 |
+
# Dimension may be default ns; try both
|
| 289 |
+
dim = layer_el.find("wms:Dimension[@name='time']", ns)
|
| 290 |
+
if dim is None:
|
| 291 |
+
# fallback without namespace
|
| 292 |
+
dim = layer_el.find("Dimension")
|
| 293 |
+
if dim is None or (dim.text or "").strip() == "":
|
| 294 |
+
return []
|
| 295 |
+
txt = (dim.text or "").strip()
|
| 296 |
+
if "/" in txt:
|
| 297 |
+
start_s, end_s, step_s = txt.split("/")
|
| 298 |
+
from datetime import datetime, timezone, timedelta
|
| 299 |
+
def parse_dt(s):
|
| 300 |
+
# Expect Zulu time
|
| 301 |
+
return datetime.fromisoformat(s.replace("Z", "+00:00")).astimezone(timezone.utc)
|
| 302 |
+
start = parse_dt(start_s)
|
| 303 |
+
end = parse_dt(end_s)
|
| 304 |
+
step = _parse_iso_duration_to_seconds(step_s)
|
| 305 |
+
if step <= 0:
|
| 306 |
+
return [start_s]
|
| 307 |
+
out = []
|
| 308 |
+
t = start
|
| 309 |
+
while t <= end:
|
| 310 |
+
out.append(t.replace(microsecond=0).isoformat().replace("+00:00", "Z"))
|
| 311 |
+
t = t + timedelta(seconds=step)
|
| 312 |
+
return out
|
| 313 |
+
# Comma-separated
|
| 314 |
+
return [s.strip() for s in txt.split(",") if s.strip()]
|
| 315 |
+
|
| 316 |
+
def get_times(accum_choice: str):
|
| 317 |
+
layer_name, _ = ACCUM_MAP.get(accum_choice, ACCUM_MAP["6h"])
|
| 318 |
+
url = f"{DWD_WMS}service=WMS&request=GetCapabilities&version=1.3.0"
|
| 319 |
+
try:
|
| 320 |
+
with urlopen(url, timeout=15) as resp:
|
| 321 |
+
xml = resp.read()
|
| 322 |
+
except (URLError, HTTPError) as e:
|
| 323 |
+
return gr.Dropdown(choices=[], value=None, label="Time (UTC)", info=f"Failed to fetch capabilities: {e}")
|
| 324 |
+
try:
|
| 325 |
+
root = ET.fromstring(xml)
|
| 326 |
+
except ET.ParseError as e:
|
| 327 |
+
return gr.Dropdown(choices=[], value=None, label="Time (UTC)", info=f"Parse error: {e}")
|
| 328 |
+
ns = {"wms": "http://www.opengis.net/wms"}
|
| 329 |
+
# Find all layer nodes
|
| 330 |
+
for lyr in root.findall(".//wms:Layer", ns):
|
| 331 |
+
name_el = lyr.find("wms:Name", ns)
|
| 332 |
+
if name_el is not None and (name_el.text or "").strip() == layer_name:
|
| 333 |
+
times = _extract_times_from_layer(lyr)
|
| 334 |
+
if not times:
|
| 335 |
+
return gr.Dropdown(choices=[], value=None, label="Time (UTC)", info="No times available")
|
| 336 |
+
# choose latest
|
| 337 |
+
return gr.Dropdown(choices=times, value=times[-1], label="Time (UTC)")
|
| 338 |
+
return gr.Dropdown(choices=[], value=None, label="Time (UTC)", info="Layer not found")
|
| 339 |
+
|
| 340 |
+
def build_legend_url(layer_name: str, style_name: str) -> str:
|
| 341 |
+
from urllib.parse import urlencode
|
| 342 |
+
qs = urlencode({
|
| 343 |
+
"service": "WMS",
|
| 344 |
+
"version": "1.3.0",
|
| 345 |
+
"request": "GetLegendGraphic",
|
| 346 |
+
"format": "image/png",
|
| 347 |
+
"width": 160,
|
| 348 |
+
"height": 300,
|
| 349 |
+
"layer": layer_name,
|
| 350 |
+
"style": style_name,
|
| 351 |
+
})
|
| 352 |
+
return f"{DWD_WMS}{qs}"
|
| 353 |
+
|
| 354 |
+
def render_folium(accum_choice: str, time_value: str, opacity: float):
|
| 355 |
+
layer_name, style_name = ACCUM_MAP.get(accum_choice, ACCUM_MAP["6h"])
|
| 356 |
+
m = folium.Map(location=[20, 0], zoom_start=2, tiles=None, control_scale=True)
|
| 357 |
+
# Basemap
|
| 358 |
+
folium.TileLayer(
|
| 359 |
+
tiles=GIBS_TILES,
|
| 360 |
+
name="NASA Blue Marble",
|
| 361 |
+
attr="Blue Marble — NASA GIBS",
|
| 362 |
+
max_zoom=8,
|
| 363 |
+
overlay=False,
|
| 364 |
+
control=True,
|
| 365 |
+
).add_to(m)
|
| 366 |
+
# DWD ICON WMS overlay with selected time
|
| 367 |
+
folium.raster_layers.WmsTileLayer(
|
| 368 |
+
url=DWD_WMS,
|
| 369 |
+
name=f"DWD ICON {accum_choice}",
|
| 370 |
+
layers=layer_name,
|
| 371 |
+
styles=style_name,
|
| 372 |
+
fmt="image/png",
|
| 373 |
+
transparent=True,
|
| 374 |
+
version="1.3.0",
|
| 375 |
+
time=time_value if time_value else None,
|
| 376 |
+
overlay=True,
|
| 377 |
+
control=True,
|
| 378 |
+
show=True,
|
| 379 |
+
opacity=opacity,
|
| 380 |
+
).add_to(m)
|
| 381 |
+
# Legend
|
| 382 |
+
legend_url = build_legend_url(layer_name, style_name)
|
| 383 |
+
try:
|
| 384 |
+
FloatImage(legend_url, bottom=5, left=5).add_to(m)
|
| 385 |
+
except Exception:
|
| 386 |
+
pass
|
| 387 |
+
folium.LayerControl(collapsed=False).add_to(m)
|
| 388 |
+
return m.get_root().render()
|
| 389 |
+
|
| 390 |
+
with gr.Tabs():
|
| 391 |
+
with gr.TabItem("Leaflet (Live)"):
|
| 392 |
+
gr.HTML(HTML)
|
| 393 |
+
with gr.TabItem("Folium"):
|
| 394 |
+
with gr.Row():
|
| 395 |
+
accum = gr.Radio(["6h", "24h", "since_start"], value="6h", label="Accumulation")
|
| 396 |
+
time_dd = gr.Dropdown(choices=[], label="Time (UTC)")
|
| 397 |
+
opacity = gr.Slider(0.2, 1.0, value=0.8, step=0.05, label="Overlay opacity")
|
| 398 |
+
render_btn = gr.Button("Render Folium Map", variant="primary")
|
| 399 |
+
folium_html = gr.HTML()
|
| 400 |
+
|
| 401 |
+
# Populate times on load and when accumulation changes
|
| 402 |
+
demo.load(fn=get_times, inputs=accum, outputs=time_dd)
|
| 403 |
+
accum.change(fn=get_times, inputs=accum, outputs=time_dd)
|
| 404 |
+
# Render map
|
| 405 |
+
render_btn.click(fn=render_folium, inputs=[accum, time_dd, opacity], outputs=folium_html)
|
| 406 |
|
| 407 |
|
| 408 |
if __name__ == "__main__":
|
| 409 |
demo.launch()
|
|
|
requirements.txt
ADDED
|
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
|
|
|
| 1 |
+
gradio==5.46.1
|
| 2 |
+
folium>=0.15.0
|