Skip to content

Commit

Permalink
[UK] Script for 2023 England council changes.
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Mar 31, 2023
1 parent d3de976 commit 17b587d
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@


class Command(BaseCommand):
county = 'Buckinghamshire County Council'
districts = (
'Aylesbury Vale District Council',
'South Bucks District Council',
'Wycombe District Council',
'Chiltern District Council'
)
counties = {
'Buckinghamshire County Council': (
'Aylesbury Vale District Council',
'South Bucks District Council',
'Wycombe District Council',
'Chiltern District Council'
)
}
new_utas = (
('Buckinghamshire Council', 'E06000060', []),
)
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@


class Command(BaseCommand):
county = 'Northamptonshire County Council'
districts = (
'Corby Borough Council',
'Daventry District Council',
'East Northamptonshire District Council',
'Kettering Borough Council',
'Northampton Borough Council',
'South Northamptonshire District Council',
'Wellingborough Borough Council',
)
counties = {
'Northamptonshire County Council': (
'Corby Borough Council',
'Daventry District Council',
'East Northamptonshire District Council',
'Kettering Borough Council',
'Northampton Borough Council',
'South Northamptonshire District Council',
'Wellingborough Borough Council',
)
}
new_utas = (
('North Northamptonshire', 'E06000061',
['Corby Borough Council',
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
import os
from django.contrib.gis.gdal import DataSource
from mapit.models import Area
from ..structural_changes import Command as BaseCommand


class Command(BaseCommand):
counties = {
"Cumbria County Council": (
"Allerdale Borough Council",
"Carlisle City Council",
"Copeland Borough Council",
"Barrow-in-Furness Borough Council",
"Eden District Council",
"South Lakeland District Council",
),
"North Yorkshire County Council": (
"Selby District Council",
"Harrogate Borough Council",
"Craven District Council",
"Richmondshire District Council",
"Hambleton District Council",
"Ryedale District Council",
"Scarborough Borough Council",
),
"Somerset County Council": (
"Mendip District Council",
"Sedgemoor District Council",
"Somerset West and Taunton District Council",
"South Somerset District Council",
),
}

new_utas = (
('Cumberland', 'E06000063', (
"Allerdale Borough Council",
"Carlisle City Council",
"Copeland Borough Council",
)),
('Westmorland and Furness', 'E06000064', (
"Barrow-in-Furness Borough Council",
"Eden District Council",
"South Lakeland District Council",
)),
("North Yorkshire County Council", "E06000065", (
"Selby District Council",
"Harrogate Borough Council",
"Craven District Council",
"Richmondshire District Council",
"Hambleton District Council",
"Ryedale District Council",
"Scarborough Borough Council",
)),
("Somerset County Council", "E06000066", (
"Mendip District Council",
"Sedgemoor District Council",
"Somerset West and Taunton District Council",
"South Somerset District Council",
)),
)

shapefiles = (
'Cumberland_interim_Wards.shp',
'Westmorland_and_Furness_interim_wards.shp',
'North_Yorkshire_interim_Electoral_Divisions.shp',
'Somerset_interim_Electoral_Divisions.shp',
)

def add_arguments(self, parser):
super().add_arguments(parser)
parser.add_argument('boundaries', help='Pass in the directory containing the shapefiles provided by OS')

def handle(self, *args, **options):
self.directory = options['boundaries']
self.verbosity = int(options['verbosity'])
super().handle(*args, **options)

def create_new_unitaries(self):
"""New areas come from passed in shapefiles manually sent to us by OS"""

# First the new councils themselves
for new_uta in self.new_utas:
area = Area.objects.filter(type__code='DIS', name__in=new_uta[2], generation_high=self.g)
self._create(new_uta[0], 'UTA', area, new_uta[1])

# And now their wards
for filename in self.shapefiles:
ds = DataSource(os.path.join(self.directory, filename))
layer = ds[0]
for feat in layer:
name = feat['WD22NM'].value or ''
ons_code = feat['WD22CD'].value
try:
m = Area.objects.get(codes__type=self.code_type, codes__code=ons_code)
if self.verbosity > 1:
print(" Area matched, %s" % (m, ))
except Area.DoesNotExist:
area_type = 'UTE' if 'Division' in filename else 'UTW'
self._create(name, area_type, feat.geom.geos, ons_code)
24 changes: 16 additions & 8 deletions mapit_gb/management/structural_changes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

from __future__ import print_function
from django.core.management.base import BaseCommand
from django.contrib.gis.geos import Polygon
from django.contrib.gis.db.models import Union
from mapit.models import Area, Generation, Type, Country, NameType, CodeType

Expand Down Expand Up @@ -36,8 +37,10 @@ def _exclude_councils_and_wards(self, qs, names, type_p, type_c):
def raise_generation_on_everything_else(self):
qs = Area.objects.filter(generation_high=self.g)
print('%d areas in database' % qs.count())
qs = self._exclude_councils_and_wards(qs, (self.county,), 'CTY', 'CED')
qs = self._exclude_councils_and_wards(qs, self.districts, 'DIS', 'DIW')

for county, districts in self.counties.items():
qs = self._exclude_councils_and_wards(qs, (county,), 'CTY', 'CED')
qs = self._exclude_councils_and_wards(qs, districts, 'DIS', 'DIW')

if self.commit:
print('Raising gen high on %d areas' % qs.count())
Expand All @@ -47,10 +50,14 @@ def raise_generation_on_everything_else(self):
print('Would raise gen high on %d areas' % qs.count())

def get_existing(self):
self.existing_cty = Area.objects.get(type__code='CTY', name=self.county)
self.existing = {}
for county in self.counties.keys():
self.existing[county] = Area.objects.get(type__code='CTY', name=county)

def _create(self, name, typ, area, gss=None):
if isinstance(area, Area):
if isinstance(area, Polygon):
geom = area
elif isinstance(area, Area):
assert area.polygons.count() == 1
geom = area.polygons.get().polygon
else:
Expand All @@ -77,9 +84,10 @@ def create_new_unitaries(self):
"""New areas are the existing county electoral divisions"""
for new_uta in self.new_utas:
if new_uta[2]:
area = Area.objects.filter(type__code='DIS', name__in=new_uta[2], generation_high=self.g)
areas = [Area.objects.filter(type__code='DIS', name__in=new_uta[2], generation_high=self.g)]
else:
area = self.existing_cty
self._create(new_uta[0], 'UTA', area, new_uta[1])
for area in self.areas.filter(type__code='CED', parent_area=self.existing_cty):
areas = self.existing.values()
for area in areas:
self._create(new_uta[0], 'UTA', area, new_uta[1])
for area in self.areas.filter(type__code='CED', parent_area__in=self.existing.values()):
self._create(area.name, 'UTW', area)

0 comments on commit 17b587d

Please sign in to comment.