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
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({
'latitude': np.random.normal(40.7128, 0.05, n_trucks),
'longitude': np.random.normal(-74.0060, 0.05, n_trucks),
'name': [f'Food Truck {i}' for i in range(n_trucks)],
'cuisine': np.random.choice(['Mexican', 'Italian', 'Asian', 'American'], 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 (grid points for heatmap)
n_points = 1000
pedestrians = pd.DataFrame({
'latitude': np.random.normal(40.7128, 0.1, n_points),
'longitude': np.random.normal(-74.0060, 0.1, n_points),
'density': np.random.exponential(100, n_points)
})
return trucks, pedestrians
# Load data
trucks_df, pedestrians_df = generate_mock_data()
# Sidebar controls
st.sidebar.title("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="MM/DD/YY"
)
# Borough filter
boroughs = st.sidebar.multiselect(
"Select Boroughs",
['Manhattan', 'Brooklyn', 'Queens', 'Bronx', 'Staten Island'],
default=['Manhattan']
)
# Layer toggles
st.sidebar.subheader("Map Layers")
show_trucks = st.sidebar.checkbox("Show Food Trucks", True)
show_heatmap = st.sidebar.checkbox("Show Pedestrian Heatmap", True)
show_routes = st.sidebar.checkbox("Show Animated Routes", False)
show_clusters = st.sidebar.checkbox("Show Taxi Clusters", False)
# Map style
map_style = st.sidebar.selectbox(
"Map Style",
["mapbox://styles/mapbox/streets-v11",
"mapbox://styles/mapbox/satellite-v9",
"mapbox://styles/mapbox/dark-v10"]
)
# Heatmap settings
if show_heatmap:
st.sidebar.subheader("Heatmap Settings")
heatmap_radius = st.sidebar.slider("Heatmap Radius", 50, 300, 100)
heatmap_intensity = st.sidebar.slider("Heatmap Intensity", 0.1, 2.0, 1.0)
# Main content
st.title("NYC Interactive Map")
# Create map layers
layers = []
if show_trucks:
trucks_layer = pdk.Layer(
"ScatterplotLayer",
data=trucks_df[trucks_df['borough'].isin(boroughs)],
get_position=['longitude', 'latitude'],
get_radius=50,
get_fill_color=[255, 0, 0, 140],
pickable=True,
)
layers.append(trucks_layer)
if show_heatmap:
heatmap_layer = pdk.Layer(
"HeatmapLayer",
data=pedestrians_df,
get_position=['longitude', 'latitude'],
get_weight='density',
radiusPixels=heatmap_radius,
intensity=heatmap_intensity,
opacity=0.7,
)
layers.append(heatmap_layer)
# Create map view
view_state = pdk.ViewState(
latitude=40.7128,
longitude=-74.0060,
zoom=11,
pitch=45,
)
# Render map
r = pdk.Deck(
layers=layers,
initial_view_state=view_state,
map_style=map_style,
)
st.pydeck_chart(r)
# Stats panel
col1, col2 = st.columns(2)
with col1:
st.subheader("Food Truck Statistics")
if show_trucks:
filtered_trucks = trucks_df[trucks_df['borough'].isin(boroughs)]
st.write(f"Total trucks: {len(filtered_trucks)}")
# Cuisine distribution
fig = px.pie(filtered_trucks, names='cuisine', title='Cuisine Distribution')
st.plotly_chart(fig)
with col2:
st.subheader("Borough Statistics")
if show_trucks:
borough_stats = filtered_trucks.groupby('borough').agg({
'rating': ['mean', 'count']
}).round(2)
borough_stats.columns = ['Avg Rating', 'Number of Trucks']
st.dataframe(borough_stats)
# Map instructions
with st.expander("How to use the map"):
st.write("""
- Use the sidebar controls to customize the map view
- Toggle different layers on/off
- Adjust time range and borough filters
- Click on food trucks to see details
- Zoom and pan the map using mouse or touchpad
- Adjust heatmap settings to visualize pedestrian density
""")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?