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
import plotly.express as px
# 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
np.random.seed(42)
n_trucks = 100
truck_data = 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', 'Asian', 'American'], n_trucks),
'borough': np.random.choice(['Manhattan', 'Brooklyn', 'Queens', 'Bronx', 'Staten Island'], n_trucks)
})
# Pedestrian activity
n_points = 1000
ped_data = pd.DataFrame({
'latitude': np.random.normal(40.7128, 0.08, n_points),
'longitude': np.random.normal(-74.0060, 0.08, n_points),
'weight': np.random.exponential(100, n_points)
})
return truck_data, ped_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='MMM DD'
)
# Borough filter
boroughs = ['Manhattan', 'Brooklyn', 'Queens', 'Bronx', 'Staten Island']
selected_boroughs = st.sidebar.multiselect('Boroughs', boroughs, default=boroughs)
# 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', value=True)
show_heatmap = st.sidebar.checkbox('Pedestrian Heatmap', value=True)
show_routes = st.sidebar.checkbox('Animated Routes', value=False)
show_clusters = st.sidebar.checkbox('Taxi Clusters', value=False)
# Get data
truck_data, ped_data = generate_mock_data()
# Filter data based on boroughs
truck_data = truck_data[truck_data['borough'].isin(selected_boroughs)]
# Create layers
layers = []
if show_heatmap:
heatmap_layer = pdk.Layer(
'HeatmapLayer',
ped_data,
opacity=0.6,
get_position=['longitude', 'latitude'],
get_weight='weight',
threshold=0.3,
)
layers.append(heatmap_layer)
if show_trucks:
truck_layer = pdk.Layer(
'ScatterplotLayer',
truck_data,
get_position=['longitude', 'latitude'],
get_color=[255, 0, 0, 160],
get_radius=100,
pickable=True,
)
layers.append(truck_layer)
# Create the deck
view_state = pdk.ViewState(
latitude=40.7128,
longitude=-74.0060,
zoom=11,
pitch=45
)
deck = 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': 'steelblue',
'color': 'white'
}
}
)
# Layout
st.title('NYC Interactive Map')
# Map column and stats column
col1, col2 = st.columns([3, 1])
with col1:
st.pydeck_chart(deck)
with col2:
st.subheader('Statistics')
# Food truck stats
st.write('Food Trucks by Borough')
borough_counts = truck_data['borough'].value_counts()
fig = px.pie(values=borough_counts.values,
names=borough_counts.index,
title='Distribution by Borough')
st.plotly_chart(fig, use_container_width=True)
# Cuisine stats
st.write('Cuisine Types')
cuisine_counts = truck_data['cuisine'].value_counts()
fig = px.bar(x=cuisine_counts.index,
y=cuisine_counts.values,
title='Distribution by Cuisine')
st.plotly_chart(fig, use_container_width=True)
# Average rating
st.metric('Average Rating', f"{truck_data['rating'].mean():.2f}⭐")
# Total trucks
st.metric('Total Food Trucks', len(truck_data))
Hi! I can help you with any questions about Streamlit and Python. What would you like to know?