Skip to content

Commit

Permalink
Merge pull request #225 from Deltares/gui-updates
Browse files Browse the repository at this point in the history
Update from the GUI updates, all tests run green again
  • Loading branch information
frederique-hub authored Dec 13, 2023
2 parents 32787a6 + 3dc3585 commit 213b59f
Show file tree
Hide file tree
Showing 12 changed files with 191 additions and 64 deletions.
8 changes: 7 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -135,4 +135,10 @@ examples/data/global_OSM_JRC/fiat_model/
examples/data/update_ground_floor_height/test_update_ground_floor_height_points/
examples/data/update_ground_floor_height/test_update_ground_floor_height_polygons/
examples/data/building_footprints/fiat_model_bfs/
examples/data/aggregation_zones/output/
examples/data/aggregation_zones/output/
examples/data/setup_exposure_buildings_with_dem_data/test_vulnerability_and_exposure_NSI_with_dem_data/
examples/data/setup_new_composite_area/test_setup_new_composite_area_datum/
examples/data/setup_new_composite_area/test_setup_new_composite_area_elevation/
examples/data/setup_new_composite_area/test_setup_new_composite_area_geom/
examples/data/update_max_potential_damage/test_update_max_potential_damage_points/
examples/data/update_max_potential_damage/test_update_max_potential_damage_polygons/
20 changes: 19 additions & 1 deletion hydromt_fiat/api/data_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,24 @@ class ExposureBuildingsSettings(BaseModel):
damage_types : Union[List[str], None]


class ExposureSetupGroundFloorHeight(BaseModel):
source: str
attribute_name: Union[str, List[str], None]
method: Union[str, List[str], None]
max_dist: Union[float, int, None]


class ExposureSetupGroundElevation(BaseModel):
source: Union[int, float, None, str]


class ExposureSetupDamages(BaseModel):
source: str
attribute_name: Union[str, List[str], None]
method: Union[str, List[str], None]
max_dist: Union[float, int, None]


class RoadVulnerabilitySettings(BaseModel):
threshold_value: float
min_hazard_value: float
Expand All @@ -86,7 +104,7 @@ class RoadVulnerabilitySettings(BaseModel):
class ExposureRoadsSettings(BaseModel):
roads_fn: str
road_types: Union[List[str], bool]
road_damage: str
road_damage: Union[str, int]
unit: Units


