Spaces:
Running
Running
import streamlit as st | |
import requests | |
import pandas as pd | |
import pydeck as pdk | |
# Base URL for PegelOnline API | |
BASE_API_URL = "https://pegelonline.wsv.de/webservices/rest-api/v2" | |
def fetch_forecasts(station_id): | |
""" | |
Fetches the forecast data for a specific station. | |
""" | |
url = f"{BASE_API_URL}/stations/{station_id}/WV/measurements.json" | |
response = requests.get(url) | |
if response.status_code == 200: | |
data = response.json() | |
# Filter only forecast data | |
forecasts = [ | |
{ | |
"initialized": item["initialized"], | |
"timestamp": item["timestamp"], | |
"value": item["value"], | |
} | |
for item in data if item["type"] == "forecast" | |
] | |
return pd.DataFrame(forecasts) | |
else: | |
return pd.DataFrame() # Return an empty DataFrame on error | |
# Streamlit app with subpage | |
def main(): | |
st.title("RiverRadar - Water Level Forecasts") | |
st.subheader("Pegel Forecast Map") | |
# Input for station ID | |
station_id = st.text_input( | |
"Enter Pegel Station ID", | |
"1d26e504-7f9e-480a-b52c-5932be6549ab", # Default value for demo | |
) | |
# Fetch forecasts for the given station | |
st.write("Fetching forecast data...") | |
forecasts_df = fetch_forecasts(station_id) | |
if not forecasts_df.empty: | |
st.success("Forecast data loaded!") | |
# Format timestamps and add as columns | |
forecasts_df["timestamp"] = pd.to_datetime(forecasts_df["timestamp"]) | |
forecasts_df["hour"] = forecasts_df["timestamp"].dt.hour | |
# Display forecast graph | |
st.write("### Forecast Data as Interactive Graph") | |
# Plot the forecast data | |
if not forecasts_df.empty: | |
import altair as alt | |
# Create an Altair line chart | |
chart = alt.Chart(forecasts_df).mark_line(point=True).encode( | |
x=alt.X("timestamp:T", title="Time"), | |
y=alt.Y("value:Q", title="Water Level (cm)"), | |
tooltip=["timestamp:T", "value:Q"] | |
).properties( | |
title="Water Level Predictions", | |
width=700, | |
height=400 | |
) | |
# Display the chart | |
st.altair_chart(chart, use_container_width=True) | |
else: | |
st.warning("No forecast data available for this station.") | |
# Map visualization | |
st.write("### Forecast Visualization on Map") | |
# Dummy station location (Replace with real station data if available) | |
station_location = { | |
"latitude": 52.520008, | |
"longitude": 13.404954, # Berlin coordinates for demo | |
} | |
# Create a PyDeck layer | |
layer = pdk.Layer( | |
"ScatterplotLayer", | |
data=forecasts_df, | |
get_position=[station_location["longitude"], station_location["latitude"]], | |
get_radius=2000, | |
get_color=[255, 0, 0, 140], | |
pickable=True, | |
) | |
# Map settings | |
view_state = pdk.ViewState( | |
latitude=station_location["latitude"], | |
longitude=station_location["longitude"], | |
zoom=6, | |
pitch=0, | |
) | |
r = pdk.Deck( | |
layers=[layer], | |
initial_view_state=view_state, | |
tooltip={"text": "Water Level: {value} cm"}, | |
) | |
st.pydeck_chart(r) | |
else: | |
st.warning("No forecast data available for this station.") | |
if __name__ == "__main__": | |
main() | |