Commit 5c113316 authored by Danijel Schorlemmer's avatar Danijel Schorlemmer
Browse files

Implemented the Geopackage output of results

parent 07bf071b
Pipeline #35771 passed with stage
in 2 minutes and 43 seconds
......@@ -21,7 +21,9 @@ import os
import argparse
import numpy as np
import pandas as pd
import geopandas as gpd
import datetime
from shapely import wkt
from .fragility_model import FragilityModel
......@@ -39,6 +41,7 @@ def damage_calculator(
gm_field_filepath,
interpolation_method="linear",
result_filepath="damage_result.csv",
output_format="gpkg",
use_xml_fragilities=True,
aggregation=True,
):
......@@ -106,7 +109,9 @@ def damage_calculator(
]
if aggregation:
aggregated = exposure.groupby(["id", "building_geometry", "tile_id", "tile_geometry"])[
aggregated = exposure.groupby(
["id", "building_geometry", "tile_id", "tile_geometry"], as_index=False
)[
[
"number",
"structural",
......@@ -118,7 +123,13 @@ def damage_calculator(
"structural_complete",
]
].sum()
aggregated.to_csv(result_filepath)
if output_format == "gpkg":
aggregated["geometry"] = aggregated["building_geometry"].apply(wkt.loads)
aggregated.drop("building_geometry", axis=1, inplace=True)
aggregated_gdf = gpd.GeoDataFrame(aggregated, geometry="geometry")
aggregated_gdf.to_file(result_filepath, driver="GPKG")
else:
aggregated.to_csv(result_filepath)
else:
exposure.to_csv(result_filepath, index=False)
......@@ -192,11 +203,22 @@ def main():
"--results",
required=False,
type=str,
default="damage_result.csv",
help="path to the file to write the results to (default set to" + "damage_result.csv) ",
default="damage_result.gpkg",
help="path to the file to write the results to (default: damage_result.gpkg) ",
)
parser.add_argument(
"-o",
"--output-format",
required=False,
type=str,
default="gpkg",
choices=["gpkg", "csv"],
help="format of the result output, either Geopackage ('gpkg') or comma-separated "
+ "values file ('csv'). Output to Geopackage only works without option '-a'."
+ "Options: ['gpkg', 'csv'] (default: gpkg)",
)
parser.add_argument(
"-w",
"--overwrite",
required=False,
action="store_true",
......@@ -207,7 +229,8 @@ def main():
"--no-aggregation",
required=False,
action="store_true",
help="do not aggregate the results for each building (detailed results for debugging)",
help="do not aggregate the results for each building (detailed results for debugging). "
+ "Does not work with output to Geopackage files ('-o gpkg).",
)
parser.add_argument(
"-t",
......@@ -230,6 +253,7 @@ def main():
gm_field_filepath = args.ground_motion_field
exposure_filepath = args.exposure
result_filepath = args.results
output_format = args.output_format
overwrite_result_file = args.overwrite
aggregation = not args.no_aggregation
taxonomy_map_filepath = args.taxonomy_map
......@@ -266,6 +290,9 @@ def main():
+ "fragility functions as CSV files."
)
exit()
if output_format == "gpkg" and not aggregation:
print("Error: Cannot output to Geopackage file without aggregation (-a option). ")
exit()
if os.path.exists(result_filepath):
if not overwrite_result_file:
print(
......@@ -287,6 +314,7 @@ def main():
gm_field_filepath,
interpolation_method,
result_filepath,
output_format,
use_xml_fragilities,
aggregation,
)
......
......@@ -17,6 +17,7 @@ setup(
"shapely",
"pyproj",
"pygeotile",
"geopandas",
],
extras_require={
"tests": tests_require,
......
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