Importation des résultats officiels + séparation par circonscription

This commit is contained in:
2024-06-14 23:58:36 +02:00
parent c8bae98862
commit a85739159f
6 changed files with 373 additions and 193 deletions

View File

@ -4,6 +4,7 @@ from datetime import datetime
import requests
from sqlalchemy import Engine, select
from sqlalchemy.orm import Session
from tqdm import tqdm
from nupes.cache import get_file
from nupes.models.geographie import BureauVote, Circonscription, Commune, Departement, Region
@ -21,7 +22,7 @@ def importer_regions(engine: Engine, verbose: bool = False) -> None:
features = json.load(f)['features']
with Session(engine) as session:
for feature in features:
for feature in tqdm(features, desc="Régions", disable=not verbose):
region_dict = feature['properties']
code_region = region_dict['reg_code'][0]
nom_region = region_dict['reg_name'][0]
@ -49,7 +50,7 @@ def importer_departements(engine: Engine, verbose: bool = False) -> None:
features = json.load(f)['features']
with Session(engine) as session:
for feature in features:
for feature in tqdm(features, desc="Départements", disable=not verbose):
dpt_dict = feature['properties']
code_dpt = dpt_dict['dep_code'][0]
nom_dpt = dpt_dict['dep_name'][0]
@ -78,7 +79,7 @@ def importer_communes(engine: Engine, verbose: bool = False) -> None:
features = json.load(f)['features']
with Session(engine) as session:
for feature in features:
for feature in tqdm(features, desc="Communes", disable=not verbose):
commune_dict = feature['properties']
code_commune = commune_dict['com_arm_code'][0]
nom_commune = commune_dict['com_name'][0]
@ -96,40 +97,25 @@ def importer_communes(engine: Engine, verbose: bool = False) -> None:
def importer_bureaux_vote(engine: Engine, verbose: bool = False) -> None:
etag = requests.get(
"https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets"
"/elections-france-bureau-vote-2022?select=data_processed").json()['data_processed']
file = get_file("https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets"
"/elections-france-bureau-vote-2022/exports/geojson?lang=fr&timezone=Europe%2FParis",
"elections-france-bureau-vote-2022.geojson", etag)
file = get_file("https://files.data.gouv.fr/reu/contours-france-entiere-latest-v2.geojson",
"contours-france-entiere-latest-v2.geojson")
with file.open('r') as f:
features = json.load(f)['features']
with Session(engine) as session:
for feature in features:
for feature in tqdm(features, desc="Bureaux de vote", disable=not verbose):
bv_dict = feature['properties']
code_commune = bv_dict['com_code']
if not code_commune:
print(feature)
continue
code_commune = code_commune.split('/')[0]
code_bv = bv_dict['code'] or "0"
code_circo = bv_dict['circonscription_code']
bv_id = f"{code_commune}_{code_bv}"
bv_libelle = bv_dict['libelle'] or "Bureau unique"
dpt_code, numero_circo = code_circo.split('-')
numero_circo = int(numero_circo)
if dpt_code == "987" or dpt_code == "988":
# Les communes de la Polynésie française et de Nouvelle-Calédonie ne sont pas disponibles,
# on les crée à la volée
if not session.execute(select(Commune).filter_by(code_insee=code_commune)).scalar_one_or_none():
session.add(Commune(code_insee=code_commune, libelle=bv_dict['com_name'], departement_code=dpt_code,
geometry={}))
code_commune = bv_dict['id_bv'].split('_')[0]
code_bv = bv_dict['numeroBureauVote']
dpt_code = bv_dict['codeDepartement']
numero_circo = int(bv_dict['codeCirconscription'][len(dpt_code):])
code_circo = f"{dpt_code}-{numero_circo:02d}"
bv_id = bv_dict['id_bv'].split()[0]
bv_libelle = f"Bureau {code_bv}"
if not session.execute(select(Commune).filter_by(code_insee=code_commune)).scalar_one_or_none():
print("Commune non trouvée avec le code", code_commune, "et le nom", bv_dict['com_name'])
print("Commune non trouvée avec le code", code_commune, "et le nom", bv_dict['nomCommune'])
continue
if not session.execute(select(Circonscription).filter_by(id=code_circo)).scalar_one_or_none():
@ -140,43 +126,11 @@ def importer_bureaux_vote(engine: Engine, verbose: bool = False) -> None:
bv.code_bureau = code_bv
bv.circo_code = code_circo
bv.libelle = bv_libelle
bv.adresse = bv_dict['adresse']
else:
bv = BureauVote(id=bv_id, commune_code=code_commune, code_bureau=code_bv, circo_code=code_circo,
libelle=bv_libelle, adresse=bv_dict['adresse'],
geometry={})
session.add(bv)
session.commit()
def importer_contours_bureaux_vote(engine: Engine, verbose: bool = False) -> None:
file = get_file("https://www.data.gouv.fr/fr/datasets/r/f98165a7-7c37-4705-a181-bcfc943edc73",
"contours-bureaux-vote.geojson")
with file.open('r') as f:
features = json.load(f)['features']
with Session(engine) as session:
for feature in features:
bv_id: str = feature['properties']['id_bv']
com_code, bv_code = bv_id.split('_')
bv_code = bv_code.replace("-", " ").replace(".", " ").strip()
while len(bv_code) >= 2 and bv_code[0] == '0':
bv_code = bv_code[1:]
while " " in bv_code:
bv_code = bv_code.replace(" ", " ")
bv_id = f"{com_code}_{bv_code}"
if bv := session.execute(select(BureauVote).filter_by(id=bv_id)).scalar_one_or_none():
bv.geometry = feature['geometry']
else:
results = session.execute(select(BureauVote).filter_by(commune_code=com_code)).scalars().all()
if len(results) == 1:
bv = results[0]
bv.geometry = feature['geometry']
else:
print(f"Bureau de vote {bv_id} non trouvé")
bv = BureauVote(id=bv_id, commune_code=code_commune, code_bureau=code_bv, circo_code=code_circo,
libelle=bv_libelle, adresse="", geometry=feature['geometry'])
session.add(bv)
session.commit()
@ -186,4 +140,3 @@ def run(engine: Engine, verbose: bool = False) -> None:
importer_departements(engine, verbose)
importer_communes(engine, verbose)
importer_bureaux_vote(engine, verbose)
importer_contours_bureaux_vote(engine, verbose)