Spaces:
Running
Running
Update app.py
Browse files
app.py
CHANGED
|
@@ -6,21 +6,22 @@ from geopy.geocoders import Nominatim
|
|
| 6 |
import folium
|
| 7 |
from streamlit_folium import st_folium
|
| 8 |
import os
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
st.set_page_config(page_title="Commute Planner", layout="centered")
|
| 12 |
st.title("🚗 Commute Planner")
|
| 13 |
-
st.markdown("Estimate
|
| 14 |
|
| 15 |
-
# API keys
|
| 16 |
ORS_API_KEY = os.getenv("ORS_API_KEY", st.secrets.get("ORS_API_KEY", ""))
|
| 17 |
OWM_API_KEY = os.getenv("OWM_API_KEY", st.secrets.get("OWM_API_KEY", ""))
|
| 18 |
|
| 19 |
-
# Initialize
|
| 20 |
client = openrouteservice.Client(key=ORS_API_KEY)
|
| 21 |
geolocator = Nominatim(user_agent="commute_planner")
|
| 22 |
|
| 23 |
-
# Transport
|
| 24 |
transport_modes = {
|
| 25 |
"Driving": "driving-car",
|
| 26 |
"Walking": "foot-walking",
|
|
@@ -28,17 +29,16 @@ transport_modes = {
|
|
| 28 |
"Transit (experimental)": "driving-hgv"
|
| 29 |
}
|
| 30 |
|
| 31 |
-
#
|
| 32 |
start_location = st.text_input("Enter Start Location", key="start")
|
| 33 |
end_location = st.text_input("Enter Destination", key="end")
|
| 34 |
mode = st.selectbox("Select Transportation Mode", list(transport_modes.keys()), key="mode")
|
| 35 |
submit = st.button("Get Route")
|
| 36 |
|
| 37 |
-
#
|
| 38 |
if submit or st.session_state.get("show_results"):
|
| 39 |
if submit:
|
| 40 |
try:
|
| 41 |
-
# Geocode locations
|
| 42 |
start_coords = geolocator.geocode(start_location)
|
| 43 |
end_coords = geolocator.geocode(end_location)
|
| 44 |
|
|
@@ -48,7 +48,6 @@ if submit or st.session_state.get("show_results"):
|
|
| 48 |
start_point = [start_coords.longitude, start_coords.latitude]
|
| 49 |
end_point = [end_coords.longitude, end_coords.latitude]
|
| 50 |
|
| 51 |
-
# Fetch route
|
| 52 |
route = client.directions(
|
| 53 |
coordinates=[start_point, end_point],
|
| 54 |
profile=transport_modes[mode],
|
|
@@ -58,23 +57,24 @@ if submit or st.session_state.get("show_results"):
|
|
| 58 |
distance_km = route['features'][0]['properties']['segments'][0]['distance'] / 1000
|
| 59 |
duration_min = route['features'][0]['properties']['segments'][0]['duration'] / 60
|
| 60 |
|
| 61 |
-
# Save to session state
|
| 62 |
st.session_state["show_results"] = True
|
| 63 |
st.session_state["distance_km"] = distance_km
|
| 64 |
st.session_state["duration_min"] = duration_min
|
| 65 |
st.session_state["start_coords"] = [start_coords.latitude, start_coords.longitude]
|
| 66 |
st.session_state["end_coords"] = [end_coords.latitude, end_coords.longitude]
|
| 67 |
st.session_state["route"] = route
|
|
|
|
|
|
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
st.error(f"Error: {e}")
|
| 71 |
|
| 72 |
-
#
|
| 73 |
if st.session_state.get("show_results"):
|
| 74 |
st.success(f"**Distance:** {st.session_state['distance_km']:.2f} km")
|
| 75 |
st.success(f"**Estimated Duration:** {st.session_state['duration_min']:.1f} minutes")
|
| 76 |
|
| 77 |
-
#
|
| 78 |
route = st.session_state["route"]
|
| 79 |
m = folium.Map(location=st.session_state["start_coords"], zoom_start=13)
|
| 80 |
folium.Marker(st.session_state["start_coords"], tooltip="Start", icon=folium.Icon(color='green')).add_to(m)
|
|
@@ -82,7 +82,7 @@ if st.session_state.get("show_results"):
|
|
| 82 |
folium.GeoJson(route, name="Route").add_to(m)
|
| 83 |
st_folium(m, width=700, height=500)
|
| 84 |
|
| 85 |
-
#
|
| 86 |
lat, lon = st.session_state["start_coords"]
|
| 87 |
weather_url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={OWM_API_KEY}&units=metric"
|
| 88 |
air_url = f"http://api.openweathermap.org/data/2.5/air_pollution?lat={lat}&lon={lon}&appid={OWM_API_KEY}"
|
|
@@ -100,3 +100,17 @@ if st.session_state.get("show_results"):
|
|
| 100 |
st.info(f"**Air Quality Index (AQI):** {aqi} (1=Good, 5=Very Poor)")
|
| 101 |
except:
|
| 102 |
st.warning("Could not fetch weather or air quality data.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
import folium
|
| 7 |
from streamlit_folium import st_folium
|
| 8 |
import os
|
| 9 |
+
from urllib.parse import quote
|
| 10 |
|
| 11 |
+
# Page setup
|
| 12 |
st.set_page_config(page_title="Commute Planner", layout="centered")
|
| 13 |
st.title("🚗 Commute Planner")
|
| 14 |
+
st.markdown("Estimate travel time, view the route, check weather, air quality, and traffic alerts.")
|
| 15 |
|
| 16 |
+
# API keys
|
| 17 |
ORS_API_KEY = os.getenv("ORS_API_KEY", st.secrets.get("ORS_API_KEY", ""))
|
| 18 |
OWM_API_KEY = os.getenv("OWM_API_KEY", st.secrets.get("OWM_API_KEY", ""))
|
| 19 |
|
| 20 |
+
# Initialize services
|
| 21 |
client = openrouteservice.Client(key=ORS_API_KEY)
|
| 22 |
geolocator = Nominatim(user_agent="commute_planner")
|
| 23 |
|
| 24 |
+
# Transport options
|
| 25 |
transport_modes = {
|
| 26 |
"Driving": "driving-car",
|
| 27 |
"Walking": "foot-walking",
|
|
|
|
| 29 |
"Transit (experimental)": "driving-hgv"
|
| 30 |
}
|
| 31 |
|
| 32 |
+
# Inputs
|
| 33 |
start_location = st.text_input("Enter Start Location", key="start")
|
| 34 |
end_location = st.text_input("Enter Destination", key="end")
|
| 35 |
mode = st.selectbox("Select Transportation Mode", list(transport_modes.keys()), key="mode")
|
| 36 |
submit = st.button("Get Route")
|
| 37 |
|
| 38 |
+
# Route planning
|
| 39 |
if submit or st.session_state.get("show_results"):
|
| 40 |
if submit:
|
| 41 |
try:
|
|
|
|
| 42 |
start_coords = geolocator.geocode(start_location)
|
| 43 |
end_coords = geolocator.geocode(end_location)
|
| 44 |
|
|
|
|
| 48 |
start_point = [start_coords.longitude, start_coords.latitude]
|
| 49 |
end_point = [end_coords.longitude, end_coords.latitude]
|
| 50 |
|
|
|
|
| 51 |
route = client.directions(
|
| 52 |
coordinates=[start_point, end_point],
|
| 53 |
profile=transport_modes[mode],
|
|
|
|
| 57 |
distance_km = route['features'][0]['properties']['segments'][0]['distance'] / 1000
|
| 58 |
duration_min = route['features'][0]['properties']['segments'][0]['duration'] / 60
|
| 59 |
|
|
|
|
| 60 |
st.session_state["show_results"] = True
|
| 61 |
st.session_state["distance_km"] = distance_km
|
| 62 |
st.session_state["duration_min"] = duration_min
|
| 63 |
st.session_state["start_coords"] = [start_coords.latitude, start_coords.longitude]
|
| 64 |
st.session_state["end_coords"] = [end_coords.latitude, end_coords.longitude]
|
| 65 |
st.session_state["route"] = route
|
| 66 |
+
st.session_state["start_location_name"] = start_coords.address
|
| 67 |
+
st.session_state["end_location_name"] = end_coords.address
|
| 68 |
|
| 69 |
except Exception as e:
|
| 70 |
st.error(f"Error: {e}")
|
| 71 |
|
| 72 |
+
# Results
|
| 73 |
if st.session_state.get("show_results"):
|
| 74 |
st.success(f"**Distance:** {st.session_state['distance_km']:.2f} km")
|
| 75 |
st.success(f"**Estimated Duration:** {st.session_state['duration_min']:.1f} minutes")
|
| 76 |
|
| 77 |
+
# Route map
|
| 78 |
route = st.session_state["route"]
|
| 79 |
m = folium.Map(location=st.session_state["start_coords"], zoom_start=13)
|
| 80 |
folium.Marker(st.session_state["start_coords"], tooltip="Start", icon=folium.Icon(color='green')).add_to(m)
|
|
|
|
| 82 |
folium.GeoJson(route, name="Route").add_to(m)
|
| 83 |
st_folium(m, width=700, height=500)
|
| 84 |
|
| 85 |
+
# Weather and AQI
|
| 86 |
lat, lon = st.session_state["start_coords"]
|
| 87 |
weather_url = f"https://api.openweathermap.org/data/2.5/weather?lat={lat}&lon={lon}&appid={OWM_API_KEY}&units=metric"
|
| 88 |
air_url = f"http://api.openweathermap.org/data/2.5/air_pollution?lat={lat}&lon={lon}&appid={OWM_API_KEY}"
|
|
|
|
| 100 |
st.info(f"**Air Quality Index (AQI):** {aqi} (1=Good, 5=Very Poor)")
|
| 101 |
except:
|
| 102 |
st.warning("Could not fetch weather or air quality data.")
|
| 103 |
+
|
| 104 |
+
# 🔎 News Alerts (Protests, Traffic, Closures)
|
| 105 |
+
st.subheader("🚨 Local Alerts: Traffic, Closures, Protests")
|
| 106 |
+
|
| 107 |
+
start_loc = st.session_state.get("start_location_name", start_location)
|
| 108 |
+
end_loc = st.session_state.get("end_location_name", end_location)
|
| 109 |
+
keywords = ["traffic jam", "protest", "road blocked", "highway closed", "accident"]
|
| 110 |
+
|
| 111 |
+
st.markdown("Here are some quick links to news related to your route:")
|
| 112 |
+
for keyword in keywords:
|
| 113 |
+
search_query = f"{keyword} near {start_loc} OR {end_loc}"
|
| 114 |
+
search_url = f"https://www.google.com/search?q={quote(search_query)}"
|
| 115 |
+
st.markdown(f"- [{keyword.title()} Alerts]({search_url})")
|
| 116 |
+
|