Expand Down
50 changes: 45 additions & 5 deletions hydromt_fiat/api/exposure_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
DataType,
Driver,
ExposureBuildingsSettings,
ExposureSetupGroundFloorHeight,
ExposureSetupDamages,
ExposureSetupGroundElevation,
ExposureRoadsSettings,
ExtractionMethod,
AggregationAreaSettings,
Expand All @@ -27,6 +30,9 @@ def __init__(
self.exposure_buildings_model = None
self.exposure_roads_model = None
self.aggregation_areas_model = None
self.exposure_ground_floor_height_model = None
self.exposure_damages_model = None
self.exposure_ground_elevation_model = None

self.database: IDatabase = database
self.data_catalog: DataCatalog = data_catalog
Expand All @@ -51,6 +57,7 @@ def create_interest_area(self, **kwargs: str):
def set_asset_locations_source(
self,
source: str,
ground_floor_height: str,
fiat_key_maps: Optional[Dict[str, str]] = None,
crs: Union[str, int] = None,
):
Expand All @@ -60,7 +67,7 @@ def set_asset_locations_source(
asset_locations=source,
occupancy_type=source,
max_potential_damage=source,
ground_floor_height=source,
ground_floor_height=ground_floor_height,
unit=Units.ft.value, # TODO: make flexible
extraction_method=ExtractionMethod.centroid.value,
damage_types=["structure", "content"],
Expand All @@ -77,8 +84,8 @@ def set_asset_locations_source(

self.exposure.setup_buildings_from_single_source(
source,
self.exposure_buildings_model.ground_floor_height,
"centroid", # TODO: MAKE FLEXIBLE
ground_floor_height,
"centroid",
)
primary_object_types = (
self.exposure.exposure_db["Primary Object Type"].unique().tolist()
Expand Down Expand Up @@ -119,6 +126,39 @@ def setup_extraction_method(self, extraction_method):
if self.exposure:
self.exposure.setup_extraction_method(extraction_method)

def set_ground_floor_height(
self,
source: str,
attribute_name: Union[str, List[str], None] = None,
method: Union[str, List[str], None] = "nearest",
max_dist: Union[float, int, None] = 10,
):
self.exposure_ground_floor_height_model = ExposureSetupGroundFloorHeight(
source=source,
attribute_name=attribute_name,
method=method,
max_dist=max_dist,
)

def set_damages(
self,
source: str,
attribute_name: Union[str, List[str], None] = None,
method: Union[str, List[str], None] = "nearest",
max_dist: Union[float, int, None] = 10,
):
self.exposure_damages_model = ExposureSetupDamages(
source=source,
attribute_name=attribute_name,
method=method,
max_dist=max_dist,
)

def set_ground_elevation(self, source: Union[int, float, None, str]):
self.exposure_ground_elevation_model = ExposureSetupGroundElevation(
source=source
)

def get_osm_roads(
self,
road_types: List[str] = [
Expand All @@ -144,7 +184,7 @@ def get_osm_roads(

self.exposure.setup_roads(
source="OSM",
road_damage="default_road_max_potential_damages",
road_damage=1,
road_types=road_types,
)
roads = self.exposure.exposure_db.loc[
Expand All @@ -155,7 +195,7 @@ def get_osm_roads(
self.exposure_roads_model = ExposureRoadsSettings(
roads_fn="OSM",
road_types=road_types,
road_damage="default_road_max_potential_damages",
road_damage=1,
unit=Units.ft.value,
)

Expand Down
8 changes: 7 additions & 1 deletion hydromt_fiat/api/hydromt_fiat_vm.py
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ def build_config_yaml(self):
if self.exposure_vm.exposure_roads_model:
config_yaml.setup_exposure_roads = self.exposure_vm.exposure_roads_model

if self.exposure_vm.exposure_damages_model:
config_yaml.update_max_potential_damage = self.exposure_vm.exposure_damages_model

if self.exposure_vm.exposure_ground_floor_height_model:
config_yaml.update_ground_floor_height = self.exposure_vm.exposure_ground_floor_height_model

if self.vulnerability_vm.vulnerability_roads_model:
config_yaml.setup_road_vulnerability = self.vulnerability_vm.vulnerability_roads_model

Expand All @@ -82,7 +88,7 @@ def build_config_yaml(self):

if self.svi_vm.equity_model:
config_yaml.setup_equity_data = self.svi_vm.equity_model

database_path = self.__class__.database.drive

with open(database_path / "config.yaml", "wb") as f:
Expand Down
72 changes: 59 additions & 13 deletions hydromt_fiat/fiat.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
create_risk_dataset,
)
from hydromt_fiat.workflows.equity_data import EquityData
from hydromt_fiat.workflows.social_vulnerability_index import SocialVulnerabilityIndex, list_of_states
from hydromt_fiat.workflows.social_vulnerability_index import (
SocialVulnerabilityIndex,
list_of_states,
)
from hydromt_fiat.workflows.vulnerability import Vulnerability
from hydromt_fiat.workflows.aggregation_areas import join_exposure_aggregation_areas
from hydromt_fiat.workflows.building_footprints import join_exposure_building_footprints
Expand Down Expand Up @@ -320,14 +323,14 @@ def setup_exposure_buildings(
"""
self.exposure = ExposureVector(self.data_catalog, self.logger, self.region)

if asset_locations == occupancy_type == max_potential_damage == ground_floor_height:
if asset_locations == occupancy_type == max_potential_damage:
# The source for the asset locations, occupancy type and maximum potential
# damage is the same, use one source to create the exposure data.
self.exposure.setup_buildings_from_single_source(
asset_locations,
ground_floor_height,
asset_locations,
ground_floor_height,
extraction_method,
ground_elevation_file=ground_elevation_file,
ground_elevation_file=ground_elevation_file,
)

else:
Expand Down Expand Up @@ -366,7 +369,7 @@ def setup_exposure_buildings(
def setup_exposure_roads(
self,
roads_fn: Union[str, Path],
road_damage: Union[str, Path],
road_damage: Union[str, Path, int],
road_types: Union[str, List[str], bool] = True,
unit: str = "m",
):
Expand All @@ -380,13 +383,55 @@ def setup_exposure_roads(
List of road types to include in the exposure data, by default True
"""
if not self.exposure:
self.exposure = ExposureVector(self.data_catalog, self.logger, self.region, unit=unit)
self.exposure = ExposureVector(
self.data_catalog, self.logger, self.region, unit=unit
)
self.exposure.setup_roads(roads_fn, road_damage, road_types)

# Link to vulnerability curves

# Combine the exposure database with pre-existing exposure data if available

def update_ground_floor_height(
self,
source: Union[int, float, str, Path, None],
attr_name: Union[str, List[str], None] = None,
method: Union[str, List[str], None] = "nearest",
max_dist: float = 10,
):
if self.exposure:
self.exposure.setup_ground_floor_height(source, attr_name, method, max_dist)

def update_max_potential_damage(
self,
source: Union[
int, float, str, Path, List[str], List[Path], pd.DataFrame
] = None,
damage_types: Union[List[str], str, None] = None,
country: Union[str, None] = None,
target_attribute: Union[str, List[str], None] = None,
attr_name: Union[str, List[str], None] = None,
method: Union[str, List[str], None] = "nearest",
max_dist: float = 10,
):
if self.exposure:
self.exposure.setup_max_potential_damage(
max_potential_damage=source,
damage_types=damage_types,
country=country,
target_attribute=target_attribute,
attr_name=attr_name,
method=method,
max_dist=max_dist,
)

def update_ground_elevation(
self,
ground_elevation: Union[int, float, None, str, Path],
):
if self.exposure:
self.exposure.setup_ground_elevation(ground_elevation)

def setup_exposure_raster(self):
"""Setup raster exposure data for Delft-FIAT.
This function will be implemented at a later stage.
Expand Down Expand Up @@ -584,15 +629,15 @@ def setup_social_vulnerability_index(
Census data
codebook_fn : Union[str, Path]
The path to the codebook excel
year_data: int
year_data: int
The year of which the census data should be downloaded, 2020, 2021, or 2022
save_all: bool
If True, all (normalized) data variables are saved, if False, only the SVI
column is added to the FIAT exposure data. By default False.
"""
# Check if the exposure data exists
if self.exposure:
# First find the state(s) and county/counties where the exposure data is
# First find the state(s) and county/counties where the exposure data is
# located in
us_states_counties = self.data_catalog.get_dataframe("us_states_counties")
counties, states = locate_from_bounding_box(self.exposure.bounding_box())
Expand Down Expand Up @@ -652,13 +697,15 @@ def setup_social_vulnerability_index(
# Only save the SVI_key_domain and composite_svi_z
cols_to_save = ["SVI_key_domain", "composite_svi_z", "geometry"]

svi_exp_joined = gpd.sjoin(exposure_data, svi.svi_data_shp[cols_to_save], how="left")
svi_exp_joined = gpd.sjoin(
exposure_data, svi.svi_data_shp[cols_to_save], how="left"
)
svi_exp_joined.drop(columns=["geometry"], inplace=True)
svi_exp_joined = pd.DataFrame(svi_exp_joined)
svi_exp_joined.rename(columns={"composite_svi_z": "SVI"}, inplace=True)
del svi_exp_joined["index_right"]
self.exposure.exposure_db = svi_exp_joined

def setup_equity_data(
self,
census_key: str,
Expand All @@ -677,7 +724,7 @@ def setup_equity_data(
path : Union[str, Path]
The path to the codebook excel
"""
# First find the state(s) and county/counties where the exposure data is
# First find the state(s) and county/counties where the exposure data is
# located in
us_states_counties = self.data_catalog.get_dataframe("us_states_counties")
counties, states = locate_from_bounding_box(self.exposure.bounding_box())
Expand Down Expand Up @@ -706,7 +753,6 @@ def setup_equity_data(

self.set_tables(df=equity.equity_data_shp, name="equity_data")


def setup_aggregation_areas(
self,
aggregation_area_fn: Union[List[str], List[Path], str, Path],
Expand Down
Loading

0 comments on commit 213b59f

Please sign in to comment.