From 0b94cc68cec11d97fcdf0891d609a7885db91cd3 Mon Sep 17 00:00:00 2001 From: tara Date: Tue, 19 Apr 2022 11:50:14 +0200 Subject: [PATCH] added update_tile_table & alter_table funcs to boundary_comparison.py --- boundarycomparator/boundary_comparator.py | 60 ++++++++++++++++++++++- 1 file changed, 59 insertions(+), 1 deletion(-) diff --git a/boundarycomparator/boundary_comparator.py b/boundarycomparator/boundary_comparator.py index 40316d4..91483f8 100644 --- a/boundarycomparator/boundary_comparator.py +++ b/boundarycomparator/boundary_comparator.py @@ -8,7 +8,7 @@ logger.setLevel(logging.DEBUG) logger.addHandler(logging.StreamHandler(sys.stdout)) -# Connecting to the exposure database using `exposurelib`. +# Connecting to the exposure database using `exposure-lib`. def connect_to_exposure_database( exposure_database_filepath, spatialite_filepath="mod_spatialite.so" ): @@ -47,6 +47,13 @@ def create_tables(exposure_db): logger.info("Table Stats created") +# Adds a field to the table `Tile` to be later fed by the function `update_tile_table` +def alter_table(exposure_db, table_name, field_name, field_type): + sql_statement = "ALTER TABLE %s " "ADD %s %s" % (table_name, field_name, field_type) + exposure_db.connection.execute(sql_statement) + return exposure_db + + # Attaches to a GeoPackage boundary file and copies over the boundaries and their names # and IDs to the table `Boundary` def import_districts_and_boundaries( @@ -163,6 +170,55 @@ def insert_into_stats(exposure_db): exposure_db.connection.commit() +# Insert the sum of number of assets per tile, for the `exposure_db` +def update_tile_table(exposure_db, field_name): + """ + Inserts the summation of number of buildings per tile of the `exposure-db` (Please note + that this summation is only for the boundary which the `exposure-db` is distributed + over) + + Args: + exposure_db (database): + The exposure database to which the results should be written (into the table + `Tile`) + field_name (str): + Name of the field to which the new data should be inserted + + The innermost query, selects tile IDs and summation of number of buildings + (named `tot_num_buildings`) per each `tile_id` from `TileAsset` table. + + Example extract: + + tile_id tot_num_buildings + 120233333333010231 0.74128 + 120233333333010233 0.74128 + 120322222222121233 0.97302 + ... + The middle query, selects only the `tot_num_buildings` field. + The outermost query, adds the `tot_num_buildings` to the respective + `field_name` in the table `Tile` mathing the tile IDs already in the table. + """ + + sql_statement = ( + """ + UPDATE Tile + SET %s = + ( + SELECT tot_num_buildings FROM + ( + SELECT tile_id, sum(TileAsset.number) AS tot_num_buildings + FROM TileAsset + GROUP BY tile_id + ) + WHERE tile_id = Tile.id + ) + """ + % field_name + ) + exposure_db.cursor.execute(sql_statement) + exposure_db.connection.commit() + + def main(): exposure_database_filepath = ( "../inputs/Attica/exposure/M023_exposure_Attica_GDE_" "cellspart_v005_ghs_Ind.db" @@ -174,6 +230,8 @@ def main(): import_districts_and_boundaries(exposure_db, gadm_boundaries_gpkg_filepath, 0) import_districts_and_boundaries(exposure_db, osm_boundaries_gpkg_filepath, 1) insert_into_stats(exposure_db) + alter_table(exposure_db, "Tile", "tot_num_buildings_per_tile", "REAL") + update_tile_table(exposure_db, "tot_num_buildings_per_tile") if __name__ == "__main__": -- GitLab