File size: 18,559 Bytes
19e0154 26f745e 21b68dd 7c32469 ce789dd 0732541 ac4e90e 9b4ad87 0732541 ac4e90e f945531 b3e7fba 48ceb28 f945531 b3e7fba 620c262 f945531 48ceb28 f945531 620c262 f945531 620c262 f945531 620c262 f945531 620c262 f945531 41e2706 f945531 0732541 f945531 48ceb28 f945531 48ceb28 f945531 48ceb28 f945531 48ceb28 f945531 48ceb28 f945531 620c262 f945531 620c262 f945531 620c262 f945531 620c262 f945531 8a27264 620c262 f945531 d139b5e 620c262 f945531 620c262 7c32469 d139b5e f945531 d139b5e f945531 d139b5e f945531 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 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 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 |
import streamlit as st
import geopandas as gpd
import pandas as pd
import plotly.graph_objects as go
from huggingface_hub import hf_hub_download
import matplotlib.pyplot as plt
import warnings
import io
from shapely.geometry import Polygon, MultiPolygon
# Page configuration
st.set_page_config(layout="wide", page_title="Geographic Data Visualization")
# Suppress warnings
warnings.filterwarnings('ignore')
def load_data():
# Load world map data
world_gdf = gpd.read_file(gpd.datasets.get_path('naturalearth_lowres'))
return world_gdf
def main():
st.title("๐บ๏ธ Geographic Data Visualization")
# Load data
world_gdf = load_data()
# Country selection
selected_country = st.selectbox(
"Select a Country",
sorted(world_gdf['name'].unique())
)
# Filter data for selected country
gdf = world_gdf[world_gdf['name'] == selected_country].copy()
# Legend settings
st.sidebar.header("Legend Settings")
legend_title = st.sidebar.text_input("Legend Title", "Regions")
legend_location = st.sidebar.selectbox("Legend Location",
["best", "upper right", "upper left", "lower left", "lower right"],
index=0)
legend_font_size = st.sidebar.slider("Legend Font Size", 8, 20, 12)
legend_font_weight = st.sidebar.selectbox("Legend Font Weight", ["Normal", "Bold"], index=0)
legend_columns = st.sidebar.slider("Legend Columns", 1, 3, 1)
legend_transparency = st.sidebar.slider("Legend Transparency", 0.0, 1.0, 0.1)
legend_bg_color = st.sidebar.color_picker("Legend Background Color", "#FFFFFF")
legend_border_color = st.sidebar.color_picker("Legend Border Color", "#000000")
legend_border_width = st.sidebar.slider("Legend Border Width", 0, 5, 1)
# Special handling for Pakistan
if selected_country == "Pakistan":
# Define administrative levels for Pakistan
admin_levels = {}
for i in range(6):
col_name = f"NAME_{i}"
if col_name in gdf.columns and not gdf[col_name].isna().all():
level_name = f"Administrative Level {i}"
admin_levels[level_name] = col_name
admin_levels["Provinces and Territories"] = "NAME_1"
# Define Pakistan-specific regions
pakistan_regions = {
"Provinces": ["Punjab", "Sindh", "Khyber Pakhtunkhwa", "Balochistan"],
"Territories": ["Islamabad Capital Territory", "Gilgit-Baltistan", "Azad Kashmir"]
}
# Selection for Pakistan's administrative levels
selected_admin_levels = st.multiselect(
"Select Administrative Levels to Display",
list(admin_levels.keys()),
default=["Provinces and Territories"]
)
# Initialize selected areas and colors
selected_areas = []
fill_colors = {}
border_colors = {}
# Handle selection for Pakistan's provinces and territories
if "Provinces and Territories" in selected_admin_levels:
col1, col2 = st.columns(2)
with col1:
selected_provinces = st.multiselect(
"Select Provinces",
pakistan_regions["Provinces"],
default=pakistan_regions["Provinces"]
)
with col2:
selected_territories = st.multiselect(
"Select Territories",
pakistan_regions["Territories"],
default=pakistan_regions["Territories"]
)
# Process selections
for area in selected_provinces + selected_territories:
selected_areas.append(area)
fill_colors[area] = st.color_picker(f"Fill color for {area}", "#3498db")
border_colors[area] = st.color_picker(f"Border color for {area}", "#2980b9")
# Create visualizations
st.subheader("2D Map Visualization")
fig_2d = create_2d_map(gdf, selected_areas, fill_colors, border_colors,
legend_title, legend_location, legend_font_size,
legend_bg_color, legend_border_color, legend_transparency,
legend_columns)
st.pyplot(fig_2d)
st.subheader("3D Map Visualization")
fig_3d = create_3d_map(gdf, selected_areas, fill_colors, border_colors,
legend_title, legend_font_size, legend_font_weight,
legend_bg_color, legend_border_color, legend_border_width,
legend_columns)
st.plotly_chart(fig_3d, use_container_width=True)
# Download buttons
col1, col2 = st.columns(2)
with col1:
if st.button("Download 2D Map"):
plt.savefig("map_2d.png", dpi=300, bbox_inches='tight')
with open("map_2d.png", "rb") as file:
st.download_button(
label="Click to Download 2D Map",
data=file,
file_name="map_2d.png",
mime="image/png"
)
with col2:
if st.button("Download 3D Map"):
html_buffer = io.StringIO()
fig_3d.write_html(html_buffer)
st.download_button(
label="Click to Download 3D Map",
data=html_buffer.getvalue(),
file_name="map_3d.html",
mime="text/html"
)
def create_2d_map(gdf, selected_areas, fill_colors, border_colors,
legend_title, legend_location, legend_font_size,
legend_bg_color, legend_border_color, legend_transparency,
legend_columns):
fig, ax = plt.subplots(figsize=(15, 10))
# Plot base map
gdf.plot(ax=ax, color='#EEEEEE', alpha=0.3, edgecolor='#CCCCCC')
# Plot selected areas
for area in selected_areas:
area_gdf = gdf[gdf['NAME_1'] == area]
area_gdf.plot(ax=ax, color=fill_colors[area], alpha=0.5,
edgecolor=border_colors[area], linewidth=1)
# Configure legend
patches = [plt.Rectangle((0, 0), 1, 1, fc=fill_colors[area],
ec=border_colors[area], alpha=0.5)
for area in selected_areas]
ax.legend(patches, selected_areas,
title=legend_title,
loc=legend_location,
fontsize=legend_font_size,
title_fontsize=legend_font_size + 2,
frameon=True,
facecolor=legend_bg_color,
edgecolor=legend_border_color,
framealpha=1-legend_transparency,
ncol=legend_columns)
ax.set_xticks([])
ax.set_yticks([])
ax.set_frame_on(False)
return fig
def create_3d_map(gdf, selected_areas, fill_colors, border_colors,
legend_title, legend_font_size, legend_font_weight,
legend_bg_color, legend_border_color, legend_border_width,
legend_columns):
fig = go.Figure()
added_to_legend = set()
# Plot base map
for _, row in gdf.iterrows():
if isinstance(row.geometry, MultiPolygon):
for poly in row.geometry.geoms:
x, y = poly.exterior.xy
fig.add_trace(go.Scatter3d(
x=list(x), y=list(y), z=[0]*len(x),
mode='lines',
line=dict(color='#CCCCCC', width=1),
showlegend=False
))
else:
x, y = row.geometry.exterior.xy
fig.add_trace(go.Scatter3d(
x=list(x), y=list(y), z=[0]*len(x),
mode='lines',
line=dict(color='#CCCCCC', width=1),
showlegend=False
))
# Plot selected areas
for area in selected_areas:
area_gdf = gdf[gdf['NAME_1'] == area]
for _, row in area_gdf.iterrows():
if isinstance(row.geometry, MultiPolygon):
for poly in row.geometry.geoms:
x, y = poly.exterior.xy
if area not in added_to_legend:
fig.add_trace(go.Scatter3d(
x=list(x), y=list(y), z=[0]*len(x),
mode='lines+markers',
line=dict(color=border_colors[area], width=2),
marker=dict(size=1, color=fill_colors[area], opacity=0.5),
name=area,
showlegend=True
))
added_to_legend.add(area)
else:
fig.add_trace(go.Scatter3d(
x=list(x), y=list(y), z=[0]*len(x),
mode='lines+markers',
line=dict(color=border_colors[area], width=2),
marker=dict(size=1, color=fill_colors[area], opacity=0.5),
name=area,
showlegend=False
))
else:
x, y = row.geometry.exterior.xy
if area not in added_to_legend:
fig.add_trace(go.Scatter3d(
x=list(x), y=list(y), z=[0]*len(x),
mode='lines+markers',
line=dict(color=border_colors[area], width=2),
marker=dict(size=1, color=fill_colors[area], opacity=0.5),
name=area,
showlegend=True
))
added_to_legend.add(area)
else:
fig.add_trace(go.Scatter3d(
x=list(x), y=list(y), z=[0]*len(x),
mode='lines+markers',
line=dict(color=border_colors[area], width=2),
marker=dict(size=1, color=fill_colors[area], opacity=0.5),
name=area,
showlegend=False
))
# Update layout
fig.update_layout(
scene=dict(
aspectmode='data',
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
zaxis=dict(showgrid=False, zeroline=False, showticklabels=False, range=[-1, 1])
),
margin=dict(t=30, b=0, l=0, r=0),
legend=dict(
font=dict(
size=legend_font_size,
weight=legend_font_weight.lower()
),
bgcolor=legend_bg_color,
bordercolor=legend_border_color,
borderwidth=legend_border_width,
orientation="h" if legend_columns > 1 else "v",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1,
title=dict(
text=legend_title,
font=dict(
size=legend_font_size + 2
)
),
itemsizing='constant'
),
showlegend=True,
height=800
)
return fig
if __name__ == "__main__":
main()
# [Previous code remains the same until line 285 in the main() function]
# Add after the 3D map visualization section in main():
# 3D Map Visualization (World Map Version)
st.subheader("๐ 3D Map Visualization (World Map)")
fig_world = create_3d_world_map(world_gdf, gdf, selected_areas, fill_colors, border_colors,
legend_title, legend_font_size, legend_font_weight,
legend_bg_color, legend_border_color, legend_border_width,
legend_columns)
st.plotly_chart(fig_world, use_container_width=True)
# Add download buttons for both 3D maps
col1, col2 = st.columns(2)
with col1:
if st.button("Download 3D Map (Current Version)"):
html_buffer = io.StringIO()
fig_3d.write_html(html_buffer)
st.download_button(
label="Click to Download 3D Map (Current Version)",
data=html_buffer.getvalue(),
file_name=f"{selected_country.lower()}_map_3d.html",
mime="text/html"
)
with col2:
if st.button("Download 3D World Map"):
html_buffer = io.StringIO()
fig_world.write_html(html_buffer)
st.download_button(
label="Click to Download 3D World Map",
data=html_buffer.getvalue(),
file_name=f"{selected_country.lower()}_world_map_3d.html",
mime="text/html"
)
# Add new function for world map visualization after create_3d_map():
def create_3d_world_map(world_gdf, selected_gdf, selected_areas, fill_colors, border_colors,
legend_title, legend_font_size, legend_font_weight,
legend_bg_color, legend_border_color, legend_border_width,
legend_columns):
fig_world = go.Figure()
added_to_legend = set()
# Plot all countries in light gray
for _, row in world_gdf.iterrows():
if isinstance(row.geometry, MultiPolygon):
for poly in row.geometry.geoms:
x, y = poly.exterior.xy
fig_world.add_trace(go.Scatter3d(
x=list(x), y=list(y), z=[0]*len(x),
mode='lines',
line=dict(color='#CCCCCC', width=1),
showlegend=False
))
else:
x, y = row.geometry.exterior.xy
fig_world.add_trace(go.Scatter3d(
x=list(x), y=list(y), z=[0]*len(x),
mode='lines',
line=dict(color='#CCCCCC', width=1),
showlegend=False
))
# Plot selected areas with their colors
for area in selected_areas:
area_gdf = selected_gdf[selected_gdf['NAME_1'] == area]
for _, row in area_gdf.iterrows():
if isinstance(row.geometry, MultiPolygon):
for poly in row.geometry.geoms:
x, y = poly.exterior.xy
if area not in added_to_legend:
fig_world.add_trace(go.Scatter3d(
x=list(x), y=list(y), z=[0]*len(x),
mode='lines+markers',
line=dict(color=border_colors[area], width=2),
marker=dict(size=1, color=fill_colors[area], opacity=0.5),
name=area,
showlegend=True
))
added_to_legend.add(area)
else:
fig_world.add_trace(go.Scatter3d(
x=list(x), y=list(y), z=[0]*len(x),
mode='lines+markers',
line=dict(color=border_colors[area], width=2),
marker=dict(size=1, color=fill_colors[area], opacity=0.5),
name=area,
showlegend=False
))
fig_world.add_trace(go.Mesh3d(
x=list(x), y=list(y), z=[0]*len(x),
color=fill_colors[area],
opacity=0.3,
showlegend=False
))
else:
x, y = row.geometry.exterior.xy
if area not in added_to_legend:
fig_world.add_trace(go.Scatter3d(
x=list(x), y=list(y), z=[0]*len(x),
mode='lines+markers',
line=dict(color=border_colors[area], width=2),
marker=dict(size=1, color=fill_colors[area], opacity=0.5),
name=area,
showlegend=True
))
added_to_legend.add(area)
else:
fig_world.add_trace(go.Scatter3d(
x=list(x), y=list(y), z=[0]*len(x),
mode='lines+markers',
line=dict(color=border_colors[area], width=2),
marker=dict(size=1, color=fill_colors[area], opacity=0.5),
name=area,
showlegend=False
))
fig_world.add_trace(go.Mesh3d(
x=list(x), y=list(y), z=[0]*len(x),
color=fill_colors[area],
opacity=0.3,
showlegend=False
))
# Update layout
fig_world.update_layout(
scene=dict(
aspectmode='data',
xaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
yaxis=dict(showgrid=False, zeroline=False, showticklabels=False),
zaxis=dict(showgrid=False, zeroline=False, showticklabels=False, range=[-1, 1])
),
margin=dict(t=30, b=0, l=0, r=0),
legend=dict(
font=dict(
size=legend_font_size,
weight=legend_font_weight.lower()
),
bgcolor=legend_bg_color,
bordercolor=legend_border_color,
borderwidth=legend_border_width,
orientation="h" if legend_columns > 1 else "v",
yanchor="bottom",
y=1.02,
xanchor="right",
x=1,
title=dict(
text=legend_title,
font=dict(
size=legend_font_size + 2
)
),
itemsizing='constant',
traceorder='normal' if legend_columns == 1 else 'grouped'
),
showlegend=True,
height=800
)
return fig_world
# Wrap the main execution in a try-except block
if __name__ == "__main__":
try:
main()
except Exception as e:
st.error(f"โ ๏ธ Error: {e}") |