Skip to content

Commit 17b587d

Browse files
committed
[UK] Script for 2023 England council changes.
1 parent d3de976 commit 17b587d

4 files changed

Lines changed: 134 additions & 25 deletions

File tree

mapit_gb/management/commands/mapit_UK_add_2020_structural_changes.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33

44
class Command(BaseCommand):
5-
county = 'Buckinghamshire County Council'
6-
districts = (
7-
'Aylesbury Vale District Council',
8-
'South Bucks District Council',
9-
'Wycombe District Council',
10-
'Chiltern District Council'
11-
)
5+
counties = {
6+
'Buckinghamshire County Council': (
7+
'Aylesbury Vale District Council',
8+
'South Bucks District Council',
9+
'Wycombe District Council',
10+
'Chiltern District Council'
11+
)
12+
}
1213
new_utas = (
1314
('Buckinghamshire Council', 'E06000060', []),
1415
)

mapit_gb/management/commands/mapit_UK_add_2021_structural_changes.py

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,17 @@
44

55

66
class Command(BaseCommand):
7-
county = 'Northamptonshire County Council'
8-
districts = (
9-
'Corby Borough Council',
10-
'Daventry District Council',
11-
'East Northamptonshire District Council',
12-
'Kettering Borough Council',
13-
'Northampton Borough Council',
14-
'South Northamptonshire District Council',
15-
'Wellingborough Borough Council',
16-
)
7+
counties = {
8+
'Northamptonshire County Council': (
9+
'Corby Borough Council',
10+
'Daventry District Council',
11+
'East Northamptonshire District Council',
12+
'Kettering Borough Council',
13+
'Northampton Borough Council',
14+
'South Northamptonshire District Council',
15+
'Wellingborough Borough Council',
16+
)
17+
}
1718
new_utas = (
1819
('North Northamptonshire', 'E06000061',
1920
['Corby Borough Council',
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
import os
2+
from django.contrib.gis.gdal import DataSource
3+
from mapit.models import Area
4+
from ..structural_changes import Command as BaseCommand
5+
6+
7+
class Command(BaseCommand):
8+
counties = {
9+
"Cumbria County Council": (
10+
"Allerdale Borough Council",
11+
"Carlisle City Council",
12+
"Copeland Borough Council",
13+
"Barrow-in-Furness Borough Council",
14+
"Eden District Council",
15+
"South Lakeland District Council",
16+
),
17+
"North Yorkshire County Council": (
18+
"Selby District Council",
19+
"Harrogate Borough Council",
20+
"Craven District Council",
21+
"Richmondshire District Council",
22+
"Hambleton District Council",
23+
"Ryedale District Council",
24+
"Scarborough Borough Council",
25+
),
26+
"Somerset County Council": (
27+
"Mendip District Council",
28+
"Sedgemoor District Council",
29+
"Somerset West and Taunton District Council",
30+
"South Somerset District Council",
31+
),
32+
}
33+
34+
new_utas = (
35+
('Cumberland', 'E06000063', (
36+
"Allerdale Borough Council",
37+
"Carlisle City Council",
38+
"Copeland Borough Council",
39+
)),
40+
('Westmorland and Furness', 'E06000064', (
41+
"Barrow-in-Furness Borough Council",
42+
"Eden District Council",
43+
"South Lakeland District Council",
44+
)),
45+
("North Yorkshire County Council", "E06000065", (
46+
"Selby District Council",
47+
"Harrogate Borough Council",
48+
"Craven District Council",
49+
"Richmondshire District Council",
50+
"Hambleton District Council",
51+
"Ryedale District Council",
52+
"Scarborough Borough Council",
53+
)),
54+
("Somerset County Council", "E06000066", (
55+
"Mendip District Council",
56+
"Sedgemoor District Council",
57+
"Somerset West and Taunton District Council",
58+
"South Somerset District Council",
59+
)),
60+
)
61+
62+
shapefiles = (
63+
'Cumberland_interim_Wards.shp',
64+
'Westmorland_and_Furness_interim_wards.shp',
65+
'North_Yorkshire_interim_Electoral_Divisions.shp',
66+
'Somerset_interim_Electoral_Divisions.shp',
67+
)
68+
69+
def add_arguments(self, parser):
70+
super().add_arguments(parser)
71+
parser.add_argument('boundaries', help='Pass in the directory containing the shapefiles provided by OS')
72+
73+
def handle(self, *args, **options):
74+
self.directory = options['boundaries']
75+
self.verbosity = int(options['verbosity'])
76+
super().handle(*args, **options)
77+
78+
def create_new_unitaries(self):
79+
"""New areas come from passed in shapefiles manually sent to us by OS"""
80+
81+
# First the new councils themselves
82+
for new_uta in self.new_utas:
83+
area = Area.objects.filter(type__code='DIS', name__in=new_uta[2], generation_high=self.g)
84+
self._create(new_uta[0], 'UTA', area, new_uta[1])
85+
86+
# And now their wards
87+
for filename in self.shapefiles:
88+
ds = DataSource(os.path.join(self.directory, filename))
89+
layer = ds[0]
90+
for feat in layer:
91+
name = feat['WD22NM'].value or ''
92+
ons_code = feat['WD22CD'].value
93+
try:
94+
m = Area.objects.get(codes__type=self.code_type, codes__code=ons_code)
95+
if self.verbosity > 1:
96+
print(" Area matched, %s" % (m, ))
97+
except Area.DoesNotExist:
98+
area_type = 'UTE' if 'Division' in filename else 'UTW'
99+
self._create(name, area_type, feat.geom.geos, ons_code)

mapit_gb/management/structural_changes.py

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import print_function
44
from django.core.management.base import BaseCommand
5+
from django.contrib.gis.geos import Polygon
56
from django.contrib.gis.db.models import Union
67
from mapit.models import Area, Generation, Type, Country, NameType, CodeType
78

@@ -36,8 +37,10 @@ def _exclude_councils_and_wards(self, qs, names, type_p, type_c):
3637
def raise_generation_on_everything_else(self):
3738
qs = Area.objects.filter(generation_high=self.g)
3839
print('%d areas in database' % qs.count())
39-
qs = self._exclude_councils_and_wards(qs, (self.county,), 'CTY', 'CED')
40-
qs = self._exclude_councils_and_wards(qs, self.districts, 'DIS', 'DIW')
40+
41+
for county, districts in self.counties.items():
42+
qs = self._exclude_councils_and_wards(qs, (county,), 'CTY', 'CED')
43+
qs = self._exclude_councils_and_wards(qs, districts, 'DIS', 'DIW')
4144

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

4952
def get_existing(self):
50-
self.existing_cty = Area.objects.get(type__code='CTY', name=self.county)
53+
self.existing = {}
54+
for county in self.counties.keys():
55+
self.existing[county] = Area.objects.get(type__code='CTY', name=county)
5156

5257
def _create(self, name, typ, area, gss=None):
53-
if isinstance(area, Area):
58+
if isinstance(area, Polygon):
59+
geom = area
60+
elif isinstance(area, Area):
5461
assert area.polygons.count() == 1
5562
geom = area.polygons.get().polygon
5663
else:
@@ -77,9 +84,10 @@ def create_new_unitaries(self):
7784
"""New areas are the existing county electoral divisions"""
7885
for new_uta in self.new_utas:
7986
if new_uta[2]:
80-
area = Area.objects.filter(type__code='DIS', name__in=new_uta[2], generation_high=self.g)
87+
areas = [Area.objects.filter(type__code='DIS', name__in=new_uta[2], generation_high=self.g)]
8188
else:
82-
area = self.existing_cty
83-
self._create(new_uta[0], 'UTA', area, new_uta[1])
84-
for area in self.areas.filter(type__code='CED', parent_area=self.existing_cty):
89+
areas = self.existing.values()
90+
for area in areas:
91+
self._create(new_uta[0], 'UTA', area, new_uta[1])
92+
for area in self.areas.filter(type__code='CED', parent_area__in=self.existing.values()):
8593
self._create(area.name, 'UTW', area)

0 commit comments

Comments
 (0)