Skip to content

Commit

Permalink
bugfixes aggregation composite areas
Browse files Browse the repository at this point in the history
  • Loading branch information
Santonia27 committed Mar 5, 2024
1 parent 0d077db commit 24c1db1
Show file tree
Hide file tree
Showing 7 changed files with 297 additions and 106 deletions.
Binary file not shown.

Large diffs are not rendered by default.

Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ file = "./exposure/exposure.csv"
crs = "EPSG:4326"
file1 = "./exposure/building_points.gpkg"
file2 = "./exposure/new_development_area.gpkg"
file3 = "./exposure/new_development_area_aggregated.gpkg"
24 changes: 17 additions & 7 deletions hydromt_fiat/workflows/aggregation_areas.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,8 +68,16 @@ def join_exposure_aggregation_multiple_areas(
)
exposure_gdf.drop_duplicates(subset="Object ID", keep="first", inplace=True)

# Check if new gdf was already created in previous loop
try:
new_exposure_aggregation
except NameError:
new_exposure_aggregation = None
else:
new_exposure_aggregation = new_exposure_aggregation

if new_composite_area[0] is True:
new_exposure_aggregation, exposure_gdf = split_composite_area(exposure_gdf, aggregated, aggregation_gdf, attribute_name)
new_exposure_aggregation, exposure_gdf = split_composite_area(exposure_gdf, aggregated, aggregation_gdf, attribute_name, new_exposure_aggregation)

else:
exposure_gdf.drop(columns=attribute_name, inplace=True)
Expand All @@ -90,7 +98,7 @@ def join_exposure_aggregation_multiple_areas(

return exposure_gdf

def split_composite_area(exposure_gdf, aggregated, aggregation_gdf, attribute_name):
def split_composite_area(exposure_gdf, aggregated, aggregation_gdf, attribute_name, new_exposure_aggregation):
if aggregated[attribute_name].apply(lambda x: len(x) >= 2).any():
# Split exposure_gdf by aggregation zone
new_exposure_gdf = exposure_gdf.rename(columns = {attribute_name: "pot"})
Expand All @@ -104,14 +112,13 @@ def split_composite_area(exposure_gdf, aggregated, aggregation_gdf, attribute_na

# Combine divided objects of new composite area with areas that fall in no zone
exposure_gdf = pd.concat([res_intersection, exposure_outside_aggregation], ignore_index=True)
idx_duplicates = exposure_gdf.index[exposure_gdf.duplicated("Object ID") == True]
idx_duplicates = exposure_gdf.index[exposure_gdf.duplicated(subset = "Object ID")]
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:

if new_exposure_aggregation is None:
data = pd.DataFrame(columns=['geometry'])
final_exposure = gpd.GeoDataFrame(data, geometry='geometry')
new_exposure_aggregation = pd.concat([final_exposure, exposure_gdf], ignore_index=True)
Expand Down Expand Up @@ -158,6 +165,9 @@ def join_exposure_aggregation_areas(

exposure_gdf = join_exposure_aggregation_multiple_areas(exposure_gdf, aggregation_area_fn, attribute_names, label_names, new_composite_area)


# Remove the geometry column from the exposure_gdf to return a dataframe
exposure_geoms = exposure_gdf[["Object ID", "geometry"]]

del exposure_gdf["geometry"]
return exposure_gdf
return exposure_gdf, exposure_geoms
5 changes: 4 additions & 1 deletion hydromt_fiat/workflows/exposure_vector.py
Original file line number Diff line number Diff line change
Expand Up @@ -1198,13 +1198,16 @@ def setup_new_composite_areas(
# If the user supplied aggregation area data, assign that to the
# new composite areas
if aggregation_area_fn is not None:
new_objects = join_exposure_aggregation_areas(
new_objects, aggregated_objects_geoms = join_exposure_aggregation_areas(
_new_exposure_geoms.merge(new_objects, on="Object ID"),
aggregation_area_fn=aggregation_area_fn,
attribute_names=attribute_names,
label_names=label_names,
new_composite_area = True
)
# Update the exposure_geoms incl aggregation
self.set_geom_names("new_development_area_aggregated")
self.set_exposure_geoms(aggregated_objects_geoms)

# Update the exposure_db
self.exposure_db = pd.concat([self.exposure_db, new_objects]).reset_index(
Expand Down

0 comments on commit 24c1db1

Please sign in to comment.