File size: 5,714 Bytes
290c3fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import streamlit as st
import pandas as pd
import folium
from folium.plugins import MarkerCluster
from streamlit_folium import st_folium
import html

# --- Configuraci贸n ---
st.set_page_config(page_title="Puntajes SIMCE de centros escolares, para estudiantes de 2do medio 2024", page_icon=":school:", layout="wide")
st.title(":school: Puntajes SIMCE 2024")

# --- Diccionarios para mapear valores a descripciones ---
dependencia_map = {
    1: 'Municipal Corporaci贸n',
    2: 'Municipal DAEM',
    3: 'Particular subvencionado',
    4: 'Particular pagado',
    5: 'Corporaci贸n de administraci贸n delegada',
    6: 'Servicio Local de Educaci贸n'
}

socioecon_map = {
    1: 'Bajo',
    2: 'Medio Bajo',
    3: 'Medio',
    4: 'Medio Alto',
    5: 'Alto'
}

rural_map = {
    1: 'Urbano',
    2: 'Rural'
}

# --- Cargar datos ---
@st.cache_data
def load_data():
    df = pd.read_csv(
        "data/simce.csv",
        sep=";",
        decimal=",",
        encoding="ISO-8859-1",
        header=0,
        on_bad_lines="skip"
    )
    
    # Crear columnas con descripciones
    df['dependencia_desc'] = df['dependencia'].map(dependencia_map)
    df['grupo_socioecon_desc'] = df['grupo_socioecon'].map(socioecon_map)
    df['es_rural_desc'] = df['es_rural'].map(rural_map)
    
    return df

# Cargar datos
df = load_data()

# --- Descripci贸n ---
st.subheader("Establecimientos educacionales en Chile")
st.markdown("Datos del MINEDUC")

# --- Filtros ---
col1, col2 = st.columns(2)
regiones = ["(Todas)"] + sorted(df["nombre_region"].dropna().unique().tolist())
ruralidades = ["(Todas)"] + sorted(df["es_rural_desc"].dropna().unique().tolist())
with col1:
    sel_region = st.selectbox("Regi贸n", regiones, index=0)
with col2:
    sel_ruralidad = st.selectbox("Ruralidad", ruralidades, index=0)

# Filtrar datos
df_f = df.copy()
if sel_region != "(Todas)":
    df_f = df_f[df_f["nombre_region"] == sel_region]
if sel_ruralidad != "(Todas)":
    df_f = df_f[df_f["es_rural_desc"] == sel_ruralidad]

# Filtrar filas con coordenadas v谩lidas
df_map = df_f.dropna(subset=["latitud", "longitud"])

# --- Mapeo de colores por tipo ---
color_map = {
    'Municipal Corporaci贸n': 'blue',
    'Municipal DAEM': 'green',
    'Particular subvencionado': 'orange',
    'Particular pagado': 'purple',
    'Corporaci贸n de administraci贸n delegada': 'red',
    'Servicio Local de Educaci贸n': 'cadetblue'
}

# Funci贸n para asignar color seg煤n tipo
def tipo_color(tipo: str) -> str:
    return color_map.get(tipo, "gray")

# --- Crear mapa centrado en Chile ---
m = folium.Map(location=[-33.45, -70.65], zoom_start=6, tiles="CartoDB positron")

# Cluster
cluster = MarkerCluster().add_to(m)

# --- Agregar marcadores ---
for _, r in df_map.iterrows():
    lat, lon = float(r["latitud"]), float(r["longitud"])
    nombre = html.escape(str(r.get("nombre_colegio", "")))
    comuna = html.escape(str(r.get("nombre_comuna", "")))
    tipo = html.escape(str(r.get("dependencia_desc", "")))
    lenguaje = html.escape(str(r.get("promedio_lectura", "")))
    matematica = html.escape(str(r.get("promedio_matematica", "")))
    rural = html.escape(str(r.get("es_rural_desc", "")))
    grupoeconomico = html.escape(str(r.get("grupo_socioecon_desc", "")))
    popup_html = f"""

    <b>{nombre}</b><br>

    <b>Tipo:</b> {tipo}<br>

    <b>Comuna:</b> {comuna}<br>

    <b>Promedio lenguaje:</b> {lenguaje}<br>

    <b>Promedio matematica:</b> {matematica}<br>

    <b>Es rural:</b> {rural}<br>

    <b>Grupo socioeconomico:</b> {grupoeconomico}

    """
    folium.Marker(
        location=[lat, lon],
        popup=folium.Popup(popup_html, max_width=350),
        icon=folium.Icon(color=tipo_color(r.get("dependencia_desc")), icon="plus", prefix="fa"),
    ).add_to(cluster)

# --- Leyenda ---
legend_html = """

<div style="

 position: fixed; 

 bottom: 30px; left: 30px; z-index: 9999; 

 background: white; padding: 10px 12px; border: 1px solid #ddd; border-radius: 8px;

 color: black; font-size: 13px;">

<b style="color:black;">Leyenda</b><br>

<span style="display:inline-block;width:12px;height:12px;background:blue;margin-right:6px;"></span><span style="color:black;">Municipal Corporaci贸n</span><br>

<span style="display:inline-block;width:12px;height:12px;background:green;margin-right:6px;"></span><span style="color:black;">Municipal DAEM</span><br>

<span style="display:inline-block;width:12px;height:12px;background:orange;margin-right:6px;"></span><span style="color:black;">Particular subvencionado</span><br>

<span style="display:inline-block;width:12px;height:12px;background:purple;margin-right:6px;"></span><span style="color:black;">Particular pagado</span><br>

<span style="display:inline-block;width:12px;height:12px;background:red;margin-right:6px;"></span><span style="color:black;">Corporaci贸n de administraci贸n delegada</span><br>

<span style="display:inline-block;width:12px;height:12px;background:cadetblue;margin-right:6px;"></span><span style="color:black;">Servicio Local de Educaci贸n</span><br>

<span style="display:inline-block;width:12px;height:12px;background:gray;margin-right:6px;"></span><span style="color:black;">Otros</span>

</div>

"""
m.get_root().html.add_child(folium.Element(legend_html))

# --- Mostrar el mapa ---
st_folium(m, width=1200, height=650)

# --- Vista de tabla ---
with st.expander("Ver tabla filtrada"):
    columns_to_display = ['nombre_region', 'nombre_comuna', 'nombre_colegio', 'dependencia_desc', 'grupo_socioecon_desc', 'es_rural_desc', 'promedio_lectura', 'promedio_matematica']
    st.dataframe(df_map[columns_to_display])