97 lines
3.9 KiB
Python
97 lines
3.9 KiB
Python
import json
|
|
import folium
|
|
from sklearn.preprocessing import MinMaxScaler
|
|
import random
|
|
from branca.colormap import linear
|
|
|
|
def get_random_color(colors):
|
|
return random.choice(colors)
|
|
|
|
|
|
# Charger les données depuis le fichier JSON
|
|
with open("elections_2024.json", "r") as f:
|
|
data = json.load(f)
|
|
|
|
# Charger les coordonnées des départements depuis le fichier GeoJSON
|
|
with open("departements.geojson", "r") as f:
|
|
departements_geo = json.load(f)
|
|
|
|
# Créer une carte centrée sur la France
|
|
m = folium.Map(location=[46.2276, 2.2137], zoom_start=6)
|
|
|
|
# Définir les couleurs pour le gradient
|
|
colors = ['#ffffff', '#b9b9b9', '#777777', '#353535']
|
|
|
|
# Normaliser les valeurs de percent_exprimes
|
|
percent_exprimes_values = [[float(value["pourcentage_exprimes"].replace(',', '.'))] for departement, values in data.items() for value in values if value["parti"] == "PARTI PIRATE"]
|
|
scaler = MinMaxScaler()
|
|
scaler.fit(percent_exprimes_values)
|
|
|
|
max_percent_exprimes = max(float(value["pourcentage_exprimes"].replace(',', '.')) for departement, values in data.items() for value in values if value["parti"] == "PARTI PIRATE")
|
|
|
|
|
|
colormap = linear.YlGn_09.scale(
|
|
0, max_percent_exprimes
|
|
)
|
|
|
|
# Parcourir les départements et ajouter des polygones avec le gradient de couleur
|
|
for departement, values in data.items():
|
|
for value in values:
|
|
if value["parti"] == "PARTI PIRATE":
|
|
percent_exprimes = float(value["pourcentage_exprimes"].replace(',', '.'))
|
|
normalized_percent_exprimes = scaler.transform([[percent_exprimes]])[0][0]
|
|
|
|
print(normalized_percent_exprimes)
|
|
|
|
color_index = int(normalized_percent_exprimes * (len(colors) - 1))
|
|
color = colors[color_index]
|
|
print('département:', departement, 'couleur:', color_index, color)
|
|
|
|
for feature in departements_geo["features"]:
|
|
|
|
if feature["properties"]["code"] == departement:
|
|
# Ajouter le champ percent_exprimes à l'élément GeoJSON
|
|
feature["properties"]["percent_exprimes"] = percent_exprimes
|
|
feature["properties"]["bg_fill"] = color
|
|
|
|
folium.GeoJson(
|
|
feature,
|
|
name=f"Département {departement}",
|
|
style_function=lambda feature: {
|
|
# "fillColor": get_random_color(colors),
|
|
"fillColor": colormap(feature["properties"]["percent_exprimes"]),
|
|
# "fillColor": '#9e9ac8',
|
|
"color": "black",
|
|
"weight": 1,
|
|
"fillOpacity": 0.7
|
|
},
|
|
tooltip=folium.features.GeoJsonTooltip(
|
|
fields=['nom', 'percent_exprimes', 'bg_fill'],
|
|
aliases=['Département:', 'Pourcentage:','Rempliassage'],
|
|
# style="background-color: white;",
|
|
localize=True,
|
|
sticky=False,
|
|
labels=True,
|
|
max_width=100,
|
|
direction='auto',
|
|
prefix='<b>',
|
|
suffix='</b>'
|
|
),
|
|
popup=folium.features.GeoJsonPopup(
|
|
fields=['percent_exprimes'],
|
|
aliases=['Pourcentage:'],
|
|
# style="background-color: white;",
|
|
localize=True,
|
|
sticky=False,
|
|
labels=True,
|
|
max_width=100,
|
|
direction='auto'
|
|
)
|
|
).add_to(m)
|
|
|
|
# Ajouter une légende
|
|
m.add_child(folium.LayerControl())
|
|
|
|
# Enregistrer la carte en HTML
|
|
m.save("france_map.html")
|