Spaces:
Sleeping
Sleeping
Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
|
2 |
+
import pandas as pd
|
3 |
+
import folium
|
4 |
+
import gradio as gr
|
5 |
+
|
6 |
+
# ----------------- CONFIGURACIÓN -----------------
|
7 |
+
DATA_PATH = "agrupacion_municipio_linea.xlsx" # Ruta al archivo que consolidaste
|
8 |
+
HUB_NAME = "Bogotá" # Cambia al nombre real de tu hub
|
9 |
+
HUB_LAT, HUB_LON = 4.7110, -74.0721 # Coordenadas del hub (ejemplo Bogotá)
|
10 |
+
|
11 |
+
# Colores por línea (ajusta si agregas nuevas)
|
12 |
+
LINE_COLOR = {
|
13 |
+
"Línea 1": "red",
|
14 |
+
"Línea 2": "blue",
|
15 |
+
"BIAC": "green"
|
16 |
+
}
|
17 |
+
# -------------------------------------------------
|
18 |
+
|
19 |
+
def load_data():
|
20 |
+
\"\"\"Carga el Excel y devuelve un DataFrame con lat/lon obligatorios.\"\"\"
|
21 |
+
df = pd.read_excel(DATA_PATH)
|
22 |
+
if not {{'lat','lon'}}.issubset(df.columns):
|
23 |
+
raise ValueError("El archivo debe contener columnas 'lat' y 'lon' con las coordenadas de cada municipio.")
|
24 |
+
return df
|
25 |
+
|
26 |
+
df = load_data()
|
27 |
+
|
28 |
+
# Desplegables dinámicos
|
29 |
+
all_areas = sorted({area.strip() for sub in df['Áreas Únicas'].dropna().tolist() for area in sub.split(',')})
|
30 |
+
all_areas.insert(0, "Todas")
|
31 |
+
all_lines = ["Todas"] + sorted(df['Línea_short'].dropna().unique().tolist())
|
32 |
+
|
33 |
+
def make_map(selected_area, selected_line):
|
34 |
+
\"\"\"Genera el HTML de un mapa Folium filtrado.\"\"\"
|
35 |
+
m = folium.Map(location=[HUB_LAT, HUB_LON], zoom_start=6, tiles="cartodbpositron")
|
36 |
+
|
37 |
+
# Marcador del hub
|
38 |
+
folium.Marker(
|
39 |
+
[HUB_LAT, HUB_LON],
|
40 |
+
tooltip=f"Hub: {HUB_NAME}",
|
41 |
+
icon=folium.Icon(color='orange', icon='home')
|
42 |
+
).add_to(m)
|
43 |
+
|
44 |
+
# Filtrado
|
45 |
+
filt = df.copy()
|
46 |
+
if selected_line != "Todas":
|
47 |
+
filt = filt[filt['Línea_short'] == selected_line]
|
48 |
+
if selected_area != "Todas":
|
49 |
+
filt = filt[filt['Áreas Únicas'].str.contains(selected_area, case=False, na=False)]
|
50 |
+
|
51 |
+
# Añadir rutas
|
52 |
+
for _, row in filt.iterrows():
|
53 |
+
lat, lon = row['lat'], row['lon']
|
54 |
+
if pd.isna(lat) or pd.isna(lon):
|
55 |
+
continue
|
56 |
+
folium.Marker(
|
57 |
+
[lat, lon],
|
58 |
+
tooltip=(f"{row['Ciudad']} ({row['Línea_short']})\\n"
|
59 |
+
f"Áreas: {row['Áreas Únicas']}"),
|
60 |
+
icon=folium.Icon(color='blue', icon='circle')
|
61 |
+
).add_to(m)
|
62 |
+
|
63 |
+
folium.PolyLine(
|
64 |
+
[[HUB_LAT, HUB_LON], [lat, lon]],
|
65 |
+
color=LINE_COLOR.get(row['Línea_short'], 'gray'),
|
66 |
+
weight=2,
|
67 |
+
opacity=0.8
|
68 |
+
).add_to(m)
|
69 |
+
|
70 |
+
return m._repr_html_()
|
71 |
+
|
72 |
+
demo = gr.Interface(
|
73 |
+
fn=make_map,
|
74 |
+
inputs=[
|
75 |
+
gr.Dropdown(choices=all_areas, value="Todas", label="Filtrar por Área"),
|
76 |
+
gr.Dropdown(choices=all_lines, value="Todas", label="Filtrar por Línea")
|
77 |
+
],
|
78 |
+
outputs=gr.HTML(),
|
79 |
+
title="Mapa de Rutas: Hub → Municipios",
|
80 |
+
description="Seleccione un Área artística y/o Línea para visualizar las rutas desde el hub a cada municipio."
|
81 |
+
)
|
82 |
+
|
83 |
+
if __name__ == "__main__":
|
84 |
+
demo.launch()
|
85 |
+
|