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
import random
# Set 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 (Manhattan-centered coordinates)
n_trucks = 100
trucks_df = pd.DataFrame({
'latitude': np.random.normal(40.7589, 0.02, n_trucks),
'longitude': np.random.normal(-73.9851, 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', 'Coffee'], n_trucks),
'rating': np.random.uniform(3.0, 5.0, n_trucks),
'last_active': [datetime.now() - timedelta(minutes=random.randint(0, 360)) for _ in range(n_trucks)]
})
# Pedestrian activity (grid points)
lat_range = np.linspace(40.7, 40.8, 50)
lon_range = np.linspace(-74.02, -73.95, 50)
grid_points = []
for lat in lat_range:
for lon in lon_range:
grid_points.append({
'latitude': lat,
'longitude': lon,
'activity': np.random.exponential(100)
})
pedestrian_df = pd.DataFrame(grid_points)
return trucks_df, pedestrian_df
# Load data
trucks_df, pedestrian_df = generate_mock_data()
# Sidebar controls
st.sidebar.title("Map Controls")
# Time filter
time_range = st.sidebar.slider(
"Time Window (minutes ago)",
min_value=0,
max_value=360,
value=(0, 360)
)
# Layer toggles
st.sidebar.subheader("Layers")
show_trucks = st.sidebar.checkbox("Food Trucks", value=True)
show_heatmap = st.sidebar.checkbox("Pedestrian Activity", value=True)
# Map style
map_style = st.sidebar.selectbox(
"Map Style",
["mapbox://styles/mapbox/light-v9",
"mapbox://styles/mapbox/dark-v9",
"mapbox://styles/mapbox/satellite-v9"]
)
# Heatmap settings
if show_heatmap:
st.sidebar.subheader("Heatmap Settings")
heatmap_radius = st.sidebar.slider("Radius", 10, 100, 50)
heatmap_intensity = st.sidebar.slider("Intensity", 0.1, 2.0, 1.0)
# Main content
st.title("NYC Interactive Map")
# Filter data based on time range
filtered_trucks = trucks_df[
(trucks_df['last_active'] >= datetime.now() - timedelta(minutes=time_range[1])) &
(trucks_df['last_active'] <= datetime.now() - timedelta(minutes=time_range[0]))
]
# Create layers
layers = []
if show_trucks:
trucks_layer = pdk.Layer(
"ScatterplotLayer",
filtered_trucks,
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",
pedestrian_df,
get_position=["longitude", "latitude"],
get_weight="activity",
radiusPixels=heatmap_radius,
intensity=heatmap_intensity
)
layers.append(heatmap_layer)
# Create the map
view_state = pdk.ViewState(
latitude=40.7589,
longitude=-73.9851,
zoom=12,
pitch=0
)
r = pdk.Deck(
layers=layers,
initial_view_state=view_state,
map_style=map_style,
tooltip={
"html": "<b>Food Truck:</b> {name}<br/>"
"<b>Type:</b> {type}<br/>"
"<b>Rating:</b> {rating:.1f}⭐<br/>"
"<b>Last Active:</b> {last_active}",
"style": {
"backgroundColor": "steelblue",
"color": "white"
}
}
)
# Create two columns
col1, col2 = st.columns([3, 1])
with col1:
st.pydeck_chart(r)
with col2:
st.subheader("Statistics")
# Active trucks
st.metric(
"Active Food Trucks",
len(filtered_trucks),
f"{len(filtered_trucks) - len(trucks_df)}"
)
# Average rating
avg_rating = filtered_trucks['rating'].mean()
st.metric(
"Average Rating",
f"{avg_rating:.1f}⭐"
)
# Most common type
most_common = filtered_trucks['type'].mode().iloc[0]
st.metric(
"Most Popular Type",
most_common
)
# Pedestrian activity
if show_heatmap:
total_activity = pedestrian_df['activity'].sum()
st.metric(
"Total Pedestrian Activity",
f"{total_activity:,.0f}"
)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?