Spaces:
Sleeping
Sleeping
add prediction subpage
Browse files- pages/Vorhersage.py +112 -0
- requirements.txt +1 -0
pages/Vorhersage.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import streamlit as st
|
2 |
+
import requests
|
3 |
+
import pandas as pd
|
4 |
+
import pydeck as pdk
|
5 |
+
|
6 |
+
# Base URL for PegelOnline API
|
7 |
+
BASE_API_URL = "https://pegelonline.wsv.de/webservices/rest-api/v2"
|
8 |
+
|
9 |
+
def fetch_forecasts(station_id):
|
10 |
+
"""
|
11 |
+
Fetches the forecast data for a specific station.
|
12 |
+
"""
|
13 |
+
url = f"{BASE_API_URL}/stations/{station_id}/WV/measurements.json"
|
14 |
+
response = requests.get(url)
|
15 |
+
if response.status_code == 200:
|
16 |
+
data = response.json()
|
17 |
+
# Filter only forecast data
|
18 |
+
forecasts = [
|
19 |
+
{
|
20 |
+
"initialized": item["initialized"],
|
21 |
+
"timestamp": item["timestamp"],
|
22 |
+
"value": item["value"],
|
23 |
+
}
|
24 |
+
for item in data if item["type"] == "forecast"
|
25 |
+
]
|
26 |
+
return pd.DataFrame(forecasts)
|
27 |
+
else:
|
28 |
+
return pd.DataFrame() # Return an empty DataFrame on error
|
29 |
+
|
30 |
+
# Streamlit app with subpage
|
31 |
+
def main():
|
32 |
+
st.title("RiverRadar - Water Level Forecasts")
|
33 |
+
|
34 |
+
st.subheader("Pegel Forecast Map")
|
35 |
+
|
36 |
+
# Input for station ID
|
37 |
+
station_id = st.text_input(
|
38 |
+
"Enter Pegel Station ID",
|
39 |
+
"1d26e504-7f9e-480a-b52c-5932be6549ab", # Default value for demo
|
40 |
+
)
|
41 |
+
|
42 |
+
# Fetch forecasts for the given station
|
43 |
+
st.write("Fetching forecast data...")
|
44 |
+
forecasts_df = fetch_forecasts(station_id)
|
45 |
+
|
46 |
+
if not forecasts_df.empty:
|
47 |
+
st.success("Forecast data loaded!")
|
48 |
+
|
49 |
+
# Format timestamps and add as columns
|
50 |
+
forecasts_df["timestamp"] = pd.to_datetime(forecasts_df["timestamp"])
|
51 |
+
forecasts_df["hour"] = forecasts_df["timestamp"].dt.hour
|
52 |
+
|
53 |
+
# Display forecast graph
|
54 |
+
st.write("### Forecast Data as Interactive Graph")
|
55 |
+
|
56 |
+
# Plot the forecast data
|
57 |
+
if not forecasts_df.empty:
|
58 |
+
import altair as alt
|
59 |
+
|
60 |
+
# Create an Altair line chart
|
61 |
+
chart = alt.Chart(forecasts_df).mark_line(point=True).encode(
|
62 |
+
x=alt.X("timestamp:T", title="Time"),
|
63 |
+
y=alt.Y("value:Q", title="Water Level (cm)"),
|
64 |
+
tooltip=["timestamp:T", "value:Q"]
|
65 |
+
).properties(
|
66 |
+
title="Water Level Predictions",
|
67 |
+
width=700,
|
68 |
+
height=400
|
69 |
+
)
|
70 |
+
|
71 |
+
# Display the chart
|
72 |
+
st.altair_chart(chart, use_container_width=True)
|
73 |
+
else:
|
74 |
+
st.warning("No forecast data available for this station.")
|
75 |
+
|
76 |
+
# Map visualization
|
77 |
+
st.write("### Forecast Visualization on Map")
|
78 |
+
|
79 |
+
# Dummy station location (Replace with real station data if available)
|
80 |
+
station_location = {
|
81 |
+
"latitude": 52.520008,
|
82 |
+
"longitude": 13.404954, # Berlin coordinates for demo
|
83 |
+
}
|
84 |
+
|
85 |
+
# Create a PyDeck layer
|
86 |
+
layer = pdk.Layer(
|
87 |
+
"ScatterplotLayer",
|
88 |
+
data=forecasts_df,
|
89 |
+
get_position=[station_location["longitude"], station_location["latitude"]],
|
90 |
+
get_radius=2000,
|
91 |
+
get_color=[255, 0, 0, 140],
|
92 |
+
pickable=True,
|
93 |
+
)
|
94 |
+
|
95 |
+
# Map settings
|
96 |
+
view_state = pdk.ViewState(
|
97 |
+
latitude=station_location["latitude"],
|
98 |
+
longitude=station_location["longitude"],
|
99 |
+
zoom=6,
|
100 |
+
pitch=0,
|
101 |
+
)
|
102 |
+
r = pdk.Deck(
|
103 |
+
layers=[layer],
|
104 |
+
initial_view_state=view_state,
|
105 |
+
tooltip={"text": "Water Level: {value} cm"},
|
106 |
+
)
|
107 |
+
st.pydeck_chart(r)
|
108 |
+
else:
|
109 |
+
st.warning("No forecast data available for this station.")
|
110 |
+
|
111 |
+
if __name__ == "__main__":
|
112 |
+
main()
|
requirements.txt
CHANGED
@@ -3,3 +3,4 @@ requests
|
|
3 |
pandas
|
4 |
geopandas
|
5 |
pydeck
|
|
|
|
3 |
pandas
|
4 |
geopandas
|
5 |
pydeck
|
6 |
+
altair
|