Skip to content

Commit

Permalink
Add option to match boundaries requiring manual check in CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
GeoWill committed Jul 11, 2023
1 parent 862bcc9 commit c207d67
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 2 deletions.
15 changes: 14 additions & 1 deletion every_election/apps/organisations/boundaries/boundaryline.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import sys

from django.contrib.gis.gdal import DataSource, OGRGeometry
from django.contrib.gis.geos import MultiPolygon
from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
Expand All @@ -13,11 +15,12 @@


class BoundaryLine:
def __init__(self, filename):
def __init__(self, filename, show_picker=False):
ds = DataSource(filename)
if len(ds) != 1:
raise ValueError("Expected 1 layer, found %i" % (len(ds)))
self.layer = ds[0]
self.show_picker = show_picker

def merge_features(self, features):
polygons = []
Expand Down Expand Up @@ -166,6 +169,16 @@ def get_division_code(self, div, org):

warning = self.get_match_warning(div, matches[0])
if warning:
if self.show_picker:
sys.stdout.write(
f"""{warning}
Do you want to match {matches[0].get('name')} ({matches[0].get('code')}) to {div.official_identifier}?
"""
)
choice = input("enter 'y' to accept, or 'n' not to: ")
if choice.lower() in ("y", "yes"):
return self.get_code_from_feature(matches[0])

raise ObjectDoesNotExist(warning)

return self.get_code_from_feature(matches[0])
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ def check_valid_date(value):
dest="dry-run",
help="Don't commit changes",
)
parser.add_argument(
"--show-picker",
action="store_true",
dest="show-picker",
help="Show picker when requiring manual review",
)
super().add_arguments(parser)

def get_divisions(self, types, date):
Expand Down Expand Up @@ -130,7 +136,10 @@ def handle(self, *args, **options):
self.stdout.write("Searching...")
lookup = get_area_type_lookup(filter=lambda x: x in self.WARD_TYPES, group=True)
for org_type, filename in lookup.items():
bl = BoundaryLine(os.path.join(base_dir, "Data", "GB", filename))
bl = BoundaryLine(
os.path.join(base_dir, "Data", "GB", filename),
show_picker=options["show-picker"],
)
divs = self.get_divisions(org_type, options["date"])
for div in divs:
org = self.get_parent_org_boundary(div)
Expand Down

0 comments on commit c207d67

Please sign in to comment.