-
Notifications
You must be signed in to change notification settings - Fork 88
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UK] Manual fix for May 2024 Boundary-Line.
- Loading branch information
mySociety services
committed
Jul 5, 2024
1 parent
a0968d5
commit 618e8bf
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
# This script is to be run as a one-off after an import of the May 2024 OS | ||
# Boundary-Line in order to fix an issue with it. Until this edition, the two | ||
# detached parts of CEDs Thorpe St Andrew and Lightwater, West End and Bisley | ||
# had the same Unit IDs as their main parts, and we matched/joined on that. But | ||
# this edition decided to change all the IDs and not even maintain this, so | ||
# when we imported, both parts matched to the same Area and one polygon | ||
# overwrote the other, leaving only one of the two in each CED. This script | ||
# manually puts the correct geometries back in for these two areas. | ||
|
||
import re | ||
from django.core.management.base import LabelCommand | ||
from django.contrib.gis.gdal import DataSource | ||
|
||
from mapit.models import Area | ||
from mapit.management.command_utils import save_polygons | ||
|
||
AREAS = ['Lightwater, West End and Bisley', 'Thorpe St Andrew'] | ||
|
||
|
||
def boundary_line_shapes(filename): | ||
for feat in DataSource(filename)[0]: | ||
name = feat['NAME'].value | ||
if not isinstance(name, str): | ||
name = name.decode('iso-8859-1') | ||
name = re.sub(r'\s*\(DET( NO \d+|)\)\s*(?i)', '', name) | ||
name = re.sub(r'\s+', ' ', name) | ||
name = name.replace('St.', 'St') | ||
name = name.replace(' ED', '') | ||
if name not in AREAS: | ||
continue | ||
feat.name = name | ||
yield feat | ||
|
||
|
||
class Command(LabelCommand): | ||
help = 'Fix issues with May 2024 OS Boundary-Line' | ||
label = '<May 2024 Boundary-Line county electoral division SHP file>' | ||
|
||
def add_arguments(self, parser): | ||
super(Command, self).add_arguments(parser) | ||
parser.add_argument('--commit', action='store_true', dest='commit', help='Actually update the database') | ||
|
||
def handle_label(self, filename, **options): | ||
geoms = {} | ||
for feat in boundary_line_shapes(filename): | ||
geoms.setdefault(feat.name, []).append(feat.geom) | ||
|
||
for name in AREAS: | ||
m = Area.objects.get(name=name, type__code='CED') | ||
print('Updating geometry of %s' % name) | ||
if options['commit']: | ||
save_polygons({"whatever": (m, geoms[name])}) |