Commit 2d1143f8 authored by Cecilia Nievas's avatar Cecilia Nievas
Browse files

Made export_to_OpenQuake_CSV export geometry of tiles w/ buildings only

parent 77b03e08
Pipeline #47913 passed with stage
in 1 minute and 57 seconds
......@@ -42,9 +42,9 @@ def export_to_OpenQuake_CSV(
export_OBM_footprints=True,
):
"""This method exports the contents of a TileExposure object into the OpenQuake Engine CSV
format and additional GeoPackage (.gpkg) files that contain the geometry of quadtiles and
OBM buildings (the latter only if "OBM" is one of the type of 'buildings_to_export' and
'export_OBM_footprints' is True).
format and additional GeoPackage (.gpkg) files that contain the geometry of quadtiles (only
if any buildings have been exported) and OBM buildings (only if "OBM" is one of the type of
'buildings_to_export' and 'export_OBM_footprints' is True).
Details on the OpenQuake Engine: https://github.com/gem/oq-engine.
......@@ -140,22 +140,7 @@ def export_to_OpenQuake_CSV(
tile_centroid_lon = (tile_bounds.west + tile_bounds.east) / 2.0
tile_centroid_lat = (tile_bounds.south + tile_bounds.north) / 2.0
# File with geometries of quadtiles
filename_quadtiles = "%s_%s_geometries_quadtiles.gpkg" % (quadkeys_group, occupancy_case)
quadtile_geom = geopandas.GeoDataFrame(
{
"quadkey": pandas.Series([quadtile.quadkey], dtype=str),
"geometry": [tile_geometry],
},
geometry=[tile_geometry],
)
quadtile_geom.crs = pyproj.CRS("epsg:4326")
if os.path.exists(os.path.join(output_path, filename_quadtiles)): # append
quadtile_geom.to_file(
os.path.join(output_path, filename_quadtiles), driver="GPKG", mode="a"
)
else: # create
quadtile_geom.to_file(os.path.join(output_path, filename_quadtiles), driver="GPKG")
buildings_exported = False # To decide whether to export the geometry of this tile
for building_type in buildings_to_export:
# Identify the attribute of 'quadtile' that 'building_type' corresponds to
......@@ -169,6 +154,8 @@ def export_to_OpenQuake_CSV(
if data.shape[0] == 0:
continue
buildings_exported = True # Then export geometry of tile
# Prefix of unique ID
prefix_id = "%s_%s_%s" % (quadkeys_group, occupancy_case, building_type)
......@@ -295,4 +282,24 @@ def export_to_OpenQuake_CSV(
data, columns_order, os.path.join(output_path, filename_csv), writemode="a", sep=","
)
if not buildings_exported:
return
# File with geometries of quadtiles (only if any exported buildings exist in it)
filename_quadtiles = "%s_%s_geometries_quadtiles.gpkg" % (quadkeys_group, occupancy_case)
quadtile_geom = geopandas.GeoDataFrame(
{
"quadkey": pandas.Series([quadtile.quadkey], dtype=str),
"geometry": [tile_geometry],
},
geometry=[tile_geometry],
)
quadtile_geom.crs = pyproj.CRS("epsg:4326")
if os.path.exists(os.path.join(output_path, filename_quadtiles)): # append
quadtile_geom.to_file(
os.path.join(output_path, filename_quadtiles), driver="GPKG", mode="a"
)
else: # create
quadtile_geom.to_file(os.path.join(output_path, filename_quadtiles), driver="GPKG")
return
quadkey;case;geometry;centroid_lon;centroid_lat
122010321033023130;both;POLYGON ((15.04852294921875 37.480307245086024, 15.049896240234375 37.480307245086024, 15.049896240234375 37.48139702942733, 15.04852294921875 37.48139702942733, 15.04852294921875 37.480307245086024));15.049209594726562;37.480852137256676
122010321033023121;only_remainder;POLYGON ((15.047149658203125 37.480307245086024, 15.04852294921875 37.480307245086024, 15.04852294921875 37.48139702942733, 15.047149658203125 37.48139702942733, 15.047149658203125 37.480307245086024));15.047836303710938;37.480852137256676
122010321033023132;only_obm;POLYGON ((15.04852294921875 37.47921744485059, 15.049896240234375 37.47921744485059, 15.049896240234375 37.480307245086024, 15.04852294921875 37.480307245086024, 15.04852294921875 37.47921744485059));15.049209594726562;37.47976234496831
122010321033023131;none;POLYGON ((15.049896240234375 37.480307245086024, 15.05126953125 37.480307245086024, 15.05126953125 37.48139702942733, 15.049896240234375 37.48139702942733, 15.049896240234375 37.480307245086024));15.050582885742188;37.480852137256676
......@@ -50,7 +50,7 @@ def test_ExportHandler_process_quadkey_occupancy_group(test_db):
- 122010321033023132: associated with data unit ID 'ABC_10269' of exposure entity 'ABC',
which contains only OBM buildings.
- 122010321033023131: associated with no data unit ID (i.e. there is no entry for this
quadkey in the 'data_unit_tiles' table of the test database.
quadkey in the 'data_unit_tiles' table of the test database).
- By having three data unit IDs associated with quadkey 122010321033023121, the test
evaluates the tricks used to avoid re-retrieving parameters from the database that are
associated with the same exposure entity ('ABC'), as well as the use of "continue" to not
......@@ -99,7 +99,7 @@ def test_ExportHandler_process_quadkey_occupancy_group(test_db):
expected_results_path = os.path.join(os.path.dirname(__file__), "data")
expected_obm_buildings_output = "test_oq_expected_OBM_buildings_with_footprints_export.csv"
expected_remainder_buildings_output = "test_handler_expected_remainder_buildings.csv"
expected_quadtiles_geometries_output = "test_handler_expected_tile_geometries.csv"
expected_quadtiles_geometries_output = "test_oq_expected_tile_geometries.csv"
expected_obm_geometries_output = "test_oq_input_OBM_geometries.csv"
# Check summary values
......
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