Create an app showing an interactive NYC map visualization. Mock data: - Food truck locations (100 points) - Pedestrian activity heatmap Features: - Interactive map centered on NYC - Toggle layers: * Food trucks with popups * Pedestrian heatmap * Animated routes * Taxi clusters Controls: - Time slider - Borough filters - Color schemes - Map style (street/satellite) - Heatmap and cluster settings Include zoom/pan and stats panel showing data summaries.
To upload files, please first save the app
import streamlit as st
import pandas as pd
import numpy as np
import pydeck as pdk
from datetime import datetime, timedelta
import folium
from streamlit_folium import folium_static
st.set_page_config(layout="wide", page_title="NYC Map Visualization")
# Generate mock data
@st.cache_data
def generate_mock_data():
# Food truck locations
np.random.seed(42)
n_trucks = 100
trucks = pd.DataFrame({
'name': [f'Food Truck {i}' for i in range(n_trucks)],
'lat': np.random.normal(40.7128, 0.05, n_trucks),
'lon': np.random.normal(-74.0060, 0.05, n_trucks),
'cuisine': np.random.choice(['Mexican', 'Italian', 'Chinese', 'American', 'Indian'], n_trucks),
'rating': np.random.uniform(3.0, 5.0, n_trucks),
'borough': np.random.choice(['Manhattan', 'Brooklyn', 'Queens', 'Bronx', 'Staten Island'], n_trucks)
})
# Pedestrian activity
n_ped = 1000
pedestrians = pd.DataFrame({
'lat': np.random.normal(40.7128, 0.08, n_ped),
'lon': np.random.normal(-74.0060, 0.08, n_ped),
'count': np.random.exponential(100, n_ped)
})
return trucks, pedestrians
# App layout
st.title('NYC Interactive Map')
# Sidebar controls
st.sidebar.header('Map Controls')
# Time slider
time_range = st.sidebar.slider(
'Time Range',
min_value=datetime(2023, 1, 1),
max_value=datetime(2023, 12, 31),
value=(datetime(2023, 6, 1), datetime(2023, 6, 30)),
format='MMM DD'
)
# Borough filter
boroughs = ['Manhattan', 'Brooklyn', 'Queens', 'Bronx', 'Staten Island']
selected_boroughs = st.sidebar.multiselect('Boroughs', boroughs, default=boroughs)
# Map style
map_style = st.sidebar.radio('Map Style', ['Streets', 'Satellite'])
# Layer toggles
st.sidebar.header('Layers')
show_trucks = st.sidebar.checkbox('Food Trucks', True)
show_heatmap = st.sidebar.checkbox('Pedestrian Heatmap', True)
show_routes = st.sidebar.checkbox('Animated Routes', False)
show_clusters = st.sidebar.checkbox('Taxi Clusters', False)
# Get data
trucks_df, pedestrians_df = generate_mock_data()
# Filter data
trucks_df = trucks_df[trucks_df['borough'].isin(selected_boroughs)]
# Create map
col1, col2 = st.columns([3, 1])
with col1:
# Initialize map centered on NYC
m = folium.Map(
location=[40.7128, -74.0060],
zoom_start=12,
tiles='cartodbpositron' if map_style == 'Streets' else 'Stamen Terrain'
)
# Add food truck markers
if show_trucks:
for idx, row in trucks_df.iterrows():
folium.Marker(
[row['lat'], row['lon']],
popup=f"<b>{row['name']}</b><br>Cuisine: {row['cuisine']}<br>Rating: {row['rating']:.1f}",
icon=folium.Icon(color='red', icon='info-sign')
).add_to(m)
# Add heatmap
if show_heatmap:
folium.plugins.HeatMap(
pedestrians_df[['lat', 'lon', 'count']].values.tolist(),
radius=15,
blur=10
).add_to(m)
# Display map
folium_static(m)
# Stats panel
with col2:
st.header('Statistics')
st.subheader('Food Trucks')
st.write(f"Total trucks: {len(trucks_df)}")
st.write("By borough:")
st.write(trucks_df['borough'].value_counts())
st.subheader('Cuisine Types')
st.write(trucks_df['cuisine'].value_counts())
st.subheader('Average Ratings')
st.write(trucks_df.groupby('borough')['rating'].mean().round(2))
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?