From d32e612ed3024f8c4868ef6e105ef1f4c2e39564 Mon Sep 17 00:00:00 2001 From: Struan Donald Date: Mon, 6 Nov 2023 17:08:08 +0000 Subject: [PATCH] fixup! IMPORT: 2023 constituency data --- .../commands/import_new_constituencies.py | 53 ++++++++++--------- 1 file changed, 27 insertions(+), 26 deletions(-) diff --git a/hub/management/commands/import_new_constituencies.py b/hub/management/commands/import_new_constituencies.py index 00910b3d9..044c03e54 100644 --- a/hub/management/commands/import_new_constituencies.py +++ b/hub/management/commands/import_new_constituencies.py @@ -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 @@ -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( @@ -23,7 +29,9 @@ 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", @@ -31,25 +39,18 @@ def handle(self, quiet: bool = False, *args, **options): 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() - """