From f806ffc2f61a1b3c64f0e54cff558b771b56699a Mon Sep 17 00:00:00 2001 From: Leo Hemsted Date: Mon, 24 Aug 2020 20:40:42 +0100 Subject: [PATCH] add keep-old-features flag got annoyed waiting to regenerate unsimplified geometry --- .../create-broadcast-areas-db.py | 50 +++++++++++-------- app/broadcast_areas/repo.py | 16 ++++-- 2 files changed, 42 insertions(+), 24 deletions(-) diff --git a/app/broadcast_areas/create-broadcast-areas-db.py b/app/broadcast_areas/create-broadcast-areas-db.py index 387a41b78..a593a7f91 100755 --- a/app/broadcast_areas/create-broadcast-areas-db.py +++ b/app/broadcast_areas/create-broadcast-areas-db.py @@ -1,6 +1,7 @@ #!/usr/bin/env python import csv +import sys from pathlib import Path import geojson @@ -23,6 +24,9 @@ def simplify_geometry(feature): def polygons_and_simplified_polygons(feature): + if keep_old_polygons: + # cheat and shortcut out + return [], [] polygons = Polygons(simplify_geometry(feature)) full_resolution = polygons.remove_too_small @@ -50,8 +54,6 @@ def polygons_and_simplified_polygons(feature): ) -repo = BroadcastAreasRepository() - ctry19_filepath = source_files_path / "Countries.geojson" # https://geoportal.statistics.gov.uk/datasets/wards-may-2020-boundaries-uk-bgc @@ -114,7 +116,7 @@ def add_countries(): feature, simple_feature, ]) - repo.insert_broadcast_areas(areas_to_add) + repo.insert_broadcast_areas(areas_to_add, keep_old_polygons) def add_wards_local_authorities_and_counties(): @@ -159,7 +161,7 @@ def _add_electoral_wards(dataset_id): except KeyError: print("Skipping", ward_code, ward_name) # noqa: T001 - repo.insert_broadcast_areas(areas_to_add) + repo.insert_broadcast_areas(areas_to_add, keep_old_polygons) def _add_local_authorities(dataset_id): @@ -187,7 +189,7 @@ def _add_local_authorities(dataset_id): feature, simple_feature ]) - repo.insert_broadcast_areas(areas_to_add) + repo.insert_broadcast_areas(areas_to_add, keep_old_polygons) # counties and unitary authorities @@ -213,23 +215,31 @@ def _add_counties_and_unitary_authorities(dataset_id): feature, simple_feature ]) - repo.insert_broadcast_areas(areas_to_add) + repo.insert_broadcast_areas(areas_to_add, keep_old_polygons) -if __name__ == '__main__': +# cheeky global variable +keep_old_polygons = sys.argv[1:] == ['--keep-old-polygons'] +print('keep_old_polygons: ', keep_old_polygons) # noqa: T001 + +repo = BroadcastAreasRepository() + +if keep_old_polygons: + repo.delete_library_data() +else: repo.delete_db() repo.create_tables() - add_countries() - add_wards_local_authorities_and_counties() +add_countries() +add_wards_local_authorities_and_counties() - most_detailed_polygons = formatted_list( - sorted(point_counts, reverse=True)[:5], - before_each='', - after_each='', - ) - print( # noqa: T001 - '\n' - 'DONE\n' - f' Processed {len(point_counts):,} polygons.\n' - f' Highest point counts once simplifed: {most_detailed_polygons}\n' - ) +most_detailed_polygons = formatted_list( + sorted(point_counts, reverse=True)[:5], + before_each='', + after_each='', +) +print( # noqa: T001 + '\n' + 'DONE\n' + f' Processed {len(point_counts):,} polygons.\n' + f' Highest point counts once simplifed: {most_detailed_polygons}\n' +) diff --git a/app/broadcast_areas/repo.py b/app/broadcast_areas/repo.py index 824a34271..3527d0abd 100644 --- a/app/broadcast_areas/repo.py +++ b/app/broadcast_areas/repo.py @@ -62,6 +62,13 @@ class BroadcastAreasRepository(object): ON broadcast_areas (broadcast_area_library_group_id); """) + def delete_library_data(self): + # delete everything except broadcast_area_polygons + with self.conn() as conn: + conn.execute('DELETE FROM broadcast_area_libraries;') + conn.execute('DELETE FROM broadcast_area_library_groups;') + conn.execute('DELETE FROM broadcast_areas;') + def insert_broadcast_area_library(self, id, *, name, name_singular, is_group): q = """ @@ -72,7 +79,7 @@ class BroadcastAreasRepository(object): with self.conn() as conn: conn.execute(q, (id, name, name_singular, is_group)) - def insert_broadcast_areas(self, areas): + def insert_broadcast_areas(self, areas, keep_old_features): areas_q = """ INSERT INTO broadcast_areas ( @@ -95,9 +102,10 @@ class BroadcastAreasRepository(object): conn.execute(areas_q, ( id, name, area_id, group, )) - conn.execute(features_q, ( - id, json.dumps(polygons), json.dumps(simple_polygons), - )) + if not keep_old_features: + conn.execute(features_q, ( + id, json.dumps(polygons), json.dumps(simple_polygons), + )) def query(self, sql, *args): with self.conn() as conn: