Skip to content

Commit

Permalink
[UK] Fix broken boundary in May 2016 Boundary-Line
Browse files Browse the repository at this point in the history
  • Loading branch information
dracos committed Jun 7, 2016
1 parent 17783e0 commit f8a2f78
Showing 1 changed file with 48 additions and 0 deletions.
48 changes: 48 additions & 0 deletions mapit_gb/management/commands/mapit_UK_fix_2016-05.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# This script is to be run as a one-off to fix up some geometries in the May
# 2016 edition of Boundary-Line that are incorrect.

from optparse import make_option
from django.core.management.base import NoArgsCommand
from mapit.models import Area, CodeType, Generation, Geometry


class Command(NoArgsCommand):
help = 'Fix the UK Boundary-Line import for May 2016'
option_list = NoArgsCommand.option_list + (
make_option('--commit', action='store_true', dest='commit', help='Actually update the database'),
)

code_version = CodeType.objects.get(code='gss')

def get_area(self, code):
area = Area.objects.get(codes__code=code, codes__type=self.code_version)
assert area.polygons.count() == 1
return area

def get_generation_prior_to_current(self):
latest_on = Generation.objects.filter(active=True).order_by('-id')
if latest_on:
return latest_on[1]
return None

def handle_noargs(self, **options):
# The area that has been included as Cheriton and Bishops Sutton should
# be part of Alresford & Itchen Valley.
area_to_add_to = self.get_area('E05010995') # Alresford & Itchen Valley
area_to_remove = self.get_area('E05004654') # Cheriton and Bishops Sutton

self.stdout.write('Copying the area of %s to %s' % (area_to_remove, area_to_add_to))
polygon = area_to_remove.polygons.all()[0]
polygon_copy = Geometry(area=area_to_add_to, polygon=polygon.polygon)
polygon_copy.save()

lower_generation = self.get_generation_prior_to_current()
self.stdout.write('Setting the generation_high of %s to %s' % (area_to_remove, lower_generation))
area_to_remove.generation_high = lower_generation
if lower_generation is None:
self.stdout.write('Only one generation, so setting generation_low to None too')
area_to_remove.generation_low = None

if options['commit']:
polygon.save()
area_to_remove.save()

0 comments on commit f8a2f78

Please sign in to comment.