Spaces:
Sleeping
Sleeping
| # main.py | |
| import streamlit as st | |
| import folium | |
| from streamlit_folium import folium_static | |
| import osmnx as ox | |
| import pandas as pd | |
| from datetime import datetime | |
| import networkx as nx | |
| def get_location_coords(location_name): | |
| try: | |
| location = ox.geocode(location_name) | |
| return location[0], location[1] | |
| except: | |
| return None | |
| def find_restaurants(center_point, distance=1000): | |
| tags = {'amenity': 'restaurant'} | |
| restaurants = ox.geometries_from_point( | |
| center_point, tags=tags, dist=distance | |
| ) | |
| return restaurants | |
| def create_app(): | |
| st.title("🗺️ RouteNBite") | |
| st.subheader("Find restaurants along your route!") | |
| col1, col2 = st.columns(2) | |
| with col1: | |
| start = st.text_input("Starting Point (e.g., 'Times Square, New York')") | |
| cuisine = st.multiselect("Cuisine Preferences", | |
| ["Italian", "Asian", "Mexican", "Indian"]) | |
| with col2: | |
| end = st.text_input("Destination") | |
| max_detour = st.slider("Maximum Detour (minutes)", 5, 30, 15) | |
| if st.button("Plan Route"): | |
| if start and end: | |
| # Get coordinates | |
| start_coords = get_location_coords(start) | |
| end_coords = get_location_coords(end) | |
| if start_coords and end_coords: | |
| # Create map centered between start and end | |
| center_lat = (start_coords[0] + end_coords[0]) / 2 | |
| center_lon = (start_coords[1] + end_coords[1]) / 2 | |
| m = folium.Map(location=[center_lat, center_lon], zoom_start=12) | |
| # Add markers for start and end | |
| folium.Marker( | |
| start_coords, | |
| popup='Start', | |
| icon=folium.Icon(color='green') | |
| ).add_to(m) | |
| folium.Marker( | |
| end_coords, | |
| popup='End', | |
| icon=folium.Icon(color='red') | |
| ).add_to(m) | |
| # Get route using OSM | |
| try: | |
| G = ox.graph_from_point( | |
| start_coords, | |
| dist=max(ox.distance.great_circle_vec( | |
| start_coords[0], start_coords[1], | |
| end_coords[0], end_coords[1] | |
| ) * 1.1, 5000) | |
| ) | |
| # Find shortest path | |
| orig_node = ox.distance.nearest_nodes(G, start_coords[1], start_coords[0]) | |
| dest_node = ox.distance.nearest_nodes(G, end_coords[1], end_coords[0]) | |
| route = nx.shortest_path(G, orig_node, dest_node, weight='length') | |
| # Get route coordinates | |
| route_coords = [] | |
| for node in route: | |
| route_coords.append([G.nodes[node]['y'], G.nodes[node]['x']]) | |
| # Draw route | |
| folium.PolyLine( | |
| route_coords, | |
| weight=2, | |
| color='blue', | |
| opacity=0.8 | |
| ).add_to(m) | |
| # Find restaurants near route | |
| midpoint = route_coords[len(route_coords)//2] | |
| restaurants = find_restaurants(midpoint) | |
| # Add restaurant markers | |
| for idx, rest in restaurants.iterrows(): | |
| if 'name' in rest: | |
| folium.Marker( | |
| [rest.geometry.y, rest.geometry.x], | |
| popup=rest.get('name', 'Restaurant'), | |
| icon=folium.Icon(color='orange', icon='info-sign') | |
| ).add_to(m) | |
| # Display map | |
| folium_static(m) | |
| # Show restaurant list | |
| st.subheader("Nearby Restaurants") | |
| rest_df = pd.DataFrame({ | |
| 'Name': restaurants.get('name', 'Restaurant'), | |
| 'Cuisine': restaurants.get('cuisine', 'Various') | |
| }) | |
| st.dataframe(rest_df) | |
| except Exception as e: | |
| st.error(f"Error planning route: {str(e)}") | |
| else: | |
| st.error("Couldn't find locations. Please check the addresses.") | |
| if __name__ == "__main__": | |
| create_app() |