Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,121 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# main.py
|
| 2 |
+
import streamlit as st
|
| 3 |
+
import folium
|
| 4 |
+
from streamlit_folium import folium_static
|
| 5 |
+
import osmnx as ox
|
| 6 |
+
import pandas as pd
|
| 7 |
+
from datetime import datetime
|
| 8 |
+
import networkx as nx
|
| 9 |
+
|
| 10 |
+
def get_location_coords(location_name):
|
| 11 |
+
try:
|
| 12 |
+
location = ox.geocode(location_name)
|
| 13 |
+
return location[0], location[1]
|
| 14 |
+
except:
|
| 15 |
+
return None
|
| 16 |
+
|
| 17 |
+
def find_restaurants(center_point, distance=1000):
|
| 18 |
+
tags = {'amenity': 'restaurant'}
|
| 19 |
+
restaurants = ox.geometries_from_point(
|
| 20 |
+
center_point, tags=tags, dist=distance
|
| 21 |
+
)
|
| 22 |
+
return restaurants
|
| 23 |
+
|
| 24 |
+
def create_app():
|
| 25 |
+
st.title("🗺️ RouteNBite")
|
| 26 |
+
st.subheader("Find restaurants along your route!")
|
| 27 |
+
|
| 28 |
+
col1, col2 = st.columns(2)
|
| 29 |
+
with col1:
|
| 30 |
+
start = st.text_input("Starting Point (e.g., 'Times Square, New York')")
|
| 31 |
+
cuisine = st.multiselect("Cuisine Preferences",
|
| 32 |
+
["Italian", "Asian", "Mexican", "Indian"])
|
| 33 |
+
|
| 34 |
+
with col2:
|
| 35 |
+
end = st.text_input("Destination")
|
| 36 |
+
max_detour = st.slider("Maximum Detour (minutes)", 5, 30, 15)
|
| 37 |
+
|
| 38 |
+
if st.button("Plan Route"):
|
| 39 |
+
if start and end:
|
| 40 |
+
# Get coordinates
|
| 41 |
+
start_coords = get_location_coords(start)
|
| 42 |
+
end_coords = get_location_coords(end)
|
| 43 |
+
|
| 44 |
+
if start_coords and end_coords:
|
| 45 |
+
# Create map centered between start and end
|
| 46 |
+
center_lat = (start_coords[0] + end_coords[0]) / 2
|
| 47 |
+
center_lon = (start_coords[1] + end_coords[1]) / 2
|
| 48 |
+
m = folium.Map(location=[center_lat, center_lon], zoom_start=12)
|
| 49 |
+
|
| 50 |
+
# Add markers for start and end
|
| 51 |
+
folium.Marker(
|
| 52 |
+
start_coords,
|
| 53 |
+
popup='Start',
|
| 54 |
+
icon=folium.Icon(color='green')
|
| 55 |
+
).add_to(m)
|
| 56 |
+
|
| 57 |
+
folium.Marker(
|
| 58 |
+
end_coords,
|
| 59 |
+
popup='End',
|
| 60 |
+
icon=folium.Icon(color='red')
|
| 61 |
+
).add_to(m)
|
| 62 |
+
|
| 63 |
+
# Get route using OSM
|
| 64 |
+
try:
|
| 65 |
+
G = ox.graph_from_point(
|
| 66 |
+
start_coords,
|
| 67 |
+
dist=max(ox.distance.great_circle_vec(
|
| 68 |
+
start_coords[0], start_coords[1],
|
| 69 |
+
end_coords[0], end_coords[1]
|
| 70 |
+
) * 1.1, 5000)
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
# Find shortest path
|
| 74 |
+
orig_node = ox.distance.nearest_nodes(G, start_coords[1], start_coords[0])
|
| 75 |
+
dest_node = ox.distance.nearest_nodes(G, end_coords[1], end_coords[0])
|
| 76 |
+
route = nx.shortest_path(G, orig_node, dest_node, weight='length')
|
| 77 |
+
|
| 78 |
+
# Get route coordinates
|
| 79 |
+
route_coords = []
|
| 80 |
+
for node in route:
|
| 81 |
+
route_coords.append([G.nodes[node]['y'], G.nodes[node]['x']])
|
| 82 |
+
|
| 83 |
+
# Draw route
|
| 84 |
+
folium.PolyLine(
|
| 85 |
+
route_coords,
|
| 86 |
+
weight=2,
|
| 87 |
+
color='blue',
|
| 88 |
+
opacity=0.8
|
| 89 |
+
).add_to(m)
|
| 90 |
+
|
| 91 |
+
# Find restaurants near route
|
| 92 |
+
midpoint = route_coords[len(route_coords)//2]
|
| 93 |
+
restaurants = find_restaurants(midpoint)
|
| 94 |
+
|
| 95 |
+
# Add restaurant markers
|
| 96 |
+
for idx, rest in restaurants.iterrows():
|
| 97 |
+
if 'name' in rest:
|
| 98 |
+
folium.Marker(
|
| 99 |
+
[rest.geometry.y, rest.geometry.x],
|
| 100 |
+
popup=rest.get('name', 'Restaurant'),
|
| 101 |
+
icon=folium.Icon(color='orange', icon='info-sign')
|
| 102 |
+
).add_to(m)
|
| 103 |
+
|
| 104 |
+
# Display map
|
| 105 |
+
folium_static(m)
|
| 106 |
+
|
| 107 |
+
# Show restaurant list
|
| 108 |
+
st.subheader("Nearby Restaurants")
|
| 109 |
+
rest_df = pd.DataFrame({
|
| 110 |
+
'Name': restaurants.get('name', 'Restaurant'),
|
| 111 |
+
'Cuisine': restaurants.get('cuisine', 'Various')
|
| 112 |
+
})
|
| 113 |
+
st.dataframe(rest_df)
|
| 114 |
+
|
| 115 |
+
except Exception as e:
|
| 116 |
+
st.error(f"Error planning route: {str(e)}")
|
| 117 |
+
else:
|
| 118 |
+
st.error("Couldn't find locations. Please check the addresses.")
|
| 119 |
+
|
| 120 |
+
if __name__ == "__main__":
|
| 121 |
+
create_app()
|