-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMapa Interactivo_2
54 lines (42 loc) · 2.04 KB
/
Mapa Interactivo_2
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
import folium
import json
import pandas as pd
import geopandas as gpd
# Cargar datos socioeconómicos y GeoJSON
data_socioeconomica_excel = pd.read_excel("./Downloads/t8598com.xlsx")
data_desempleo_excel = pd.read_excel("./Downloads/t4299com.xlsx")
with open("D:/Divisions_Territorials_Municipis_Catalunya/divisions-administratives-v2r1-comarques-500000-20230928.json", 'r', encoding='utf-8') as file:
geojson_data_comarcas = json.load(file)
# Convertir GeoJSON a GeoDataFrame
gdf = gpd.GeoDataFrame.from_features(geojson_data_comarcas['features'])
# Combinar ambos conjuntos de datos con el GeoDataFrame
gdf_combinado = gdf.merge(data_socioeconomica_excel, left_on='NOMCOMAR', right_on='Nom', how='left')
gdf_combinado = gdf_combinado.merge(data_desempleo_excel, left_on='NOMCOMAR', right_on='Nom', how='left', suffixes=('', '_desempleo'))
# Eliminar espacios adicionales en los nombres de las columnas
gdf_combinado.columns = gdf_combinado.columns.str.strip()
# Crear un mapa base
mapa = folium.Map(location=[41.5912, 1.5209], zoom_start=7)
# Función para el estilo y agregar tooltips
def add_features(mapa, gdf_combinado):
sectores = ['Agricultura', 'Indústria', 'Construcció', 'Serveis', 'Total']
sectores_desempleo = ['Atur Homes', 'Atur Dones', 'Total Atur']
for _, row in gdf_combinado.iterrows():
comarca = row['NOMCOMAR']
tooltip_text = f"Comarca: {comarca}<br>"
# Datos socioeconómicos
for sector in sectores:
value = row.get(sector, 'N/A')
tooltip_text += f"{sector}: {value}<br>"
# Datos de desempleo
for sector in sectores_desempleo:
value = row.get(sector, 'N/A')
tooltip_text += f"{sector}: {value}<br>"
folium.GeoJson(
row.geometry.__geo_interface__,
style_function=lambda feature: {'color': 'black', 'weight': 1, 'fillColor': '#ADD8E6', 'fillOpacity': 0.7},
tooltip=tooltip_text
).add_to(mapa)
# Aplicar la función al mapa
add_features(mapa, gdf_combinado)
# Mostrar el mapa
display(mapa)