|
| 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) |
0 commit comments