Skip to content

Commit

Permalink
Use our register to link combined authorities
Browse files Browse the repository at this point in the history
We are ahead of the ONS publication! Need to use our data to be accurate
  • Loading branch information
ajparsons committed Feb 5, 2024
1 parent 7ecf12f commit 1e4fb16
Showing 1 changed file with 10 additions and 27 deletions.
37 changes: 10 additions & 27 deletions caps/management/commands/link_combined_authorities.py
Original file line number Diff line number Diff line change
@@ -1,36 +1,21 @@
from os.path import join

import pandas as pd
import requests
from caps.models import Council
from django.conf import settings
from django.core.management.base import BaseCommand, CommandError

# Download file link from https://geoportal.statistics.gov.uk/datasets/local-authority-district-to-combined-authority-december-2019-lookup-in-england
COMBINED_AUTHORITY_MAPPING_URL = "https://opendata.arcgis.com/api/v3/datasets/86b7c99d0fe042a2975880ff9ec51c1c_0/downloads/data?format=csv&spatialRefId=4326&where=1=1"
COMBINED_AUTHORITY_MAPPING_NAME = "council_to_combined_authority.csv"
COMBINED_AUTHORITY_MAPPING = join(settings.DATA_DIR, COMBINED_AUTHORITY_MAPPING_NAME)


def get_data_files():

data_files = [(COMBINED_AUTHORITY_MAPPING_URL, COMBINED_AUTHORITY_MAPPING)]

for (source, destination) in data_files:
r = requests.get(source)
with open(destination, "wb") as outfile:
outfile.write(r.content)
from caps.import_utils import get_council_df


def link_combined_authorities():
combined_authority_df = pd.read_csv(COMBINED_AUTHORITY_MAPPING)
for index, row in combined_authority_df.iterrows():
council_gss_code = row["LAD22CD"]
combined_auth_gss_code = row["CAUTH22CD"]
df = get_council_df()

council = Council.objects.get(gss_code=council_gss_code)
combined_authority = Council.objects.get(gss_code=combined_auth_gss_code)
# limit just to those that have 'combined-authority'
df = df[~df["combined-authority"].isna()]

# iterate and link
for _, row in df.iterrows():
council = Council.objects.get(authority_code=row["local-authority-code"])
combined_authority = Council.objects.get(
authority_code=row["combined-authority"]
)
council.combined_authority = combined_authority
council.save()

Expand All @@ -39,7 +24,5 @@ class Command(BaseCommand):
help = "Adds authority codes to the csv of plans"

def handle(self, *args, **options):
print("getting data files")
get_data_files()
print("linking combined authorities")
link_combined_authorities()

0 comments on commit 1e4fb16

Please sign in to comment.