WaysAhead Global commited on
Update src/streamlit_app.py
Browse files- 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("-
|
| 27 |
-
st.sidebar.markdown("-
|
| 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 |
-
|
| 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 |
-
|
| 58 |
-
|
| 59 |
-
footfall = pd.read_excel("data/jj_with_footfall.xlsx")
|
| 60 |
except Exception as e:
|
| 61 |
-
st.warning(f"β οΈ
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
|
| 63 |
-
#
|
| 64 |
-
if
|
| 65 |
-
|
| 66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
|
|
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 87 |
|
| 88 |
-
|
| 89 |
-
|
|
|
|
| 90 |
folium.Marker(
|
| 91 |
-
location=[row
|
| 92 |
-
popup=
|
| 93 |
-
tooltip=row.get('name', 'Metro'),
|
| 94 |
draggable=True,
|
| 95 |
-
icon=folium.Icon(color="
|
| 96 |
).add_to(fmap)
|
| 97 |
|
| 98 |
-
|
| 99 |
-
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
|
|
|
| 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()
|