Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added option for bfe from table when elevating #130

Merged
merged 1 commit into from
Sep 13, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
91 changes: 56 additions & 35 deletions hydromt_fiat/workflows/exposure_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -457,8 +457,10 @@ def setup_max_potential_damage(
)
]
except KeyError as e:
self.logger.warning(f"Not found in the {max_potential_damage} damage "
f"value data: {e}")
self.logger.warning(
f"Not found in the {max_potential_damage} damage "
f"value data: {e}"
)

def update_max_potential_damage(
self, updated_max_potential_damages: pd.DataFrame
Expand Down Expand Up @@ -550,7 +552,7 @@ def raise_ground_floor_height(
"Ground Floor Height",
].iloc[idx, :] = raise_by

elif height_reference.lower() == "geom":
elif height_reference.lower() in ["geom", "table"]:
# Elevate the objects relative to the surface water elevation map that the
# user submitted.
self.logger.info(
Expand All @@ -564,6 +566,7 @@ def raise_ground_floor_height(
self.exposure_db.iloc[idx, :] = self.set_height_relative_to_reference(
self.exposure_db.iloc[idx, :],
self.exposure_geoms[0].iloc[idx, :],
height_reference,
path_ref,
attr_ref,
raise_by,
Expand All @@ -574,10 +577,6 @@ def raise_ground_floor_height(
"original exposure data."
)

elif height_reference.lower() == "table":
# Input a CSV with Object IDs and elevations
NotImplemented

else:
self.logger.warning(
"The height reference of the Ground Floor Height is set to "
Expand Down Expand Up @@ -836,6 +835,7 @@ def setup_new_composite_areas(
new_objects = self.set_height_relative_to_reference(
new_objects,
_new_exposure_geoms,
elevation_reference,
path_ref,
attr_ref,
ground_floor_height,
Expand Down Expand Up @@ -1064,6 +1064,7 @@ def set_height_relative_to_reference(
self,
exposure_to_modify: pd.DataFrame,
exposure_geoms: gpd.GeoDataFrame,
height_reference: str,
path_ref: str,
attr_ref: str,
raise_by: Union[int, float],
Expand All @@ -1077,6 +1078,8 @@ def set_height_relative_to_reference(
_description_
exposure_geoms : gpd.GeoDataFrame
_description_
height_reference : str
_description_
path_ref : str
_description_
attr_ref : str
Expand All @@ -1095,36 +1098,54 @@ def set_height_relative_to_reference(
the same as that of the exposure data
"""
# Add the different options of input data: vector, raster, table
reference_shp = gpd.read_file(path_ref, engine="pyogrio") # Vector

# Reproject the input flood map if necessary
if reference_shp.crs != CRS.from_user_input(out_crs):
reference_shp = reference_shp.to_crs(
out_crs
) # TODO: make sure that the exposure_geoms file is projected in the out_crs (this doesn't happen now)

# Spatially join the data
modified_objects_gdf = gpd.sjoin(
exposure_geoms,
reference_shp[[attr_ref, "geometry"]],
how="left",
)
if height_reference == "geom":
reference_shp = gpd.read_file(path_ref, engine="pyogrio") # Vector

# Reproject the input flood map if necessary
if reference_shp.crs != CRS.from_user_input(out_crs):
reference_shp = reference_shp.to_crs(
out_crs
) # TODO: make sure that the exposure_geoms file is projected in the out_crs (this doesn't happen now)

# Spatially join the data
modified_objects_gdf = gpd.sjoin(
exposure_geoms,
reference_shp[[attr_ref, "geometry"]],
how="left",
)

# Sort and add the elevation to the shp values, append to the exposure dataframe
# To be able to append the values from the GeoDataFrame to the DataFrame, it
# must be sorted on the Object ID.
identifier = (
"Object ID" if "Object ID" in modified_objects_gdf.columns else "object_id"
)
# Sort and add the elevation to the shp values, append to the exposure dataframe
# To be able to append the values from the GeoDataFrame to the DataFrame, it
# must be sorted on the Object ID.
identifier = (
"Object ID"
if "Object ID" in modified_objects_gdf.columns
else "object_id"
)

# Group by the identifier and take the maximum value of the attribute reference
# to avoid duplicates in the case of overlapping polygons in the data used
# as reference.
modified_objects_gdf = (
modified_objects_gdf.groupby(identifier)
.max(attr_ref)
.sort_values(by=[identifier])
)

elif height_reference == "table":
# Add table
reference_table = pd.read_csv(path_ref) # Vector
# Join the data based on "Object ID"
modified_objects_gdf = pd.merge(
exposure_geoms,
reference_table[["Object ID", attr_ref]],
on="Object ID",
how="left",
)
modified_objects_gdf = modified_objects_gdf.sort_values(
by=["Object ID"]
).set_index("Object ID", drop=False)

# Group by the identifier and take the maximum value of the attribute reference
# to avoid duplicates in the case of overlapping polygons in the data used
# as reference.
modified_objects_gdf = (
modified_objects_gdf.groupby(identifier)
.max(attr_ref)
.sort_values(by=[identifier])
)
exposure_to_modify = exposure_to_modify.sort_values(by=["Object ID"]).set_index(
"Object ID", drop=False
)
Expand Down