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 pydeck as pdk
import pandas as pd
import numpy as np
from datetime import datetime, timedelta
# Initialize 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 around NYC
n_trucks = 100
trucks_df = 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)],
'rating': np.random.uniform(3.0, 5.0, n_trucks),
'cuisine': np.random.choice(['Mexican', 'Italian', 'American', 'Asian', 'Mediterranean'], 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.1, n_points),
'longitude': np.random.normal(-74.0060, 0.1, n_points),
'weight': np.random.exponential(1, n_points)
})
return trucks_df, heatmap_df
# Load data
trucks_df, heatmap_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
selected_boroughs = st.sidebar.multiselect(
"Filter by Borough",
options=['Manhattan', 'Brooklyn', 'Queens', 'Bronx', 'Staten Island'],
default=['Manhattan', 'Brooklyn']
)
# 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)
# 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
st.sidebar.subheader("Heatmap Settings")
radius = st.sidebar.slider("Heatmap Radius", 20, 100, 50)
intensity = st.sidebar.slider("Heatmap Intensity", 1, 10, 5)
# Filter data based on selections
filtered_trucks = trucks_df[trucks_df['borough'].isin(selected_boroughs)]
# Create map layers
layers = []
if show_trucks:
trucks_layer = pdk.Layer(
"ScatterplotLayer",
filtered_trucks,
get_position=['longitude', 'latitude'],
get_color=[255, 0, 0, 160],
get_radius=50,
pickable=True
)
layers.append(trucks_layer)
if show_heatmap:
heatmap_layer = pdk.Layer(
"HeatmapLayer",
heatmap_df,
get_position=['longitude', 'latitude'],
get_weight='weight',
radiusPixels=radius,
intensity=intensity,
threshold=0.05
)
layers.append(heatmap_layer)
# Create the map view
view_state = pdk.ViewState(
latitude=40.7128,
longitude=-74.0060,
zoom=11,
pitch=0
)
# Main content
st.title("NYC Interactive Map")
# Create two columns
col1, col2 = st.columns([3, 1])
with col1:
# Render the map
st.pydeck_chart(pdk.Deck(
map_style=map_style,
initial_view_state=view_state,
layers=layers,
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"
}
}
))
with col2:
# Stats panel
st.subheader("Statistics")
# Food truck stats
st.write("Food Trucks:")
st.write(f"Total trucks: {len(filtered_trucks)}")
# Average rating by borough
st.write("\nAverage Rating by Borough:")
borough_stats = filtered_trucks.groupby('borough')['rating'].mean().round(2)
st.dataframe(borough_stats)
# Cuisine distribution
st.write("\nCuisine Distribution:")
cuisine_dist = filtered_trucks['cuisine'].value_counts()
st.bar_chart(cuisine_dist)
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?