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 plotly.express as px
# Page config
st.set_page_config(layout="wide", page_title="NYC Interactive Map")
# 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)],
'latitude': np.random.uniform(40.65, 40.85, n_trucks),
'longitude': np.random.uniform(-74.1, -73.9, n_trucks),
'borough': np.random.choice(['Manhattan', 'Brooklyn', 'Queens', 'Bronx', 'Staten Island'], n_trucks),
'cuisine': np.random.choice(['Hot Dogs', 'Tacos', 'Halal', 'Ice Cream', 'BBQ'], n_trucks),
'rating': np.random.uniform(3.0, 5.0, n_trucks)
})
# Pedestrian activity (heatmap)
n_ped = 1000
pedestrians = pd.DataFrame({
'latitude': np.random.normal(40.75, 0.1, n_ped),
'longitude': np.random.normal(-73.98, 0.1, n_ped),
'weight': np.random.exponential(1, n_ped)
})
return trucks, pedestrians
# Load data
trucks_df, pedestrians_df = generate_mock_data()
# Sidebar controls
st.sidebar.title("Map Controls")
# Time controls
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="MM/DD/YY"
)
# Borough filter
selected_boroughs = st.sidebar.multiselect(
"Borough Filter",
options=['All'] + list(trucks_df['borough'].unique()),
default=['All']
)
# Map style
map_style = st.sidebar.selectbox(
"Map Style",
options=['light', 'dark', 'satellite'],
index=0
)
# Layer toggles
st.sidebar.subheader("Layers")
show_trucks = st.sidebar.checkbox("Show Food Trucks", True)
show_heatmap = st.sidebar.checkbox("Show Pedestrian Heatmap", True)
# Main content
st.title("NYC Interactive Map")
# Create columns for map and stats
col1, col2 = st.columns([7, 3])
with col1:
# Initial view state
initial_view_state = pdk.ViewState(
latitude=40.7128,
longitude=-74.0060,
zoom=11,
pitch=45
)
# Create layers
layers = []
if show_trucks:
trucks_layer = pdk.Layer(
"ScatterplotLayer",
data=trucks_df,
get_position=['longitude', 'latitude'],
get_color=[255, 0, 0, 160],
get_radius=100,
pickable=True,
opacity=0.8,
stroked=True,
filled=True
)
layers.append(trucks_layer)
if show_heatmap:
heatmap_layer = pdk.Layer(
"HeatmapLayer",
data=pedestrians_df,
get_position=['longitude', 'latitude'],
get_weight='weight',
radiusPixels=60,
opacity=0.6
)
layers.append(heatmap_layer)
# Create deck
deck = pdk.Deck(
map_style=f"mapbox://styles/mapbox/{map_style}-v9",
initial_view_state=initial_view_state,
layers=layers,
tooltip={
"html": "<b>Food Truck:</b> {name}<br/>"
"<b>Cuisine:</b> {cuisine}<br/>"
"<b>Rating:</b> {rating:.1f}⭐",
"style": {
"backgroundColor": "white",
"color": "black"
}
}
)
st.pydeck_chart(deck)
with col2:
st.subheader("Statistics")
# Food truck stats
st.write("Food Trucks by Borough")
borough_counts = trucks_df['borough'].value_counts()
fig = px.pie(values=borough_counts.values,
names=borough_counts.index,
hole=0.3)
st.plotly_chart(fig, use_container_width=True)
# Cuisine distribution
st.write("Cuisine Distribution")
cuisine_counts = trucks_df['cuisine'].value_counts()
fig = px.bar(x=cuisine_counts.index,
y=cuisine_counts.values)
st.plotly_chart(fig, use_container_width=True)
# Average ratings
st.write("Average Ratings by Cuisine")
avg_ratings = trucks_df.groupby('cuisine')['rating'].mean().round(2)
st.dataframe(avg_ratings)
# Footer
st.markdown("---")
st.markdown("Data is simulated for demonstration purposes.")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?