diff --git a/buildings/gui/reference_data.py b/buildings/gui/reference_data.py index 049d8385..174fcf96 100644 --- a/buildings/gui/reference_data.py +++ b/buildings/gui/reference_data.py @@ -265,7 +265,7 @@ def topo_layer_processing(self, dataset): else: status = topo50.update_topo50(api_key, dataset, self.db) self.update_message(status, dataset) - if status != "error": + if status == "updated": self.updates.append(dataset) def admin_bdy_layer_processing(self, dataset): @@ -273,14 +273,19 @@ def admin_bdy_layer_processing(self, dataset): api_key = self.check_api_key(dataset) if api_key is None: return - status = admin_bdys.update_admin_bdys(api_key, dataset, self.db) - self.update_message(status, dataset) - if status != "error": + status, ids_bo = admin_bdys.update_admin_bdys(api_key, dataset, self.db) + if status == "updated": + self.update_message( + status, + f"{dataset} ({len(ids_bo)} building outlines modified)", + ) self.updates.append(dataset) if dataset == "territorial_authority": self.db.execute_no_commit(reference_select.refresh_ta_grid_view) self.update_message("updated", "territorial_authority_grid") self.updates.append("territorial_authority_grid") + else: + self.update_message(status, dataset) def check_api_key(self, layer): # check for API key @@ -301,9 +306,9 @@ def check_api_key(self, layer): def update_message(self, status, name): """add to message for display at end of processing""" if status == "current": - self.message += "The {} table was up to date\n".format(name) + self.message += "The table {} was up to date\n".format(name) if status == "updated": - self.message += "The {} table has been updated\n".format(name) + self.message += "The table {} has been updated\n".format(name) if status == "error": - self.message += "The request errored on the {} table\n".format(name) + self.message += "The request errored on the table {}\n".format(name) self.request_error() diff --git a/buildings/reference_data/admin_bdys.py b/buildings/reference_data/admin_bdys.py index 1e5845cf..b275d3ca 100644 --- a/buildings/reference_data/admin_bdys.py +++ b/buildings/reference_data/admin_bdys.py @@ -56,7 +56,6 @@ def current_date(): # todo: add kx_api_key in config # todo: combine suburb_locality- and town city def update_admin_bdys(kx_api_key, dataset, dbconn: db): - # get last update of layer date from log from_var = last_update(dataset) @@ -76,10 +75,10 @@ def update_admin_bdys(kx_api_key, dataset, dbconn: db): if not layer.isValid(): # something went wrong - return "error" + return "error", [] if layer.featureCount() == 0: - return "current" + return "current", [] external_id = LAYERS[dataset]["primary_id"] @@ -201,21 +200,21 @@ def update_admin_bdys(kx_api_key, dataset, dbconn: db): feature.geometry().asWkt(), ), ) - print("updated_attrs: {}".format(ids_attr_updates)) - print("updated_geom: {}".format(len(geoms_diff))) geom_union = QgsGeometry.unaryUnion(geoms_diff).asWkt() - print(geom_union) sql = "SELECT buildings_reference.{}_attribute_update_building_outlines(%s)".format( dataset ) - dbconn.execute_no_commit(sql, (ids_attr_updates,)) + result = dbconn.execute_no_commit(sql, (ids_attr_updates,)) + ids_bo = [r[0] for r in result.fetchall() if r[0] is not None] sql = "SELECT buildings_reference.{}_geometry_update_building_outlines(%s)".format( dataset ) - dbconn.execute_no_commit(sql, (geom_union,)) - - return "updated" + result = dbconn.execute_no_commit(sql, (geom_union,)) + ids_bo.extend( + [r[0] for r in result.fetchall() if r[0] not in ids_bo and r[0] is not None] + ) + return "updated", ids_bo def correct_name_format(name): diff --git a/db/sql/deploy/buildings_reference/functions/reference_update_log.sql b/db/sql/deploy/buildings_reference/functions/reference_update_log.sql index 8031f575..c1ec22e5 100644 --- a/db/sql/deploy/buildings_reference/functions/reference_update_log.sql +++ b/db/sql/deploy/buildings_reference/functions/reference_update_log.sql @@ -25,7 +25,7 @@ RETURNS integer AS $$ INSERT INTO buildings_reference.reference_update_log (river, lake, pond, swamp, lagoon, canal, coastlines_and_islands, capture_source_area, territorial_authority, territorial_authority_grid, suburb_locality, hut, shelter, bivouac, protected_areas) - VALUES(CASE WHEN ('river_polygon' = ANY(p_list)) THEN True ELSE False END, + VALUES(CASE WHEN ('river_polygons' = ANY(p_list)) THEN True ELSE False END, CASE WHEN ('lake_polygons' = ANY(p_list)) THEN True ELSE False END, CASE WHEN ('pond_polygons' = ANY(p_list)) THEN True ELSE False END, CASE WHEN ('swamp_polygons' = ANY(p_list)) THEN True ELSE False END, diff --git a/db/sql/deploy/buildings_reference/functions/suburb_locality.sql b/db/sql/deploy/buildings_reference/functions/suburb_locality.sql index 31e79df4..1a384b5f 100644 --- a/db/sql/deploy/buildings_reference/functions/suburb_locality.sql +++ b/db/sql/deploy/buildings_reference/functions/suburb_locality.sql @@ -121,7 +121,7 @@ COMMENT ON FUNCTION buildings_reference.suburb_locality_update_by_external_id(in -- params: integer[] suburb_locality_id -- return: integer building_outline_id CREATE OR REPLACE FUNCTION buildings_reference.suburb_locality_attribute_update_building_outlines(integer[]) -RETURNS integer AS +RETURNS SETOF integer AS $$ UPDATE buildings.building_outlines SET last_modified = NOW() @@ -138,7 +138,7 @@ COMMENT ON FUNCTION buildings_reference.suburb_locality_attribute_update_buildin -- params: varchar shape -- return: integer building_outline_id CREATE OR REPLACE FUNCTION buildings_reference.suburb_locality_geometry_update_building_outlines(varchar) -RETURNS integer AS +RETURNS SETOF integer AS $$ WITH sub_tas AS ( SELECT ST_Subdivide(ST_SetSRID(ST_GeometryFromText($1), 2193)) AS shape diff --git a/db/sql/deploy/buildings_reference/functions/territorial_authority.sql b/db/sql/deploy/buildings_reference/functions/territorial_authority.sql index c54342e8..9d119f8a 100644 --- a/db/sql/deploy/buildings_reference/functions/territorial_authority.sql +++ b/db/sql/deploy/buildings_reference/functions/territorial_authority.sql @@ -152,7 +152,7 @@ COMMENT ON FUNCTION buildings_reference.territorial_authority_update_by_external -- params: integer[] territorial_authority_id -- return: integer building_outline_id CREATE OR REPLACE FUNCTION buildings_reference.territorial_authority_attribute_update_building_outlines(integer[]) -RETURNS integer AS +RETURNS SETOF integer AS $$ UPDATE buildings.building_outlines SET last_modified = NOW() @@ -169,7 +169,7 @@ COMMENT ON FUNCTION buildings_reference.territorial_authority_attribute_update_b -- params: varchar shape -- return: integer building_outline_id CREATE OR REPLACE FUNCTION buildings_reference.territorial_authority_geometry_update_building_outlines(varchar) -RETURNS integer AS +RETURNS SETOF integer AS $$ WITH sub_tas AS ( SELECT ST_Subdivide(ST_SetSRID(ST_GeometryFromText($1), 2193)) AS shape