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.
Drop files here
or click to upload
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
# Set page config
st.set_page_config(layout="wide", page_title="NYC Interactive Map")
# Initialize session state
if "map_style" not in st.session_state:
st.session_state.map_style = "mapbox://styles/mapbox/streets-v11"
if "show_trucks" not in st.session_state:
st.session_state.show_trucks = True
if "show_heatmap" not in st.session_state:
st.session_state.show_heatmap = True
if "show_routes" not in st.session_state:
st.session_state.show_routes = False
if "show_taxis" not in st.session_state:
st.session_state.show_taxis = False
# Generate mock data
@st.cache_data
def generate_mock_data():
# Food truck locations
np.random.seed(42)
n_trucks = 100
trucks_df = pd.DataFrame({
'latitude': np.random.normal(40.7128, 0.1, n_trucks),
'longitude': np.random.normal(-74.0060, 0.1, n_trucks),
'name': [f"Food Truck #{i}" for i in range(n_trucks)],
'type': np.random.choice(['Hot Dogs', 'Tacos', 'Halal', 'Ice Cream'], 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 (heatmap)
n_points = 1000
heatmap_df = pd.DataFrame({
'latitude': np.random.normal(40.7128, 0.15, n_points),
'longitude': np.random.normal(-74.0060, 0.15, n_points),
'weight': np.random.exponential(1, n_points)
})
return trucks_df, heatmap_df
trucks_df, heatmap_df = generate_mock_data()
# Sidebar controls
st.sidebar.title("Map Controls")
# Map style selector
map_style = st.sidebar.selectbox(
"Map Style",
["Streets", "Satellite"],
key="map_style_selector"
)
st.session_state.map_style = "mapbox://styles/mapbox/satellite-v9" if map_style == "Satellite" else "mapbox://styles/mapbox/streets-v11"
# Layer toggles
st.sidebar.subheader("Layers")
st.session_state.show_trucks = st.sidebar.checkbox("Food Trucks", value=True)
st.session_state.show_heatmap = st.sidebar.checkbox("Pedestrian Heatmap", value=True)
st.session_state.show_routes = st.sidebar.checkbox("Animated Routes", value=False)
st.session_state.show_taxis = st.sidebar.checkbox("Taxi Clusters", value=False)
# Borough filter
selected_boroughs = st.sidebar.multiselect(
"Filter by Borough",
options=trucks_df['borough'].unique(),
default=trucks_df['borough'].unique()
)
# Filter data
filtered_trucks = trucks_df[trucks_df['borough'].isin(selected_boroughs)]
# Create map layers
layers = []
if st.session_state.show_trucks:
trucks_layer = pdk.Layer(
"ScatterplotLayer",
filtered_trucks,
get_position=['longitude', 'latitude'],
get_color=[255, 0, 0, 160],
get_radius=100,
pickable=True,
)
layers.append(trucks_layer)
if st.session_state.show_heatmap:
heatmap_layer = pdk.Layer(
"HeatmapLayer",
heatmap_df,
get_position=['longitude', 'latitude'],
get_weight='weight',
opacity=0.6,
)
layers.append(heatmap_layer)
# Create the map
st.title("NYC Interactive Map")
col1, col2 = st.columns([3, 1])
with col1:
map_view = pdk.ViewState(
latitude=40.7128,
longitude=-74.0060,
zoom=11,
pitch=0
)
deck = pdk.Deck(
map_style=st.session_state.map_style,
initial_view_state=map_view,
layers=layers,
tooltip={
"html": "<b>Food Truck:</b> {name}<br/>"
"<b>Type:</b> {type}<br/>"
"<b>Rating:</b> {rating:.1f}⭐<br/>"
"<b>Borough:</b> {borough}",
"style": {
"backgroundColor": "white",
"color": "black"
}
}
)
st.pydeck_chart(deck)
# Stats panel
with col2:
st.subheader("Statistics")
# Trucks by borough
st.write("Food Trucks by Borough")
borough_counts = filtered_trucks['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)
# Average ratings
st.write("Average Ratings by Type")
avg_ratings = filtered_trucks.groupby('type')['rating'].mean().sort_values(ascending=True)
fig = px.bar(avg_ratings)
st.plotly_chart(fig, use_container_width=True)
# Summary stats
st.metric("Total Food Trucks", len(filtered_trucks))
st.metric("Average Rating", f"{filtered_trucks['rating'].mean():.1f}⭐")
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?