Skip to content

Commit

Permalink
define function to split composite area based on aggregation
Browse files Browse the repository at this point in the history
  • Loading branch information
Santonia27 committed Mar 5, 2024
1 parent ddeb454 commit 68e9c85
Showing 1 changed file with 30 additions and 28 deletions.
58 changes: 30 additions & 28 deletions hydromt_fiat/workflows/aggregation_areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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,
Expand Down

0 comments on commit 68e9c85

Please sign in to comment.