File size: 4,198 Bytes
a08a4d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
45a8948
 
a08a4d0
 
1df9c2f
 
 
a08a4d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
114
115
116
# app.py
import streamlit as st
from datetime import datetime
from streamlit_folium import st_folium
from roadmap.RoadMap import RoadMapManager

SUPPORTED_CITIES = ["Los Angeles"]
LA_ROADS = [
    'I 405 North', 'I 405 South',
    'US 101 North', 'US 101 South',
    'I 5 North', 'I 5 South',
    'I 110 North', 'I 110 South',
    'CA 170 North', 'CA 170 South',
    'CA 118 East', 'CA 118 West',
    'CA 134 East', 'CA 134 West',
    'I 605 North', 'I 605 South',
    'I 210 East', 'I 210 West'
]
LA_BBOX = (-118.569946, 33.252470, -116.976929, 34.388779)

st.title("TrafCast: Traffic Forecasting for Los Angeles")

city = st.selectbox("Select City", SUPPORTED_CITIES)

@st.cache_resource
def get_map_manager(city_name):
    return RoadMapManager(city_name, LA_BBOX)

map_manager = get_map_manager(city)

selected_roads = st.multiselect("Select Roads to Load", LA_ROADS)

if selected_roads and st.button("Load Road Data"):
    with st.spinner("Loading road data..."):
        map_manager.set_roads(selected_roads)
        st.session_state["roads_loaded"] = True
    st.success("Road data loaded successfully.")

if st.session_state.get("roads_loaded"):
    # Set default date to March 15, 2025 if no date is stored in session state
    default_date = st.session_state.get("selected_date", datetime(2025, 3, 15).date())
    default_time = st.session_state.get("selected_time", datetime.now().time())

    st.date_input("Choose Date", value=default_date, key="selected_date", 
                  min_value=datetime(2025, 3, 1).date(), 
                  max_value=datetime(2025, 3, 31).date())
    st.time_input("Choose Time", value=default_time, key="selected_time")

    predict_time = datetime.combine(
        st.session_state["selected_date"],
        st.session_state["selected_time"]
    )


    map_option = st.radio(
        "Choose map visualization:",
        ["Predicted Speed Only", "Real Speed Only", "Side by Side Comparison"],
        key="map_option"
    )
    
    if st.button("Apply Prediction"):
        with st.spinner("Running prediction and generating map..."):
            map_manager.apply_prediction_data(predict_time)
            
            if map_option == "Predicted Speed Only":
                folium_map = map_manager.draw_map_offset()
                st.session_state["folium_map"] = folium_map
                st.session_state["map_type"] = "predicted"
            elif map_option == "Real Speed Only":
                folium_map = map_manager.draw_map_with_real_speed()
                st.session_state["folium_map"] = folium_map
                st.session_state["map_type"] = "real"
            else:  # Side by Side Comparison
                predicted_map, real_map = map_manager.draw_side_by_side_maps()
                st.session_state["predicted_map"] = predicted_map
                st.session_state["real_map"] = real_map
                st.session_state["map_type"] = "side_by_side"
        st.success("Map updated!")

if st.session_state.get("map_type") == "side_by_side":
    if "predicted_map" in st.session_state and "real_map" in st.session_state:
        # Use container to control spacing
        with st.container():
            st.subheader("🟢 Predicted Speed")
            st_folium(
                st.session_state["predicted_map"],
                width=1200,
                height=600,
                returned_objects=[],
                key="predicted_map"
            )
        
        # Minimal spacing
        st.markdown("<div style='margin-top: 10px;'></div>", unsafe_allow_html=True)
        
        with st.container():
            st.subheader("🔴 Real Speed")
            st_folium(
                st.session_state["real_map"],
                width=1200,
                height=600,
                returned_objects=[],
                key="real_map"
            )
            
elif "folium_map" in st.session_state:
    map_title = "Predicted Speed" if st.session_state.get("map_type") == "predicted" else "Real Speed"
    st.subheader(f"🗺️ {map_title}")
    st_folium(
        st.session_state["folium_map"],
        width=1000,
        height=1000,
        returned_objects=[],
        key="traffic_map"
    )