WaysAhead Global commited on
Commit
ec9cce4
Β·
verified Β·
1 Parent(s): a464c43

Update src/streamlit_app.py

Browse files
Files changed (1) hide show
  1. src/streamlit_app.py +81 -66
src/streamlit_app.py CHANGED
@@ -1,115 +1,130 @@
1
  import streamlit as st
2
- import geopandas as gpd
3
  import pandas as pd
4
- import pydeck as pdk
5
- import numpy as np
6
  import osmnx as ox
7
  from streamlit_folium import st_folium
8
  import folium
9
 
10
  st.set_page_config(layout="wide")
11
- st.title("🧠 Dubai Digital Twin – Interactive Map")
12
 
13
  # --- Sidebar Layer Controls ---
14
  st.sidebar.title("πŸ—ΊοΈ Layer Toggles")
15
- show_buildings = st.sidebar.checkbox("Show Buildings", True)
16
  show_malls = st.sidebar.checkbox("Show Malls", True)
 
17
  show_footfall = st.sidebar.checkbox("Show Footfall", True)
18
  show_parks = st.sidebar.checkbox("Show Parks", True)
19
  show_metro_lines = st.sidebar.checkbox("Show Metro Lines", True)
20
  show_metro_stations = st.sidebar.checkbox("Show Metro Stations", True)
21
- show_move_sim = st.sidebar.checkbox("Simulate Entity Movement", True)
22
 
23
  # --- Legend ---
24
  st.sidebar.markdown("---")
25
  st.sidebar.markdown("### πŸ“Œ Legend")
26
- st.sidebar.markdown("- πŸ—οΈ Buildings: Gray")
27
- st.sidebar.markdown("- πŸ›οΈ Malls: Blue / Green / Orange")
28
  st.sidebar.markdown("- 🌳 Parks: Green")
29
- st.sidebar.markdown("- πŸ“Š Footfall: Red")
30
  st.sidebar.markdown("- πŸš‡ Metro Lines: Blue Line")
31
  st.sidebar.markdown("- πŸš‰ Metro Stations: Yellow Dot")
32
 
33
  # --- Load Dubai Boundary ---
34
  st.subheader("🌍 Getting Dubai boundary...")
35
  try:
36
- ox.settings.use_cache = False # Disable disk cache to avoid permission error
37
  dubai = ox.geocode_to_gdf("Dubai, United Arab Emirates")
38
  dubai_polygon = dubai.geometry.values[0]
39
  bounds = dubai_polygon.bounds
40
- west, south, east, north = bounds[0], bounds[1], bounds[2], bounds[3]
41
- bbox = (west, south, east, north)
42
  st.success("βœ… Dubai boundary fetched.")
43
  except Exception as e:
44
  st.error(f"❌ Boundary fetch failed: {e}")
45
  st.stop()
46
 
47
-
48
- # --- Real Data Sources ---
49
  try:
50
  malls = ox.features_from_bbox(bbox, tags={"shop": "mall"})
51
- malls_points = malls[malls.geometry.type == "Point"]
52
-
53
  parks = ox.features_from_bbox(bbox, tags={"leisure": "park"})
54
- parks_points = parks[parks.geometry.type == "Point"]
55
-
56
  metro_stations = ox.features_from_bbox(bbox, tags={"railway": "station"})
57
- metro_station_points = metro_stations[metro_stations.geometry.type == "Point"]
58
-
59
- footfall = pd.read_excel("data/jj_with_footfall.xlsx")
60
  except Exception as e:
61
- st.warning(f"⚠️ OSM or data file fetch failed: {e}")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
62
 
63
- # --- Movement Simulation Map ---
64
- if show_move_sim:
65
- st.subheader("🎯 Simulate Object Movement")
66
- fmap = folium.Map(location=[25.2048, 55.2708], zoom_start=12)
 
 
 
 
 
 
67
 
68
- # Malls
69
- for _, row in malls_points.iterrows():
70
- folium.Marker(
71
- location=[row.geometry.y, row.geometry.x],
72
- popup=f"Mall: {row.get('name', 'Unnamed')} (Drag me)",
73
- tooltip=row.get('name', 'Mall'),
74
- draggable=True,
75
- icon=folium.Icon(color="blue", icon="shopping-cart", prefix="fa")
76
- ).add_to(fmap)
 
