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 Map Visualization")
# Generate mock data
def generate_mock_data():
# Food truck locations around NYC
np.random.seed(42)
n_trucks = 100
# Roughly NYC bounds
nyc_bounds = {
"lat": (40.4774, 40.9176),
"lon": (-74.2591, -73.7004)
}
food_trucks = pd.DataFrame({
"lat": np.random.uniform(nyc_bounds["lat"][0], nyc_bounds["lat"][1], n_trucks),
"lon": np.random.uniform(nyc_bounds["lon"][0], nyc_bounds["lon"][1], n_trucks),
"name": [f"Food Truck #{i}" for i in range(n_trucks)],
"rating": np.random.uniform(3.0, 5.0, n_trucks),
"cuisine": np.random.choice(["Hot Dogs", "Tacos", "Halal", "Ice Cream", "Pizza"], n_trucks),
"borough": np.random.choice(["Manhattan", "Brooklyn", "Queens", "Bronx", "Staten Island"], n_trucks)
})
# Generate pedestrian activity heatmap data
n_points = 1000
pedestrian_data = pd.DataFrame({
"lat": np.random.uniform(nyc_bounds["lat"][0], nyc_bounds["lat"][1], n_points),
"lon": np.random.uniform(nyc_bounds["lon"][0], nyc_bounds["lon"][1], n_points),
"weight": np.random.exponential(1, n_points)
})
return food_trucks, pedestrian_data
# Initialize session state
if 'food_trucks' not in st.session_state:
st.session_state.food_trucks, st.session_state.pedestrian_data = generate_mock_data()
# Sidebar controls
st.sidebar.title("Map Controls")
# Layer toggles
st.sidebar.subheader("Layers")
show_trucks = st.sidebar.checkbox("Show Food Trucks", True)
show_heatmap = st.sidebar.checkbox("Show Pedestrian Heatmap", True)
# Borough filter
boroughs = st.sidebar.multiselect(
"Filter Boroughs",
["Manhattan", "Brooklyn", "Queens", "Bronx", "Staten Island"],
["Manhattan", "Brooklyn", "Queens", "Bronx", "Staten Island"]
)
# Map style
map_style = st.sidebar.selectbox(
"Map Style",
["mapbox://styles/mapbox/streets-v11", "mapbox://styles/mapbox/satellite-v9"]
)
# Heatmap settings
st.sidebar.subheader("Heatmap Settings")
radius = st.sidebar.slider("Heatmap Radius", 50, 300, 100)
intensity = st.sidebar.slider("Heatmap Intensity", 0.1, 2.0, 1.0)
# Main content
st.title("NYC Food Trucks & Pedestrian Activity")
# Filter data based on borough selection
filtered_trucks = st.session_state.food_trucks[
st.session_state.food_trucks['borough'].isin(boroughs)
]
# Create map layers
layers = []
if show_heatmap:
heatmap_layer = pdk.Layer(
"HeatmapLayer",
data=st.session_state.pedestrian_data,
opacity=0.9,
get_position=["lon", "lat"],
get_weight="weight",
radius_pixels=radius,
intensity=intensity,
threshold=0.05,
)
layers.append(heatmap_layer)
if show_trucks:
truck_layer = pdk.Layer(
"ScatterplotLayer",
data=filtered_trucks,
get_position=["lon", "lat"],
get_color=[255, 0, 0, 160],
get_radius=50,
pickable=True,
)
layers.append(truck_layer)
# Create the map view
view_state = pdk.ViewState(
latitude=40.7128,
longitude=-74.0060,
zoom=11,
pitch=0,
)
# Render the map
r = pdk.Deck(
layers=layers,
initial_view_state=view_state,
map_style=map_style,
tooltip={
"html": "<b>Food Truck:</b> {name}<br/>"
"<b>Cuisine:</b> {cuisine}<br/>"
"<b>Rating:</b> {rating:.1f}⭐<br/>"
"<b>Borough:</b> {borough}",
"style": {
"backgroundColor": "white",
"color": "black"
}
}
)
st.pydeck_chart(r)
# Stats Panel
col1, col2, col3 = st.columns(3)
with col1:
st.metric(
"Total Food Trucks",
len(filtered_trucks)
)
with col2:
avg_rating = filtered_trucks['rating'].mean()
st.metric(
"Average Rating",
f"{avg_rating:.2f}⭐"
)
with col3:
top_cuisine = filtered_trucks['cuisine'].mode().iloc[0]
st.metric(
"Most Popular Cuisine",
top_cuisine
)
# Show cuisine distribution
fig = px.pie(
filtered_trucks,
names='cuisine',
title='Cuisine Distribution'
)
st.plotly_chart(fig, use_container_width=True)
# Show borough distribution
fig = px.bar(
filtered_trucks['borough'].value_counts().reset_index(),
x='index',
y='borough',
title='Food Trucks by Borough',
labels={'index': 'Borough', 'borough': 'Count'}
)
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?