File size: 3,527 Bytes
f1e784c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
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()