From 68e9c85c8ee2c2f3c646c0aeb3ae6d4f69077245 Mon Sep 17 00:00:00 2001 From: "sarah.rautenbach" Date: Tue, 5 Mar 2024 08:30:38 +0100 Subject: [PATCH] define function to split composite area based on aggregation --- hydromt_fiat/workflows/aggregation_areas.py | 58 +++++++++++---------- 1 file changed, 30 insertions(+), 28 deletions(-) diff --git a/hydromt_fiat/workflows/aggregation_areas.py b/hydromt_fiat/workflows/aggregation_areas.py index b783a5a4..0fb2ed9e 100644 --- a/hydromt_fiat/workflows/aggregation_areas.py +++ b/hydromt_fiat/workflows/aggregation_areas.py @@ -69,34 +69,7 @@ def join_exposure_aggregation_multiple_areas( exposure_gdf.drop_duplicates(subset="Object ID", keep="first", inplace=True) if new_composite_area[0] is True: - if aggregated[attribute_name].apply(lambda x: len(x) >= 2).any(): - # If new composite area falls in multiple aggregation zones should do an overlay function and where they overlay, split the composite area into - # polygons and assign the aggregation area - new_exposure_gdf = exposure_gdf.rename(columns = {attribute_name: "pot"}) - res_intersection = new_exposure_gdf.overlay(aggregation_gdf[[attribute_name, 'geometry']], how='intersection') - res_intersection.drop(["index_right", "pot"], axis = 1, inplace = True) - - # exposure area - res_intersection = left over shape in no zone - exposure_outside_aggregation = new_exposure_gdf.overlay(res_intersection[["geometry"]], how = "symmetric_difference") - exposure_outside_aggregation.drop(["index_right", "pot"], axis = 1, inplace = True) - exposure_outside_aggregation = exposure_outside_aggregation.dropna() - - # Combine divided objects of new composite area within aggregation zone and outside #TODO create OBJECT IDs - exposure_gdf = pd.concat([res_intersection, exposure_outside_aggregation], ignore_index=True) - idx_duplicates = exposure_gdf.index[exposure_gdf.duplicated("Object ID") == True] - exposure_gdf["Object ID"] = exposure_gdf["Object ID"].astype(int) - exposure_gdf.loc[idx_duplicates, "Object ID"] = np.random.choice(range(exposure_gdf["Object ID"].values.max() + 1 ,exposure_gdf["Object ID"].values.max() +1 + len(idx_duplicates)), size=len(idx_duplicates), replace=False) - - # Create an empty GeoDataFrame and append the exposure data - try: - new_exposure_aggregation - except NameError: - data = pd.DataFrame(columns=['geometry']) - final_exposure = gpd.GeoDataFrame(data, geometry='geometry') - new_exposure_aggregation = pd.concat([final_exposure, exposure_gdf], ignore_index=True) - exposure_gdf = new_exposure_aggregation - else: - exposure_gdf = pd.concat([new_exposure_aggregation, exposure_gdf], ignore_index=True) + split_composite_area(exposure_gdf, aggregated, aggregation_gdf, attribute_name) else: exposure_gdf.drop(columns=attribute_name, inplace=True) exposure_gdf = exposure_gdf.merge(aggregated, on="Object ID") @@ -116,6 +89,35 @@ def join_exposure_aggregation_multiple_areas( return exposure_gdf +def split_composite_area(exposure_gdf, aggregated, aggregation_gdf, attribute_name): + if aggregated[attribute_name].apply(lambda x: len(x) >= 2).any(): + # If new composite area falls in multiple aggregation zones should do an overlay function and where they overlay, split the composite area into + # polygons and assign the aggregation area + new_exposure_gdf = exposure_gdf.rename(columns = {attribute_name: "pot"}) + res_intersection = new_exposure_gdf.overlay(aggregation_gdf[[attribute_name, 'geometry']], how='intersection') + res_intersection.drop(["index_right", "pot"], axis = 1, inplace = True) + + # exposure area - res_intersection = left over shape in no zone + exposure_outside_aggregation = new_exposure_gdf.overlay(res_intersection[["geometry"]], how = "symmetric_difference") + exposure_outside_aggregation.drop(["index_right", "pot"], axis = 1, inplace = True) + exposure_outside_aggregation = exposure_outside_aggregation.dropna() + + # Combine divided objects of new composite area within aggregation zone and outside #TODO create OBJECT IDs + exposure_gdf = pd.concat([res_intersection, exposure_outside_aggregation], ignore_index=True) + idx_duplicates = exposure_gdf.index[exposure_gdf.duplicated("Object ID") == True] + exposure_gdf["Object ID"] = exposure_gdf["Object ID"].astype(int) + exposure_gdf.loc[idx_duplicates, "Object ID"] = np.random.choice(range(exposure_gdf["Object ID"].values.max() + 1 ,exposure_gdf["Object ID"].values.max() +1 + len(idx_duplicates)), size=len(idx_duplicates), replace=False) + + # Create an empty GeoDataFrame and append the exposure data + try: + new_exposure_aggregation + except NameError: + data = pd.DataFrame(columns=['geometry']) + final_exposure = gpd.GeoDataFrame(data, geometry='geometry') + new_exposure_aggregation = pd.concat([final_exposure, exposure_gdf], ignore_index=True) + exposure_gdf = new_exposure_aggregation + else: + exposure_gdf = pd.concat([new_exposure_aggregation, exposure_gdf], ignore_index=True) def join_exposure_aggregation_areas( exposure_gdf: gpd.GeoDataFrame,