Skip to content

Commit

Permalink
fixup! import_areas script with more diagnostic output
Browse files Browse the repository at this point in the history
  • Loading branch information
struan committed Sep 19, 2024
1 parent 5c290e0 commit 56accf3
Showing 1 changed file with 24 additions and 13 deletions.
37 changes: 24 additions & 13 deletions hub/management/commands/import_areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,20 @@ def add_arguments(self, parser):
"-q", "--quiet", action="store_true", help="Silence progress bars."
)

def handle(self, quiet: bool = False, *args, **options):
quiet = True
parser.add_argument(
"--diagnostics",
action="store_true",
help="Print out extra diagnostics - very verbose",
)

def handle(self, quiet: bool = False, diagnostics: bool = False, *args, **options):
mapit_client = mapit.MapIt()
for b_type in self.boundary_types:
areas = mapit_client.areas_of_type(b_type["mapit_type"])
print(
f"fetched mapit areas with type {b_type['mapit_type']}, our type {b_type['code']}"
)
if diagnostics:
print(
f"fetched mapit areas with type {b_type['mapit_type']}, our type {b_type['code']}"
)
area_type, created = AreaType.objects.get_or_create(
code=b_type["code"],
area_type=b_type["area_type"],
Expand All @@ -57,10 +63,12 @@ def handle(self, quiet: bool = False, *args, **options):
},
)

if not quiet:
if diagnostics or not quiet:
print(f"Importing {b_type['name']} Areas")
for area in tqdm(areas, disable=quiet):
print(f"looking at {area['name']}, mapit type {area['type']}")
disable = quiet or diagnostics
for area in tqdm(areas, disable=disable):
if diagnostics:
print(f"looking at {area['name']}, mapit type {area['type']}")
try:
geom = mapit_client.area_geometry(area["id"])
geom = {
Expand All @@ -77,9 +85,10 @@ def handle(self, quiet: bool = False, *args, **options):
print(f"could not find mapit area for {area['name']}")
geom = None

print(
f"creating area for {area['name']} with GSS {area['codes']['gss']}, mapit_id {area['id']}"
)
if diagnostics:
print(
f"creating area for {area['name']} with GSS {area['codes']['gss']}, mapit_id {area['id']}"
)
try:
a, created = Area.objects.update_or_create(
name=area["name"],
Expand All @@ -97,6 +106,8 @@ def handle(self, quiet: bool = False, *args, **options):

a.geometry = geom
a.save()
print("--")
if diagnostics:
print("--")

print("\n\033[31m######################\033[0m\n")
if diagnostics:
print("\n\033[31m######################\033[0m\n")

0 comments on commit 56accf3

Please sign in to comment.