From 3d60013670c2bd5ea4c054cb1c49380df0a0852c Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Thu, 20 Jun 2024 18:36:52 +0200 Subject: [PATCH] =?UTF-8?q?S=C3=A9paration=20des=20r=C3=A9sultats=20par=20?= =?UTF-8?q?tour?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...ons2024.js => ElectionsEuropeennes2024.js} | 36 +++-- .../src/includes/composants_elections.js | 129 +++++++++-------- nupes-elections-front/src/utils.js | 14 +- .../europeennes2024/export_resultats.py | 134 ++++++++++++------ .../legislatives2022/export_resultats.py | 87 +++++++----- 5 files changed, 243 insertions(+), 157 deletions(-) rename nupes-elections-front/src/{Elections2024.js => ElectionsEuropeennes2024.js} (69%) diff --git a/nupes-elections-front/src/Elections2024.js b/nupes-elections-front/src/ElectionsEuropeennes2024.js similarity index 69% rename from nupes-elections-front/src/Elections2024.js rename to nupes-elections-front/src/ElectionsEuropeennes2024.js index a46f4c9..c68dca3 100644 --- a/nupes-elections-front/src/Elections2024.js +++ b/nupes-elections-front/src/ElectionsEuropeennes2024.js @@ -16,7 +16,7 @@ import 'leaflet/dist/leaflet.css' highchartsItem(Highcharts) -export default function Election2024() { +export default function ElectionsEuropeennes2024() { const {typeResultats, zoneId} = useParams() const [grouperParBloc, setGrouperParBloc] = useState(false) @@ -25,7 +25,8 @@ export default function Election2024() { const [nuances, setNuances] = useState([]) const [listes, setListes] = useState([]) const [resultats, setResultats] = useState([]) - const [typeZone, setTypeZone] = useState("region") + const [typeSousZone, setTypeSousZone] = useState("region") + const tour = 1 useEffect(() => { fetch("/data/resultats/europeennes/2024/blocs.json").then(response => response.json()) @@ -46,12 +47,20 @@ export default function Election2024() { } }, [typeResultats, zoneId]) - const nomZone = useMemo(() => getNomZone(typeResultats, resultats), - [typeResultats, resultats]) + const zoneInfo = useMemo(() => resultats?.zone ?? {}, [resultats]) + const nomZone = useMemo(() => getNomZone(typeResultats, zoneInfo), [typeResultats, zoneInfo]) - const [voixParBloc, voixParNuance] = regrouperVoix(resultats.voix, listes, blocs, nuances) + const donnees = useMemo(() => { + if (tour === 1) + return resultats?.tour1 ?? {} + else if (tour === 2) + return resultats?.tour2 ?? {} + else + return {} + }, [resultats, tour]) + const [voixParBloc, voixParNuance] = regrouperVoix(donnees.voix, listes, blocs, nuances) - const siegesParListe = calculerSieges(listes, resultats, retirerSeuil ? 0 : 0.05) + const siegesParListe = calculerSieges(listes, donnees, retirerSeuil ? 0 : 0.05) const [siegesParBloc, siegesParNuance] = regrouperVoix(siegesParListe, listes, blocs, nuances) return <> @@ -59,22 +68,21 @@ export default function Election2024() { - + - - - + + + } diff --git a/nupes-elections-front/src/includes/composants_elections.js b/nupes-elections-front/src/includes/composants_elections.js index d1a558c..aaa3a4e 100644 --- a/nupes-elections-front/src/includes/composants_elections.js +++ b/nupes-elections-front/src/includes/composants_elections.js @@ -184,23 +184,37 @@ export function RetirerSeuil({retirerSeuil, setRetirerSeuil}) { label="Retirer le seuil des 5 %" /> } -export function SelectionAffichage({typeResultats, typeZone, setTypeZone}) { +export function SelectionTour({tour, setTour}) { + return + + Zone à afficher + + + +} + +export function SelectionAffichage({typeResultats, typeSousZone, setTypeSousZone}) { const items = useMemo(() => { const items = [] if (typeResultats === "france") { - setTypeZone("region") + setTypeSousZone("region") items.push(Région) } if (typeResultats === "france" || typeResultats === "region") { if (typeResultats !== "france") - setTypeZone("departement") + setTypeSousZone("departement") items.push(Département) } if (typeResultats === "france" || typeResultats === "region" || typeResultats === "departement") { if (typeResultats !== "france" && typeResultats !== "region") - setTypeZone("circonscription") + setTypeSousZone("circonscription") items.push(Circonscription) } @@ -209,19 +223,19 @@ export function SelectionAffichage({typeResultats, typeZone, setTypeZone}) { } if (typeResultats === "circonscription" || typeResultats === "commune" || typeResultats === "bureau_vote") { - setTypeZone("bureau_vote") + setTypeSousZone("bureau_vote") items.push(Bureau de vote) } return items - }, [typeResultats, setTypeZone]) + }, [typeResultats, setTypeSousZone]) return Zone à afficher - setTypeSousZone(event.target.value)} label="Zone à afficher"> {items} @@ -229,23 +243,26 @@ export function SelectionAffichage({typeResultats, typeZone, setTypeZone}) { } -function ZoneGeoJSON({typeElection, anneeElection, resultatsZone, typeZone, - candidats, blocs, nuances, grouperParBloc = false}) { +function ZoneGeoJSON({typeElection, anneeElection, resultatsZone, typeSousZone, + candidats, blocs, nuances, tour, grouperParBloc = false}) { + const sousZoneInfo = resultatsZone.zone + const donnees = resultatsZone ? (tour === 1 ? resultatsZone.tour1 : resultatsZone.tour2) : {} + const [idZone, nomZone] = useMemo(() => { - if (!resultatsZone[typeZone]) + if (!sousZoneInfo) return ["", ""] - if (typeZone === "region" || typeZone === "departement" || typeZone === "commune") - return [resultatsZone[typeZone].code_insee, resultatsZone[typeZone].nom] - else if (typeZone === "circonscription") - return [resultatsZone.circonscription.id, `Circonscription ${resultatsZone.circonscription.id}`] - else if (typeZone === "bureau_vote") - return [resultatsZone.bureau_vote.id, resultatsZone.bureau_vote.libelle] + if (typeSousZone === "region" || typeSousZone === "departement" || typeSousZone === "commune") + return [sousZoneInfo.code_insee, sousZoneInfo.nom] + else if (typeSousZone === "circonscription") + return [sousZoneInfo.id, `Circonscription ${sousZoneInfo.id}`] + else if (typeSousZone === "bureau_vote") + return [sousZoneInfo.id, sousZoneInfo.libelle] else return ["", ""] - }, [typeZone, resultatsZone]) + }, [typeSousZone, sousZoneInfo]) - const voixCandidats = useMemo(() => resultatsZone?.voix ?? {}, [resultatsZone]) + const voixCandidats = useMemo(() => donnees?.voix ?? {}, [resultatsZone]) const candidatsTries = trierCandidats(candidats, voixCandidats) const [voixParBloc, voixParNuance] = regrouperVoix(voixCandidats, candidats, blocs, nuances) @@ -271,59 +288,58 @@ function ZoneGeoJSON({typeElection, anneeElection, resultatsZone, typeZone, } return - {nomZone} + {nomZone}
    {candidatsTries.slice(0, 5).map(candidat =>
  • - {candidat.nom} : {voixCandidats[candidat.numero]} ({(100 * voixCandidats[candidat.numero] / resultatsZone.exprimes).toFixed(2)} %) + {candidat.nom} : {voixCandidats[candidat.numero]} ({(100 * voixCandidats[candidat.numero] / donnees.exprimes).toFixed(2)} %)
  • )}
} -function ContenuCarte({typeElection, anneeElection, typeResultats, resultats, - typeZone, candidats, blocs, nuances, grouperParBloc = false}) { +function ContenuCarte({typeElection, anneeElection, zoneInfo, typeSousZone, candidats, blocs, nuances, tour, + grouperParBloc = false}) { const map = useMap() const [resultatsZones, setResultatsZones] = useState([]) const zones = useMemo(() => { - const data = resultats[typeResultats] - if (!data) + if (!zoneInfo) return [] - if (typeZone === "region") - return data?.regions ?? [] - else if (typeZone === "departement") - return data?.departements ?? [] - else if (typeZone === "circonscription") - return data?.circonscriptions ?? [] - else if (typeZone === "commune") - return data?.communes ?? [] - else if (typeZone === "bureau_vote") { - if (typeResultats === "bureau_vote") - return data ? [data.id] : [] + if (typeSousZone === "region") + return zoneInfo?.regions ?? [] + else if (typeSousZone === "departement") + return zoneInfo?.departements ?? [] + else if (typeSousZone === "circonscription") + return zoneInfo?.circonscriptions ?? [] + else if (typeSousZone === "commune") + return zoneInfo?.communes ?? [] + else if (typeSousZone === "bureau_vote") { + if (zoneInfo.type === "bureau_vote") + return zoneInfo ? [zoneInfo.id] : [] else - return data?.bureaux_vote ?? [] + return zoneInfo?.bureaux_vote ?? [] } else return [] - }, [typeResultats, resultats, typeZone]) + }, [zoneInfo, typeSousZone]) useEffect(() => { - if (typeResultats === "france") + if (zoneInfo.type === "france") return - const geometry = resultats.geometry + const geometry = zoneInfo.geometry if (geometry) { // On centre la carte sur la zone const geometry_bbox = bbox(geometry) map.fitBounds([[geometry_bbox[1], geometry_bbox[0]], [geometry_bbox[3], geometry_bbox[2]]]) } - }, [typeResultats, resultats, map]) + }, [zoneInfo, map]) useEffect(() => { if (!zones) @@ -332,32 +348,33 @@ function ContenuCarte({typeElection, anneeElection, typeResultats, resultats, setResultatsZones(resultatsZones => []) zones.forEach(zoneId => { - fetch(`/data/resultats/${typeElection}/${anneeElection}/${typeZone}/${zoneId}.json`) + fetch(`/data/resultats/${typeElection}/${anneeElection}/${typeSousZone}/${zoneId}.json`) .then(response => response.json()) .then(resultatsZone => setResultatsZones(resultatsZones => [...resultatsZones, resultatsZone])) }) - }, [typeElection, anneeElection, typeZone, zones, resultats]) + }, [typeElection, anneeElection, typeSousZone, zones]) - function getZoneIdentifier(typeZone, zone) { - if (typeZone === "region" || typeZone === "departement" || typeZone === "commune") + function getZoneIdentifier(typeSousZone, zone) { + if (typeSousZone === "region" || typeSousZone === "departement" || typeSousZone === "commune") return zone.code_insee - else if (typeZone === "circonscription" || typeZone === "bureau_vote") + else if (typeSousZone === "circonscription" || typeSousZone === "bureau_vote") return zone.id else return "" } return <> - {resultatsZones.filter(resultatsZone => resultatsZone.geometry['type']).map(resultatsZone => - resultatsZone.zone.geometry['type']) + .map(resultatsZone => + )} + resultatsZone={resultatsZone} typeSousZone={typeSousZone} candidats={candidats} + blocs={blocs} nuances={nuances} tour={tour} grouperParBloc={grouperParBloc}/>)} } -export function CarteResultats({typeElection, anneeElection, typeResultats, resultats, - typeZone, candidats, blocs, nuances, grouperParBloc = false}) { +export function CarteResultats({typeElection, anneeElection, typeResultats, zoneInfo, typeSousZone, candidats, + blocs, nuances, tour, grouperParBloc = false}) { const center = typeResultats === "france" ? [46.603354, 1.888334] : [0, 0] return <> @@ -366,9 +383,9 @@ export function CarteResultats({typeElection, anneeElection, typeResultats, resu attribution='© Les contributeur⋅rices OpenStreetMap' url="https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png" /> - + } diff --git a/nupes-elections-front/src/utils.js b/nupes-elections-front/src/utils.js index 7d3f0aa..d6e4a95 100644 --- a/nupes-elections-front/src/utils.js +++ b/nupes-elections-front/src/utils.js @@ -1,18 +1,18 @@ -export function getNomZone(typeResultats, resultats) { - if (!resultats || resultats.length === 0) +export function getNomZone(typeResultats, zoneInfo) { + if (!zoneInfo) return "" else if (typeResultats === "france") return "France" else if (typeResultats === "region") - return `Région ${resultats.region.nom}` + return `Région ${zoneInfo.nom}` else if (typeResultats === "departement") - return `Département ${resultats.departement.nom}` + return `Département ${zoneInfo.nom}` else if (typeResultats === "circonscription") - return `Circonscription ${resultats.circonscription.id}` + return `Circonscription ${zoneInfo.id}` else if (typeResultats === "commune") - return `Commune ${resultats.commune.nom}` + return `Commune ${zoneInfo.nom}` else if (typeResultats === "bureau_vote") - return resultats.bureau_vote.libelle + return zoneInfo.libelle } export function trierCandidats(candidats, voix_par_candidat) { diff --git a/nupes/scripts/europeennes2024/export_resultats.py b/nupes/scripts/europeennes2024/export_resultats.py index 28d9a15..068ab24 100644 --- a/nupes/scripts/europeennes2024/export_resultats.py +++ b/nupes/scripts/europeennes2024/export_resultats.py @@ -8,7 +8,7 @@ from tqdm import tqdm from nupes.models.geographie import Circonscription, BureauVote from nupes.models.europeennes2024 import * -DATA_DIR = Path(__file__).parent.parent.parent / 'data' +DATA_DIR = Path(__file__).parent.parent.parent.parent / 'data' def exporter_listes(engine: Engine, verbose: bool = False) -> None: @@ -17,7 +17,11 @@ def exporter_listes(engine: Engine, verbose: bool = False) -> None: blocs_json = [] for bloc in blocs: - bloc_json = {'id': bloc.id, 'nom': bloc.nom, 'couleur': bloc.couleur} + bloc_json = { + 'id': bloc.id, + 'nom': bloc.nom, + 'couleur': bloc.couleur, + } blocs_json.append(bloc_json) file = DATA_DIR / "resultats" / "europeennes" / "2024" / "blocs.json" @@ -31,7 +35,11 @@ def exporter_listes(engine: Engine, verbose: bool = False) -> None: nuances_json = [] for nuance in nuances: - nuance_json = {'code': nuance.code, 'nom': nuance.nom, 'couleur': nuance.couleur} + nuance_json = { + 'code': nuance.code, + 'nom': nuance.nom, + 'couleur': nuance.couleur, + } nuances_json.append(nuance_json) file = DATA_DIR / "resultats" / "europeennes" / "2024" / "nuances.json" @@ -46,13 +54,25 @@ def exporter_listes(engine: Engine, verbose: bool = False) -> None: for liste in listes: candidats = [ - {'ordre': candidat.ordre, 'nom': candidat.nom, 'prenom': candidat.prenom, 'sexe': candidat.sexe.value, - 'date_naissance': candidat.date_naissance.isoformat(), 'profession': candidat.profession, - 'code_personnalite': candidat.code_personnalite.value, 'sortant': candidat.sortant} + { + 'ordre': candidat.ordre, + 'nom': candidat.nom, + 'prenom': candidat.prenom, + 'sexe': candidat.sexe.value, + 'date_naissance': candidat.date_naissance.isoformat(), + 'profession': candidat.profession, + 'code_personnalite': candidat.code_personnalite.value, + 'sortant': candidat.sortant + } for candidat in liste.candidats ] - liste_json = {'numero': liste.numero, 'nom': liste.nom, 'nuance': liste.nuance_id, - 'bloc': liste.bloc.nom, 'candidats': candidats} + liste_json = { + 'numero': liste.numero, + 'nom': liste.nom, + 'nuance': liste.nuance_id, + 'bloc': liste.bloc.nom, + 'candidats': candidats, + } listes_json.append(liste_json) file = DATA_DIR / "resultats" / "europeennes" / "2024" / "listes.json" @@ -71,20 +91,21 @@ def exporter_resultats_france(engine: Engine, verbose: bool = False) -> None: session.add(resultats_france) resultats_dict = { - 'france': { + 'zone': { + 'type': "france", 'regions': [reg.code_insee for reg in session.execute(select(Region)).scalars().all()], 'departements': [dpt.code_insee for dpt in session.execute(select(Departement)).scalars().all()], 'circonscriptions': [circo.id for circo in session.execute(select(Circonscription)).scalars().all()], + 'geometry': {}, }, - "tour1": { - "inscrits": resultats_france.inscrits, - "votants": resultats_france.votants, - "abstentions": resultats_france.abstentions, - "exprimes": resultats_france.exprimes, - "blancs": resultats_france.blancs, - "nuls": resultats_france.nuls, + 'tour1': { + 'inscrits': resultats_france.inscrits, + 'votants': resultats_france.votants, + 'abstentions': resultats_france.abstentions, + 'exprimes': resultats_france.exprimes, + 'blancs': resultats_france.blancs, + 'nuls': resultats_france.nuls, }, - 'geometry': {}, } resultats_listes = {} @@ -109,12 +130,17 @@ def exporter_resultats_regions(engine: Engine, verbose: bool = False) -> None: regions_iterator = tqdm(regions, desc="Régions") if verbose else regions for region in regions_iterator: - region_json = {'code_insee': region.code_insee, 'nom': region.libelle, - 'departements': [dpt.code_insee for dpt in region.departements], - 'circonscriptions': - [circo.id for circo in session.execute( - select(Circonscription).join(Departement).filter_by(region_code=region.code_insee)) - .scalars().all()]} + region_json = { + 'type': "region", + 'code_insee': region.code_insee, 'nom': region.libelle, + 'departements': [dpt.code_insee for dpt in region.departements], + 'circonscriptions': [ + circo.id for circo in session.execute( + select(Circonscription).join(Departement).filter_by(region_code=region.code_insee)) + .scalars().all() + ], + 'geometry': region.geometry, + } regions_json.append(region_json) resultats_region = session.execute(select(ResultatsRegionEuropeennes2024) @@ -126,7 +152,7 @@ def exporter_resultats_regions(engine: Engine, verbose: bool = False) -> None: session.add(resultats_region) resultats_dict = { - 'region': region_json, + 'zone': region_json, 'tour1': { 'inscrits': resultats_region.inscrits, 'votants': resultats_region.votants, @@ -135,7 +161,6 @@ def exporter_resultats_regions(engine: Engine, verbose: bool = False) -> None: 'blancs': resultats_region.blancs, 'nuls': resultats_region.nuls, }, - 'geometry': region.geometry, } resultats_listes = {} @@ -167,10 +192,15 @@ def exporter_resultats_departements(engine: Engine, verbose: bool = False) -> No iterator = tqdm(departements, desc="Départements") if verbose else departements for departement in iterator: - departement_json = {'code_insee': departement.code_insee, 'nom': departement.libelle, - 'region': departement.region_code, - 'circonscriptions': [circo.id for circo in departement.circonscriptions], - 'communes': [commune.code_insee for commune in departement.communes]} + departement_json = { + 'type': "departement", + 'code_insee': departement.code_insee, + 'nom': departement.libelle, + 'region': departement.region_code, + 'circonscriptions': [circo.id for circo in departement.circonscriptions], + 'communes': [commune.code_insee for commune in departement.communes], + 'geometry': departement.geometry, + } departements_json.append(departement_json) resultats_departement = session.execute( @@ -184,7 +214,7 @@ def exporter_resultats_departements(engine: Engine, verbose: bool = False) -> No session.add(resultats_departement) resultats_dict = { - 'departement': departement_json, + 'zone': departement_json, 'tour1': { 'inscrits': resultats_departement.inscrits, 'votants': resultats_departement.votants, @@ -193,7 +223,6 @@ def exporter_resultats_departements(engine: Engine, verbose: bool = False) -> No 'blancs': resultats_departement.blancs, 'nuls': resultats_departement.nuls, }, - 'geometry': departement.geometry, } resultats_listes = {} @@ -225,9 +254,14 @@ def exporter_resultats_circonscriptions(engine: Engine, verbose: bool = False) - iterator = tqdm(circonscriptions, desc="Circonscriptions") if verbose else circonscriptions for circonscription in iterator: - circonscription_json = {'id': circonscription.id, 'departement': circonscription.departement_code, - 'numero': circonscription.numero, - 'bureaux_vote': [bv.id for bv in circonscription.bureaux_vote]} + circonscription_json = { + 'type': "circonscription", + 'id': circonscription.id, + 'departement': circonscription.departement_code, + 'numero': circonscription.numero, + 'bureaux_vote': [bv.id for bv in circonscription.bureaux_vote], + 'geometry': circonscription.geometry, + } circonscriptions_json.append(circonscription_json) resultats_circonscription = session.execute( @@ -242,7 +276,7 @@ def exporter_resultats_circonscriptions(engine: Engine, verbose: bool = False) - session.add(resultats_circonscription) resultats_dict = { - 'circonscription': circonscription_json, + 'zone': circonscription_json, 'tour1': { 'inscrits': resultats_circonscription.inscrits, 'votants': resultats_circonscription.votants, @@ -251,7 +285,6 @@ def exporter_resultats_circonscriptions(engine: Engine, verbose: bool = False) - 'blancs': resultats_circonscription.blancs, 'nuls': resultats_circonscription.nuls, }, - 'geometry': circonscription.geometry, } resultats_listes = {} @@ -284,9 +317,14 @@ def exporter_resultats_communes(engine: Engine, verbose: bool = False) -> None: iterator = tqdm(communes, desc="Communes") if verbose else communes for commune in iterator: - commune_json = {'code_insee': commune.code_insee, 'nom': commune.libelle, - 'departement': commune.departement_code, - 'bureaux_vote': [bv.id for bv in commune.bureaux_vote]} + commune_json = { + 'type': "commune", + 'code_insee': commune.code_insee, + 'nom': commune.libelle, + 'departement': commune.departement_code, + 'bureaux_vote': [bv.id for bv in commune.bureaux_vote], + 'geometry': commune.geometry, + } communes_json.append(commune_json) resultats_commune = session.execute( @@ -299,7 +337,7 @@ def exporter_resultats_communes(engine: Engine, verbose: bool = False) -> None: session.add(resultats_commune) resultats_dict = { - 'commune': commune_json, + 'zone': commune_json, 'tour1': { 'inscrits': resultats_commune.inscrits, 'votants': resultats_commune.votants, @@ -308,7 +346,6 @@ def exporter_resultats_communes(engine: Engine, verbose: bool = False) -> None: 'blancs': resultats_commune.blancs, 'nuls': resultats_commune.nuls, }, - 'geometry': commune.geometry, } resultats_listes = {} @@ -340,10 +377,14 @@ def exporter_resultats_bureaux_vote(engine: Engine, verbose: bool = False) -> No iterator = tqdm(bureaux_vote, desc="Bureaux de vote") if verbose else bureaux_vote for bureau_vote in iterator: - bureau_vote_json = {'id': bureau_vote.id, - 'libelle': bureau_vote.libelle, - 'commune': bureau_vote.commune_code, - 'circonscription': bureau_vote.circo_code} + bureau_vote_json = { + 'type': "bureau_vote", + 'id': bureau_vote.id, + 'libelle': bureau_vote.libelle, + 'commune': bureau_vote.commune_code, + 'circonscription': bureau_vote.circo_code, + 'geometry': bureau_vote.geometry, + } bureaux_vote_json.append(bureau_vote_json) resultats_bureau_vote = session.execute( @@ -356,7 +397,7 @@ def exporter_resultats_bureaux_vote(engine: Engine, verbose: bool = False) -> No session.add(resultats_bureau_vote) resultats_dict = { - 'bureau_vote': bureau_vote_json, + 'zone': bureau_vote_json, 'tour1': { 'inscrits': resultats_bureau_vote.inscrits, 'votants': resultats_bureau_vote.votants, @@ -365,7 +406,6 @@ def exporter_resultats_bureaux_vote(engine: Engine, verbose: bool = False) -> No 'blancs': resultats_bureau_vote.blancs, 'nuls': resultats_bureau_vote.nuls, }, - 'geometry': bureau_vote.geometry, } resultats_listes = {} diff --git a/nupes/scripts/legislatives2022/export_resultats.py b/nupes/scripts/legislatives2022/export_resultats.py index 9d79949..045034b 100644 --- a/nupes/scripts/legislatives2022/export_resultats.py +++ b/nupes/scripts/legislatives2022/export_resultats.py @@ -8,7 +8,7 @@ from tqdm import tqdm from nupes.models.geographie import BureauVote from nupes.models.legislatives2022 import * -DATA_DIR = Path(__file__).parent.parent.parent / 'data' +DATA_DIR = Path(__file__).parent.parent.parent.parent / 'data' def exporter_nuances(engine: Engine, verbose: bool = False) -> None: @@ -77,10 +77,12 @@ def exporter_resultats_france(engine: Engine, verbose: bool = False) -> None: session.add(resultats_france) resultats_dict = { - 'france': { + 'zone': { + 'type': "france", 'regions': [reg.code_insee for reg in session.execute(select(Region)).scalars().all()], 'departements': [dpt.code_insee for dpt in session.execute(select(Departement)).scalars().all()], 'circonscriptions': [circo.id for circo in session.execute(select(Circonscription)).scalars().all()], + 'geometry': {}, }, 'tour1': { 'inscrits': resultats_france.inscrits_t1, @@ -98,7 +100,6 @@ def exporter_resultats_france(engine: Engine, verbose: bool = False) -> None: 'blancs': resultats_france.blancs_t2, 'nuls': resultats_france.nuls_t2, }, - 'geometry': {}, } resultats_t1, resultats_t2 = {}, {} @@ -125,12 +126,18 @@ def exporter_resultats_regions(engine: Engine, verbose: bool = False) -> None: regions_iterator = tqdm(regions, desc="Régions") if verbose else regions for region in regions_iterator: - region_json = {'code_insee': region.code_insee, 'nom': region.libelle, - 'departements': [dpt.code_insee for dpt in region.departements], - 'circonscriptions': - [circo.id for circo in session.execute( - select(Circonscription).join(Departement).filter_by(region_code=region.code_insee)) - .scalars().all()]} + region_json = { + 'type': "region", + 'code_insee': region.code_insee, + 'nom': region.libelle, + 'departements': [dpt.code_insee for dpt in region.departements], + 'circonscriptions': [ + circo.id for circo in session.execute( + select(Circonscription).join(Departement).filter_by(region_code=region.code_insee)) + .scalars().all() + ], + 'geometry': region.geometry, + } regions_json.append(region_json) resultats_region = session.execute(select(ResultatsRegionLegislatives2022) @@ -142,7 +149,7 @@ def exporter_resultats_regions(engine: Engine, verbose: bool = False) -> None: session.add(resultats_region) resultats_dict = { - 'region': region_json, + 'zone': region_json, 'tour1': { 'inscrits': resultats_region.inscrits_t1, 'votants': resultats_region.votants_t1, @@ -159,7 +166,6 @@ def exporter_resultats_regions(engine: Engine, verbose: bool = False) -> None: 'blancs': resultats_region.blancs_t2, 'nuls': resultats_region.nuls_t2, }, - 'geometry': region.geometry, } resultats_t1, resultats_t2 = {}, {} @@ -193,10 +199,15 @@ def exporter_resultats_departements(engine: Engine, verbose: bool = False) -> No iterator = tqdm(departements, desc="Départements") if verbose else departements for departement in iterator: - departement_json = {'code_insee': departement.code_insee, 'nom': departement.libelle, - 'region': departement.region_code, - 'circonscriptions': [circo.id for circo in departement.circonscriptions], - 'communes': [commune.code_insee for commune in departement.communes]} + departement_json = { + 'type': "departement", + 'code_insee': departement.code_insee, + 'nom': departement.libelle, + 'region': departement.region_code, + 'circonscriptions': [circo.id for circo in departement.circonscriptions], + 'communes': [commune.code_insee for commune in departement.communes], + 'geometry': departement.geometry, + } departements_json.append(departement_json) resultats_departement = session.execute( @@ -210,7 +221,7 @@ def exporter_resultats_departements(engine: Engine, verbose: bool = False) -> No session.add(resultats_departement) resultats_dict = { - 'departement': departement_json, + 'zone': departement_json, 'tour1': { 'inscrits': resultats_departement.inscrits_t1, 'votants': resultats_departement.votants_t1, @@ -227,7 +238,6 @@ def exporter_resultats_departements(engine: Engine, verbose: bool = False) -> No 'blancs': resultats_departement.blancs_t2, 'nuls': resultats_departement.nuls_t2, }, - 'geometry': departement.geometry, } resultats_t1, resultats_t2 = {}, {} @@ -261,9 +271,14 @@ def exporter_resultats_circonscriptions(engine: Engine, verbose: bool = False) - iterator = tqdm(circonscriptions, desc="Circonscriptions") if verbose else circonscriptions for circonscription in iterator: - circonscription_json = {'id': circonscription.id, 'departement': circonscription.departement_code, - 'numero': circonscription.numero, - 'bureaux_vote': [bv.id for bv in circonscription.bureaux_vote]} + circonscription_json = { + 'type': "circonscription", + 'id': circonscription.id, + 'departement': circonscription.departement_code, + 'numero': circonscription.numero, + 'bureaux_vote': [bv.id for bv in circonscription.bureaux_vote], + 'geometry': circonscription.geometry, + } circonscriptions_json.append(circonscription_json) resultats_circonscription = session.execute( @@ -278,7 +293,7 @@ def exporter_resultats_circonscriptions(engine: Engine, verbose: bool = False) - session.add(resultats_circonscription) resultats_dict = { - 'circonscription': circonscription_json, + 'zone': circonscription_json, 'tour1': { 'inscrits': resultats_circonscription.inscrits_t1, 'votants': resultats_circonscription.votants_t1, @@ -295,7 +310,6 @@ def exporter_resultats_circonscriptions(engine: Engine, verbose: bool = False) - 'blancs': resultats_circonscription.blancs_t2, 'nuls': resultats_circonscription.nuls_t2, }, - 'geometry': circonscription.geometry, } resultats_t1, resultats_t2 = {}, {} @@ -330,9 +344,14 @@ def exporter_resultats_communes(engine: Engine, verbose: bool = False) -> None: iterator = tqdm(communes, desc="Communes") if verbose else communes for commune in iterator: - commune_json = {'code_insee': commune.code_insee, 'nom': commune.libelle, - 'departement': commune.departement_code, - 'bureaux_vote': [bv.id for bv in commune.bureaux_vote]} + commune_json = { + 'type': "commune", + 'code_insee': commune.code_insee, + 'nom': commune.libelle, + 'departement': commune.departement_code, + 'bureaux_vote': [bv.id for bv in commune.bureaux_vote], + 'geometry': commune.geometry, + } communes_json.append(commune_json) resultats_commune = session.execute( @@ -345,7 +364,7 @@ def exporter_resultats_communes(engine: Engine, verbose: bool = False) -> None: session.add(resultats_commune) resultats_dict = { - 'commune': commune_json, + 'zone': commune_json, 'tour1': { 'inscrits': resultats_commune.inscrits_t1, 'votants': resultats_commune.votants_t1, @@ -362,7 +381,6 @@ def exporter_resultats_communes(engine: Engine, verbose: bool = False) -> None: 'blancs': resultats_commune.blancs_t2, 'nuls': resultats_commune.nuls_t2, }, - 'geometry': commune.geometry, } resultats_t1, resultats_t2 = {}, {} @@ -396,10 +414,14 @@ def exporter_resultats_bureaux_vote(engine: Engine, verbose: bool = False) -> No iterator = tqdm(bureaux_vote, desc="Bureaux de vote") if verbose else bureaux_vote for bureau_vote in iterator: - bureau_vote_json = {'id': bureau_vote.id, - 'libelle': bureau_vote.libelle, - 'commune': bureau_vote.commune_code, - 'circonscription': bureau_vote.circo_code} + bureau_vote_json = { + 'type': "bureau_vote", + 'id': bureau_vote.id, + 'libelle': bureau_vote.libelle, + 'commune': bureau_vote.commune_code, + 'circonscription': bureau_vote.circo_code, + 'geometry': bureau_vote.geometry, + } bureaux_vote_json.append(bureau_vote_json) resultats_bureau_vote = session.execute( @@ -412,7 +434,7 @@ def exporter_resultats_bureaux_vote(engine: Engine, verbose: bool = False) -> No session.add(resultats_bureau_vote) resultats_dict = { - 'bureau_vote': bureau_vote_json, + 'zone': bureau_vote_json, 'tour1': { 'inscrits': resultats_bureau_vote.inscrits_t1, 'votants': resultats_bureau_vote.votants_t1, @@ -429,7 +451,6 @@ def exporter_resultats_bureaux_vote(engine: Engine, verbose: bool = False) -> No 'blancs': resultats_bureau_vote.blancs_t2, 'nuls': resultats_bureau_vote.nuls_t2, }, - 'geometry': bureau_vote.geometry, } resultats_t1, resultats_t2 = {}, {}