nupes-elections/nupes/scripts/import_geographie.py

251 lines
100 KiB
Python
Raw Normal View History

import json
from datetime import datetime
import requests
from sqlalchemy import Engine, select
from sqlalchemy.orm import Session
from tqdm import tqdm
2024-06-08 21:06:41 +00:00
from nupes.cache import get_file
from nupes.models.geographie import BureauVote, Circonscription, Commune, Departement, Region
2024-06-13 10:21:11 +00:00
def importer_regions(engine: Engine, verbose: bool = False) -> None:
etag = requests.get(
"https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets"
"/georef-france-region?select=data_processed").json()['data_processed']
file = get_file("https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets"
"/georef-france-region/exports/geojson?lang=fr&timezone=Europe%2FParis",
"georef-france-region.geojson", etag)
with file.open('r') as f:
features = json.load(f)['features']
with Session(engine) as session:
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]
if region := session.execute(select(Region).filter_by(code_insee=code_region)).scalar_one_or_none():
region.libelle = nom_region
region.geometry = feature['geometry']
else:
region = Region(code_insee=code_region, libelle=nom_region, geometry=feature['geometry'])
session.add(region)
if reg := session.execute(select(Region).filter_by(code_insee="ZZ")).scalar_one_or_none():
reg.libelle = "Français⋅es de l'étranger"
else:
session.add(Region(code_insee="ZZ", libelle="Français⋅es de l'étranger", geometry={}))
session.commit()
2024-06-13 10:21:11 +00:00
def importer_departements(engine: Engine, verbose: bool = False) -> None:
etag = requests.get(
"https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets"
"/georef-france-departement?select=data_processed").json()['data_processed']
file = get_file("https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets"
"/georef-france-departement/exports/geojson?lang=fr&timezone=Europe%2FParis",
"georef-france-departement.geojson", etag)
with file.open('r') as f:
features = json.load(f)['features']
with Session(engine) as session:
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]
if dpt := session.execute(select(Departement).filter_by(code_insee=code_dpt)).scalar_one_or_none():
dpt.libelle = nom_dpt
dpt.region_code = dpt_dict['reg_code'][0]
dpt.geometry = feature['geometry']
else:
dpt = Departement(code_insee=code_dpt, libelle=nom_dpt, region_code=dpt_dict['reg_code'][0],
geometry=feature['geometry'])
session.add(dpt)
reg_etranger = session.execute(select(Region).filter_by(code_insee="ZZ")).scalar_one_or_none()
if dpt := session.execute(select(Departement).filter_by(code_insee="ZZ")).scalar_one_or_none():
dpt.libelle = "Français⋅es de l'étranger"
dpt.region_code = reg_etranger.code_insee
else:
session.add(Departement(code_insee="ZZ", region_code=reg_etranger.code_insee,
libelle="Français⋅es de l'étranger", geometry={}))
session.commit()
2024-06-14 22:11:31 +00:00
def importer_circonscriptions(engine: Engine, verbose: bool = False) -> None:
file = get_file("https://www.data.gouv.fr/fr/datasets/r/67c0f382-dc8d-4d1f-8a76-1162c53b9dfe",
"circonscriptions-legislatives-p10.geojson")
with file.open('r') as f:
features = json.load(f)['features']
with Session(engine) as session:
for feature in tqdm(features, desc="Circonscriptions", disable=not verbose):
circo_dict = feature['properties']
code_circo = circo_dict['codeCirconscription']
code_dpt = circo_dict['codeDepartement']
match code_dpt:
case "ZA":
code_dpt = "971"
case "ZB":
code_dpt = "972"
case "ZC":
code_dpt = "973"
case "ZD":
code_dpt = "974"
case "ZS":
code_dpt = "975"
case "ZM":
code_dpt = "976"
numero_circo = int(code_circo[len(code_dpt):])
circo_id = f"{code_dpt}-{numero_circo:02d}"
if not session.execute(select(Departement).filter_by(code_insee=code_dpt)).scalar_one_or_none():
print("Département non trouvé avec le code", code_dpt)
continue
if circo := session.execute(select(Circonscription).filter_by(id=circo_id)).scalar_one_or_none():
circo.departement_code = code_dpt
circo.numero = numero_circo
circo.geometry = feature['geometry']
else:
circo = Circonscription(id=circo_id, departement_code=code_dpt, numero=numero_circo,
geometry=feature['geometry'])
session.add(circo)
circos_manquantes = [
{"id": "977-01", "dpt_id": "977", "numero": 1, "geometry": {"type": "MultiPolygon", "coordinates": [[[[-63.0962231, 18.0626091], [-63.0810858, 18.0775442], [-63.0733853, 18.0992942], [-63.0594206, 18.1002068], [-63.0261544, 18.1244364], [-63.0186821, 18.0864266], [-63.0400725, 18.0558043], [-63.0630925, 18.0637612], [-63.0779648, 18.0494371], [-63.0962231, 18.0626091]]], [[[-62.8741868, 17.921114], [-62.8487618, 17.9173852], [-62.8400082, 17.9045077], [-62.8089408, 17.916332], [-62.7955482, 17.8976021], [-62.836895, 17.8782328], [-62.8568851, 17.9084379], [-62.8741868, 17.921114]]]]}},
{"id": "986-01", "dpt_id": "986", "numero": 1, "geometry": {"type": "MultiPolygon", "coordinates": [[[[-176.250845700000013, -13.2905881], [-176.2246769, -13.2779465], [-176.225896, -13.2524594], [-176.217776500000014, -13.2304078], [-176.1985703, -13.2188482], [-176.1623126, -13.2652837], [-176.183170499999989, -13.2977833], [-176.1993955, -13.3437724], [-176.2188721, -13.3480159], [-176.220019400000012, -13.3293755], [-176.2464393, -13.3138842], [-176.250845700000013, -13.2905881]]], [[[-178.0732659, -14.3461554], [-178.0674405, -14.3328053], [-178.0304501, -14.3235867], [-178.020343700000012, -14.3452517], [-178.041211, -14.3608773], [-178.0732659, -14.3461554]]], [[[-178.1817531, -14.2541568], [-178.178612, -14.2419722], [-178.131804499999987, -14.2592065], [-178.0879071, -14.2973257], [-178.0970111, -14.3085861], [-178.138506799999988, -14.3110959], [-178.168384, -14.2877385], [-178.1817531, -14.2541568]]]]}},
{"id": "987-01", "dpt_id": "987", "numero": 1, "geometry": { "type": "MultiPolygon", "coordinates": [[[[-150.6519841, -17.664573], [-150.6280461, -17.6339506], [-150.609507400000012, -17.6579606], [-150.61044480000001, -17.671378], [-150.6290152, -17.6802192], [-150.6519841, -17.664573]]], [[[-140.7236136, -8.0183416], [-140.71225720000001, -7.984662], [-140.6478894, -7.9546087], [-140.6394687, -7.973936], [-140.6837973, -8.0014269], [-140.6946399, -8.0276016], [-140.693979500000012, -8.046678], [-140.718686600000012, -8.0419891], [-140.7236136, -8.0183416]]], [[[-140.5991139, -7.9301536], [-140.58308310000001, -7.9225675], [-140.5582193, -7.8983243], [-140.5514852, -7.9092729], [-140.581088, -7.9310629], [-140.5991139, -7.9301536]]], [[[-139.132470399999988, -9.9782264], [-139.1216646, -9.9556726], [-139.118212699999987, -9.9299696], [-139.09761, -9.8937184], [-139.0657965, -9.8925865], [-139.0354839, -9.9290056], [-139.039257600000013, -9.9476863], [-139.0778161, -9.9564439], [-139.1016903, -9.9824592], [-139.1090299, -10.0062867], [-139.1243585, -10.0123891], [-139.132470399999988, -9.9782264]]], [[[-139.163654, -9.7761829], [-139.149958, -9.7584125], [-139.1326685, -9.7501672], [-139.0921167, -9.7440426], [-139.0514784, -9.7204604], [-139.0057894, -9.6986602], [-138.9846197, -9.6978483], [-138.9686026, -9.7089725], [-138.9615633, -9.7386328], [-138.895173, -9.7374691], [-138.877850599999988, -9.7652924], [-138.8616372, -9.7588146], [-138.8466152, -9.7712134], [-138.8821929, -9.7993121], [-138.9120294, -9.8082275], [-138.9600164, -9.8075675], [-138.9738806, -9.8020459], [-138.9919047, -9.8125329], [-139.0407289, -9.8065608], [-139.0723288, -9.8504307], [-139.10159250000001, -9.8471271], [-139.133196, -9.8288693], [-139.163654, -9.7761829]]], [[[-140.1357659, -9.3804539], [-140.128599, -9.3828039], [-140.0995019, -9.3477965], [-140.074235499999986, -9.3374747], [-140.0237749, -9.3666652], [-140.036835200000013, -9.4482146], [-140.053206499999987, -9.4661609], [-140.076097299999986, -9.4614452], [-140.095427300000011, -9.4347038], [-140.1305946, -9.3994648], [-140.1357659, -9.3804539]]], [[[-138.8421208, -9.9761737], [-138.825354800000014, -9.9739804], [-138.812691199999989, -9.9965577], [-138.8271196, -10.0061471], [-138.8421208, -9.9761737]]], [[[-138.6922348, -10.520104], [-138.676520399999987, -10.4951894], [-138.6729919, -10.4521527], [-138.6891464, -10.425858], [-138.656529, -10.42167], [-138.6132548, -10.4599586], [-138.6129132, -10.5035526], [-138.6363565, -10.5412218], [-138.66452670000001, -10.5408913], [-138.6922348, -10.520104]]], [[[-139.614458, -8.9220143], [-139.6034282, -8.8940112], [-139.5505729, -8.8653155], [-139.5085587, -8.8817651], [-139.486114, -8.9214481], [-139.5431484, -8.9313261], [-139.5912108, -8.9505286], [-139.614458, -8.9220143]]], [[[-140.2519428, -8.8247989], [-140.247368200000011, -8.7999327], [-140.2006218, -8.7821794], [-140.1568292, -8.7927946], [-140.125704, -8.8162535], [-140.106980600000014, -8.8096688], [-140.092098099999987, -8.8169331], [-140.0729318, -8.8032571], [-140.066427, -8.8254414], [-140.0538114, -8.8272718], [-140.036185499999988, -8.7997735], [-140.014711299999988, -8.8065925], [-140.046659299999988, -8.827925], [-140.0336077, -8.8460475], [-140.012434500000012, -8.8472179], [-140.0159272, -8.874392], [-140.00946110000001, -8.9033851], [-140.0288899, -8.9007037], [-140.0368492, -8.8849592], [-140.0532033, -8.8957564], [-140.0426843, -8.9129162], [-140.0823157, -8.925017], [-140.1047249, -8.9121983], [-140.108773, -8.9342903], [-140.132307200000014, -8.9431044], [-140.1699045, -8.9428567], [-140.1894847, -8.9601218], [-140.197847599999989, -8.9465403], [-140.225009300000011, -8.929522], [-140.2293546, -8.8821535], [-140.2485179, -8.8466101], [-140.2519428, -8.8247989]]], [[[-145.213262899999989, -19.8669427], [-145.2080728, -19.8234612], [-145.195310400000011, -19.7865424], [-145.1710969, -19.7467797], [-145.1542288, -19.727656], [-145.099858500000011, -19.6772658], [-145.0698708, -19.6584942], [-145.008476300000012, -19.6375906], [-144.9642409, -19.6339
{"id": "987-02", "dpt_id": "987", "numero": 2, "geometry": { "type": "MultiPolygon", "coordinates": [[[[-153.038424099999986, -22.6448564], [-153.0332558, -22.6035292], [-153.0195333, -22.5648842], [-152.9987697, -22.5305092], [-152.9670533, -22.4954454], [-152.9133888, -22.4595129], [-152.868428200000011, -22.4431992], [-152.8282436, -22.4360917], [-152.7675729, -22.4376125], [-152.70395, -22.4551294], [-152.6566583, -22.4823976], [-152.6184552, -22.5200231], [-152.594981, -22.5575912], [-152.5811215, -22.594171], [-152.5740429, -22.657453], [-152.582950899999986, -22.7083866], [-152.610660200000012, -22.765019], [-152.6550566, -22.8130233], [-152.695200699999987, -22.8390314], [-152.748278699999986, -22.85887], [-152.7815199, -22.8643972], [-152.8409259, -22.8625722], [-152.8846514, -22.8519269], [-152.92181930000001, -22.8349273], [-152.9552242, -22.8126504], [-152.994828899999987, -22.7739315], [-153.0262837, -22.7185203], [-153.0370791, -22.6710935], [-153.038424099999986, -22.6448564]]], [[[-143.7366199, -27.8853489], [-143.7316147, -27.8494733], [-143.7048892, -27.78659], [-143.6818625, -27.7530793], [-143.6459423, -27.719938], [-143.5985048, -27.6937027], [-143.5747189, -27.6849674], [-143.520286199999987, -27.6751266], [-143.4864528, -27.6748962], [-143.435683899999987, -27.6814069], [-143.39449590000001, -27.6936837], [-143.353790800000013, -27.7155476], [-143.3128213, -27.7480007], [-143.2937421, -27.7715186], [-143.272501, -27.8093298], [-143.258351, -27.8693321], [-143.2677026, -27.9391259], [-143.29316750000001, -27.9986809], [-143.3206333, -28.0316802], [-143.362451600000014, -28.0644728], [-143.3908535, -28.0786659], [-143.4426899, -28.0947924], [-143.4966506, -28.0990231], [-143.5589449, -28.0897807], [-143.6081044, -28.074115], [-143.6484663, -28.052496], [-143.6727885, -28.0337659], [-143.69941750000001, -28.0045612], [-143.722559600000011, -27.9636127], [-143.735198200000013, -27.9156432], [-143.7366199, -27.8853489]]], [[[-144.606319, -27.5960964], [-144.6009345, -27.5427442], [-144.586761599999988, -27.5014105], [-144.5499747, -27.448013], [-144.5096072, -27.4150526], [-144.4586984, -27.387271], [-144.4255283, -27.3762801], [-144.3608628, -27.3670361], [-144.2883347, -27.3714361], [-144.230578400000013, -27.389373], [-144.169934899999987, -27.4235338], [-144.1377334, -27.4534535], [-144.1117513, -27.4875425], [-144.0915252, -27.5276172], [-144.0821627, -27.5623333], [-144.076523, -27.6303228], [-144.0851313, -27.6856926], [-144.102560899999986, -27.7318278], [-144.127110499999986, -27.7674066], [-144.1605549, -27.7986008], [-144.1890252, -27.8170073], [-144.2313419, -27.8349963], [-144.2780032, -27.8477891], [-144.3295004, -27.8525776], [-144.376470899999987, -27.8477387], [-144.4229651, -27.8343146], [-144.49898, -27.7949174], [-144.5419595, -27.7587943], [-144.5648291, -27.729873], [-144.58778430000001, -27.6896595], [-144.6005375, -27.6530346], [-144.606319, -27.5960964]]], [[[-147.919209, -23.8984786], [-147.9181021, -23.8425525], [-147.9063104, -23.7915065], [-147.8924771, -23.7628372], [-147.8668319, -23.7283508], [-147.8179615, -23.6890638], [-147.7621914, -23.6656038], [-147.7147679, -23.6578015], [-147.6752342, -23.6426374], [-147.6264008, -23.6335001], [-147.5751656, -23.6337578], [-147.547418099999987, -23.638264], [-147.4946783, -23.6550933], [-147.4406533, -23.6887044], [-147.4197957, -23.7089006], [-147.387571, -23.7555024], [-147.3758121, -23.7849503], [-147.367494599999986, -23.835578], [-147.369089300000013, -23.8777934], [-147.3813686, -23.9320347], [-147.410874400000012, -23.9845109], [-147.4435454, -24.0196348], [-147.51139520000001, -24.0640514], [-147.5861301, -24.0938317], [-147.618931800000013, -24.1020814], [-147.6789483, -24.1067595], [-147.7305485, -24.1025889], [-147.763748099999987, -24.0938582], [-147.8109824, -24.0733777], [-147.850709599999988, -24.0460819], [-147.873783800000012, -24.0226716], [-147.8994123, -23.9836653], [-147.912203299999987, -23.9506154], [-147.919209, -23.8984786]]], [[[-149.747968, -23.3680274], [-149.7443637, -23.3331582], [-149.72
{"id": "987-03", "dpt_id": "987", "numero": 3, "geometry": { "type": "MultiPolygon", "coordinates": [[[[-149.5813852, -17.5448667], [-149.5825792, -17.5563973], [-149.521774599999986, -17.5971371], [-149.512820299999987, -17.6234262], [-149.4945684, -17.6126505], [-149.4777379, -17.6182341], [-149.4832418, -17.6444309], [-149.5039056, -17.6610953], [-149.5285893, -17.653474], [-149.5716334, -17.6496707], [-149.5963583, -17.6640151], [-149.6131858, -17.6258377], [-149.6137694, -17.5905964], [-149.6208733, -17.5623657], [-149.6134197, -17.5519992], [-149.5813852, -17.5448667]]], [[[-151.5501212, -16.6201048], [-151.5456993, -16.604077], [-151.5155114, -16.5773987], [-151.4839398, -16.5844802], [-151.442873, -16.6051918], [-151.4371582, -16.6308823], [-151.4573174, -16.6490271], [-151.4562732, -16.6768276], [-151.472043, -16.6823991], [-151.488514400000014, -16.6605908], [-151.5036751, -16.6714631], [-151.5451615, -16.6417057], [-151.5501212, -16.6201048]]], [[[-151.02755160000001, -16.7789733], [-150.9822948, -16.7545427], [-150.9643817, -16.7632476], [-150.958648899999986, -16.7869679], [-150.983207599999986, -16.8105217], [-151.001341700000012, -16.8061796], [-151.02755160000001, -16.7789733]]], [[[-151.493491299999988, -16.7942601], [-151.490006599999987, -16.7463005], [-151.472916199999986, -16.7232946], [-151.443735299999986, -16.7264987], [-151.408005, -16.7864104], [-151.4079963, -16.8045607], [-151.3583501, -16.8351236], [-151.3563421, -16.8638785], [-151.3833232, -16.8769688], [-151.3942396, -16.8909176], [-151.4261248, -16.8927547], [-151.4455877, -16.916984], [-151.4704803, -16.9063437], [-151.45812140000001, -16.889135], [-151.4781757, -16.8502553], [-151.4813596, -16.825892], [-151.493491299999988, -16.7942601]]], [[[-151.0473354, -16.7552992], [-151.03373, -16.7154913], [-151.0408916, -16.7047068], [-151.0024564, -16.6965301], [-150.985791299999988, -16.7018842], [-150.986676100000011, -16.7223917], [-150.9774914, -16.7450107], [-151.0013003, -16.7475919], [-151.02265890000001, -16.7689156], [-151.0473354, -16.7552992]]], [[[-151.763596, -16.4738192], [-151.7466679, -16.4652399], [-151.7226908, -16.4788584], [-151.7202328, -16.5186763], [-151.732632700000011, -16.520961], [-151.7529357, -16.5064722], [-151.7513181, -16.4800991], [-151.763596, -16.4738192]]]]}},
{"id": "988-01", "dpt_id": "988", "numero": 1, "geometry": {"type": "MultiPolygon", "coordinates": [[[[166.4570625, -22.2228779], [166.4948262, -22.2262117], [166.4964669, -22.2536752], [166.4799009, -22.2568489], [166.460756800000013, -22.2793455], [166.4584619, -22.3106898], [166.4370757, -22.303758], [166.441406199999989, -22.2645445], [166.4269477, -22.2465556], [166.3989767, -22.2349237], [166.431085200000012, -22.2224769], [166.4570625, -22.2228779]]], [[[166.4710939, -20.6997581], [166.5156878, -20.6713433], [166.54620030000001, -20.6349014], [166.5601437, -20.5954324], [166.5605343, -20.5602855], [166.57227850000001, -20.5191934], [166.593341, -20.481324], [166.5921511, -20.4421922], [166.5508184, -20.4042541], [166.5888474, -20.391861], [166.6073897, -20.3936595], [166.6252733, -20.4283412], [166.6582922, -20.4512005], [166.6671347, -20.4661874], [166.6090611, -20.4713577], [166.604426, -20.4854323], [166.5810486, -20.5103623], [166.58199110000001, -20.537108], [166.5715706, -20.5465683], [166.5760508, -20.5693654], [166.5693695, -20.5878889], [166.5839258, -20.6039205], [166.61284280000001, -20.5976035], [166.6093463, -20.6161726], [166.5678374, -20.6882544], [166.5053615, -20.7060615], [166.4710939, -20.6997581]]], [[[167.7937511, -21.0915304], [167.8154667, -21.0858568], [167.8395636, -21.1161721], [167.8234464, -21.1287658], [167.7937511, -21.0915304]]], [[[167.800344200000012, -21.3813244], [167.83895290000001, -21.375015], [167.874326599999989, -21.400139], [167.89654010000001, -21.3982129], [167.8988338, -21.3822229], [167.9459147, -21.3672534], [167.98254270000001, -21.4399213], [167.999671, -21.4578791], [168.0296649, -21.4666688], [168.0545688, -21.4545801], [168.083463200000011, -21.4545907], [168.1149896, -21.4339793], [168.1362245, -21.4484887], [168.1193284, -21.475408], [168.1245554, -21.5027045], [168.113346400000012, -21.5138938], [168.114390500000013, -21.5445257], [168.1081366, -21.5894374], [168.1153606, -21.6273797], [168.048076900000012, -21.6416324], [168.0397538, -21.6584625], [167.9836927, -21.657399], [167.960986600000012, -21.6504746], [167.9355979, -21.6049476], [167.909449200000012, -21.5975158], [167.8639259, -21.5985386], [167.8645626, -21.5749886], [167.878796099999988, -21.5505103], [167.8537732, -21.5270702], [167.832480800000013, -21.4808628], [167.8393362, -21.4516752], [167.8296212, -21.4253409], [167.8076906, -21.4064145], [167.800344200000012, -21.3813244]]], [[[167.0099999, -20.9188888], [167.0410875, -20.9114831], [167.061843900000014, -20.9269346], [167.1240654, -20.9065166], [167.1410648, -20.8724233], [167.1638575, -20.857723], [167.180218599999989, -20.8147775], [167.1705317, -20.7928618], [167.1483354, -20.7835567], [167.1240522, -20.7863878], [167.1017713, -20.7652297], [167.0357496, -20.7795015], [167.0397661, -20.732092], [167.064709, -20.7151918], [167.0999285, -20.7100042], [167.135689, -20.7241062], [167.163008499999989, -20.7117968], [167.1616803, -20.6897916], [167.179613, -20.6807807], [167.211595499999987, -20.6798096], [167.2482225, -20.6955513], [167.2763582, -20.7271479], [167.298144, -20.7268049], [167.3007665, -20.7464436], [167.276128699999987, -20.7640098], [167.2738957, -20.8285801], [167.290513299999986, -20.873331], [167.257395300000013, -20.9072379], [167.2741748, -20.9197909], [167.3016892, -20.9184638], [167.319778899999989, -20.9370063], [167.349934, -20.9383951], [167.3764826, -20.9542404], [167.3852483, -20.9801101], [167.381558, -21.0151614], [167.412088899999986, -21.0350885], [167.4334311, -21.0365585], [167.4584561, -21.066564], [167.4262454, -21.0836256], [167.4086734, -21.0991885], [167.414495499999987, -21.1639099], [167.382314, -21.1835545], [167.338219400000014, -21.1826454], [167.3138976, -21.1516836], [167.3132869, -21.1375764], [167.2672508, -21.1227576], [167.2705025, -21.1021249], [167.2607265, -21.0904169], [167.2160405, -21.0913924], [167.1786247, -21.0832558], [167.1567292, -21.0862376], [167.12716, -21.0703961], [167.111507, -21.0493576], [167.096062, -21.0428571], [167.0891565, -21.0195198], [167.0545888, -20.
{"id": "988-02", "dpt_id": "988", "numero": 2, "geometry": { "type": "MultiPolygon", "coordinates": [[[[164.1559595, -20.0846892], [164.1822953, -20.0783751], [164.216349900000012, -20.1091748], [164.231996900000013, -20.1429792], [164.207226, -20.1434991], [164.1811049, -20.1239509], [164.16358790000001, -20.0853688], [164.1559595, -20.0846892]]], [[[166.7721799, -22.4411543], [166.785250600000012, -22.4334774], [166.776693200000011, -22.4074709], [166.785614399999986, -22.3982807], [166.8393, -22.3974213], [166.8476316, -22.4154598], [166.8330779, -22.4509022], [166.8175536, -22.4665719], [166.7827897, -22.4614331], [166.7721799, -22.4411543]]], [[[164.2713731, -20.2124506], [164.2880673, -20.2228583], [164.300102900000013, -20.2601077], [164.2906864, -20.2613434], [164.2713731, -20.2124506]]], [[[163.570347, -19.5548696], [163.5871895, -19.5547448], [163.610425, -19.5889559], [163.5854783, -19.5951582], [163.570347, -19.5548696]]], [[[166.4948262, -22.2262117], [166.4570625, -22.2228779], [166.437535700000012, -22.1900944], [166.4213866, -22.1735024], [166.3849008, -22.1689048], [166.37207, -22.2071866], [166.3453891, -22.1932437], [166.333509099999986, -22.1973746], [166.3211296, -22.1694752], [166.3234909, -22.152711], [166.2690675, -22.1700276], [166.2221135, -22.1394298], [166.218707, -22.1254282], [166.182529, -22.1053896], [166.1618775, -22.0843172], [166.137722200000013, -22.0982048], [166.1226805, -22.0908285], [166.130698200000012, -22.0638862], [166.145812400000011, -22.074395], [166.162593, -22.0701808], [166.1684367, -22.0298981], [166.1368004, -21.9809553], [166.1478505, -21.9620313], [166.1232522, -21.9472984], [166.0928677, -21.9471691], [166.082016900000013, -21.9363814], [166.0782783, -21.9133694], [166.062046400000014, -21.8977964], [166.0441774, -21.9004932], [166.0189789, -21.9303557], [166.0140481, -21.9479874], [165.9904372, -21.9590534], [165.9780055, -21.9290168], [165.9496385, -21.9253941], [165.9424074, -21.9075575], [165.9067972, -21.8704328], [165.8523898, -21.8586385], [165.842804300000012, -21.8683343], [165.819004299999989, -21.8635337], [165.8332911, -21.8457307], [165.817228, -21.8188046], [165.801713299999989, -21.810503], [165.801762599999989, -21.7900466], [165.760931699999986, -21.7891877], [165.7692949, -21.7669992], [165.7274779, -21.7445789], [165.703298, -21.7504941], [165.6816901, -21.7245057], [165.6616679, -21.7139762], [165.627704, -21.7349228], [165.6242516, -21.710669], [165.60982, -21.7136876], [165.563938799999988, -21.7081164], [165.5311149, -21.6985685], [165.50709470000001, -21.6843536], [165.4718594, -21.6542148], [165.45829040000001, -21.6076147], [165.4368744, -21.6060631], [165.428659, -21.62], [165.3928467, -21.6124117], [165.329265099999986, -21.5866462], [165.2870484, -21.5734803], [165.266922099999988, -21.5567205], [165.260665, -21.5077702], [165.234508299999987, -21.5151351], [165.197273700000011, -21.4846182], [165.1677049, -21.4793849], [165.147871900000013, -21.4606831], [165.137290299999989, -21.4323863], [165.111481, -21.4049999], [165.1281897, -21.3875042], [165.1094697, -21.3675914], [165.094413300000014, -21.3716231], [165.072213, -21.3912179], [165.0458485, -21.3449325], [164.9885032, -21.3132894], [164.9720015, -21.3509321], [164.947123800000014, -21.3567193], [164.9489399, -21.293265], [164.910481800000014, -21.2783032], [164.8942975, -21.2442662], [164.8672483, -21.2277938], [164.8584826, -21.2035809], [164.8326597, -21.183746], [164.8317284, -21.1530188], [164.8611623, -21.1382427], [164.8552637, -21.1038603], [164.838397, -21.1082718], [164.8284349, -21.0662996], [164.7904768, -21.054839], [164.7755831, -21.0633081], [164.7688106, -21.0849098], [164.7529318, -21.0788458], [164.757637100000011, -21.0597639], [164.7450355, -21.0496065], [164.7231668, -21.0637544], [164.6932373, -21.0395856], [164.6897497, -21.0017249], [164.7094894, -21.0040772], [164.7067892, -20.9825698], [164.6556483, -20.9841485], [164.6723464, -20.9342235], [164.6216822, -20.9163504], [164.5890382, -20.9273479], [164.569130300000012, -20.892148], [164.552801, -
{"id": "ZZ-01", "dpt_id": "ZZ", "numero": 1, "geometry": {}},
{"id": "ZZ-02", "dpt_id": "ZZ", "numero": 2, "geometry": {}},
{"id": "ZZ-03", "dpt_id": "ZZ", "numero": 3, "geometry": {}},
{"id": "ZZ-04", "dpt_id": "ZZ", "numero": 4, "geometry": {}},
{"id": "ZZ-05", "dpt_id": "ZZ", "numero": 5, "geometry": {}},
{"id": "ZZ-06", "dpt_id": "ZZ", "numero": 6, "geometry": {}},
{"id": "ZZ-07", "dpt_id": "ZZ", "numero": 7, "geometry": {}},
{"id": "ZZ-08", "dpt_id": "ZZ", "numero": 8, "geometry": {}},
{"id": "ZZ-09", "dpt_id": "ZZ", "numero": 9, "geometry": {}},
{"id": "ZZ-10", "dpt_id": "ZZ", "numero": 10, "geometry": {}},
{"id": "ZZ-11", "dpt_id": "ZZ", "numero": 11, "geometry": {}},
]
for circo_dict in circos_manquantes:
if not session.execute(select(Departement).filter_by(code_insee=circo_dict['dpt_id'])).scalar_one_or_none():
print("Département non trouvé avec le code", circo_dict['dpt_id'])
continue
if circo := session.execute(select(Circonscription).filter_by(id=circo_dict['id'])).scalar_one_or_none():
circo.departement_code = circo_dict['dpt_id']
circo.numero = circo_dict['numero']
circo.geometry = circo_dict['geometry']
else:
circo = Circonscription(id=circo_dict['id'], departement_code=circo_dict['dpt_id'],
numero=circo_dict['numero'], geometry=circo_dict['geometry'])
session.add(circo)
2024-06-14 22:11:31 +00:00
session.commit()
2024-06-13 10:21:11 +00:00
def importer_communes(engine: Engine, verbose: bool = False) -> None:
etag = requests.get(
"https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets"
"/georef-france-commune-arrondissement-municipal?select=data_processed").json()['data_processed']
file = get_file("https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets"
"/georef-france-commune-arrondissement-municipal/exports/geojson?lang=fr&timezone=Europe%2FParis",
"georef-france-commune-arrondissement-municipal.geojson", etag)
with file.open('r') as f:
features = json.load(f)['features']
with Session(engine) as session:
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]
if commune := session.execute(select(Commune).filter_by(code_insee=code_commune)).scalar_one_or_none():
commune.libelle = nom_commune
commune.departement_code = commune_dict['dep_code'][0]
commune.geometry = feature['geometry']
else:
commune = Commune(code_insee=code_commune, libelle=nom_commune,
departement_code=commune_dict['dep_code'][0], geometry=feature['geometry'])
session.add(commune)
session.commit()
2024-06-13 10:21:11 +00:00
def importer_bureaux_vote(engine: Engine, verbose: bool = False) -> None:
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 tqdm(features, desc="Bureaux de vote", disable=not verbose):
bv_dict = feature['properties']
code_commune = bv_dict['id_bv'].split('_')[0]
code_bv = bv_dict['numeroBureauVote']
dpt_code = bv_dict['codeDepartement']
match dpt_code:
case "ZA":
dpt_code = "971"
case "ZB":
dpt_code = "972"
case "ZC":
dpt_code = "973"
case "ZD":
dpt_code = "974"
case "ZS":
dpt_code = "975"
case "ZM":
dpt_code = "976"
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['nomCommune'])
continue
if not session.execute(select(Circonscription).filter_by(id=code_circo)).scalar_one_or_none():
2024-06-13 09:50:18 +00:00
session.add(Circonscription(id=code_circo, departement_code=dpt_code, numero=numero_circo))
if bv := session.execute(select(BureauVote).filter_by(id=bv_id)).scalar_one_or_none():
bv.commune_code = code_commune
bv.code_bureau = code_bv
bv.circo_code = code_circo
bv.libelle = bv_libelle
bv.geometry = feature['geometry']
else:
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()
2024-06-13 10:21:11 +00:00
def run(engine: Engine, verbose: bool = False) -> None:
importer_regions(engine, verbose)
importer_departements(engine, verbose)
2024-06-14 22:11:31 +00:00
importer_circonscriptions(engine, verbose)
2024-06-13 10:21:11 +00:00
importer_communes(engine, verbose)
importer_bureaux_vote(engine, verbose)