2024-06-10 11:23:51 +02:00
|
|
|
import json
|
|
|
|
import folium
|
2024-06-10 11:51:58 +02:00
|
|
|
from sklearn.preprocessing import MinMaxScaler
|
|
|
|
import random
|
|
|
|
from branca.colormap import linear
|
|
|
|
|
|
|
|
def get_random_color(colors):
|
|
|
|
return random.choice(colors)
|
|
|
|
|
2024-06-10 11:23:51 +02:00
|
|
|
|
|
|
|
# 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
|
2024-06-10 11:51:58 +02:00
|
|
|
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)
|
2024-06-10 11:23:51 +02:00
|
|
|
|
|
|
|
max_percent_exprimes = max(float(value["pourcentage_exprimes"].replace(',', '.')) for departement, values in data.items() for value in values if value["parti"] == "PARTI PIRATE")
|
|
|
|
|
2024-06-10 11:51:58 +02:00
|
|
|
|
|
|
|
colormap = linear.YlGn_09.scale(
|
|
|
|
0, max_percent_exprimes
|
|
|
|
)
|
|
|
|
|
2024-06-10 11:23:51 +02:00
|
|
|
# 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(',', '.'))
|
2024-06-10 11:51:58 +02:00
|
|
|
normalized_percent_exprimes = scaler.transform([[percent_exprimes]])[0][0]
|
|
|
|
|
|
|
|
print(normalized_percent_exprimes)
|
|
|
|
|
|
|
|
color_index = int(normalized_percent_exprimes * (len(colors) - 1))
|
2024-06-10 11:23:51 +02:00
|
|
|
color = colors[color_index]
|
2024-06-10 11:51:58 +02:00
|
|
|
print('département:', departement, 'couleur:', color_index, color)
|
|
|
|
|
2024-06-10 11:23:51 +02:00
|
|
|
for feature in departements_geo["features"]:
|
2024-06-10 11:51:58 +02:00
|
|
|
|
2024-06-10 11:23:51 +02:00
|
|
|
if feature["properties"]["code"] == departement:
|
|
|
|
# Ajouter le champ percent_exprimes à l'élément GeoJSON
|
|
|
|
feature["properties"]["percent_exprimes"] = percent_exprimes
|
2024-06-10 11:51:58 +02:00
|
|
|
feature["properties"]["bg_fill"] = color
|
|
|
|
|
2024-06-10 11:23:51 +02:00
|
|
|
folium.GeoJson(
|
|
|
|
feature,
|
|
|
|
name=f"Département {departement}",
|
2024-06-10 11:51:58 +02:00
|
|
|
style_function=lambda feature: {
|
|
|
|
# "fillColor": get_random_color(colors),
|
|
|
|
"fillColor": colormap(feature["properties"]["percent_exprimes"]),
|
|
|
|
# "fillColor": '#9e9ac8',
|
2024-06-10 11:23:51 +02:00
|
|
|
"color": "black",
|
|
|
|
"weight": 1,
|
|
|
|
"fillOpacity": 0.7
|
|
|
|
},
|
|
|
|
tooltip=folium.features.GeoJsonTooltip(
|
2024-06-10 11:51:58 +02:00
|
|
|
fields=['nom', 'percent_exprimes', 'bg_fill'],
|
|
|
|
aliases=['Département:', 'Pourcentage:','Rempliassage'],
|
|
|
|
# style="background-color: white;",
|
2024-06-10 11:23:51 +02:00
|
|
|
localize=True,
|
|
|
|
sticky=False,
|
|
|
|
labels=True,
|
|
|
|
max_width=100,
|
|
|
|
direction='auto',
|
|
|
|
prefix='<b>',
|
|
|
|
suffix='</b>'
|
|
|
|
),
|
|
|
|
popup=folium.features.GeoJsonPopup(
|
|
|
|
fields=['percent_exprimes'],
|
|
|
|
aliases=['Pourcentage:'],
|
2024-06-10 11:51:58 +02:00
|
|
|
# style="background-color: white;",
|
2024-06-10 11:23:51 +02:00
|
|
|
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")
|