Skip to content

Commit

Permalink
fixup! IMPORT: 2023 constituency data
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed Nov 6, 2023
1 parent 17e9db8 commit d32e612
Showing 1 changed file with 27 additions and 26 deletions.
53 changes: 27 additions & 26 deletions hub/management/commands/import_new_constituencies.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
# XXX uncomment when we import geometry below
# import json
import json

from django.conf import settings
from django.core.management.base import BaseCommand
from django.db.utils import IntegrityError

import pandas as pd
from tqdm import tqdm

from hub.models import Area, AreaType
Expand All @@ -13,7 +12,14 @@
class Command(BaseCommand):
help = "Import basic area information for new constituencies"

data_file = settings.BASE_DIR / "data" / "new_constituencies.csv"
"""
This uses the geopackage from
https://pages.mysociety.org/2025-constituencies/datasets/parliament_con_2025/latest
and then run
ogr2ogr -f GeoJSON new_constituencies.json parl_constituencies_2025.gpkg -simplify 0.001
to generate a GeoJSON file to import.
"""
data_file = settings.BASE_DIR / "data" / "new_constituencies.json"

def add_arguments(self, parser):
parser.add_argument(
Expand All @@ -23,33 +29,28 @@ def add_arguments(self, parser):
def handle(self, quiet: bool = False, *args, **options):
if not quiet:
print("Importing Areas")
df = pd.read_csv(self.data_file)
with open(self.data_file) as f:
cons = json.load(f)

area_type, created = AreaType.objects.get_or_create(
name="2023 Parliamentary Constituency",
code="WMC23",
area_type="Westminster Constituency",
description="Westminster Parliamentary Constituency boundaries, as created in 2023",
)

for index, area in tqdm(df.iterrows(), disable=quiet):
a, created = Area.objects.get_or_create(
gss=area["PCON25CD"],
name=area["PCON25NM"],
area_type=area_type,
)
"""
# skip this for now till we get actual geometry
geom = area[
[
"BNG_E",
"BNG_N",
"LAT",
"LONG",
"Shape__Area",
"Shape__Length",
"GlobalID",
]
].to_dict()
a.geometry = json.dumps(geom)
for con in tqdm(cons["features"], disable=quiet):
area = con["properties"]
try:
a, created = Area.objects.get_or_create(
gss=area["gss_code"],
name=area["name"],
area_type=area_type,
)
except IntegrityError as e:
print(f"error creating {area['name']}: {e}")
continue

con["properties"]["PCON13CD"] = area["gss_code"]
a.geometry = json.dumps(con)
a.save()
"""

0 comments on commit d32e612

Please sign in to comment.