WaysAheadGlobal commited on
Commit
a3f5328
Β·
verified Β·
1 Parent(s): 2b52006

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. 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
- import numpy as np
4
- import osmnx as ox
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("🧠 Dubai Digital Twin – Interactive 2D Map with Folium")
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
- # --- Get Dubai Boundary ---
168
- st.subheader("🌍 Getting Dubai boundary...")
169
  try:
170
- dubai = ox.geocode_to_gdf("Dubai, United Arab Emirates")
171
- dubai_polygon = dubai.geometry.values[0]
172
- bounds = dubai_polygon.bounds
173
- west, south, east, north = bounds[0], bounds[1], bounds[2], bounds[3]
174
- bbox = (west, south, east, north)
175
- st.success("βœ… Dubai boundary fetched.")
176
  except Exception as e:
177
- st.error(f"❌ Boundary fetch failed: {e}")
178
  st.stop()
179
 
180
- # --- Load Data ---
 
181
  try:
182
- malls = ox.features_from_bbox(bbox, tags={"shop": "mall"})
183
- parks = ox.features_from_bbox(bbox, tags={"leisure": "park"})
184
- metro_stations = ox.features_from_bbox(bbox, tags={"railway": "station"})
185
- metro_lines = ox.features_from_bbox(bbox, tags={"railway": ["subway", "light_rail"]})
186
- stores = ox.features_from_bbox(bbox, tags={"shop": True})
187
-
188
- footfall = pd.read_excel("data/jj_with_footfall.xlsx")
189
- footfall.dropna(subset=["latitude", "longitude"], inplace=True)
190
  except Exception as e:
191
- st.warning(f"⚠️ Data load failed: {e}")
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}")