Spaces:
Sleeping
Sleeping
Update src/streamlit_app.py
Browse files- src/streamlit_app.py +21 -253
src/streamlit_app.py
CHANGED
|
@@ -1,264 +1,32 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
-
|
| 4 |
-
import
|
| 5 |
-
import folium
|
| 6 |
-
from streamlit_folium import st_folium
|
| 7 |
-
import os
|
| 8 |
-
import streamlit as st
|
| 9 |
-
import pandas as pd
|
| 10 |
-
import osmnx as ox
|
| 11 |
-
from streamlit_folium import st_folium
|
| 12 |
-
import folium
|
| 13 |
-
ox.settings.use_cache = False
|
| 14 |
-
st.set_page_config(layout="wide")
|
| 15 |
-
st.title("π§ Dubai Digital Twin β Interactive 2D Map with Folium")
|
| 16 |
-
|
| 17 |
-
# --- Sidebar Layer Controls ---
|
| 18 |
-
st.sidebar.title("πΊοΈ Layer Toggles")
|
| 19 |
-
show_malls = st.sidebar.checkbox("Show Malls", True)
|
| 20 |
-
show_stores = st.sidebar.checkbox("Show Stores in Malls", True)
|
| 21 |
-
show_footfall = st.sidebar.checkbox("Show Footfall", True)
|
| 22 |
-
show_parks = st.sidebar.checkbox("Show Parks", True)
|
| 23 |
-
show_metro_lines = st.sidebar.checkbox("Show Metro Lines", True)
|
| 24 |
-
show_metro_stations = st.sidebar.checkbox("Show Metro Stations", True)
|
| 25 |
-
|
| 26 |
-
# --- Legend ---
|
| 27 |
-
st.sidebar.markdown("---")
|
| 28 |
-
st.sidebar.markdown("### π Legend")
|
| 29 |
-
st.sidebar.markdown("- ποΈ Malls: Blue")
|
| 30 |
-
st.sidebar.markdown("- π¬ Stores: Purple")
|
| 31 |
-
st.sidebar.markdown("- π³ Parks: Green")
|
| 32 |
-
st.sidebar.markdown("- π Footfall: Red")
|
| 33 |
-
st.sidebar.markdown("- π Metro Lines: Blue Line")
|
| 34 |
-
st.sidebar.markdown("- π Metro Stations: Yellow Dot")
|
| 35 |
-
|
| 36 |
-
# --- Load Dubai Boundary ---
|
| 37 |
-
st.subheader("π Getting Dubai boundary...")
|
| 38 |
-
try:
|
| 39 |
-
dubai = ox.geocode_to_gdf("Dubai, United Arab Emirates")
|
| 40 |
-
dubai_polygon = dubai.geometry.values[0]
|
| 41 |
-
bounds = dubai_polygon.bounds
|
| 42 |
-
bbox = (bounds[0], bounds[1], bounds[2], bounds[3])
|
| 43 |
-
st.success("β
Dubai boundary fetched.")
|
| 44 |
-
except Exception as e:
|
| 45 |
-
st.error(f"β Boundary fetch failed: {e}")
|
| 46 |
-
st.stop()
|
| 47 |
-
|
| 48 |
-
# --- Data Fetch with Status ---
|
| 49 |
-
try:
|
| 50 |
-
with st.spinner("ποΈ Loading malls..."):
|
| 51 |
-
malls = ox.features_from_bbox(bbox, tags={"shop": "mall"})
|
| 52 |
-
|
| 53 |
-
with st.spinner("π¬ Loading stores..."):
|
| 54 |
-
stores = ox.features_from_bbox(bbox, tags={"shop": True})
|
| 55 |
-
|
| 56 |
-
with st.spinner("π³ Loading parks..."):
|
| 57 |
-
parks = ox.features_from_bbox(bbox, tags={"leisure": "park"})
|
| 58 |
-
|
| 59 |
-
with st.spinner("π Loading metro stations..."):
|
| 60 |
-
metro_stations = ox.features_from_bbox(bbox, tags={"railway": "station"})
|
| 61 |
-
|
| 62 |
-
with st.spinner("π Loading metro lines..."):
|
| 63 |
-
metro_lines = ox.features_from_bbox(bbox, tags={"railway": ["subway", "light_rail"]})
|
| 64 |
-
|
| 65 |
-
with st.spinner("π Loading footfall data..."):
|
| 66 |
-
footfall = pd.read_excel("data/jj_with_footfall.xlsx")
|
| 67 |
-
footfall.dropna(subset=["latitude", "longitude"], inplace=True)
|
| 68 |
-
|
| 69 |
-
st.success("β
All features loaded.")
|
| 70 |
-
except Exception as e:
|
| 71 |
-
st.warning(f"β οΈ Data load failed: {e}")
|
| 72 |
-
|
| 73 |
-
# --- Create Map ---
|
| 74 |
-
st.subheader("πΊοΈ Dubai Map (2D - Folium)")
|
| 75 |
-
fmap = folium.Map(location=[25.2048, 55.2708], zoom_start=12)
|
| 76 |
-
|
| 77 |
-
# --- Malls ---
|
| 78 |
-
if show_malls:
|
| 79 |
-
for _, row in malls.iterrows():
|
| 80 |
-
if row.geometry.geom_type == "Point":
|
| 81 |
-
folium.Marker(
|
| 82 |
-
location=[row.geometry.y, row.geometry.x],
|
| 83 |
-
popup=f"Mall: {row.get('name', 'Unnamed')}",
|
| 84 |
-
tooltip=row.get('name', 'Mall'),
|
| 85 |
-
icon=folium.Icon(color="blue", icon="shopping-cart", prefix="fa")
|
| 86 |
-
).add_to(fmap)
|
| 87 |
-
|
| 88 |
-
# --- Stores ---
|
| 89 |
-
if show_stores:
|
| 90 |
-
for _, row in stores.iterrows():
|
| 91 |
-
if row.geometry.geom_type == "Point":
|
| 92 |
-
folium.Marker(
|
| 93 |
-
location=[row.geometry.y, row.geometry.x],
|
| 94 |
-
popup=f"Store: {row.get('name', 'Unnamed Store')}",
|
| 95 |
-
tooltip=row.get('name', 'Store'),
|
| 96 |
-
icon=folium.Icon(color="purple", icon="shopping-bag", prefix="fa")
|
| 97 |
-
).add_to(fmap)
|
| 98 |
-
|
| 99 |
-
# --- Parks ---
|
| 100 |
-
if show_parks:
|
| 101 |
-
for _, row in parks.iterrows():
|
| 102 |
-
if hasattr(row.geometry, 'y') and hasattr(row.geometry, 'x'):
|
| 103 |
-
folium.Marker(
|
| 104 |
-
location=[row.geometry.y, row.geometry.x],
|
| 105 |
-
popup=f"Park: {row.get('name', 'Unnamed')}",
|
| 106 |
-
tooltip=row.get('name', 'Park'),
|
| 107 |
-
icon=folium.Icon(color="green", icon="tree", prefix="fa")
|
| 108 |
-
).add_to(fmap)
|
| 109 |
-
|
| 110 |
-
# --- Metro Stations ---
|
| 111 |
-
if show_metro_stations:
|
| 112 |
-
for _, row in metro_stations.iterrows():
|
| 113 |
-
if hasattr(row.geometry, 'y') and hasattr(row.geometry, 'x'):
|
| 114 |
-
folium.Marker(
|
| 115 |
-
location=[row.geometry.y, row.geometry.x],
|
| 116 |
-
popup=f"Metro Station: {row.get('name', 'Unnamed')}",
|
| 117 |
-
tooltip=row.get('name', 'Metro'),
|
| 118 |
-
icon=folium.Icon(color="orange", icon="train", prefix="fa")
|
| 119 |
-
).add_to(fmap)
|
| 120 |
-
|
| 121 |
-
# --- Metro Lines ---
|
| 122 |
-
if show_metro_lines:
|
| 123 |
-
for _, row in metro_lines.iterrows():
|
| 124 |
-
if row.geometry.geom_type == "LineString":
|
| 125 |
-
folium.PolyLine(
|
| 126 |
-
locations=[(y, x) for x, y in row.geometry.coords],
|
| 127 |
-
color="blue",
|
| 128 |
-
weight=4,
|
| 129 |
-
opacity=0.7
|
| 130 |
-
).add_to(fmap)
|
| 131 |
-
|
| 132 |
-
# --- Footfall ---
|
| 133 |
-
if show_footfall:
|
| 134 |
-
for idx, row in footfall.iterrows():
|
| 135 |
-
folium.Marker(
|
| 136 |
-
location=[row["latitude"], row["longitude"]],
|
| 137 |
-
popup=f"Footfall Point {idx}",
|
| 138 |
-
tooltip=f"Footfall {idx}",
|
| 139 |
-
icon=folium.Icon(color="red", icon="circle", prefix="fa")
|
| 140 |
-
).add_to(fmap)
|
| 141 |
-
|
| 142 |
-
# --- Show map ---
|
| 143 |
-
st_folium(fmap, width=1200, height=700)
|
| 144 |
|
| 145 |
st.set_page_config(layout="wide")
|
| 146 |
-
st.title("
|
| 147 |
-
|
| 148 |
-
# --- Sidebar Layer Controls ---
|
| 149 |
-
st.sidebar.title("πΊοΈ Layer Toggles")
|
| 150 |
-
show_malls = st.sidebar.checkbox("Show Malls", True)
|
| 151 |
-
show_stores = st.sidebar.checkbox("Show Stores in Malls", True)
|
| 152 |
-
show_footfall = st.sidebar.checkbox("Show Footfall", True)
|
| 153 |
-
show_parks = st.sidebar.checkbox("Show Parks", True)
|
| 154 |
-
show_metro_lines = st.sidebar.checkbox("Show Metro Lines", True)
|
| 155 |
-
show_metro_stations = st.sidebar.checkbox("Show Metro Stations", True)
|
| 156 |
-
|
| 157 |
-
# --- Legend ---
|
| 158 |
-
st.sidebar.markdown("---")
|
| 159 |
-
st.sidebar.markdown("### π Legend")
|
| 160 |
-
st.sidebar.markdown("- π¬ Malls: Blue")
|
| 161 |
-
st.sidebar.markdown("- ποΈ Stores: Purple")
|
| 162 |
-
st.sidebar.markdown("- π³ Parks: Green")
|
| 163 |
-
st.sidebar.markdown("- π Footfall: Red (Draggable)")
|
| 164 |
-
st.sidebar.markdown("- π Metro Lines: Blue Line")
|
| 165 |
-
st.sidebar.markdown("- π Metro Stations: Yellow Dot")
|
| 166 |
|
| 167 |
-
# ---
|
| 168 |
-
st.
|
| 169 |
try:
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
st.success("β
Dubai boundary fetched.")
|
| 176 |
except Exception as e:
|
| 177 |
-
st.error(f"β
|
| 178 |
st.stop()
|
| 179 |
|
| 180 |
-
# ---
|
|
|
|
| 181 |
try:
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
footfall.dropna(subset=["latitude", "longitude"], inplace=True)
|
| 190 |
except Exception as e:
|
| 191 |
-
st.
|
| 192 |
-
footfall = pd.DataFrame(columns=["latitude", "longitude"])
|
| 193 |
-
|
| 194 |
-
# --- Render Folium Map ---
|
| 195 |
-
st.subheader("πΊοΈ Dubai Map (2D - Folium)")
|
| 196 |
-
fmap = folium.Map(location=[25.2048, 55.2708], zoom_start=12)
|
| 197 |
-
|
| 198 |
-
# Malls
|
| 199 |
-
if show_malls:
|
| 200 |
-
for _, row in malls.iterrows():
|
| 201 |
-
if row.geometry.geom_type == "Point":
|
| 202 |
-
folium.Marker(
|
| 203 |
-
location=[row.geometry.y, row.geometry.x],
|
| 204 |
-
tooltip=row.get('name', 'Mall'),
|
| 205 |
-
icon=folium.Icon(color="blue", icon="shopping-cart", prefix="fa")
|
| 206 |
-
).add_to(fmap)
|
| 207 |
-
|
| 208 |
-
# Stores
|
| 209 |
-
if show_stores:
|
| 210 |
-
for _, row in stores.iterrows():
|
| 211 |
-
if row.geometry.geom_type == "Point":
|
| 212 |
-
folium.Marker(
|
| 213 |
-
location=[row.geometry.y, row.geometry.x],
|
| 214 |
-
tooltip=row.get('name', 'Store'),
|
| 215 |
-
icon=folium.Icon(color="purple", icon="shopping-bag", prefix="fa")
|
| 216 |
-
).add_to(fmap)
|
| 217 |
-
|
| 218 |
-
# Parks
|
| 219 |
-
if show_parks:
|
| 220 |
-
for _, row in parks.iterrows():
|
| 221 |
-
if hasattr(row.geometry, 'y') and hasattr(row.geometry, 'x'):
|
| 222 |
-
folium.Marker(
|
| 223 |
-
location=[row.geometry.y, row.geometry.x],
|
| 224 |
-
tooltip=row.get('name', 'Park'),
|
| 225 |
-
icon=folium.Icon(color="green", icon="tree", prefix="fa")
|
| 226 |
-
).add_to(fmap)
|
| 227 |
-
|
| 228 |
-
# Metro Stations
|
| 229 |
-
if show_metro_stations:
|
| 230 |
-
for _, row in metro_stations.iterrows():
|
| 231 |
-
if hasattr(row.geometry, 'y') and hasattr(row.geometry, 'x'):
|
| 232 |
-
folium.Marker(
|
| 233 |
-
location=[row.geometry.y, row.geometry.x],
|
| 234 |
-
tooltip=row.get('name', 'Metro'),
|
| 235 |
-
icon=folium.Icon(color="orange", icon="train", prefix="fa")
|
| 236 |
-
).add_to(fmap)
|
| 237 |
-
|
| 238 |
-
# Metro Lines
|
| 239 |
-
if show_metro_lines:
|
| 240 |
-
for _, row in metro_lines.iterrows():
|
| 241 |
-
if row.geometry.geom_type == "LineString":
|
| 242 |
-
folium.PolyLine(
|
| 243 |
-
locations=[(y, x) for x, y in row.geometry.coords],
|
| 244 |
-
color="blue",
|
| 245 |
-
weight=4,
|
| 246 |
-
opacity=0.7
|
| 247 |
-
).add_to(fmap)
|
| 248 |
-
|
| 249 |
-
# Footfall - Draggable
|
| 250 |
-
if show_footfall:
|
| 251 |
-
for idx, row in footfall.iterrows():
|
| 252 |
-
folium.Marker(
|
| 253 |
-
location=[row["latitude"], row["longitude"]],
|
| 254 |
-
tooltip=f"Footfall {idx}",
|
| 255 |
-
draggable=True,
|
| 256 |
-
icon=folium.Icon(color="red", icon="circle", prefix="fa")
|
| 257 |
-
).add_to(fmap)
|
| 258 |
-
|
| 259 |
-
# Render the map
|
| 260 |
-
result = st_folium(fmap, width=1200, height=700)
|
| 261 |
-
|
| 262 |
-
# Option to refresh
|
| 263 |
-
if st.sidebar.button("π Refresh Map"):
|
| 264 |
-
st.rerun()
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
import pandas as pd
|
| 3 |
+
from keplergl import KeplerGl
|
| 4 |
+
import streamlit.components.v1 as components
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
st.set_page_config(layout="wide")
|
| 7 |
+
st.title("π Dubai Footfall β Kepler.gl Interactive Map")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 8 |
|
| 9 |
+
# --- Load Data ---
|
| 10 |
+
st.info("π¦ Loading footfall data...")
|
| 11 |
try:
|
| 12 |
+
df = pd.read_excel("data/jj_with_footfall.xlsx")
|
| 13 |
+
df = df.dropna(subset=["latitude", "longitude"])
|
| 14 |
+
df = df.astype({"latitude": float, "longitude": float})
|
| 15 |
+
df = df[["latitude", "longitude"]]
|
| 16 |
+
st.success(f"β
Loaded {len(df)} footfall records.")
|
|
|
|
| 17 |
except Exception as e:
|
| 18 |
+
st.error(f"β Failed to load footfall data: {e}")
|
| 19 |
st.stop()
|
| 20 |
|
| 21 |
+
# --- Create Kepler Map ---
|
| 22 |
+
st.info("πΊοΈ Rendering map...")
|
| 23 |
try:
|
| 24 |
+
map_ = KeplerGl(height=600)
|
| 25 |
+
map_.add_data(data=df, name="Dubai Footfall")
|
| 26 |
+
map_.save_to_html("kepler_output.html")
|
| 27 |
+
with open("kepler_output.html", "r", encoding="utf-8") as f:
|
| 28 |
+
html = f.read()
|
| 29 |
+
components.html(html, height=650)
|
| 30 |
+
st.success("β
Map rendered successfully.")
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
+
st.error(f"β Failed to render Kepler map: {e}")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|