77
 
78
- # Parks
79
- for _, row in parks_points.iterrows():
80
- folium.Marker(
81
- location=[row.geometry.y, row.geometry.x],
82
- popup=f"Park: {row.get('name', 'Unnamed')} (Drag me)",
83
- tooltip=row.get('name', 'Park'),
84
- draggable=True,
85
- icon=folium.Icon(color="green", icon="tree", prefix="fa")
86
- ).add_to(fmap)
 
 
 
 
 
 
 
 
 
 
87
 
88
- # Metro Stations
89
- for _, row in metro_station_points.iterrows():
 
90
  folium.Marker(
91
- location=[row.geometry.y, row.geometry.x],
92
- popup=f"Metro Station: {row.get('name', 'Unnamed')} (Drag me)",
93
- tooltip=row.get('name', 'Metro'),
94
  draggable=True,
95
- icon=folium.Icon(color="orange", icon="train", prefix="fa")
96
  ).add_to(fmap)
97
 
98
- # Footfall points
99
- if show_footfall:
100
- for _, row in footfall.dropna(subset=["latitude", "longitude"]).iterrows():
101
- folium.CircleMarker(
102
- location=[row["latitude"], row["longitude"]],
103
- radius=4,
104
- color="red",
105
- fill=True,
106
- fill_opacity=0.6,
107
- tooltip="Footfall"
108
- ).add_to(fmap)
109
 
110
- moved = st_folium(fmap, width=1000, height=600)
 
 
 
 
111
 
112
- if moved and moved.get("last_object_clicked"):
113
- lat = moved["last_object_clicked"]["lat"]
114
- lon = moved["last_object_clicked"]["lng"]
115
- st.success(f"πŸ“ New Position: {lat}, {lon}")
 
1
  import streamlit as st
 
2
  import pandas as pd
 
 
3
  import osmnx as ox
4
  from streamlit_folium import st_folium
5
  import folium
6
 
7
  st.set_page_config(layout="wide")
8
+ st.title("🧠 Dubai Digital Twin – Interactive 2D Map with Folium")
9
 
10
  # --- Sidebar Layer Controls ---
11
  st.sidebar.title("πŸ—ΊοΈ Layer Toggles")
 
12
  show_malls = st.sidebar.checkbox("Show Malls", True)
13
+ show_stores = st.sidebar.checkbox("Show Stores in Malls", True)
14
  show_footfall = st.sidebar.checkbox("Show Footfall", True)
15
  show_parks = st.sidebar.checkbox("Show Parks", True)
16
  show_metro_lines = st.sidebar.checkbox("Show Metro Lines", True)
17
  show_metro_stations = st.sidebar.checkbox("Show Metro Stations", True)
 
18
 
19
  # --- Legend ---
20
  st.sidebar.markdown("---")
21
  st.sidebar.markdown("### πŸ“Œ Legend")
22
+ st.sidebar.markdown("- πŸ›οΈ Malls: Blue")
23
+ st.sidebar.markdown("- 🏬 Stores: Purple")
24
  st.sidebar.markdown("- 🌳 Parks: Green")
25
+ st.sidebar.markdown("- πŸ“Š Footfall: Red (Draggable)")
26
  st.sidebar.markdown("- πŸš‡ Metro Lines: Blue Line")
27
  st.sidebar.markdown("- πŸš‰ Metro Stations: Yellow Dot")
28
 
29
  # --- Load Dubai Boundary ---
30
  st.subheader("🌍 Getting Dubai boundary...")
31
  try:
 
32
  dubai = ox.geocode_to_gdf("Dubai, United Arab Emirates")
33
  dubai_polygon = dubai.geometry.values[0]
34
  bounds = dubai_polygon.bounds
35
+ bbox = (bounds[0], bounds[1], bounds[2], bounds[3])
 
36
  st.success("βœ… Dubai boundary fetched.")
37
  except Exception as e:
38
  st.error(f"❌ Boundary fetch failed: {e}")
39
  st.stop()
40
 
41
+ # --- Load Real Data ---
 
42
  try:
43
  malls = ox.features_from_bbox(bbox, tags={"shop": "mall"})
 
 
44
  parks = ox.features_from_bbox(bbox, tags={"leisure": "park"})
 
 
