Spaces:
Sleeping
Sleeping
Update modules/visuals.py
Browse files- modules/visuals.py +32 -99
modules/visuals.py
CHANGED
|
@@ -1,105 +1,38 @@
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import plotly.express as px
|
| 3 |
-
import plotly.graph_object as go
|
| 4 |
-
import pandas as pd
|
| 5 |
|
| 6 |
-
def
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
col3.metric("⚡ Power Issues", df[df['PowerSufficient'] == "No"].shape[0])
|
| 12 |
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
st.warning("No data available for this location.")
|
| 22 |
-
return
|
| 23 |
|
| 24 |
-
#
|
| 25 |
-
|
| 26 |
-
'latitude': [17.385044, 17.444418],
|
| 27 |
-
'longitude': [78.486671, 78.348397],
|
| 28 |
-
'alert_level': ['red', 'yellow'],
|
| 29 |
-
'location': ['Location A', 'Location B']
|
| 30 |
-
})
|
| 31 |
-
|
| 32 |
-
# Debug: Print DataFrame to verify coordinates
|
| 33 |
-
st.write("Debug: Sample Data", df[["Latitude", "Longitude", "AlertLevel"]].head()) # Temporary debug
|
| 34 |
-
|
| 35 |
-
# Map AlertLevel to sizes, colors, and styles with dark theme preference
|
| 36 |
-
df = df.copy()
|
| 37 |
-
df["MarkerColor"] = df["AlertLevel"].map({"Green": "green", "Yellow": "yellow", "Red": "red"})
|
| 38 |
-
df["MarkerSize"] = df["AlertLevel"].map({"Green": 20, "Yellow": 25, "Red": 35})
|
| 39 |
-
df["MarkerSymbol"] = df["AlertLevel"].map({"Green": "circle", "Yellow": "circle", "Red": "star"})
|
| 40 |
-
df["MarkerOpacity"] = df["AlertLevel"].map({"Green": 0.6, "Yellow": 0.8, "Red": 1.0}) # Higher opacity for red
|
| 41 |
-
|
| 42 |
-
# Create scatter map with dark theme
|
| 43 |
-
fig = px.scatter_mapbox(
|
| 44 |
-
df,
|
| 45 |
-
lat="Latitude",
|
| 46 |
-
lon="Longitude",
|
| 47 |
-
color="AlertLevel",
|
| 48 |
-
color_discrete_map={"Green": "green", "Yellow": "yellow", "Red": "red"},
|
| 49 |
-
size="MarkerSize",
|
| 50 |
-
size_max=35,
|
| 51 |
-
zoom=15 if location == "Hyderabad" else 11,
|
| 52 |
-
hover_data={
|
| 53 |
-
"PoleID": True,
|
| 54 |
-
"RFID": True,
|
| 55 |
-
"Timestamp": True,
|
| 56 |
-
"AlertLevel": True,
|
| 57 |
-
"Anomalies": True,
|
| 58 |
-
"Zone": True,
|
| 59 |
-
"Latitude": False,
|
| 60 |
-
"Longitude": False
|
| 61 |
-
},
|
| 62 |
-
title=f"Pole Alert Map - {location}",
|
| 63 |
-
height=600
|
| 64 |
-
)
|
| 65 |
-
fig.update_traces(
|
| 66 |
-
marker=dict(
|
| 67 |
-
color=df["MarkerColor"], # Explicitly set marker color
|
| 68 |
-
symbol=df["MarkerSymbol"],
|
| 69 |
-
opacity=df["MarkerOpacity"],
|
| 70 |
-
size=df["MarkerSize"]
|
| 71 |
-
)
|
| 72 |
-
)
|
| 73 |
-
fig.update_layout(
|
| 74 |
-
mapbox_style="dark", # Changed to dark theme
|
| 75 |
-
margin={"r": 0, "t": 50, "l": 0, "b": 0},
|
| 76 |
-
showlegend=True,
|
| 77 |
-
legend=dict(
|
| 78 |
-
itemsizing="constant",
|
| 79 |
-
bgcolor="rgba(0, 0, 0, 0.7)",
|
| 80 |
-
font=dict(color="white"),
|
| 81 |
-
traceorder="normal"
|
| 82 |
-
)
|
| 83 |
-
)
|
| 84 |
-
fig.update_layout(
|
| 85 |
-
mapbox=dict(
|
| 86 |
-
style="open-street-map", # Base style
|
| 87 |
-
center=dict(lat=17.385044, lon=78.486671),
|
| 88 |
-
zoom=12 # Zoom to an appropriate level
|
| 89 |
-
)
|
| 90 |
-
)
|
| 91 |
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
mode='markers',
|
| 97 |
-
marker=go.scattermapbox.Marker(
|
| 98 |
-
size=14, # Increased size
|
| 99 |
-
color=df['alert_level'], # Colors based on alert level
|
| 100 |
-
colorscale='YlOrRd', # Set a color scale
|
| 101 |
-
opacity=0.9 # Increased opacity for visibility
|
| 102 |
-
),
|
| 103 |
-
text=df['location'],
|
| 104 |
-
))
|
| 105 |
-
st.plotly_chart(fig, use_container_width=True)
|
|
|
|
| 1 |
+
import folium
|
| 2 |
+
from folium.plugins import HeatMap
|
| 3 |
import streamlit as st
|
|
|
|
|
|
|
|
|
|
| 4 |
|
| 5 |
+
def display_heatmap(df):
|
| 6 |
+
# Initialize the base map at a central location (around Hyderabad)
|
| 7 |
+
center_lat = 17.385044 # Example latitude for Hyderabad
|
| 8 |
+
center_lon = 78.486671 # Example longitude for Hyderabad
|
| 9 |
+
map = folium.Map(location=[center_lat, center_lon], zoom_start=6)
|
|
|
|
| 10 |
|
| 11 |
+
# Add heatmap layer
|
| 12 |
+
heat_data = []
|
| 13 |
+
for index, row in df.iterrows():
|
| 14 |
+
lat = row["Latitude"]
|
| 15 |
+
lon = row["Longitude"]
|
| 16 |
+
alert = row["Alert Level"]
|
| 17 |
+
|
| 18 |
+
# Color mapping for heatmap intensity based on alert level
|
| 19 |
+
if alert == "Green":
|
| 20 |
+
color = [0, 255, 0] # Green
|
| 21 |
+
elif alert == "Yellow":
|
| 22 |
+
color = [255, 255, 0] # Yellow
|
| 23 |
+
elif alert == "Red":
|
| 24 |
+
color = [255, 0, 0] # Red
|
| 25 |
+
|
| 26 |
+
# Add data to the heatmap layer (latitude, longitude, intensity)
|
| 27 |
+
heat_data.append([lat, lon, 1 if alert == "Red" else 0.5 if alert == "Yellow" else 0.1])
|
| 28 |
|
| 29 |
+
# Create HeatMap with data
|
| 30 |
+
HeatMap(heat_data).add_to(map)
|
|
|
|
|
|
|
| 31 |
|
| 32 |
+
# Display the map in Streamlit
|
| 33 |
+
folium_static(map)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
def folium_static(map):
|
| 36 |
+
# This is a helper function to render Folium maps in Streamlit
|
| 37 |
+
from streamlit.components.v1 import html
|
| 38 |
+
html(map._repr_html_(), width=725, height=500)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|