Spaces:
Sleeping
Sleeping
File size: 5,708 Bytes
7e2d748 | 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 | import streamlit as st
st.set_page_config(page_title="India Map Visualizer", layout="centered")
from streamlit_folium import st_folium
import folium
import random
from utils import add_hover_tooltips
import geopandas as gpd
import matplotlib.pyplot as plt
from shapely.geometry import Point
from utils import plot_full_india_map, draw_map_lines_with_labels
import io
import os
# === Load GeoJSONs ===
state_path = os.path.join("data", "in.json")
district_path = os.path.join("data", "output.geojson")
states = gpd.read_file(state_path).rename(columns={"name": "State"}).to_crs(epsg=4326)
states["State"] = states["State"].str.strip()
# Title
st.title("πΊοΈ India Map Visualizer")
st.markdown("Select states or districts and optionally draw connection lines.")
# Show full India map
fig_full = plot_full_india_map(states)
st.pyplot(fig_full)
# Download full map
buf = io.BytesIO()
fig_full.savefig(buf, format="png")
st.download_button("π₯ Download India Map", data=buf.getvalue(), file_name="india_map.png", mime="image/png")
# --- Selection ---
st.markdown("---")
st.header("π Select Regions & Draw Connections")
mode = st.radio("Choose connection type:", ["State-to-State", "District-to-District"])
line_type = st.selectbox("Choose line style", ["Straight", "Dashed", "Curved"])
show_boundaries = st.checkbox("Show boundaries of other states/districts", value=True)
# Target point input (optional)
st.markdown("### π Optional: Click on map to add target points")
clicks = st.session_state.get("clicks", [])
if st.button("Clear Clicks"):
st.session_state["clicks"] = []
clicks = []
if "clicks" not in st.session_state:
st.session_state["clicks"] = []
clicked_lon = st.number_input("Click Longitude", value=77.0)
clicked_lat = st.number_input("Click Latitude", value=28.0)
if st.button("Add Target Point"):
st.session_state["clicks"].append((clicked_lon, clicked_lat))
clicks = st.session_state["clicks"]
st.markdown(f"**Current Target Points:** {st.session_state['clicks']}")
# --- Region selection ---
selected_data = None
if mode == "State-to-State":
state_options = states["State"].sort_values().unique().tolist()
selected_states = st.multiselect("Select State(s)", state_options, default=["Madhya Pradesh"])
selected_data = states[states["State"].isin(selected_states)]
else:
try:
districts = gpd.read_file(district_path).to_crs(epsg=4326)
if 'district' not in districts.columns and 'dtname' in districts.columns:
districts = districts.rename(columns={"dtname": "district"})
if 'district' not in districts.columns:
st.warning("β οΈ 'district' column not found in district data.")
st.write("Available columns:", list(districts.columns))
districts["district"] = districts["district"].str.strip()
district_options = districts["district"].sort_values().unique().tolist()
selected_districts = st.multiselect("Select District(s)", district_options)
selected_data = districts[districts["district"].isin(selected_districts)]
except Exception as e:
st.error("β Could not load district GeoJSON.")
st.exception(e)
st.stop()
# --- Draw button ---
# --- Draw button ---
if st.button("ποΈ Generate Map") and selected_data is not None:
fig = draw_map_lines_with_labels(
geo_data=states if mode == "State-to-State" else districts,
selected_data=selected_data,
target_coords=clicks if clicks else None,
line_style=line_type,
label_field='State' if mode == 'State-to-State' else 'district',
show_boundaries=show_boundaries
)
st.session_state["generated_fig"] = fig # Save figure in session state
# --- Display map & download ---
if "generated_fig" in st.session_state:
fig = st.session_state["generated_fig"]
st.pyplot(fig)
buf = io.BytesIO()
fig.savefig(buf, format="png")
st.download_button("π₯ Download Map", data=buf.getvalue(), file_name="map_with_lines.png", mime="image/png")
# --- Interactive Folium Map ---
st.markdown("---")
st.header("πΊοΈ Interactive Map with Hover Tooltips & Coloring")
folium_mode = st.radio("Select interactive map type", ["State Map (Population Color)", "District Map (Index Color)"])
@st.cache_data
def get_population(_states_df):
return [random.randint(1_000_000, 50_000_000) for _ in range(len(_states_df))]
if folium_mode == "State Map (Population Color)":
states["Population"] = get_population(states)
m = folium.Map(location=[22.0, 78.0], zoom_start=5, tiles="cartodbpositron")
add_hover_tooltips(
m,
states,
field_to_color="Population",
tooltip_fields=["State", "Population"],
tooltip_aliases=["State:", "Population:"]
)
st_folium(m, width=1000, height=600)
elif folium_mode == "District Map (Index Color)":
try:
districts = gpd.read_file(district_path).to_crs(epsg=4326)
if 'district' not in districts.columns and 'dtname' in districts.columns:
districts = districts.rename(columns={"dtname": "district"})
districts["district"] = districts["district"].str.strip()
districts["Index"] = list(range(len(districts))) # dummy data
m = folium.Map(location=[22.0, 78.0], zoom_start=5, tiles="cartodbpositron")
add_hover_tooltips(
m,
districts,
field_to_color="Index",
tooltip_fields=["district", "Index"],
tooltip_aliases=["District:", "Index:"]
)
st_folium(m, width=1000, height=600)
except Exception as e:
st.error("β Could not load district GeoJSON.")
st.exception(e)
|