Commit 8119210b authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Added feature to retrieve quadkeys of exposure entity

parent 6168e7a2
Pipeline #41198 passed with stage
in 2 minutes and 51 seconds
......@@ -631,3 +631,66 @@ class DatabaseQueries:
)
return building_classes_proportions
@staticmethod
def get_quadkeys_of_exposure_entity(
exposure_entity, aggregated_source_id, db_gde_tiles_config, db_table
):
"""This function retrieves the quadkeys of the tiles associated with an
'exposure_entity' and an 'aggregated_source_id' from the table with name 'db_table' in
the database whose credentials are indicated in 'db_gde_tiles_config'.
Args:
exposure_entity (str):
3-character code of the exposure entity for which the quadkeys will be
retrieved.
aggregated_source_id (int):
ID of the source of the aggregated exposure model.
db_gde_tiles_config (dict):
Dictionary containing the credentials needed to connect to the SQL database in
which information on the data-unit tiles is stored. The keys of the dictionary
need to be:
host (str):
SQL database host address.
dbname (str):
Name of the SQL database.
port (int):
Port where the SQL database can be found.
username (str):
User name to connect to the SQL database.
password (str):
Password associated with self.username.
db_table (str):
Name of the table of the SQL database where the data-unit tiles are stored. It
is assumed that this table contains, at least, the following fields:
quadkey (str):
String indicating the quadkey of a tile.
aggregated_source_id (int):
ID of the source of the aggregated exposure model.
exposure_entity (str):
3-character code of the exposure entity.
Returns:
quadkeys (array of str):
Unique quadkeys associated with 'exposure_entity' and 'aggregated_source_id'.
"""
sql_query = "SELECT DISTINCT quadkey FROM %s"
sql_query += " WHERE exposure_entity='%s' AND aggregated_source_id=%s;"
db_gde_tiles = Database(**db_gde_tiles_config)
db_gde_tiles.create_connection_and_cursor()
db_gde_tiles.cursor.execute(
sql_query % (db_table, exposure_entity, aggregated_source_id)
)
exec_result = db_gde_tiles.cursor.fetchall()
db_gde_tiles.close_connection()
if len(exec_result) > 1: # Entry exists --> retrieve
quadkeys = numpy.array([exec_result[i][0] for i in range(len(exec_result))])
else: # No entries found
quadkeys = numpy.array([], dtype=str)
return quadkeys
......@@ -159,6 +159,19 @@ def main():
% (aux_log_string, str(len(obm_buildings_building_classes.keys())))
)
# Get quadkeys associated with this exposure entity (all occupancies)
quadkeys = DatabaseQueries.get_quadkeys_of_exposure_entity(
exposure_entity_code,
aggregated_source_id,
config.database_gde_tiles,
"data_unit_tiles",
)
logger.info(
"%s quadkeys retrieved for exposure entity '%s'"
% (str(len(quadkeys)), exposure_entity_code)
)
# Leave the program
logger.info("gde-core has finished")
sys.exit()
......
......@@ -2,6 +2,7 @@ DROP TABLE IF EXISTS aggregated_sources;
DROP TABLE IF EXISTS data_units;
DROP TABLE IF EXISTS obm_buildings;
DROP TABLE IF EXISTS data_units_buildings;
DROP TABLE IF EXISTS data_unit_tiles;
DROP TYPE IF EXISTS occupancycase;
DROP TYPE IF EXISTS settlement;
DROP EXTENSION IF EXISTS postgis;
......@@ -164,3 +165,35 @@ VALUES ('A1/HBET:1-3', 'urban', 'all', 2, 'ABC', 'residential', 'ABC_10269', 0.2
('C4/HBET:2-3', 'urban', 'Trade', 2, 'ABC', 'commercial', 'ABC_10269', 0.10, 0.0, 0.0, 2, 3),
('C5/HBET:1-2', 'urban', 'Offices', 2, 'ABC', 'commercial', 'ABC_10269', 0.20, 0.0, 0.0, 1, 2),
('C6/HBET:3-5', 'urban', 'Offices', 2, 'ABC', 'commercial', 'ABC_10269', 0.30, 0.0, 0.0, 3, 5);
CREATE TABLE data_unit_tiles
(
quadkey char(18),
aggregated_source_id SMALLINT,
occupancy_case occupancycase,
exposure_entity char(3),
data_unit_id varchar,
size_data_unit_tile_area FLOAT,
size_data_unit_tile_built_up_area FLOAT,
fraction_data_unit_area FLOAT,
fraction_data_unit_built_up_area FLOAT,
aggregated_buildings FLOAT,
PRIMARY KEY (quadkey, aggregated_source_id, occupancy_case, data_unit_id)
);
INSERT INTO data_unit_tiles(quadkey,
aggregated_source_id,
occupancy_case,
exposure_entity,
data_unit_id,
size_data_unit_tile_area,
size_data_unit_tile_built_up_area,
fraction_data_unit_area,
fraction_data_unit_built_up_area,
aggregated_buildings)
VALUES ('122010321033023130', 2, 'residential', 'ABC', 'ABC_10269', 0.0, 0.0, 0.0, 0.0, 15.7),
('122010321033023130', 2, 'commercial', 'ABC', 'ABC_10269', 0.0, 0.0, 0.0, 0.0, 23.4),
('122010321033023120', 2, 'residential', 'ABC', 'ABC_10269', 0.0, 0.0, 0.0, 0.0, 39.1),
('122010321033023120', 2, 'commercial', 'ABC', 'ABC_10269', 0.0, 0.0, 0.0, 0.0, 17.6),
('122010321033023132', 2, 'residential', 'ABC', 'ABC_10269', 0.0, 0.0, 0.0, 0.0, 34.4),
('122010321033023132', 2, 'commercial', 'ABC', 'ABC_10269', 0.0, 0.0, 0.0, 0.0, 11.5);
......@@ -339,3 +339,30 @@ def test_get_building_classes_of_data_unit(test_db):
for col_name in expected_columns:
assert col_name in returned_building_classes.columns
assert round(returned_building_classes["proportions"].sum(), 5) == 0.0
def test_get_quadkeys_of_exposure_entity(test_db):
# Database connection (the Configuration class will define the credentials based on whether
# the code is running in the CI or locally)
config = Configuration(
os.path.join(os.path.dirname(__file__), "data", "config_for_testing_good.yml")
)
# Test case: quadkeys will be found
returned_quadkeys = DatabaseQueries.get_quadkeys_of_exposure_entity(
"ABC", 2, config.database_gde_tiles, "data_unit_tiles"
)
expected_quadkeys = ["122010321033023130", "122010321033023120", "122010321033023132"]
assert len(returned_quadkeys) == len(expected_quadkeys)
for quadkey in expected_quadkeys:
assert quadkey in returned_quadkeys
# Test case: no quadkeys will be found
returned_quadkeys = DatabaseQueries.get_quadkeys_of_exposure_entity(
"XYZ", 2, config.database_gde_tiles, "data_unit_tiles"
)
assert len(returned_quadkeys) == 0
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment