diff --git a/buildings/gui/bulk_load_changes.py b/buildings/gui/bulk_load_changes.py index 548da3d1..c4f2c8f3 100644 --- a/buildings/gui/bulk_load_changes.py +++ b/buildings/gui/bulk_load_changes.py @@ -735,7 +735,7 @@ def select_comboboxes_value(self): # suburb result = self.edit_dialog.db.execute_return( - reference_select.suburb_locality_suburb_4th_by_bulk_outline_id, + reference_select.suburb_locality_name_by_bulk_outline_id, (self.edit_dialog.bulk_load_outline_id,), ) result = result.fetchall()[0][0] diff --git a/buildings/gui/production_changes.py b/buildings/gui/production_changes.py index 5b3e971f..d628fa17 100644 --- a/buildings/gui/production_changes.py +++ b/buildings/gui/production_changes.py @@ -708,7 +708,7 @@ def select_comboboxes_value(self): # suburb result = self.edit_dialog.db.execute_return( - reference_select.suburb_locality_suburb_4th_by_building_outline_id, (self.edit_dialog.building_outline_id,) + reference_select.suburb_locality_name_by_building_outline_id, (self.edit_dialog.building_outline_id,) ) result = result.fetchall()[0][0] self.edit_dialog.cmb_suburb.setCurrentIndex(self.edit_dialog.cmb_suburb.findText(result)) diff --git a/buildings/sql/buildings_reference_select_statements.py b/buildings/sql/buildings_reference_select_statements.py index 11dded86..1e03f8cc 100644 --- a/buildings/sql/buildings_reference_select_statements.py +++ b/buildings/sql/buildings_reference_select_statements.py @@ -15,9 +15,9 @@ - suburb_locality - suburb_locality_intersect_geom (geometry) - - suburb_locality_suburb_4th - - suburb_locality_suburb_4th_by_building_outline_id (building_outline_id) - - suburb_locality_suburb_4th_by_bulk_outline_id (bulk_load_outline_id) + - suburb_locality_name + - suburb_locality_name_by_building_outline_id (building_outline_id) + - suburb_locality_name_by_bulk_outline_id (bulk_load_outline_id) - territorial_authority - territorial_authority_intersect_geom (geometry) @@ -97,27 +97,27 @@ # suburb locality suburb_locality_intersect_geom = """ -SELECT suburb_locality_id, suburb_4th +SELECT suburb_locality_id, name FROM buildings_reference.suburb_locality WHERE shape && ST_Expand(%s::Geometry, 1000) -ORDER BY suburb_4th; +ORDER BY name; """ -suburb_locality_suburb_4th = """ -SELECT DISTINCT suburb_4th +suburb_locality_name = """ +SELECT DISTINCT name FROM buildings_reference.suburb_locality; """ -suburb_locality_suburb_4th_by_building_outline_id = """ -SELECT suburb_4th +suburb_locality_name_by_building_outline_id = """ +SELECT name FROM buildings_reference.suburb_locality sl, buildings.building_outlines bo WHERE sl.suburb_locality_id = bo.suburb_locality_id AND bo.building_outline_id = %s; """ -suburb_locality_suburb_4th_by_bulk_outline_id = """ -SELECT suburb_4th +suburb_locality_name_by_bulk_outline_id = """ +SELECT name FROM buildings_reference.suburb_locality sl, buildings_bulk_load.bulk_load_outlines blo WHERE sl.suburb_locality_id = blo.suburb_locality_id diff --git a/buildings/tests/gui/test_processes_edit_attribute_bulk_load.py b/buildings/tests/gui/test_processes_edit_attribute_bulk_load.py index 95f2bc12..22433b3d 100644 --- a/buildings/tests/gui/test_processes_edit_attribute_bulk_load.py +++ b/buildings/tests/gui/test_processes_edit_attribute_bulk_load.py @@ -409,7 +409,7 @@ def test_save_clicked(self): capture_method = capture_method.fetchall()[0][0] self.assertEqual("Unknown", capture_method) # suburb - sql = "SELECT suburb_4th FROM buildings_reference.suburb_locality WHERE suburb_locality_id = %s;" + sql = "SELECT name FROM buildings_reference.suburb_locality WHERE suburb_locality_id = %s;" suburb = db._execute(sql, (result[2],)) suburb = suburb.fetchall()[0][0] self.assertEqual("Hokowhitu", suburb) @@ -515,7 +515,7 @@ def test_edit_mutiple_attributes(self): capture_method = capture_method.fetchall()[0][0] self.assertEqual("Unknown", capture_method) # suburb - sql = "SELECT suburb_4th FROM buildings_reference.suburb_locality WHERE suburb_locality_id = %s;" + sql = "SELECT name FROM buildings_reference.suburb_locality WHERE suburb_locality_id = %s;" suburb = db._execute(sql, (result[2],)) suburb = suburb.fetchall()[0][0] self.assertEqual("Hokowhitu", suburb) diff --git a/buildings/tests/gui/test_processes_edit_attribute_production.py b/buildings/tests/gui/test_processes_edit_attribute_production.py index ca949605..b63c2ea7 100644 --- a/buildings/tests/gui/test_processes_edit_attribute_production.py +++ b/buildings/tests/gui/test_processes_edit_attribute_production.py @@ -203,7 +203,7 @@ def test_save_clicked(self): capture_method = capture_method.fetchall()[0][0] self.assertEqual("Unknown", capture_method) # suburb - sql = "SELECT suburb_4th FROM buildings_reference.suburb_locality WHERE suburb_locality_id = %s;" + sql = "SELECT name FROM buildings_reference.suburb_locality WHERE suburb_locality_id = %s;" suburb = db._execute(sql, (result[2],)) suburb = suburb.fetchall()[0][0] self.assertEqual("Hokowhitu", suburb) @@ -311,7 +311,7 @@ def test_edit_mutiple_attributes(self): capture_method = capture_method.fetchall()[0][0] self.assertEqual("Unknown", capture_method) # suburb - sql = "SELECT suburb_4th FROM buildings_reference.suburb_locality WHERE suburb_locality_id = %s;" + sql = "SELECT name FROM buildings_reference.suburb_locality WHERE suburb_locality_id = %s;" suburb = db._execute(sql, (result[2],)) suburb = suburb.fetchall()[0][0] self.assertEqual("Hokowhitu", suburb) diff --git a/buildings/tests/gui/test_processes_update_reference_data_admin_bdys.py b/buildings/tests/gui/test_processes_update_reference_data_admin_bdys.py index c13f90a8..69aec1f5 100644 --- a/buildings/tests/gui/test_processes_update_reference_data_admin_bdys.py +++ b/buildings/tests/gui/test_processes_update_reference_data_admin_bdys.py @@ -93,7 +93,7 @@ def tearDown(self): # count_added = result.fetchone()[0] # self.assertEqual(count_added, 1) # # updated suburb locality - # sql_updated = "SELECT suburb_4th FROM buildings_reference.suburb_locality WHERE external_suburb_locality_id = 101;" + # sql_updated = "SELECT name FROM buildings_reference.suburb_locality WHERE external_suburb_locality_id = 101;" # result = db._execute(sql_updated) # name_updated = result.fetchone()[0] # self.assertEqual(name_updated, "Kelburn North") diff --git a/db/sql/deploy/buildings_lds/functions/populate_buildings_lds.sql b/db/sql/deploy/buildings_lds/functions/populate_buildings_lds.sql index 73ca3c3c..89f05ba0 100644 --- a/db/sql/deploy/buildings_lds/functions/populate_buildings_lds.sql +++ b/db/sql/deploy/buildings_lds/functions/populate_buildings_lds.sql @@ -31,7 +31,7 @@ $$ buildings.building_id , COALESCE(building_name.building_name, '') AS name , COALESCE(use.value, 'Unknown') AS use - , COALESCE(suburb_locality.suburb_4th, suburb_locality.suburb_3rd, suburb_locality.suburb_2nd, suburb_locality.suburb_1st) AS suburb_locality + , suburb_locality.name AS suburb_locality , COALESCE(town_city.name, '') AS town_city , territorial_authority.name AS territorial_authority , capture_method.value AS capture_method @@ -147,7 +147,7 @@ $$ , buildings.building_id , COALESCE(building_name.building_name, '') AS name , COALESCE(use.value, 'Unknown') AS use - , COALESCE(suburb_locality.suburb_4th, suburb_locality.suburb_3rd, suburb_locality.suburb_2nd, suburb_locality.suburb_1st) AS suburb_locality + , suburb_locality.name AS suburb_locality , COALESCE(town_city.name, '') AS town_city , territorial_authority.name AS territorial_authority , capture_method.value AS capture_method diff --git a/db/sql/deploy/buildings_lds/functions/populate_buildings_lds@v4.0.0-dev1.sql b/db/sql/deploy/buildings_lds/functions/populate_buildings_lds@v4.0.0-dev1.sql new file mode 100644 index 00000000..73ca3c3c --- /dev/null +++ b/db/sql/deploy/buildings_lds/functions/populate_buildings_lds@v4.0.0-dev1.sql @@ -0,0 +1,231 @@ +-- Deploy nz-buildings:buildings_lds/functions/populate_buildings_lds to pg + +BEGIN; + +------------------------------------------------------------------------------ +-- Populate buildings_lds schema in preparation for loading data to LDS +------------------------------------------------------------------------------ + +CREATE OR REPLACE FUNCTION buildings_lds.nz_building_outlines_insert() +RETURNS integer AS +$$ + + WITH populate_nz_building_outlines AS ( + INSERT INTO buildings_lds.nz_building_outlines ( + building_id + , name + , use + , suburb_locality + , town_city + , territorial_authority + , capture_method + , capture_source_group + , capture_source_id + , capture_source_name + , capture_source_from + , capture_source_to + , last_modified + , shape + ) + SELECT + buildings.building_id + , COALESCE(building_name.building_name, '') AS name + , COALESCE(use.value, 'Unknown') AS use + , COALESCE(suburb_locality.suburb_4th, suburb_locality.suburb_3rd, suburb_locality.suburb_2nd, suburb_locality.suburb_1st) AS suburb_locality + , COALESCE(town_city.name, '') AS town_city + , territorial_authority.name AS territorial_authority + , capture_method.value AS capture_method + , capture_source_group.value AS capture_source_group + , LEFT(capture_source.external_source_id, 4)::integer AS capture_source_id + , nz_imagery_survey_index.name AS capture_source_name + , nz_imagery_survey_index.flown_from AS capture_source_from + , nz_imagery_survey_index.flown_to AS capture_source_to + , GREATEST(building_outlines.begin_lifespan, COALESCE(building_outlines.end_lifespan, '1900-01-01 00:00:00'), COALESCE(building_outlines.last_modified, '1900-01-01 00:00:00'), COALESCE(building_name.begin_lifespan, '1900-01-01 00:00:00'), COALESCE(building_use.begin_lifespan, '1900-01-01 00:00:00'))::date AS last_modified + , building_outlines.shape + FROM buildings.building_outlines + JOIN buildings.buildings USING (building_id) + LEFT JOIN buildings.building_name ON buildings.building_id = building_name.building_id + AND building_name.end_lifespan IS NULL + LEFT JOIN buildings.building_use ON buildings.building_id = building_use.building_id + AND building_use.end_lifespan IS NULL + LEFT JOIN buildings.use USING (use_id) + JOIN buildings.lifecycle_stage USING (lifecycle_stage_id) + JOIN buildings_common.capture_method USING (capture_method_id) + JOIN buildings_common.capture_source USING (capture_source_id) + JOIN buildings_reference.nz_imagery_survey_index ON LEFT(capture_source.external_source_id, 4)::integer = nz_imagery_survey_index.imagery_survey_id + JOIN buildings_common.capture_source_group USING (capture_source_group_id) + JOIN buildings_reference.suburb_locality ON suburb_locality.suburb_locality_id = building_outlines.suburb_locality_id + LEFT JOIN buildings_reference.town_city ON town_city.town_city_id = building_outlines.town_city_id + JOIN buildings_reference.territorial_authority ON territorial_authority.territorial_authority_id = building_outlines.territorial_authority_id + WHERE building_outlines.end_lifespan IS NULL + AND buildings.end_lifespan IS NULL + ORDER BY buildings.building_id + RETURNING * + ) + SELECT count(*)::integer FROM populate_nz_building_outlines; + +$$ +LANGUAGE sql VOLATILE; + + +CREATE OR REPLACE FUNCTION buildings_lds.nz_building_outlines_all_sources_insert() +RETURNS integer AS +$$ + + WITH populate_nz_building_outlines_all_sources AS ( + INSERT INTO buildings_lds.nz_building_outlines_all_sources ( + building_outline_id + , building_id + , name + , use + , suburb_locality + , town_city + , territorial_authority + , capture_method + , capture_source_group + , capture_source_id + , capture_source_name + , capture_source_from + , capture_source_to + , building_outline_lifecycle + , begin_lifespan + , end_lifespan + , last_modified + , shape + ) + WITH transfer_dates AS ( + SELECT DISTINCT transfer_date + FROM buildings_bulk_load.supplied_datasets + ) + , deleted_in_production AS ( + SELECT building_outlines.building_outline_id, supplied_datasets.processed_date, supplied_datasets.transfer_date, building_outlines.begin_lifespan, building_outlines.end_lifespan + FROM buildings.building_outlines + JOIN buildings_bulk_load.transferred ON building_outlines.building_outline_id = transferred.new_building_outline_id + JOIN buildings_bulk_load.bulk_load_outlines USING (bulk_load_outline_id) + JOIN buildings_bulk_load.supplied_datasets USING (supplied_dataset_id) + WHERE building_outlines.end_lifespan IS NOT NULL + AND building_outlines.end_lifespan NOT IN ( + SELECT transfer_date + FROM transfer_dates + ) + ) + , removed AS ( + SELECT b1.building_outline_id + FROM buildings.building_outlines b1 + LEFT JOIN buildings.building_outlines b2 ON b1.building_id = b2.building_id AND b1.building_outline_id != b2.building_outline_id AND b2.end_lifespan IS NULL + LEFT JOIN buildings.lifecycle l ON b1.building_id = l.parent_building_id + LEFT JOIN deleted_in_production d ON b1.building_outline_id = d.building_outline_id + WHERE b1.end_lifespan IS NOT NULL + AND b2.building_outline_id IS NULL + AND l.parent_building_id IS NULL + AND d.building_outline_id IS NULL + ) + , replaced AS ( + SELECT b1.building_outline_id + FROM buildings.building_outlines b1 + JOIN buildings.building_outlines b2 ON b1.building_id = b2.building_id AND b1.building_outline_id != b2.building_outline_id AND b1.end_lifespan = b2.begin_lifespan AND b2.end_lifespan IS NULL + WHERE b1.end_lifespan IS NOT NULL + ) + , recombined AS ( + SELECT b.building_outline_id + FROM buildings.building_outlines b + JOIN buildings.lifecycle l ON b.building_id = l.parent_building_id + WHERE b.end_lifespan IS NOT NULL + ) + , building_outline_lifecycle AS ( + SELECT building_outline_id, 'Removed' AS status + FROM removed + UNION + SELECT building_outline_id, 'Replaced' AS status + FROM replaced + UNION + SELECT building_outline_id, 'Recombined' AS status + FROM recombined + ) + SELECT + building_outlines.building_outline_id + , buildings.building_id + , COALESCE(building_name.building_name, '') AS name + , COALESCE(use.value, 'Unknown') AS use + , COALESCE(suburb_locality.suburb_4th, suburb_locality.suburb_3rd, suburb_locality.suburb_2nd, suburb_locality.suburb_1st) AS suburb_locality + , COALESCE(town_city.name, '') AS town_city + , territorial_authority.name AS territorial_authority + , capture_method.value AS capture_method + , capture_source_group.value AS capture_source_group + , LEFT(capture_source.external_source_id, 4)::integer AS capture_source_id + , nz_imagery_survey_index.name AS capture_source_name + , nz_imagery_survey_index.flown_from AS capture_source_from + , nz_imagery_survey_index.flown_to AS capture_source_to + , COALESCE(building_outline_lifecycle.status, 'Current') AS building_outline_lifecycle + , building_outlines.begin_lifespan::date AS begin_lifespan + , building_outlines.end_lifespan::date AS end_lifespan + , GREATEST(building_outlines.begin_lifespan, COALESCE(building_outlines.end_lifespan, '1900-01-01 00:00:00'), COALESCE(building_outlines.last_modified, '1900-01-01 00:00:00'), COALESCE(building_name.begin_lifespan, '1900-01-01 00:00:00'), COALESCE(building_use.begin_lifespan, '1900-01-01 00:00:00'))::date AS last_modified + , building_outlines.shape + FROM buildings.building_outlines + JOIN buildings.buildings USING (building_id) + LEFT JOIN buildings.building_name ON buildings.building_id = building_name.building_id + AND building_name.end_lifespan IS NULL + LEFT JOIN buildings.building_use ON buildings.building_id = building_use.building_id + AND building_use.end_lifespan IS NULL + LEFT JOIN buildings.use USING (use_id) + JOIN buildings.lifecycle_stage USING (lifecycle_stage_id) + JOIN buildings_common.capture_method USING (capture_method_id) + JOIN buildings_common.capture_source USING (capture_source_id) + JOIN buildings_reference.nz_imagery_survey_index ON LEFT(capture_source.external_source_id, 4)::integer = nz_imagery_survey_index.imagery_survey_id + JOIN buildings_common.capture_source_group USING (capture_source_group_id) + JOIN buildings_reference.suburb_locality ON suburb_locality.suburb_locality_id = building_outlines.suburb_locality_id + LEFT JOIN buildings_reference.town_city ON town_city.town_city_id = building_outlines.town_city_id + JOIN buildings_reference.territorial_authority ON territorial_authority.territorial_authority_id = building_outlines.territorial_authority_id + LEFT JOIN deleted_in_production USING (building_outline_id) + LEFT JOIN building_outline_lifecycle USING (building_outline_id) + ORDER BY building_outlines.building_outline_id + RETURNING * + ) + SELECT count(*)::integer FROM populate_nz_building_outlines_all_sources; + +$$ +LANGUAGE sql VOLATILE; + +CREATE OR REPLACE FUNCTION buildings_lds.nz_building_outlines_lifecycle_insert() +RETURNS integer AS +$$ + + WITH populate_nz_building_outlines_lifecycle AS ( + INSERT INTO buildings_lds.nz_building_outlines_lifecycle ( + lifecycle_id + , parent_building_id + , building_id + ) + SELECT + lifecycle_id + , parent_building_id + , building_id + FROM buildings.lifecycle + RETURNING * + ) + SELECT count(*)::integer FROM populate_nz_building_outlines_lifecycle; + +$$ +LANGUAGE sql VOLATILE; + +CREATE OR REPLACE FUNCTION buildings_lds.populate_buildings_lds() +RETURNS TABLE( + table_name text + , rows_inserted integer +) AS +$$ + + TRUNCATE buildings_lds.nz_building_outlines; + TRUNCATE buildings_lds.nz_building_outlines_all_sources; + TRUNCATE buildings_lds.nz_building_outlines_lifecycle; + + VALUES + ('nz_building_outlines' , buildings_lds.nz_building_outlines_insert()) + , ('nz_building_outlines_all_sources' , buildings_lds.nz_building_outlines_all_sources_insert()) + , ('nz_building_outlines_lifecycle' , buildings_lds.nz_building_outlines_lifecycle_insert()) + ; + +$$ +LANGUAGE sql VOLATILE; + +COMMIT; diff --git a/db/sql/deploy/buildings_reference/functions/suburb_locality_optimised.sql b/db/sql/deploy/buildings_reference/functions/suburb_locality_optimised.sql index caffb8f4..57176ef8 100644 --- a/db/sql/deploy/buildings_reference/functions/suburb_locality_optimised.sql +++ b/db/sql/deploy/buildings_reference/functions/suburb_locality_optimised.sql @@ -2,12 +2,6 @@ BEGIN; -DROP FUNCTION IF EXISTS buildings_reference.suburb_locality_insert_new_areas(); -DROP FUNCTION IF EXISTS buildings_reference.suburb_locality_update_suburb_locality(); -DROP FUNCTION IF EXISTS buildings_reference.suburb_locality_delete_removed_areas(); -DROP FUNCTION IF EXISTS buildings.building_outlines_update_suburb(integer[]); -DROP FUNCTION IF EXISTS buildings_bulk_load.bulk_load_outlines_update_all_suburbs(integer[]); - -- building_outlines_update_changed_and_deleted_suburb (replace suburb values with the intersection result) -- params: -- return: integer count of number of building outlines updated @@ -38,10 +32,7 @@ BEGIN -- Update the changed suburbs UPDATE buildings_reference.suburb_locality bsl SET - suburb_4th = nzl.suburb_4th, - suburb_3rd = nzl.suburb_3rd, - suburb_2nd = nzl.suburb_2nd, - suburb_1st = nzl.suburb_1st, + name = COALESCE(nzl.suburb_4th, nzl.suburb_3rd, nzl.suburb_2nd, nzl.suburb_1st), shape = ST_SetSRID(ST_Transform(nzl.shape, 2193), 2193) FROM admin_bdys.nz_locality nzl @@ -49,10 +40,7 @@ BEGIN bsl.external_suburb_locality_id = nzl.id AND ( NOT ST_Equals(ST_SetSRID(ST_Transform(nzl.shape, 2193), 2193), bsl.shape) - OR nzl.suburb_4th != bsl.suburb_4th - OR nzl.suburb_3rd != bsl.suburb_3rd - OR nzl.suburb_2nd != bsl.suburb_2nd - OR nzl.suburb_1st != bsl.suburb_1st + OR bsl.name != COALESCE(nzl.suburb_4th, nzl.suburb_3rd, nzl.suburb_2nd, nzl.suburb_1st) ); -- Remove deleted suburbs @@ -104,25 +92,17 @@ BEGIN CREATE TEMP TABLE added_suburbs ( suburb_locality_id integer , external_suburb_locality_id integer - , suburb_4th character varying(60) - , suburb_3rd character varying(60) - , suburb_2nd character varying(60) - , suburb_1st character varying(60) + , name character varying(60) , shape public.geometry(MultiPolygon, 2193) ); WITH add_new_suburbs AS ( INSERT INTO buildings_reference.suburb_locality ( - external_suburb_locality_id, suburb_4th, - suburb_3rd, suburb_2nd, suburb_1st, - shape + external_suburb_locality_id, name, shape ) SELECT id, - suburb_4th, - suburb_3rd, - suburb_2nd, - suburb_1st, + COALESCE(suburb_4th, suburb_3rd, suburb_2nd, suburb_1st), ST_Transform(shape, 2193) FROM admin_bdys.nz_locality @@ -137,7 +117,7 @@ BEGIN ) INSERT INTO added_suburbs - SELECT * + SELECT suburb_locality_id, external_suburb_locality_id, name, shape FROM add_new_suburbs; -- update building outline suburb locality Id and last modified date where: diff --git a/db/sql/deploy/buildings_reference/functions/suburb_locality_optimised@v4.0.0-dev1.sql b/db/sql/deploy/buildings_reference/functions/suburb_locality_optimised@v4.0.0-dev1.sql new file mode 100644 index 00000000..caffb8f4 --- /dev/null +++ b/db/sql/deploy/buildings_reference/functions/suburb_locality_optimised@v4.0.0-dev1.sql @@ -0,0 +1,164 @@ +-- Deploy nz-buildings:buildings_reference/functions/update_suburb_locality_changed_deleted_buildings to pg + +BEGIN; + +DROP FUNCTION IF EXISTS buildings_reference.suburb_locality_insert_new_areas(); +DROP FUNCTION IF EXISTS buildings_reference.suburb_locality_update_suburb_locality(); +DROP FUNCTION IF EXISTS buildings_reference.suburb_locality_delete_removed_areas(); +DROP FUNCTION IF EXISTS buildings.building_outlines_update_suburb(integer[]); +DROP FUNCTION IF EXISTS buildings_bulk_load.bulk_load_outlines_update_all_suburbs(integer[]); + +-- building_outlines_update_changed_and_deleted_suburb (replace suburb values with the intersection result) + -- params: + -- return: integer count of number of building outlines updated + +CREATE OR REPLACE FUNCTION buildings_reference.building_outlines_update_changed_and_deleted_suburb() +RETURNS void AS +$$ +BEGIN + -- Find new ids and geometry differences of changed + CREATE TEMP TABLE changed_geometries AS + SELECT + nzl.id AS new_external_id, + bsl.suburb_locality_id AS new_id, + ST_Difference(ST_Transform(nzl.shape, 2193),bsl.shape) AS shape + FROM + admin_bdys.nz_locality nzl, + buildings_reference.suburb_locality bsl + WHERE + bsl.external_suburb_locality_id = nzl.id + AND ( + NOT ST_Equals(ST_Transform(nzl.shape, 2193), bsl.shape) + ) + AND ( + NOT st_isempty(ST_Difference(ST_Transform(nzl.shape, 2193),bsl.shape) + ) + ); + + -- Update the changed suburbs + UPDATE buildings_reference.suburb_locality bsl + SET + suburb_4th = nzl.suburb_4th, + suburb_3rd = nzl.suburb_3rd, + suburb_2nd = nzl.suburb_2nd, + suburb_1st = nzl.suburb_1st, + shape = ST_SetSRID(ST_Transform(nzl.shape, 2193), 2193) + FROM + admin_bdys.nz_locality nzl + WHERE + bsl.external_suburb_locality_id = nzl.id + AND ( + NOT ST_Equals(ST_SetSRID(ST_Transform(nzl.shape, 2193), 2193), bsl.shape) + OR nzl.suburb_4th != bsl.suburb_4th + OR nzl.suburb_3rd != bsl.suburb_3rd + OR nzl.suburb_2nd != bsl.suburb_2nd + OR nzl.suburb_1st != bsl.suburb_1st + ); + + -- Remove deleted suburbs + DELETE FROM + buildings_reference.suburb_locality + WHERE + external_suburb_locality_id NOT IN ( + SELECT + id + FROM + admin_bdys.nz_locality + ); + + -- Updates building outlines where: + -- It's external id is an 'old id' + -- It Overlaps or is Within the difference polygon for the 'old id' + -- It overlaps the difference polygon by the largest proportion + -- The suburb with the most overlap is not the same as the old id + UPDATE + buildings.building_outlines b + SET + suburb_locality_id = buildings_reference.suburb_locality_intersect_polygon(b.shape), + last_modified = NOW() + FROM + changed_geometries cg + WHERE + (ST_Within(b.shape, cg.shape) OR ST_Overlaps(b.shape, cg.shape)) + AND buildings_reference.suburb_locality_intersect_polygon(b.shape) != b.suburb_locality_id; + + DISCARD TEMP; + +END; +$$ LANGUAGE plpgsql; + +COMMENT ON FUNCTION buildings_reference.building_outlines_update_changed_and_deleted_suburb() IS +'Replace suburb values with the intersection result of buildings in the building_outlines table'; + + +-- building_outlines_update_added_suburb (replace suburb values with the intersection result) + -- params: + -- return: integer count of number of building outlines updated + +CREATE OR REPLACE FUNCTION buildings_reference.building_outlines_update_added_suburb() +RETURNS void AS +$$ +BEGIN + -- add new suburbs to buildings_reference.suburb_locality + -- (to get new suburb_locality_ids) + CREATE TEMP TABLE added_suburbs ( + suburb_locality_id integer + , external_suburb_locality_id integer + , suburb_4th character varying(60) + , suburb_3rd character varying(60) + , suburb_2nd character varying(60) + , suburb_1st character varying(60) + , shape public.geometry(MultiPolygon, 2193) + ); + + WITH add_new_suburbs AS ( + INSERT INTO buildings_reference.suburb_locality ( + external_suburb_locality_id, suburb_4th, + suburb_3rd, suburb_2nd, suburb_1st, + shape + ) + SELECT + id, + suburb_4th, + suburb_3rd, + suburb_2nd, + suburb_1st, + ST_Transform(shape, 2193) + FROM + admin_bdys.nz_locality + WHERE + type IN ('ISLAND','LOCALITY','PARK_RESERVE','SUBURB') + AND id NOT IN ( + SELECT + external_suburb_locality_id + FROM + buildings_reference.suburb_locality + ) RETURNING * + ) + + INSERT INTO added_suburbs + SELECT * + FROM add_new_suburbs; + + -- update building outline suburb locality Id and last modified date where: + -- the building is within/overlaps the new suburb + -- The largest overlapping suburb has changed + UPDATE + buildings.building_outlines b + SET + suburb_locality_id = buildings_reference.suburb_locality_intersect_polygon(b.shape), + last_modified = NOW() + FROM + added_suburbs asb + WHERE + (ST_Within(b.shape, asb.shape) OR ST_Overlaps(b.shape, asb.shape)) + AND buildings_reference.suburb_locality_intersect_polygon(b.shape) != b.suburb_locality_id; + + DISCARD TEMP; +END; +$$ LANGUAGE plpgsql; + +COMMENT ON FUNCTION buildings_reference.building_outlines_update_added_suburb() IS +'Insert new suburb localities and replace old suburb values with the intersection result in the building_outlines table'; + +COMMIT; diff --git a/db/sql/deploy/buildings_reference/remove_suburb_locality_old_name_column.sql b/db/sql/deploy/buildings_reference/remove_suburb_locality_old_name_column.sql new file mode 100644 index 00000000..d46a4c62 --- /dev/null +++ b/db/sql/deploy/buildings_reference/remove_suburb_locality_old_name_column.sql @@ -0,0 +1,11 @@ +-- Deploy nz-buildings:buildings_reference/remove_suburb_locality_old_name_column to pg + +BEGIN; + +ALTER TABLE buildings_reference.suburb_locality +DROP COLUMN suburb_4th, +DROP COLUMN suburb_3rd, +DROP COLUMN suburb_2nd, +DROP COLUMN suburb_1st; + +COMMIT; diff --git a/db/sql/deploy/buildings_reference/update_suburb_locality_name_column.sql b/db/sql/deploy/buildings_reference/update_suburb_locality_name_column.sql new file mode 100644 index 00000000..935c15cb --- /dev/null +++ b/db/sql/deploy/buildings_reference/update_suburb_locality_name_column.sql @@ -0,0 +1,14 @@ +-- Deploy nz-buildings:buildings_reference/update_suburb_locality_name_column to pg + +BEGIN; + +ALTER TABLE buildings_reference.suburb_locality +ADD COLUMN name varchar(100); + +UPDATE buildings_reference.suburb_locality +SET name = COALESCE(suburb_4th, suburb_3rd, suburb_2nd, suburb_1st); + +ALTER TABLE buildings_reference.suburb_locality +ALTER COLUMN name SET NOT NULL; + +COMMIT; diff --git a/db/sql/revert/buildings_lds/functions/populate_buildings_lds.sql b/db/sql/revert/buildings_lds/functions/populate_buildings_lds.sql index 5b60e516..73ca3c3c 100644 --- a/db/sql/revert/buildings_lds/functions/populate_buildings_lds.sql +++ b/db/sql/revert/buildings_lds/functions/populate_buildings_lds.sql @@ -40,7 +40,7 @@ $$ , nz_imagery_survey_index.name AS capture_source_name , nz_imagery_survey_index.flown_from AS capture_source_from , nz_imagery_survey_index.flown_to AS capture_source_to - , GREATEST(building_outlines.begin_lifespan, COALESCE(building_outlines.end_lifespan, '1900-01-01 00:00:00'), COALESCE(building_outlines.last_modified, '1900-01-01 00:00:00'))::date AS last_modified + , GREATEST(building_outlines.begin_lifespan, COALESCE(building_outlines.end_lifespan, '1900-01-01 00:00:00'), COALESCE(building_outlines.last_modified, '1900-01-01 00:00:00'), COALESCE(building_name.begin_lifespan, '1900-01-01 00:00:00'), COALESCE(building_use.begin_lifespan, '1900-01-01 00:00:00'))::date AS last_modified , building_outlines.shape FROM buildings.building_outlines JOIN buildings.buildings USING (building_id) @@ -52,7 +52,7 @@ $$ JOIN buildings.lifecycle_stage USING (lifecycle_stage_id) JOIN buildings_common.capture_method USING (capture_method_id) JOIN buildings_common.capture_source USING (capture_source_id) - JOIN aerial_lds.nz_imagery_survey_index ON LEFT(capture_source.external_source_id, 4)::integer = nz_imagery_survey_index.imagery_survey_id + JOIN buildings_reference.nz_imagery_survey_index ON LEFT(capture_source.external_source_id, 4)::integer = nz_imagery_survey_index.imagery_survey_id JOIN buildings_common.capture_source_group USING (capture_source_group_id) JOIN buildings_reference.suburb_locality ON suburb_locality.suburb_locality_id = building_outlines.suburb_locality_id LEFT JOIN buildings_reference.town_city ON town_city.town_city_id = building_outlines.town_city_id @@ -159,7 +159,7 @@ $$ , COALESCE(building_outline_lifecycle.status, 'Current') AS building_outline_lifecycle , building_outlines.begin_lifespan::date AS begin_lifespan , building_outlines.end_lifespan::date AS end_lifespan - , GREATEST(building_outlines.begin_lifespan, COALESCE(building_outlines.end_lifespan, '1900-01-01 00:00:00'), COALESCE(building_outlines.last_modified, '1900-01-01 00:00:00'))::date AS last_modified + , GREATEST(building_outlines.begin_lifespan, COALESCE(building_outlines.end_lifespan, '1900-01-01 00:00:00'), COALESCE(building_outlines.last_modified, '1900-01-01 00:00:00'), COALESCE(building_name.begin_lifespan, '1900-01-01 00:00:00'), COALESCE(building_use.begin_lifespan, '1900-01-01 00:00:00'))::date AS last_modified , building_outlines.shape FROM buildings.building_outlines JOIN buildings.buildings USING (building_id) @@ -171,7 +171,7 @@ $$ JOIN buildings.lifecycle_stage USING (lifecycle_stage_id) JOIN buildings_common.capture_method USING (capture_method_id) JOIN buildings_common.capture_source USING (capture_source_id) - JOIN aerial_lds.nz_imagery_survey_index ON LEFT(capture_source.external_source_id, 4)::integer = nz_imagery_survey_index.imagery_survey_id + JOIN buildings_reference.nz_imagery_survey_index ON LEFT(capture_source.external_source_id, 4)::integer = nz_imagery_survey_index.imagery_survey_id JOIN buildings_common.capture_source_group USING (capture_source_group_id) JOIN buildings_reference.suburb_locality ON suburb_locality.suburb_locality_id = building_outlines.suburb_locality_id LEFT JOIN buildings_reference.town_city ON town_city.town_city_id = building_outlines.town_city_id diff --git a/db/sql/revert/buildings_lds/functions/populate_buildings_lds@v4.0.0-dev1.sql b/db/sql/revert/buildings_lds/functions/populate_buildings_lds@v4.0.0-dev1.sql new file mode 100644 index 00000000..5b60e516 --- /dev/null +++ b/db/sql/revert/buildings_lds/functions/populate_buildings_lds@v4.0.0-dev1.sql @@ -0,0 +1,231 @@ +-- Deploy nz-buildings:buildings_lds/functions/populate_buildings_lds to pg + +BEGIN; + +------------------------------------------------------------------------------ +-- Populate buildings_lds schema in preparation for loading data to LDS +------------------------------------------------------------------------------ + +CREATE OR REPLACE FUNCTION buildings_lds.nz_building_outlines_insert() +RETURNS integer AS +$$ + + WITH populate_nz_building_outlines AS ( + INSERT INTO buildings_lds.nz_building_outlines ( + building_id + , name + , use + , suburb_locality + , town_city + , territorial_authority + , capture_method + , capture_source_group + , capture_source_id + , capture_source_name + , capture_source_from + , capture_source_to + , last_modified + , shape + ) + SELECT + buildings.building_id + , COALESCE(building_name.building_name, '') AS name + , COALESCE(use.value, 'Unknown') AS use + , COALESCE(suburb_locality.suburb_4th, suburb_locality.suburb_3rd, suburb_locality.suburb_2nd, suburb_locality.suburb_1st) AS suburb_locality + , COALESCE(town_city.name, '') AS town_city + , territorial_authority.name AS territorial_authority + , capture_method.value AS capture_method + , capture_source_group.value AS capture_source_group + , LEFT(capture_source.external_source_id, 4)::integer AS capture_source_id + , nz_imagery_survey_index.name AS capture_source_name + , nz_imagery_survey_index.flown_from AS capture_source_from + , nz_imagery_survey_index.flown_to AS capture_source_to + , GREATEST(building_outlines.begin_lifespan, COALESCE(building_outlines.end_lifespan, '1900-01-01 00:00:00'), COALESCE(building_outlines.last_modified, '1900-01-01 00:00:00'))::date AS last_modified + , building_outlines.shape + FROM buildings.building_outlines + JOIN buildings.buildings USING (building_id) + LEFT JOIN buildings.building_name ON buildings.building_id = building_name.building_id + AND building_name.end_lifespan IS NULL + LEFT JOIN buildings.building_use ON buildings.building_id = building_use.building_id + AND building_use.end_lifespan IS NULL + LEFT JOIN buildings.use USING (use_id) + JOIN buildings.lifecycle_stage USING (lifecycle_stage_id) + JOIN buildings_common.capture_method USING (capture_method_id) + JOIN buildings_common.capture_source USING (capture_source_id) + JOIN aerial_lds.nz_imagery_survey_index ON LEFT(capture_source.external_source_id, 4)::integer = nz_imagery_survey_index.imagery_survey_id + JOIN buildings_common.capture_source_group USING (capture_source_group_id) + JOIN buildings_reference.suburb_locality ON suburb_locality.suburb_locality_id = building_outlines.suburb_locality_id + LEFT JOIN buildings_reference.town_city ON town_city.town_city_id = building_outlines.town_city_id + JOIN buildings_reference.territorial_authority ON territorial_authority.territorial_authority_id = building_outlines.territorial_authority_id + WHERE building_outlines.end_lifespan IS NULL + AND buildings.end_lifespan IS NULL + ORDER BY buildings.building_id + RETURNING * + ) + SELECT count(*)::integer FROM populate_nz_building_outlines; + +$$ +LANGUAGE sql VOLATILE; + + +CREATE OR REPLACE FUNCTION buildings_lds.nz_building_outlines_all_sources_insert() +RETURNS integer AS +$$ + + WITH populate_nz_building_outlines_all_sources AS ( + INSERT INTO buildings_lds.nz_building_outlines_all_sources ( + building_outline_id + , building_id + , name + , use + , suburb_locality + , town_city + , territorial_authority + , capture_method + , capture_source_group + , capture_source_id + , capture_source_name + , capture_source_from + , capture_source_to + , building_outline_lifecycle + , begin_lifespan + , end_lifespan + , last_modified + , shape + ) + WITH transfer_dates AS ( + SELECT DISTINCT transfer_date + FROM buildings_bulk_load.supplied_datasets + ) + , deleted_in_production AS ( + SELECT building_outlines.building_outline_id, supplied_datasets.processed_date, supplied_datasets.transfer_date, building_outlines.begin_lifespan, building_outlines.end_lifespan + FROM buildings.building_outlines + JOIN buildings_bulk_load.transferred ON building_outlines.building_outline_id = transferred.new_building_outline_id + JOIN buildings_bulk_load.bulk_load_outlines USING (bulk_load_outline_id) + JOIN buildings_bulk_load.supplied_datasets USING (supplied_dataset_id) + WHERE building_outlines.end_lifespan IS NOT NULL + AND building_outlines.end_lifespan NOT IN ( + SELECT transfer_date + FROM transfer_dates + ) + ) + , removed AS ( + SELECT b1.building_outline_id + FROM buildings.building_outlines b1 + LEFT JOIN buildings.building_outlines b2 ON b1.building_id = b2.building_id AND b1.building_outline_id != b2.building_outline_id AND b2.end_lifespan IS NULL + LEFT JOIN buildings.lifecycle l ON b1.building_id = l.parent_building_id + LEFT JOIN deleted_in_production d ON b1.building_outline_id = d.building_outline_id + WHERE b1.end_lifespan IS NOT NULL + AND b2.building_outline_id IS NULL + AND l.parent_building_id IS NULL + AND d.building_outline_id IS NULL + ) + , replaced AS ( + SELECT b1.building_outline_id + FROM buildings.building_outlines b1 + JOIN buildings.building_outlines b2 ON b1.building_id = b2.building_id AND b1.building_outline_id != b2.building_outline_id AND b1.end_lifespan = b2.begin_lifespan AND b2.end_lifespan IS NULL + WHERE b1.end_lifespan IS NOT NULL + ) + , recombined AS ( + SELECT b.building_outline_id + FROM buildings.building_outlines b + JOIN buildings.lifecycle l ON b.building_id = l.parent_building_id + WHERE b.end_lifespan IS NOT NULL + ) + , building_outline_lifecycle AS ( + SELECT building_outline_id, 'Removed' AS status + FROM removed + UNION + SELECT building_outline_id, 'Replaced' AS status + FROM replaced + UNION + SELECT building_outline_id, 'Recombined' AS status + FROM recombined + ) + SELECT + building_outlines.building_outline_id + , buildings.building_id + , COALESCE(building_name.building_name, '') AS name + , COALESCE(use.value, 'Unknown') AS use + , COALESCE(suburb_locality.suburb_4th, suburb_locality.suburb_3rd, suburb_locality.suburb_2nd, suburb_locality.suburb_1st) AS suburb_locality + , COALESCE(town_city.name, '') AS town_city + , territorial_authority.name AS territorial_authority + , capture_method.value AS capture_method + , capture_source_group.value AS capture_source_group + , LEFT(capture_source.external_source_id, 4)::integer AS capture_source_id + , nz_imagery_survey_index.name AS capture_source_name + , nz_imagery_survey_index.flown_from AS capture_source_from + , nz_imagery_survey_index.flown_to AS capture_source_to + , COALESCE(building_outline_lifecycle.status, 'Current') AS building_outline_lifecycle + , building_outlines.begin_lifespan::date AS begin_lifespan + , building_outlines.end_lifespan::date AS end_lifespan + , GREATEST(building_outlines.begin_lifespan, COALESCE(building_outlines.end_lifespan, '1900-01-01 00:00:00'), COALESCE(building_outlines.last_modified, '1900-01-01 00:00:00'))::date AS last_modified + , building_outlines.shape + FROM buildings.building_outlines + JOIN buildings.buildings USING (building_id) + LEFT JOIN buildings.building_name ON buildings.building_id = building_name.building_id + AND building_name.end_lifespan IS NULL + LEFT JOIN buildings.building_use ON buildings.building_id = building_use.building_id + AND building_use.end_lifespan IS NULL + LEFT JOIN buildings.use USING (use_id) + JOIN buildings.lifecycle_stage USING (lifecycle_stage_id) + JOIN buildings_common.capture_method USING (capture_method_id) + JOIN buildings_common.capture_source USING (capture_source_id) + JOIN aerial_lds.nz_imagery_survey_index ON LEFT(capture_source.external_source_id, 4)::integer = nz_imagery_survey_index.imagery_survey_id + JOIN buildings_common.capture_source_group USING (capture_source_group_id) + JOIN buildings_reference.suburb_locality ON suburb_locality.suburb_locality_id = building_outlines.suburb_locality_id + LEFT JOIN buildings_reference.town_city ON town_city.town_city_id = building_outlines.town_city_id + JOIN buildings_reference.territorial_authority ON territorial_authority.territorial_authority_id = building_outlines.territorial_authority_id + LEFT JOIN deleted_in_production USING (building_outline_id) + LEFT JOIN building_outline_lifecycle USING (building_outline_id) + ORDER BY building_outlines.building_outline_id + RETURNING * + ) + SELECT count(*)::integer FROM populate_nz_building_outlines_all_sources; + +$$ +LANGUAGE sql VOLATILE; + +CREATE OR REPLACE FUNCTION buildings_lds.nz_building_outlines_lifecycle_insert() +RETURNS integer AS +$$ + + WITH populate_nz_building_outlines_lifecycle AS ( + INSERT INTO buildings_lds.nz_building_outlines_lifecycle ( + lifecycle_id + , parent_building_id + , building_id + ) + SELECT + lifecycle_id + , parent_building_id + , building_id + FROM buildings.lifecycle + RETURNING * + ) + SELECT count(*)::integer FROM populate_nz_building_outlines_lifecycle; + +$$ +LANGUAGE sql VOLATILE; + +CREATE OR REPLACE FUNCTION buildings_lds.populate_buildings_lds() +RETURNS TABLE( + table_name text + , rows_inserted integer +) AS +$$ + + TRUNCATE buildings_lds.nz_building_outlines; + TRUNCATE buildings_lds.nz_building_outlines_all_sources; + TRUNCATE buildings_lds.nz_building_outlines_lifecycle; + + VALUES + ('nz_building_outlines' , buildings_lds.nz_building_outlines_insert()) + , ('nz_building_outlines_all_sources' , buildings_lds.nz_building_outlines_all_sources_insert()) + , ('nz_building_outlines_lifecycle' , buildings_lds.nz_building_outlines_lifecycle_insert()) + ; + +$$ +LANGUAGE sql VOLATILE; + +COMMIT; diff --git a/db/sql/revert/buildings_reference/functions/suburb_locality_optimised.sql b/db/sql/revert/buildings_reference/functions/suburb_locality_optimised.sql index 2075ec5a..caffb8f4 100644 --- a/db/sql/revert/buildings_reference/functions/suburb_locality_optimised.sql +++ b/db/sql/revert/buildings_reference/functions/suburb_locality_optimised.sql @@ -1,171 +1,164 @@ --- Revert nz-buildings:buildings_reference/functions/update_suburb_locality_changed_deleted_buildings from pg +-- Deploy nz-buildings:buildings_reference/functions/update_suburb_locality_changed_deleted_buildings to pg BEGIN; -DROP FUNCTION buildings_reference.building_outlines_update_changed_and_deleted_suburb(); +DROP FUNCTION IF EXISTS buildings_reference.suburb_locality_insert_new_areas(); +DROP FUNCTION IF EXISTS buildings_reference.suburb_locality_update_suburb_locality(); +DROP FUNCTION IF EXISTS buildings_reference.suburb_locality_delete_removed_areas(); +DROP FUNCTION IF EXISTS buildings.building_outlines_update_suburb(integer[]); +DROP FUNCTION IF EXISTS buildings_bulk_load.bulk_load_outlines_update_all_suburbs(integer[]); -DROP FUNCTION buildings_reference.building_outlines_update_added_suburb(); - --- suburb_locality_delete_removed_areas (delete suburbs that are no longer is admin_bdys) - -- params: - -- return: integer list of outlines deleted - -CREATE OR REPLACE FUNCTION buildings_reference.suburb_locality_delete_removed_areas() -RETURNS integer[] AS -$$ - - WITH delete_suburb AS ( - DELETE FROM buildings_reference.suburb_locality - WHERE external_suburb_locality_id NOT IN ( - SELECT id - FROM admin_bdys.nz_locality - ) - RETURNING * - ) - SELECT ARRAY( - SELECT suburb_locality_id - FROM delete_suburb - ); - -$$ -LANGUAGE sql VOLATILE; - -COMMENT ON FUNCTION buildings_reference.suburb_locality_delete_removed_areas() IS -'Function to delete from the buildings_reference suburb locality table the areas that have been removed in the admin_bdys schema'; - --- suburb_locality_insert_new_areas (insert new areas from admin_bdys) - -- params: - -- return: integer list of areas inserted +-- building_outlines_update_changed_and_deleted_suburb (replace suburb values with the intersection result) + -- params: + -- return: integer count of number of building outlines updated -CREATE OR REPLACE FUNCTION buildings_reference.suburb_locality_insert_new_areas() -RETURNS integer[] AS +CREATE OR REPLACE FUNCTION buildings_reference.building_outlines_update_changed_and_deleted_suburb() +RETURNS void AS $$ - - WITH insert_suburb AS ( - INSERT INTO buildings_reference.suburb_locality ( - external_suburb_locality_id - , suburb_4th - , suburb_3rd - , suburb_2nd - , suburb_1st - , shape +BEGIN + -- Find new ids and geometry differences of changed + CREATE TEMP TABLE changed_geometries AS + SELECT + nzl.id AS new_external_id, + bsl.suburb_locality_id AS new_id, + ST_Difference(ST_Transform(nzl.shape, 2193),bsl.shape) AS shape + FROM + admin_bdys.nz_locality nzl, + buildings_reference.suburb_locality bsl + WHERE + bsl.external_suburb_locality_id = nzl.id + AND ( + NOT ST_Equals(ST_Transform(nzl.shape, 2193), bsl.shape) ) - SELECT - id - , suburb_4th - , suburb_3rd - , suburb_2nd - , suburb_1st - , ST_SetSRID(ST_Transform(shape, 2193), 2193) - FROM admin_bdys.nz_locality - WHERE type in ('ISLAND','LOCALITY','PARK_RESERVE','SUBURB') - AND id NOT IN ( - SELECT external_suburb_locality_id - FROM buildings_reference.suburb_locality + AND ( + NOT st_isempty(ST_Difference(ST_Transform(nzl.shape, 2193),bsl.shape) ) - RETURNING * - ) - SELECT ARRAY( - SELECT suburb_locality_id - FROM insert_suburb + ); + + -- Update the changed suburbs + UPDATE buildings_reference.suburb_locality bsl + SET + suburb_4th = nzl.suburb_4th, + suburb_3rd = nzl.suburb_3rd, + suburb_2nd = nzl.suburb_2nd, + suburb_1st = nzl.suburb_1st, + shape = ST_SetSRID(ST_Transform(nzl.shape, 2193), 2193) + FROM + admin_bdys.nz_locality nzl + WHERE + bsl.external_suburb_locality_id = nzl.id + AND ( + NOT ST_Equals(ST_SetSRID(ST_Transform(nzl.shape, 2193), 2193), bsl.shape) + OR nzl.suburb_4th != bsl.suburb_4th + OR nzl.suburb_3rd != bsl.suburb_3rd + OR nzl.suburb_2nd != bsl.suburb_2nd + OR nzl.suburb_1st != bsl.suburb_1st ); -$$ -LANGUAGE sql VOLATILE; - -COMMENT ON FUNCTION buildings_reference.suburb_locality_insert_new_areas() IS -'Function to insert from the admin_bdys schema new areas not in the buildings_reference suburb locality table'; - --- suburb_locality_update_suburb_locality (update geometries based on those in admin_bdys) - -- params: - -- return: integer list of areas updated - -CREATE OR REPLACE FUNCTION buildings_reference.suburb_locality_update_suburb_locality() -RETURNS integer[] AS -$$ - - WITH update_suburb AS ( - UPDATE buildings_reference.suburb_locality bsl - SET - suburb_4th = nzl.suburb_4th - , suburb_3rd = nzl.suburb_3rd - , suburb_2nd = nzl.suburb_2nd - , suburb_1st = nzl.suburb_1st - , shape = ST_SetSRID(ST_Transform(nzl.shape, 2193), 2193) - FROM admin_bdys.nz_locality nzl - WHERE bsl.external_suburb_locality_id = nzl.id - AND ( NOT ST_Equals(ST_SetSRID(ST_Transform(nzl.shape, 2193), 2193), bsl.shape) - OR nzl.suburb_4th != bsl.suburb_4th - OR nzl.suburb_3rd != bsl.suburb_3rd - OR nzl.suburb_2nd != bsl.suburb_2nd - OR nzl.suburb_1st != bsl.suburb_1st - ) - RETURNING * - ) - SELECT ARRAY(SELECT suburb_locality_id FROM update_suburb); - -$$ -LANGUAGE sql VOLATILE; + -- Remove deleted suburbs + DELETE FROM + buildings_reference.suburb_locality + WHERE + external_suburb_locality_id NOT IN ( + SELECT + id + FROM + admin_bdys.nz_locality + ); + + -- Updates building outlines where: + -- It's external id is an 'old id' + -- It Overlaps or is Within the difference polygon for the 'old id' + -- It overlaps the difference polygon by the largest proportion + -- The suburb with the most overlap is not the same as the old id + UPDATE + buildings.building_outlines b + SET + suburb_locality_id = buildings_reference.suburb_locality_intersect_polygon(b.shape), + last_modified = NOW() + FROM + changed_geometries cg + WHERE + (ST_Within(b.shape, cg.shape) OR ST_Overlaps(b.shape, cg.shape)) + AND buildings_reference.suburb_locality_intersect_polygon(b.shape) != b.suburb_locality_id; + + DISCARD TEMP; + +END; +$$ LANGUAGE plpgsql; + +COMMENT ON FUNCTION buildings_reference.building_outlines_update_changed_and_deleted_suburb() IS +'Replace suburb values with the intersection result of buildings in the building_outlines table'; -COMMENT ON FUNCTION buildings_reference.suburb_locality_update_suburb_locality() IS -'Function to update the attributes in the buildings_reference suburb locality table from the admin_bdys schema'; --- building_outlines_update_suburb (replace suburb values with the intersection result) - -- params: integer[] list of suburb localities building must be within +-- building_outlines_update_added_suburb (replace suburb values with the intersection result) + -- params: -- return: integer count of number of building outlines updated -CREATE OR REPLACE FUNCTION buildings.building_outlines_update_suburb(integer[]) -RETURNS integer AS +CREATE OR REPLACE FUNCTION buildings_reference.building_outlines_update_added_suburb() +RETURNS void AS $$ - - WITH update_suburb AS ( - UPDATE buildings.building_outlines outlines - SET suburb_locality_id = suburb_locality_intersect.suburb_locality_intersect_polygon - FROM ( - SELECT - buildings_reference.suburb_locality_intersect_polygon(outlines.shape) - , outlines.building_outline_id - FROM buildings.building_outlines outlines - ) suburb_locality_intersect - WHERE outlines.building_outline_id = suburb_locality_intersect.building_outline_id - AND suburb_locality_id = ANY($1) - RETURNING * +BEGIN + -- add new suburbs to buildings_reference.suburb_locality + -- (to get new suburb_locality_ids) + CREATE TEMP TABLE added_suburbs ( + suburb_locality_id integer + , external_suburb_locality_id integer + , suburb_4th character varying(60) + , suburb_3rd character varying(60) + , suburb_2nd character varying(60) + , suburb_1st character varying(60) + , shape public.geometry(MultiPolygon, 2193) + ); + + WITH add_new_suburbs AS ( + INSERT INTO buildings_reference.suburb_locality ( + external_suburb_locality_id, suburb_4th, + suburb_3rd, suburb_2nd, suburb_1st, + shape + ) + SELECT + id, + suburb_4th, + suburb_3rd, + suburb_2nd, + suburb_1st, + ST_Transform(shape, 2193) + FROM + admin_bdys.nz_locality + WHERE + type IN ('ISLAND','LOCALITY','PARK_RESERVE','SUBURB') + AND id NOT IN ( + SELECT + external_suburb_locality_id + FROM + buildings_reference.suburb_locality + ) RETURNING * ) - SELECT count(*)::integer FROM update_suburb; - -$$ -LANGUAGE sql VOLATILE; - -COMMENT ON FUNCTION buildings.building_outlines_update_suburb(integer[]) IS -'Replace suburb values with the intersection result of buildings in the building_outlines table'; - --- bulk_load_outlines_update_all_suburbs (replace all suburb values with the intersection result) - -- params: integer[] list of suburb localities building must be within - -- return: count(integer) number of building outlines updated - -CREATE OR REPLACE FUNCTION buildings_bulk_load.bulk_load_outlines_update_all_suburbs(integer[]) -RETURNS integer AS -$$ - - WITH update_suburb AS ( - UPDATE buildings_bulk_load.bulk_load_outlines outlines - SET suburb_locality_id = suburb_locality_intersect.suburb_locality_intersect_polygon - FROM ( - SELECT - buildings_reference.suburb_locality_intersect_polygon(outlines.shape) - , outlines.bulk_load_outline_id - FROM buildings_bulk_load.bulk_load_outlines outlines - ) suburb_locality_intersect - WHERE outlines.bulk_load_outline_id = suburb_locality_intersect.bulk_load_outline_id - AND suburb_locality_id = ANY($1) - RETURNING * - ) - SELECT count(*)::integer FROM update_suburb; - -$$ -LANGUAGE sql VOLATILE; -COMMENT ON FUNCTION buildings_bulk_load.bulk_load_outlines_update_all_suburbs(integer[]) IS -'Replace suburb values with the intersection result of all buildings in the bulk_load_outlines table'; + INSERT INTO added_suburbs + SELECT * + FROM add_new_suburbs; + + -- update building outline suburb locality Id and last modified date where: + -- the building is within/overlaps the new suburb + -- The largest overlapping suburb has changed + UPDATE + buildings.building_outlines b + SET + suburb_locality_id = buildings_reference.suburb_locality_intersect_polygon(b.shape), + last_modified = NOW() + FROM + added_suburbs asb + WHERE + (ST_Within(b.shape, asb.shape) OR ST_Overlaps(b.shape, asb.shape)) + AND buildings_reference.suburb_locality_intersect_polygon(b.shape) != b.suburb_locality_id; + + DISCARD TEMP; +END; +$$ LANGUAGE plpgsql; + +COMMENT ON FUNCTION buildings_reference.building_outlines_update_added_suburb() IS +'Insert new suburb localities and replace old suburb values with the intersection result in the building_outlines table'; COMMIT; diff --git a/db/sql/revert/buildings_reference/functions/suburb_locality_optimised@v4.0.0-dev1.sql b/db/sql/revert/buildings_reference/functions/suburb_locality_optimised@v4.0.0-dev1.sql new file mode 100644 index 00000000..2075ec5a --- /dev/null +++ b/db/sql/revert/buildings_reference/functions/suburb_locality_optimised@v4.0.0-dev1.sql @@ -0,0 +1,171 @@ +-- Revert nz-buildings:buildings_reference/functions/update_suburb_locality_changed_deleted_buildings from pg + +BEGIN; + +DROP FUNCTION buildings_reference.building_outlines_update_changed_and_deleted_suburb(); + +DROP FUNCTION buildings_reference.building_outlines_update_added_suburb(); + +-- suburb_locality_delete_removed_areas (delete suburbs that are no longer is admin_bdys) + -- params: + -- return: integer list of outlines deleted + +CREATE OR REPLACE FUNCTION buildings_reference.suburb_locality_delete_removed_areas() +RETURNS integer[] AS +$$ + + WITH delete_suburb AS ( + DELETE FROM buildings_reference.suburb_locality + WHERE external_suburb_locality_id NOT IN ( + SELECT id + FROM admin_bdys.nz_locality + ) + RETURNING * + ) + SELECT ARRAY( + SELECT suburb_locality_id + FROM delete_suburb + ); + +$$ +LANGUAGE sql VOLATILE; + +COMMENT ON FUNCTION buildings_reference.suburb_locality_delete_removed_areas() IS +'Function to delete from the buildings_reference suburb locality table the areas that have been removed in the admin_bdys schema'; + +-- suburb_locality_insert_new_areas (insert new areas from admin_bdys) + -- params: + -- return: integer list of areas inserted + +CREATE OR REPLACE FUNCTION buildings_reference.suburb_locality_insert_new_areas() +RETURNS integer[] AS +$$ + + WITH insert_suburb AS ( + INSERT INTO buildings_reference.suburb_locality ( + external_suburb_locality_id + , suburb_4th + , suburb_3rd + , suburb_2nd + , suburb_1st + , shape + ) + SELECT + id + , suburb_4th + , suburb_3rd + , suburb_2nd + , suburb_1st + , ST_SetSRID(ST_Transform(shape, 2193), 2193) + FROM admin_bdys.nz_locality + WHERE type in ('ISLAND','LOCALITY','PARK_RESERVE','SUBURB') + AND id NOT IN ( + SELECT external_suburb_locality_id + FROM buildings_reference.suburb_locality + ) + RETURNING * + ) + SELECT ARRAY( + SELECT suburb_locality_id + FROM insert_suburb + ); + +$$ +LANGUAGE sql VOLATILE; + +COMMENT ON FUNCTION buildings_reference.suburb_locality_insert_new_areas() IS +'Function to insert from the admin_bdys schema new areas not in the buildings_reference suburb locality table'; + +-- suburb_locality_update_suburb_locality (update geometries based on those in admin_bdys) + -- params: + -- return: integer list of areas updated + +CREATE OR REPLACE FUNCTION buildings_reference.suburb_locality_update_suburb_locality() +RETURNS integer[] AS +$$ + + WITH update_suburb AS ( + UPDATE buildings_reference.suburb_locality bsl + SET + suburb_4th = nzl.suburb_4th + , suburb_3rd = nzl.suburb_3rd + , suburb_2nd = nzl.suburb_2nd + , suburb_1st = nzl.suburb_1st + , shape = ST_SetSRID(ST_Transform(nzl.shape, 2193), 2193) + FROM admin_bdys.nz_locality nzl + WHERE bsl.external_suburb_locality_id = nzl.id + AND ( NOT ST_Equals(ST_SetSRID(ST_Transform(nzl.shape, 2193), 2193), bsl.shape) + OR nzl.suburb_4th != bsl.suburb_4th + OR nzl.suburb_3rd != bsl.suburb_3rd + OR nzl.suburb_2nd != bsl.suburb_2nd + OR nzl.suburb_1st != bsl.suburb_1st + ) + RETURNING * + ) + SELECT ARRAY(SELECT suburb_locality_id FROM update_suburb); + +$$ +LANGUAGE sql VOLATILE; + +COMMENT ON FUNCTION buildings_reference.suburb_locality_update_suburb_locality() IS +'Function to update the attributes in the buildings_reference suburb locality table from the admin_bdys schema'; + +-- building_outlines_update_suburb (replace suburb values with the intersection result) + -- params: integer[] list of suburb localities building must be within + -- return: integer count of number of building outlines updated + +CREATE OR REPLACE FUNCTION buildings.building_outlines_update_suburb(integer[]) +RETURNS integer AS +$$ + + WITH update_suburb AS ( + UPDATE buildings.building_outlines outlines + SET suburb_locality_id = suburb_locality_intersect.suburb_locality_intersect_polygon + FROM ( + SELECT + buildings_reference.suburb_locality_intersect_polygon(outlines.shape) + , outlines.building_outline_id + FROM buildings.building_outlines outlines + ) suburb_locality_intersect + WHERE outlines.building_outline_id = suburb_locality_intersect.building_outline_id + AND suburb_locality_id = ANY($1) + RETURNING * + ) + SELECT count(*)::integer FROM update_suburb; + +$$ +LANGUAGE sql VOLATILE; + +COMMENT ON FUNCTION buildings.building_outlines_update_suburb(integer[]) IS +'Replace suburb values with the intersection result of buildings in the building_outlines table'; + +-- bulk_load_outlines_update_all_suburbs (replace all suburb values with the intersection result) + -- params: integer[] list of suburb localities building must be within + -- return: count(integer) number of building outlines updated + +CREATE OR REPLACE FUNCTION buildings_bulk_load.bulk_load_outlines_update_all_suburbs(integer[]) +RETURNS integer AS +$$ + + WITH update_suburb AS ( + UPDATE buildings_bulk_load.bulk_load_outlines outlines + SET suburb_locality_id = suburb_locality_intersect.suburb_locality_intersect_polygon + FROM ( + SELECT + buildings_reference.suburb_locality_intersect_polygon(outlines.shape) + , outlines.bulk_load_outline_id + FROM buildings_bulk_load.bulk_load_outlines outlines + ) suburb_locality_intersect + WHERE outlines.bulk_load_outline_id = suburb_locality_intersect.bulk_load_outline_id + AND suburb_locality_id = ANY($1) + RETURNING * + ) + SELECT count(*)::integer FROM update_suburb; + +$$ +LANGUAGE sql VOLATILE; + +COMMENT ON FUNCTION buildings_bulk_load.bulk_load_outlines_update_all_suburbs(integer[]) IS +'Replace suburb values with the intersection result of all buildings in the bulk_load_outlines table'; + +COMMIT; diff --git a/db/sql/revert/buildings_reference/remove_suburb_locality_old_name_column.sql b/db/sql/revert/buildings_reference/remove_suburb_locality_old_name_column.sql new file mode 100644 index 00000000..47b781c9 --- /dev/null +++ b/db/sql/revert/buildings_reference/remove_suburb_locality_old_name_column.sql @@ -0,0 +1,11 @@ +-- Revert nz-buildings:buildings_reference/remove_suburb_locality_old_name_column from pg + +BEGIN; + +ALTER TABLE buildings_reference.suburb_locality +ADD COLUMN suburb_4th varchar(60), +ADD COLUMN suburb_3rd varchar(60), +ADD COLUMN suburb_2nd varchar(60), +ADD COLUMN suburb_1st varchar(60); + +COMMIT; diff --git a/db/sql/revert/buildings_reference/update_suburb_locality_name_column.sql b/db/sql/revert/buildings_reference/update_suburb_locality_name_column.sql new file mode 100644 index 00000000..cde5648c --- /dev/null +++ b/db/sql/revert/buildings_reference/update_suburb_locality_name_column.sql @@ -0,0 +1,8 @@ +-- Revert nz-buildings:buildings_reference/update_suburb_locality_name_column from pg + +BEGIN; + +ALTER TABLE buildings_reference.suburb_locality +DROP COLUMN name; + +COMMIT; diff --git a/db/sql/sqitch.plan b/db/sql/sqitch.plan index 5fa4ee54..bda98614 100644 --- a/db/sql/sqitch.plan +++ b/db/sql/sqitch.plan @@ -94,3 +94,8 @@ buildings_bulk_load/functions/add_facilities_functions 2022-06-15T02:04:34Z Jan buildings/functions/add_names_and_uses_functions 2022-06-15T02:54:14Z Jan Ducnuigeen # Functions to modify names and uses in the buildings tables buildings_bulk_load/functions/load_to_production [buildings_bulk_load/functions/load_to_production@v3.6.0-dev1] 2022-06-15T03:07:57Z Jan Ducnuigeen # Insert names and uses when loading bulk outlines into production @v4.0.0-dev1 2022-09-14T22:18:19Z Daniel Silk # Tag v4.0.0-dev1 + +buildings_reference/update_suburb_locality_name_column 2024-07-18T03:44:07Z Yingting Chen # Update suburb_locality name column to match NZ Suburbs and Localities +buildings_reference/functions/suburb_locality_optimised [buildings_reference/functions/suburb_locality_optimised@v4.0.0-dev1] 2024-07-18T05:10:43Z Yingting Chen # Update suburb_locality name fields in functions +buildings_lds/functions/populate_buildings_lds [buildings_lds/functions/populate_buildings_lds@v4.0.0-dev1] 2024-07-18T05:45:55Z Yingting Chen # Update suburb_locality name column in populate_buildings_lds function +buildings_reference/remove_suburb_locality_old_name_column 2024-07-18T06:26:12Z Yingting Chen # Remove suburb_locality old name columns from FENZ diff --git a/db/sql/verify/buildings_lds/functions/populate_buildings_lds.sql b/db/sql/verify/buildings_lds/functions/populate_buildings_lds.sql index 74e80bc2..fe98a24c 100644 --- a/db/sql/verify/buildings_lds/functions/populate_buildings_lds.sql +++ b/db/sql/verify/buildings_lds/functions/populate_buildings_lds.sql @@ -16,9 +16,9 @@ BEGIN PERFORM proname, proargnames, prosrc FROM pg_proc WHERE proname = 'nz_building_outlines_insert' - AND prosrc LIKE '%suburb_3rd%'; + AND prosrc LIKE '%suburb_locality.name%'; IF NOT FOUND THEN - RAISE EXCEPTION 'suburb_3rd not found.'; + RAISE EXCEPTION 'suburb_locality name not found.'; END IF; END $$; @@ -28,54 +28,23 @@ BEGIN PERFORM proname, proargnames, prosrc FROM pg_proc WHERE proname = 'nz_building_outlines_all_sources_insert' - AND prosrc LIKE '%suburb_3rd%'; + AND prosrc LIKE '%suburb_locality.name%'; IF NOT FOUND THEN - RAISE EXCEPTION 'suburb_3rd not found.'; + RAISE EXCEPTION 'suburb_locality name not found.'; END IF; END $$; DO $$ BEGIN - PERFORM proname, proargnames, prosrc - FROM pg_proc - WHERE proname = 'nz_building_outlines_all_sources_insert' - AND prosrc LIKE '%deleted_in_production.building_outline_id IS NULL%'; - IF FOUND THEN - RAISE EXCEPTION 'Building outlines that are deleted in production should not be excluded.'; - END IF; -END $$; -DO $$ -BEGIN PERFORM proname, proargnames, prosrc FROM pg_proc WHERE proname = 'nz_building_outlines_insert' - AND prosrc LIKE '%aerial_lds.nz_imagery_survey_index%'; - IF FOUND THEN - RAISE EXCEPTION 'aerial_lds found - should now refer to buildings_reference'; - END IF; -END $$; - -DO $$ -BEGIN - PERFORM proname, proargnames, prosrc - FROM pg_proc - WHERE proname = 'nz_building_outlines_all_sources_insert' - AND prosrc LIKE '%aerial_lds.nz_imagery_survey_index%'; + AND prosrc LIKE '%suburb_3rd%'; IF FOUND THEN - RAISE EXCEPTION 'aerial_lds found - should now refer to buildings_reference'; + RAISE EXCEPTION 'suburb_3rd found, should have been replaced by one name column.'; END IF; -END $$; -DO $$ -BEGIN - PERFORM proname, proargnames, prosrc - FROM pg_proc - WHERE proname = 'nz_building_outlines_insert' - AND prosrc LIKE '%building_name.begin_lifespan%'; - IF NOT FOUND THEN - RAISE EXCEPTION 'building_name date not taken into account for published last_modified date'; - END IF; END $$; DO $$ @@ -83,9 +52,9 @@ BEGIN PERFORM proname, proargnames, prosrc FROM pg_proc WHERE proname = 'nz_building_outlines_all_sources_insert' - AND prosrc LIKE '%building_name.begin_lifespan%'; - IF NOT FOUND THEN - RAISE EXCEPTION 'building_name date not taken into account for published last_modified date'; + AND prosrc LIKE '%suburb_3rd%'; + IF FOUND THEN + RAISE EXCEPTION 'suburb_3rd found, should have been replaced by one name column.'; END IF; END $$; diff --git a/db/sql/verify/buildings_lds/functions/populate_buildings_lds@v4.0.0-dev1.sql b/db/sql/verify/buildings_lds/functions/populate_buildings_lds@v4.0.0-dev1.sql new file mode 100644 index 00000000..74e80bc2 --- /dev/null +++ b/db/sql/verify/buildings_lds/functions/populate_buildings_lds@v4.0.0-dev1.sql @@ -0,0 +1,92 @@ +-- Verify nz-buildings:buildings_lds/functions/populate_buildings_lds on pg + +BEGIN; + +SELECT has_function_privilege('buildings_lds.nz_building_outlines_insert()', 'execute'); + +SELECT has_function_privilege('buildings_lds.nz_building_outlines_all_sources_insert()', 'execute'); + +SELECT has_function_privilege('buildings_lds.nz_building_outlines_lifecycle_insert()', 'execute'); + +SELECT has_function_privilege('buildings_lds.populate_buildings_lds()', 'execute'); + +DO $$ +BEGIN + + PERFORM proname, proargnames, prosrc + FROM pg_proc + WHERE proname = 'nz_building_outlines_insert' + AND prosrc LIKE '%suburb_3rd%'; + IF NOT FOUND THEN + RAISE EXCEPTION 'suburb_3rd not found.'; + END IF; + +END $$; + +DO $$ +BEGIN + PERFORM proname, proargnames, prosrc + FROM pg_proc + WHERE proname = 'nz_building_outlines_all_sources_insert' + AND prosrc LIKE '%suburb_3rd%'; + IF NOT FOUND THEN + RAISE EXCEPTION 'suburb_3rd not found.'; + END IF; +END $$; + +DO $$ +BEGIN + PERFORM proname, proargnames, prosrc + FROM pg_proc + WHERE proname = 'nz_building_outlines_all_sources_insert' + AND prosrc LIKE '%deleted_in_production.building_outline_id IS NULL%'; + IF FOUND THEN + RAISE EXCEPTION 'Building outlines that are deleted in production should not be excluded.'; + END IF; +END $$; + +DO $$ +BEGIN + PERFORM proname, proargnames, prosrc + FROM pg_proc + WHERE proname = 'nz_building_outlines_insert' + AND prosrc LIKE '%aerial_lds.nz_imagery_survey_index%'; + IF FOUND THEN + RAISE EXCEPTION 'aerial_lds found - should now refer to buildings_reference'; + END IF; +END $$; + +DO $$ +BEGIN + PERFORM proname, proargnames, prosrc + FROM pg_proc + WHERE proname = 'nz_building_outlines_all_sources_insert' + AND prosrc LIKE '%aerial_lds.nz_imagery_survey_index%'; + IF FOUND THEN + RAISE EXCEPTION 'aerial_lds found - should now refer to buildings_reference'; + END IF; +END $$; + +DO $$ +BEGIN + PERFORM proname, proargnames, prosrc + FROM pg_proc + WHERE proname = 'nz_building_outlines_insert' + AND prosrc LIKE '%building_name.begin_lifespan%'; + IF NOT FOUND THEN + RAISE EXCEPTION 'building_name date not taken into account for published last_modified date'; + END IF; +END $$; + +DO $$ +BEGIN + PERFORM proname, proargnames, prosrc + FROM pg_proc + WHERE proname = 'nz_building_outlines_all_sources_insert' + AND prosrc LIKE '%building_name.begin_lifespan%'; + IF NOT FOUND THEN + RAISE EXCEPTION 'building_name date not taken into account for published last_modified date'; + END IF; +END $$; + +ROLLBACK; diff --git a/db/sql/verify/buildings_reference/functions/suburb_locality_optimised.sql b/db/sql/verify/buildings_reference/functions/suburb_locality_optimised.sql index 14eac599..05c5bbe4 100644 --- a/db/sql/verify/buildings_reference/functions/suburb_locality_optimised.sql +++ b/db/sql/verify/buildings_reference/functions/suburb_locality_optimised.sql @@ -6,54 +6,4 @@ SELECT has_function_privilege('buildings_reference.building_outlines_update_chan SELECT has_function_privilege('buildings_reference.building_outlines_update_added_suburb()', 'execute'); -DO $$ -BEGIN - PERFORM * - FROM pg_proc - WHERE proname = 'suburb_locality_insert_new_areas'; - IF FOUND THEN - RAISE EXCEPTION 'Dropped function found.'; - END IF; -END $$; - -DO $$ -BEGIN - PERFORM * - FROM pg_proc - WHERE proname = 'suburb_locality_update_suburb_locality'; - IF FOUND THEN - RAISE EXCEPTION 'Dropped function found.'; - END IF; -END $$; - -DO $$ -BEGIN - PERFORM * - FROM pg_proc - WHERE proname = 'suburb_locality_delete_removed_areas'; - IF FOUND THEN - RAISE EXCEPTION 'Dropped function found.'; - END IF; -END $$; - -DO $$ -BEGIN - PERFORM * - FROM pg_proc - WHERE proname = 'building_outlines_update_suburb'; - IF FOUND THEN - RAISE EXCEPTION 'Dropped function found.'; - END IF; -END $$; - -DO $$ -BEGIN - PERFORM * - FROM pg_proc - WHERE proname = 'bulk_load_outlines_update_all_suburbs'; - IF FOUND THEN - RAISE EXCEPTION 'Dropped function found.'; - END IF; -END $$; - ROLLBACK; diff --git a/db/sql/verify/buildings_reference/functions/suburb_locality_optimised@v4.0.0-dev1.sql b/db/sql/verify/buildings_reference/functions/suburb_locality_optimised@v4.0.0-dev1.sql new file mode 100644 index 00000000..14eac599 --- /dev/null +++ b/db/sql/verify/buildings_reference/functions/suburb_locality_optimised@v4.0.0-dev1.sql @@ -0,0 +1,59 @@ +-- Verify nz-buildings:buildings_reference/functions/update_suburb_locality_changed_deleted_buildings on pg + +BEGIN; + +SELECT has_function_privilege('buildings_reference.building_outlines_update_changed_and_deleted_suburb()', 'execute'); + +SELECT has_function_privilege('buildings_reference.building_outlines_update_added_suburb()', 'execute'); + +DO $$ +BEGIN + PERFORM * + FROM pg_proc + WHERE proname = 'suburb_locality_insert_new_areas'; + IF FOUND THEN + RAISE EXCEPTION 'Dropped function found.'; + END IF; +END $$; + +DO $$ +BEGIN + PERFORM * + FROM pg_proc + WHERE proname = 'suburb_locality_update_suburb_locality'; + IF FOUND THEN + RAISE EXCEPTION 'Dropped function found.'; + END IF; +END $$; + +DO $$ +BEGIN + PERFORM * + FROM pg_proc + WHERE proname = 'suburb_locality_delete_removed_areas'; + IF FOUND THEN + RAISE EXCEPTION 'Dropped function found.'; + END IF; +END $$; + +DO $$ +BEGIN + PERFORM * + FROM pg_proc + WHERE proname = 'building_outlines_update_suburb'; + IF FOUND THEN + RAISE EXCEPTION 'Dropped function found.'; + END IF; +END $$; + +DO $$ +BEGIN + PERFORM * + FROM pg_proc + WHERE proname = 'bulk_load_outlines_update_all_suburbs'; + IF FOUND THEN + RAISE EXCEPTION 'Dropped function found.'; + END IF; +END $$; + +ROLLBACK; diff --git a/db/sql/verify/buildings_reference/remove_suburb_locality_old_name_column.sql b/db/sql/verify/buildings_reference/remove_suburb_locality_old_name_column.sql new file mode 100644 index 00000000..566eb70e --- /dev/null +++ b/db/sql/verify/buildings_reference/remove_suburb_locality_old_name_column.sql @@ -0,0 +1,20 @@ +-- Verify nz-buildings:buildings_reference/remove_suburb_locality_old_name_column on pg + +BEGIN; + +DO $$ +BEGIN + + PERFORM column_name + FROM information_schema.columns + WHERE table_schema = 'buildings_reference' + AND table_name = 'suburb_locality' + AND column_name in ('suburb_4th', 'suburb_3rd', 'suburb_2nd', 'suburb_1st'); + + IF FOUND THEN + RAISE EXCEPTION 'Columns suburb_4th, suburb_3rd, suburb_2nd, suburb_1st Found.'; + END IF; + +END $$; + +ROLLBACK; diff --git a/db/sql/verify/buildings_reference/update_suburb_locality_name_column.sql b/db/sql/verify/buildings_reference/update_suburb_locality_name_column.sql new file mode 100644 index 00000000..90b66060 --- /dev/null +++ b/db/sql/verify/buildings_reference/update_suburb_locality_name_column.sql @@ -0,0 +1,9 @@ +-- Verify nz-buildings:buildings_reference/update_suburb_locality_name_column on pg + +BEGIN; + +SELECT name +FROM buildings_reference.suburb_locality +WHERE FALSE; + +ROLLBACK; diff --git a/db/tests/testdata/db/buildings_reference.sql b/db/tests/testdata/db/buildings_reference.sql index 2ca88594..1cc37994 100644 --- a/db/tests/testdata/db/buildings_reference.sql +++ b/db/tests/testdata/db/buildings_reference.sql @@ -47,11 +47,11 @@ INSERT INTO buildings_reference.river_polygons (river_polygon_id, external_river -- buildings_reference.suburb_locality -INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, suburb_4th, suburb_3rd, suburb_2nd, suburb_1st, shape) VALUES (101, 101, 'Kelburn', NULL, NULL, NULL, '010600002091080000010000000103000000010000000500000066CEB1BCE2A93C41897FD94E1E315541B8C07DEFDDA73C4153A0BCF81F315541EBBFD613DBA73C41E6696DE47A3155417ECF8220DFA93C41692FC9727B31554166CEB1BCE2A93C41897FD94E1E315541'); -INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, suburb_4th, suburb_3rd, suburb_2nd, suburb_1st, shape) VALUES (102, 102, 'Aro Valley', NULL, NULL, NULL, '010600002091080000010000000103000000010000000500000062B26B4CADA83C419541DA4D1F31554157B126DEADA83C415EEB31D0EC30554130ECB486DFA73C417A89315EED305541B8C07DEFDDA73C4153A0BCF81F31554162B26B4CADA83C419541DA4D1F315541'); -INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, suburb_4th, suburb_3rd, suburb_2nd, suburb_1st, shape) VALUES (103, 103, 'Newtown', NULL, NULL, NULL, '010600002091080000010000000103000000010000000500000057B126DEADA83C415EEB31D0EC30554162B26B4CADA83C419541DA4D1F31554166CEB1BCE2A93C41897FD94E1E315541E08618B0E4A93C41AD1B4CFAEB30554157B126DEADA83C415EEB31D0EC305541'); -INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, suburb_4th, suburb_3rd, suburb_2nd, suburb_1st, shape) VALUES (104, 104, 'Hokowhitu', NULL, NULL, NULL, '010600002091080000010000000103000000010000000500000025FC5C6832AA3C41FCB6751C8631554125FC5C6832AA3C41E86F6C4BDA305541E3B2A05848AB3C41E86F6C4BDA305541E3B2A05848AB3C41E191179B8531554125FC5C6832AA3C41FCB6751C86315541'); -INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, suburb_4th, suburb_3rd, suburb_2nd, suburb_1st, shape) VALUES (107, 107, NULL, 'Test Island', NULL, NULL, '0106000020910800000100000001030000000100000005000000529CA67245A93C416CC0358DB1315541BB6C28D49BA93C4124914C0CB2315541C9D567C7A2A93C4180CF6FD28B315541AEB1EB2E47A93C417A11C4A58B315541529CA67245A93C416CC0358DB1315541'); +INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, name, shape) VALUES (101, 101, 'Kelburn', '010600002091080000010000000103000000010000000500000066CEB1BCE2A93C41897FD94E1E315541B8C07DEFDDA73C4153A0BCF81F315541EBBFD613DBA73C41E6696DE47A3155417ECF8220DFA93C41692FC9727B31554166CEB1BCE2A93C41897FD94E1E315541'); +INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, name, shape) VALUES (102, 102, 'Aro Valley', '010600002091080000010000000103000000010000000500000062B26B4CADA83C419541DA4D1F31554157B126DEADA83C415EEB31D0EC30554130ECB486DFA73C417A89315EED305541B8C07DEFDDA73C4153A0BCF81F31554162B26B4CADA83C419541DA4D1F315541'); +INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, name, shape) VALUES (103, 103, 'Newtown', '010600002091080000010000000103000000010000000500000057B126DEADA83C415EEB31D0EC30554162B26B4CADA83C419541DA4D1F31554166CEB1BCE2A93C41897FD94E1E315541E08618B0E4A93C41AD1B4CFAEB30554157B126DEADA83C415EEB31D0EC305541'); +INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, name, shape) VALUES (104, 104, 'Hokowhitu', '010600002091080000010000000103000000010000000500000025FC5C6832AA3C41FCB6751C8631554125FC5C6832AA3C41E86F6C4BDA305541E3B2A05848AB3C41E86F6C4BDA305541E3B2A05848AB3C41E191179B8531554125FC5C6832AA3C41FCB6751C86315541'); +INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, name, shape) VALUES (107, 107, 'Test Island', '0106000020910800000100000001030000000100000005000000529CA67245A93C416CC0358DB1315541BB6C28D49BA93C4124914C0CB2315541C9D567C7A2A93C4180CF6FD28B315541AEB1EB2E47A93C417A11C4A58B315541529CA67245A93C416CC0358DB1315541'); -- buildings_reference.swamp_polygons diff --git a/db/tests/testdata/plugin/buildings_reference.sql b/db/tests/testdata/plugin/buildings_reference.sql index cbd59f29..0c1ec988 100644 --- a/db/tests/testdata/plugin/buildings_reference.sql +++ b/db/tests/testdata/plugin/buildings_reference.sql @@ -62,10 +62,10 @@ INSERT INTO buildings_reference.shelter_points (shelter_points_id, external_shel -- buildings_reference.suburb_locality -INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, suburb_4th, suburb_3rd, suburb_2nd, suburb_1st, shape) VALUES (101, 101, 'Kelburn', NULL, NULL, NULL, '010600002091080000010000000103000000010000000500000066CEB1BCE2A93C41897FD94E1E315541B8C07DEFDDA73C4153A0BCF81F315541EBBFD613DBA73C41E6696DE47A3155417ECF8220DFA93C41692FC9727B31554166CEB1BCE2A93C41897FD94E1E315541'); -INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, suburb_4th, suburb_3rd, suburb_2nd, suburb_1st, shape) VALUES (102, 102, 'Aro Valley', NULL, NULL, NULL, '010600002091080000010000000103000000010000000500000062B26B4CADA83C419541DA4D1F31554157B126DEADA83C415EEB31D0EC30554130ECB486DFA73C417A89315EED305541B8C07DEFDDA73C4153A0BCF81F31554162B26B4CADA83C419541DA4D1F315541'); -INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, suburb_4th, suburb_3rd, suburb_2nd, suburb_1st, shape) VALUES (103, 103, 'Newtown', NULL, NULL, NULL, '010600002091080000010000000103000000010000000500000057B126DEADA83C415EEB31D0EC30554162B26B4CADA83C419541DA4D1F31554166CEB1BCE2A93C41897FD94E1E315541E08618B0E4A93C41AD1B4CFAEB30554157B126DEADA83C415EEB31D0EC305541'); -INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, suburb_4th, suburb_3rd, suburb_2nd, suburb_1st, shape) VALUES (104, 104, 'Hokowhitu', NULL, NULL, NULL, '010600002091080000010000000103000000010000000500000025FC5C6832AA3C41FCB6751C8631554125FC5C6832AA3C41E86F6C4BDA305541E3B2A05848AB3C41E86F6C4BDA305541E3B2A05848AB3C41E191179B8531554125FC5C6832AA3C41FCB6751C86315541'); +INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, name, shape) VALUES (101, 101, 'Kelburn', '010600002091080000010000000103000000010000000500000066CEB1BCE2A93C41897FD94E1E315541B8C07DEFDDA73C4153A0BCF81F315541EBBFD613DBA73C41E6696DE47A3155417ECF8220DFA93C41692FC9727B31554166CEB1BCE2A93C41897FD94E1E315541'); +INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, name, shape) VALUES (102, 102, 'Aro Valley', '010600002091080000010000000103000000010000000500000062B26B4CADA83C419541DA4D1F31554157B126DEADA83C415EEB31D0EC30554130ECB486DFA73C417A89315EED305541B8C07DEFDDA73C4153A0BCF81F31554162B26B4CADA83C419541DA4D1F315541'); +INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, name, shape) VALUES (103, 103, 'Newtown', '010600002091080000010000000103000000010000000500000057B126DEADA83C415EEB31D0EC30554162B26B4CADA83C419541DA4D1F31554166CEB1BCE2A93C41897FD94E1E315541E08618B0E4A93C41AD1B4CFAEB30554157B126DEADA83C415EEB31D0EC305541'); +INSERT INTO buildings_reference.suburb_locality (suburb_locality_id, external_suburb_locality_id, name, shape) VALUES (104, 104, 'Hokowhitu', '010600002091080000010000000103000000010000000500000025FC5C6832AA3C41FCB6751C8631554125FC5C6832AA3C41E86F6C4BDA305541E3B2A05848AB3C41E86F6C4BDA305541E3B2A05848AB3C41E191179B8531554125FC5C6832AA3C41FCB6751C86315541'); -- buildings_reference.swamp_polygons