Import circonscriptions et contours

This commit is contained in:
Emmy D'Anello 2024-06-15 00:11:31 +02:00
parent a85739159f
commit d913379421
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
3 changed files with 52 additions and 3 deletions

View File

@ -1,8 +1,8 @@
"""initial
Revision ID: 5bf5e6526891
Revision ID: d7fed76a55bd
Revises:
Create Date: 2024-06-14 23:04:55.436991
Create Date: 2024-06-15 00:03:00.687812
"""
from typing import Sequence, Union
@ -12,7 +12,7 @@ import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision: str = '5bf5e6526891'
revision: str = 'd7fed76a55bd'
down_revision: Union[str, None] = None
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None
@ -106,6 +106,7 @@ def upgrade() -> None:
sa.Column('id', sa.String(length=6), nullable=False),
sa.Column('departement_code', sa.String(length=3), nullable=False),
sa.Column('numero', sa.Integer(), nullable=False),
sa.Column('geometry', sa.JSON(), nullable=False),
sa.ForeignKeyConstraint(['departement_code'], ['departement.code_insee'], ),
sa.PrimaryKeyConstraint('id')
)

View File

@ -52,6 +52,7 @@ class Circonscription(Base):
id: Mapped[str] = mapped_column(String(6), primary_key=True)
departement_code: Mapped[str] = mapped_column(ForeignKey("departement.code_insee"))
numero: Mapped[int] = mapped_column(Integer())
geometry: Mapped[dict] = mapped_column(JSON())
departement: Mapped[Departement] = relationship(Departement)
bureaux_vote: Mapped[List["BureauVote"]] = relationship("BureauVote", back_populates="circonscription")

View File

@ -67,6 +67,52 @@ def importer_departements(engine: Engine, verbose: bool = False) -> None:
session.commit()
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)
session.commit()
def importer_communes(engine: Engine, verbose: bool = False) -> None:
etag = requests.get(
"https://public.opendatasoft.com/api/explore/v2.1/catalog/datasets"
@ -138,5 +184,6 @@ def importer_bureaux_vote(engine: Engine, verbose: bool = False) -> None:
def run(engine: Engine, verbose: bool = False) -> None:
importer_regions(engine, verbose)
importer_departements(engine, verbose)
importer_circonscriptions(engine, verbose)
importer_communes(engine, verbose)
importer_bureaux_vote(engine, verbose)