Commit ee608425 authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Made store_OBM_building_classes() erase first existing database entries

parent 67bc50b3
Pipeline #44365 passed with stage
in 2 minutes and 36 seconds
......@@ -163,7 +163,11 @@ class DatabaseStorage:
probabilities for each of the OBM buildings in 'obm_buildings_building_classes'.
Reference to the corresponding 'data_unit_id', 'occupancy_case' and
'aggregated_source_id' is needed to be able (at a later stage) to retrieve attributes of
the building classes.
the building classes. All existing entries for this combination of 'data_unit_id' AND
'occupancy_case' AND 'aggregated_source_id' are first erased. If an entry existed for a
particular OSM ID and 'aggregated_source_id' but it belonged to a different
'data_unit_id' and/or 'occupancy_case', both 'data_unit_id' and 'occupancy_case' are
updated to the new values passed to this method.
Args:
data_unit_id (str):
......@@ -246,6 +250,9 @@ class DatabaseStorage:
sql_commands = {}
sql_commands["delete_previous"] = "DELETE FROM %s WHERE (data_unit_id='%s' AND "
sql_commands["delete_previous"] += "occupancy_case='%s' AND aggregated_source_id=%s);"
sql_commands["query"] = "SELECT COUNT(*) FROM %s "
sql_commands["query"] += "WHERE (osm_id=%s AND aggregated_source_id=%s);"
......@@ -264,6 +271,13 @@ class DatabaseStorage:
db_gde_tiles = Database(**db_gde_tiles_config)
db_gde_tiles.create_connection_and_cursor()
# Delete all existing entries for this combination of
# (data_unit_id AND occupancy_case AND aggregated_source_id)
db_gde_tiles.cursor.execute(
sql_commands["delete_previous"]
% (db_table, data_unit_id, occupancy_case, aggregated_source_id)
)
for osm_id in obm_buildings_building_classes.keys():
building_classes = obm_buildings_building_classes[osm_id]
......
......@@ -242,10 +242,19 @@ INSERT INTO gde_buildings(osm_id,
building_class_names,
settlement_types,
occupancy_subtypes,
probabilities)
probabilities,
geometry)
VALUES (-101010, 2, 'industrial', 'ABC_10269', '333333333333333333',
'{"CLASS/X/params/H:1", "CLASS/Y/params/H:2"}',
'{"rural", "rural"}',
'{"all", "all"}',
'{0.723, 0.277}'
);
'{0.723, 0.277}',
ST_GeomFromText('POLYGON((15.0491 37.4811,15.0494 37.4814,15.0495 37.4813,15.0497 37.4812,15.0495 37.4811,15.0494 37.4812,15.0492 37.4810,15.0491 37.4811))')
),
(99999999, 2, 'residential', 'ABC_10269', '333333333333333333',
'{"CLASS/X/params/H:1", "CLASS/Y/params/H:2"}',
'{"rural", "rural"}',
'{"all", "all"}',
'{0.6, 0.4}',
ST_GeomFromText('POLYGON((0.0 0.0,0.002 0.0,0.002 0.003,0.0 0.003,0.0 0.0))')
);
......@@ -132,6 +132,23 @@ def test_store_OBM_building_classes(test_db):
os.path.join(os.path.dirname(__file__), "data", "config_for_testing_good.yml")
)
# Check that an certain entry for this combination of 'data_unit_id' AND 'occupancy_case'
# AND 'aggregated_source_id' exists in the database (it should later be erased by
# DatabaseStorage.store_OBM_building_classes())
(
returned_occupancy_case,
returned_data_unit_id,
returned_quadkey,
returned_building_class_names,
returned_settlement_types,
returned_occupancy_subtypes,
returned_probabilities,
returned_geometry,
) = query_OBM_building_classes(config.database_gde_tiles, 99999999, 2)
assert returned_data_unit_id == "ABC_10269"
assert returned_occupancy_case == "residential"
obm_bdg_classes = {}
obm_bdg_classes[11223344] = pandas.DataFrame(
{
......@@ -207,6 +224,23 @@ def test_store_OBM_building_classes(test_db):
obm_bdg_classes[11223344]["probabilities"].to_numpy()[which], 4
)
# Check that an existing entry for this combination of 'data_unit_id' AND 'occupancy_case'
# AND 'aggregated_source_id' was erased from the database by
# DatabaseStorage.store_OBM_building_classes()
(
returned_occupancy_case,
returned_data_unit_id,
returned_quadkey,
returned_building_class_names,
returned_settlement_types,
returned_occupancy_subtypes,
returned_probabilities,
returned_geometry,
) = query_OBM_building_classes(config.database_gde_tiles, 99999999, 2)
assert returned_data_unit_id is None
assert returned_occupancy_subtypes is None
obm_bdg_classes = {}
obm_bdg_classes[-101010] = pandas.DataFrame(
{
......@@ -364,18 +398,27 @@ def query_OBM_building_classes(credentials, osm_id, aggregated_source_id):
db_test.create_connection_and_cursor()
db_test.cursor.execute(sql_command)
result = db_test.cursor.fetchall()
occupancy_case = result[0][0]
data_unit_id = result[0][1]
quadkey = result[0][2]
building_class_names = result[0][3]
settlement_types = result[0][4].replace("{", "").replace("}", "").split(",")
occupancy_subtypes = result[0][5]
probabilities = result[0][6]
geometry = shapely.wkb.loads(result[0][7], hex=True)
db_test.close_connection()
if len(result) > 0:
occupancy_case = result[0][0]
data_unit_id = result[0][1]
quadkey = result[0][2]
building_class_names = result[0][3]
settlement_types = result[0][4].replace("{", "").replace("}", "").split(",")
occupancy_subtypes = result[0][5]
probabilities = result[0][6]
geometry = shapely.wkb.loads(result[0][7], hex=True)
else:
occupancy_case = None
data_unit_id = None
quadkey = None
building_class_names = None
settlement_types = None
occupancy_subtypes = None
probabilities = None
geometry = None
return (
occupancy_case,
data_unit_id,
......
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