Danial7 commited on
Commit
0192592
·
verified ·
1 Parent(s): b11ccbd

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +59 -0
  2. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,59 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import streamlit as st
2
+ import openrouteservice
3
+ from openrouteservice import convert
4
+ import folium
5
+ from streamlit_folium import st_folium
6
+
7
+ # Load API key from Streamlit secrets
8
+ ORS_API_KEY = st.secrets["ORS_API_KEY"]
9
+
10
+ # Initialize ORS client
11
+ client = openrouteservice.Client(key=ORS_API_KEY)
12
+
13
+ # Supported transport modes
14
+ MODES = {
15
+ "Driving": "driving-car",
16
+ "Walking": "foot-walking",
17
+ "Cycling": "cycling-regular"
18
+ }
19
+
20
+ # App title
21
+ st.set_page_config(page_title="Commute Planner", layout="centered")
22
+ st.title("🚗 Commute Planner")
23
+
24
+ # Input fields
25
+ start_location = st.text_input("Start location (address or place name):")
26
+ end_location = st.text_input("Destination (address or place name):")
27
+ mode = st.selectbox("Transportation mode", list(MODES.keys()))
28
+
29
+ # Process user input
30
+ if st.button("Get Route") and start_location and end_location:
31
+ try:
32
+ # Geocode start and end addresses
33
+ geocode_start = client.pelias_search(start_location)
34
+ start_coords = geocode_start["features"][0]["geometry"]["coordinates"]
35
+
36
+ geocode_end = client.pelias_search(end_location)
37
+ end_coords = geocode_end["features"][0]["geometry"]["coordinates"]
38
+
39
+ coords = (start_coords, end_coords)
40
+
41
+ # Get route info
42
+ route = client.directions(coords, profile=MODES[mode], format="geojson")
43
+ properties = route["features"][0]["properties"]["summary"]
44
+ distance_km = properties["distance"] / 1000
45
+ duration_min = properties["duration"] / 60
46
+
47
+ st.success(f"🛣️ Distance: {distance_km:.2f} km | ⏱️ Duration: {duration_min:.1f} minutes")
48
+
49
+ # Map
50
+ m = folium.Map(location=[start_coords[1], start_coords[0]], zoom_start=13)
51
+ folium.Marker([start_coords[1], start_coords[0]], popup="Start", icon=folium.Icon(color='green')).add_to(m)
52
+ folium.Marker([end_coords[1], end_coords[0]], popup="End", icon=folium.Icon(color='red')).add_to(m)
53
+ folium.GeoJson(route, name="Route").add_to(m)
54
+
55
+ st.subheader("📍 Route Map")
56
+ st_folium(m, width=700, height=500)
57
+
58
+ except Exception as e:
59
+ st.error(f"Error fetching route: {e}")
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ streamlit
2
+ openrouteservice
3
+ folium
4
+ streamlit-folium