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
# Set page config
st.set_page_config(layout="wide", page_title="NYC Map Visualization")
# ----- Data Generation -----
@st.cache_data
def generate_mock_data():
# Food truck locations (Manhattan-centered)
n_trucks = 100
trucks = pd.DataFrame({
'latitude': np.random.normal(40.7580, 0.02, n_trucks),
'longitude': np.random.normal(-73.9855, 0.02, 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': 'Manhattan' # Simplified for demo
})
# Pedestrian activity (heatmap)
n_points = 1000
pedestrians = pd.DataFrame({
'latitude': np.random.normal(40.7580, 0.03, n_points),
'longitude': np.random.normal(-73.9855, 0.03, n_points),
'weight': np.random.exponential(100, n_points)
})
return trucks, pedestrians
# Generate data
trucks_df, pedestrians_df = generate_mock_data()
# ----- Sidebar Controls -----
st.sidebar.title("Map Controls")
# Time slider
time_range = st.sidebar.slider(
"Time of Day",
min_value=datetime(2023,1,1,0,0),
max_value=datetime(2023,1,1,23,59),
value=datetime(2023,1,1,12,0),
format="HH:mm"
)
# Map style
map_style = st.sidebar.selectbox(
"Map Style",
["mapbox://styles/mapbox/light-v9",
"mapbox://styles/mapbox/dark-v9",
"mapbox://styles/mapbox/satellite-v9"]
)
# Layer toggles
st.sidebar.subheader("Layers")
show_trucks = st.sidebar.checkbox("Food Trucks", True)
show_heatmap = st.sidebar.checkbox("Pedestrian Heatmap", True)
# Heatmap settings
if show_heatmap:
heatmap_intensity = st.sidebar.slider("Heatmap Intensity", 0.1, 2.0, 1.0)
heatmap_radius = st.sidebar.slider("Heatmap Radius", 50, 300, 100)
# ----- Main Content -----
st.title("NYC Interactive Map")
# Create map layers
layers = []
if show_trucks:
trucks_layer = pdk.Layer(
"ScatterplotLayer",
data=trucks_df,
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="weight",
intensity=heatmap_intensity,
radius_pixels=heatmap_radius
)
layers.append(heatmap_layer)
# Create the map
map_view = pdk.ViewState(
latitude=40.7580,
longitude=-73.9855,
zoom=12,
pitch=0
)
r = pdk.Deck(
layers=layers,
initial_view_state=map_view,
map_style=map_style,
tooltip={
"html": "<b>Food Truck:</b> {name}<br/>"
"<b>Type:</b> {type}<br/>"
"<b>Rating:</b> {rating:.1f}⭐",
"style": {
"backgroundColor": "white",
"color": "black"
}
}
)
# Display the map
st.pydeck_chart(r)
# Stats Panel
col1, col2, col3 = st.columns(3)
with col1:
st.metric("Total Food Trucks", len(trucks_df))
with col2:
st.metric("Average Rating", f"{trucks_df['rating'].mean():.1f}⭐")
with col3:
st.metric("Active Areas", f"{len(pedestrians_df):,}")
# Additional visualizations
st.subheader("Food Truck Distribution")
fig = px.histogram(trucks_df, x='type', title='Food Trucks by Type')
st.plotly_chart(fig, use_container_width=True)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?