45
  metro_stations = ox.features_from_bbox(bbox, tags={"railway": "station"})
46
+ metro_lines = ox.features_from_bbox(bbox, tags={"railway": ["subway", "light_rail"]})
47
+ stores = ox.features_from_bbox(bbox, tags={"shop": True})
48
+ footfall = pd.read_excel("data/jj_with_footfall.xlsx").dropna(subset=["latitude", "longitude"])
49
  except Exception as e:
50
+ st.warning(f"⚠️ Data load failed: {e}")
51
+
52
+ # --- Render Map ---
53
+ fmap = folium.Map(location=[25.2048, 55.2708], zoom_start=12)
54
+
55
+ # Malls
56
+ if show_malls:
57
+ for _, row in malls.iterrows():
58
+ if row.geometry.geom_type == "Point":
59
+ folium.Marker(
60
+ location=[row.geometry.y, row.geometry.x],
61
+ popup=f"Mall: {row.get('name', 'Unnamed')}",
62
+ tooltip=row.get('name', 'Mall'),
63
+ icon=folium.Icon(color="blue", icon="shopping-cart", prefix="fa")
64
+ ).add_to(fmap)
65
 
66
+ # Stores
67
+ if show_stores:
68
+ for _, row in stores.iterrows():
69
+ if row.geometry.geom_type == "Point":
70
+ folium.Marker(
71
+ location=[row.geometry.y, row.geometry.x],
72
+ popup=f"Store: {row.get('name', 'Unnamed Store')}",
73
+ tooltip=row.get('name', 'Store'),
74
+ icon=folium.Icon(color="purple", icon="shopping-bag", prefix="fa")
75
+ ).add_to(fmap)
76
 
77
+ # Parks
78
+ if show_parks:
79
+ for _, row in parks.iterrows():
80
+ if hasattr(row.geometry, 'y') and hasattr(row.geometry, 'x'):
81
+ folium.Marker(
82
+ location=[row.geometry.y, row.geometry.x],
83
+ popup=f"Park: {row.get('name', 'Unnamed')}",
84
+ tooltip=row.get('name', 'Park'),
85
+ icon=folium.Icon(color="green", icon="tree", prefix="fa")
86
+ ).add_to(fmap)
87
 
88
+ # Metro Stations
89
+ if show_metro_stations:
90
+ for _, row in metro_stations.iterrows():
91
+ if hasattr(row.geometry, 'y') and hasattr(row.geometry, 'x'):
92
+ folium.Marker(
93
+ location=[row.geometry.y, row.geometry.x],
94
+ popup=f"Metro: {row.get('name', 'Unnamed')}",
95
+ tooltip=row.get('name', 'Metro'),
96
+ icon=folium.Icon(color="orange", icon="train", prefix="fa")
97
+ ).add_to(fmap)
98
+
99
+ # Metro Lines
100
+ if show_metro_lines:
101
+ for _, row in metro_lines.iterrows():
102
+ if row.geometry.geom_type == "LineString":
103
+ folium.PolyLine(
104
+ locations=[(y, x) for x, y in row.geometry.coords],
105
+ color="blue", weight=4, opacity=0.7
106
+ ).add_to(fmap)
107
 
108
+ # Draggable Footfall
109
+ if show_footfall:
110
+ for _, row in footfall.iterrows():
111
  folium.Marker(
112
+ location=[row["latitude"], row["longitude"]],
113
+ popup="Drag me",
 
114
  draggable=True,
115
+ icon=folium.Icon(color="red", icon="map-marker", prefix="fa")
116
  ).add_to(fmap)
117
 
118
+ # Display map
119
+ fmap_state = st_folium(fmap, width=1200, height=700)
 
 
 
 
 
 
 
 
 
120
 
121
+ # Show moved coordinates
122
+ if fmap_state and fmap_state.get("last_object_clicked"):
123
+ lat = fmap_state["last_object_clicked"]["lat"]
124
+ lon = fmap_state["last_object_clicked"]["lng"]
125
+ st.success(f"πŸ“ New Position: {lat}, {lon}")
126
 
127
+ # Button to refresh
128
+ if st.sidebar.button("πŸ”„ Refresh Map"):
129
+ st.session_state["fmap_rendered"] = False
130
+ st.rerun()