diff --git a/gdecore/database_queries.py b/gdecore/database_queries.py index f0c4c08742725acffd9fc267ef1689437e4c48f4..6bbe4eb8e9344f5d941dcddbb57e68d684d9322c 100644 --- a/gdecore/database_queries.py +++ b/gdecore/database_queries.py @@ -202,13 +202,13 @@ class DatabaseQueries: Geometry of the data unit. Returns: - data_units_ids (array of str): + data_units_ids (list of str): IDs of all data units associated with 'exposure_entity', 'occupancy_case' and 'aggregated_source_id' in 'db_table', for which a corresponding geometry was retrieved. - data_units_geometries (array of Shapely Polygons/MultiPolygons): + data_units_geometries (list of Shapely Polygons/MultiPolygons): Geometries associated with the data units whose IDs are 'data_units_ids'. - data_units_ids_no_geometry (array of str): + data_units_ids_no_geometry (list of str): IDs of data units associated with 'exposure_entity', 'occupancy_case' and 'aggregated_source_id' in 'db_table', for which no geometry exists in 'db_table'. @@ -229,13 +229,10 @@ class DatabaseQueries: db_gde_tiles.close_connection() if len(exec_result) > 0: # Entries exist --> retrieve - data_units_ids_raw = numpy.array( - [exec_result[i][0] for i in range(len(exec_result))], dtype="str" - ) - data_units_geometries_raw = numpy.array( - [exec_result[i][1] for i in range(len(exec_result))], - dtype="object", - ) # geometries in WKB format, might include Nones + data_units_ids_raw = [exec_result[i][0] for i in range(len(exec_result))] + data_units_geometries_raw = [ + exec_result[i][1] for i in range(len(exec_result)) + ] # geometries in WKB format, might include Nones ( data_units_ids, data_units_geometries, @@ -244,9 +241,9 @@ class DatabaseQueries: data_units_ids_raw, data_units_geometries_raw ) else: # No entries found - data_units_ids = numpy.array([], dtype="str") - data_units_geometries = numpy.array([], dtype="object") - data_units_ids_no_geometry = numpy.array([], dtype="str") + data_units_ids = [] + data_units_geometries = [] + data_units_ids_no_geometry = [] return data_units_ids, data_units_geometries, data_units_ids_no_geometry @@ -257,39 +254,32 @@ class DatabaseQueries: are assumed to correspond to one another. Args: - ids (array of str): + ids (list of str): IDs of elements whose geometry is contained in 'geometries'. - geometries (array of Well-Known Binary (WKB) geometries): + geometries (list of Well-Known Binary (WKB) geometries): Geometries of the elements with IDs contained in 'ids', in Well-Known Binary (WKB) format. Returns: - ids_processed (array of str): + ids_processed (list of str): Elements of 'ids' for which their corresponding element in 'geometries' is not "None". - geometries_processed (array of Shapely Polygons/MultiPolygons): + geometries_processed (list of Shapely Polygons/MultiPolygons): Elements of 'geometries' that are not "None" (in correspondence with 'ids_processed'). - ids_no_geometry (array of str): + ids_no_geometry (list of str): Elements of 'ids' for which their corresponding element in 'geometries' is "None". """ - which_not_none = numpy.where(geometries != None)[0] # noqa: E711 - which_none = numpy.where(geometries == None)[0] # noqa: E711 - - ids_processed = ids[which_not_none] - if len(which_not_none) > 0: - geometries_processed = numpy.array( - [ - shapely.wkb.loads(geometries[which_not_none][i], hex=True) - for i in range(len(which_not_none)) - ], - dtype="object", - ) - else: - geometries_processed = numpy.array([], dtype="object") - ids_no_geometry = ids[which_none] + # Geometries different from None + geometries_processed = [ + shapely.wkb.loads(val, hex=True) for val in geometries if val is not None + ] + ids_processed = [ids[i] for i, val in enumerate(geometries) if val is not None] + + # Geometries that are None + ids_no_geometry = [ids[i] for i, val in enumerate(geometries) if val is None] return ids_processed, geometries_processed, ids_no_geometry diff --git a/tests/test_database_queries.py b/tests/test_database_queries.py index c28550871ff9df6de2acd0bf0f68bd054999b5b3..76332737ebfeba8b88bdf00926be628c40043e3a 100644 --- a/tests/test_database_queries.py +++ b/tests/test_database_queries.py @@ -108,7 +108,7 @@ def test_get_data_unit_ids_geometries_of_entity_and_occupancy_case(test_db): for i, data_unit_id in enumerate(expected_data_units_ids): assert data_unit_id in returned_data_units_ids - which = numpy.where(returned_data_units_ids == data_unit_id)[0][0] + which = returned_data_units_ids.index(data_unit_id) assert returned_data_units_geometries[which].bounds[0] == expected_bounds_west[i] assert returned_data_units_geometries[which].bounds[1] == expected_bounds_south[i] @@ -164,9 +164,7 @@ def test_get_OBM_buildings_in_data_unit_by_occupancy_types(test_db): ) # Test cases in which buildings will be retrieved - geometry = returned_data_units_geometries[ - numpy.where(returned_data_units_ids == "ABC_10269")[0][0] - ] + geometry = returned_data_units_geometries[returned_data_units_ids.index("ABC_10269")] returned_obm_buildings = DatabaseQueries.get_OBM_buildings_in_data_unit_by_occupancy_types( ["RES", "RES99", "RES1", "RES2", "RES4", "RES6", "MIX1", "MIX2", "MIX4"], @@ -209,9 +207,7 @@ def test_get_OBM_buildings_in_data_unit_by_occupancy_types(test_db): ) # Test case in which no buildings will be retrieved - geometry = returned_data_units_geometries[ - numpy.where(returned_data_units_ids == "ABC_10277")[0][0] - ] + geometry = returned_data_units_geometries[returned_data_units_ids.index("ABC_10277")] returned_obm_buildings = DatabaseQueries.get_OBM_buildings_in_data_unit_by_occupancy_types( ["RES", "RES99", "RES1", "RES2", "RES4", "RES6", "MIX1", "MIX2", "MIX4"], diff --git a/tests/test_processor.py b/tests/test_processor.py index 32c046bf2833aeb0ceffcf4a7c32e5f5c6ec7d0d..832e7cdaf59d1ae21c5d5cda51cbb73a32d808ce 100644 --- a/tests/test_processor.py +++ b/tests/test_processor.py @@ -43,9 +43,7 @@ def test_post_process_obm_relations(test_db): "ABC", "residential", 2, config.database_gde_tiles, "data_units" ) # auxiliary, to retrieve the geometry of the data unit - geometry = returned_data_units_geometries[ - numpy.where(returned_data_units_ids == "ABC_10269")[0][0] - ] + geometry = returned_data_units_geometries[returned_data_units_ids.index("ABC_10269")] # Group of residential buildings that do not belong to relations raw_obm_buildings = DatabaseQueries.get_OBM_buildings_in_data_unit_by_occupancy_types( @@ -167,9 +165,7 @@ def test_assign_building_classes_to_obm_buildings(test_db): "ABC", "residential", 2, config.database_gde_tiles, "data_units" ) # auxiliary, to retrieve the geometry of the data unit - geometry = returned_data_units_geometries[ - numpy.where(returned_data_units_ids == "ABC_10269")[0][0] - ] + geometry = returned_data_units_geometries[returned_data_units_ids.index("ABC_10269")] # Group of residential buildings that do not belong to relations raw_obm_buildings = DatabaseQueries.get_OBM_buildings_in_data_unit_by_occupancy_types( @@ -495,9 +491,8 @@ def test_process_data_unit(test_db): ) = DatabaseQueries.get_data_unit_ids_geometries_of_entity_and_occupancy_case( "ABC", "residential", 2, config.database_gde_tiles, "data_units" ) - geometry = returned_data_units_geometries[ - numpy.where(returned_data_units_ids == "ABC_10269")[0][0] - ] + + geometry = returned_data_units_geometries[returned_data_units_ids.index("ABC_10269")] returned_summary_values = GDEProcessor.process_data_unit( config,