|
| 1 | +# This script is to be run as a one-off to fix up some geometries in the May |
| 2 | +# 2016 edition of Boundary-Line that are incorrect. |
| 3 | + |
| 4 | +from optparse import make_option |
| 5 | +from django.core.management.base import NoArgsCommand |
| 6 | +from mapit.models import Area, CodeType, Generation, Geometry |
| 7 | + |
| 8 | + |
| 9 | +class Command(NoArgsCommand): |
| 10 | + help = 'Fix the UK Boundary-Line import for May 2016' |
| 11 | + option_list = NoArgsCommand.option_list + ( |
| 12 | + make_option('--commit', action='store_true', dest='commit', help='Actually update the database'), |
| 13 | + ) |
| 14 | + |
| 15 | + code_version = CodeType.objects.get(code='gss') |
| 16 | + |
| 17 | + def get_area(self, code): |
| 18 | + area = Area.objects.get(codes__code=code, codes__type=self.code_version) |
| 19 | + assert area.polygons.count() == 1 |
| 20 | + return area |
| 21 | + |
| 22 | + def get_generation_prior_to_current(self): |
| 23 | + latest_on = Generation.objects.filter(active=True).order_by('-id') |
| 24 | + if latest_on: |
| 25 | + return latest_on[1] |
| 26 | + return None |
| 27 | + |
| 28 | + def handle_noargs(self, **options): |
| 29 | + # The area that has been included as Cheriton and Bishops Sutton should |
| 30 | + # be part of Alresford & Itchen Valley. |
| 31 | + area_to_add_to = self.get_area('E05010995') # Alresford & Itchen Valley |
| 32 | + area_to_remove = self.get_area('E05004654') # Cheriton and Bishops Sutton |
| 33 | + |
| 34 | + self.stdout.write('Copying the area of %s to %s' % (area_to_remove, area_to_add_to)) |
| 35 | + polygon = area_to_remove.polygons.all()[0] |
| 36 | + polygon_copy = Geometry(area=area_to_add_to, polygon=polygon.polygon) |
| 37 | + polygon_copy.save() |
| 38 | + |
| 39 | + lower_generation = self.get_generation_prior_to_current() |
| 40 | + self.stdout.write('Setting the generation_high of %s to %s' % (area_to_remove, lower_generation)) |
| 41 | + area_to_remove.generation_high = lower_generation |
| 42 | + if lower_generation is None: |
| 43 | + self.stdout.write('Only one generation, so setting generation_low to None too') |
| 44 | + area_to_remove.generation_low = None |
| 45 | + |
| 46 | + if options['commit']: |
| 47 | + polygon.save() |
| 48 | + area_to_remove.save() |
0 commit comments