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

Addition of an additional crown cleaning function to remove very small "false" fragmented tree crowns #132

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
20 changes: 20 additions & 0 deletions detectree2/models/outputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,6 +458,26 @@ def post_clean(unclean_df: gpd.GeoDataFrame,

return reclean_df.reset_index(drop=True)

def remove_very_small_polygons(crowns:gpd.GeoDataFrame, size_threshold=1.0) -> gpd.GeoDataFrame:
"""Removes small fragmentary crowns below or equal to the threshold.

Args:
crowns (gpd.GeoDataFrame): A GeoDataFrame with small fragmentary crowns.
size_threshold (float, optional): given in m² (square meter). Defaults to 1.0.

Returns:
gpd.GeoDataFrame: A GeoDataFrame with no small fragmentary crowns.
"""
areas = [(row.geometry.area, index) for index, row in crowns.iterrows()]
areas.sort()
indexes_to_remove = [area[1] for area in areas if area[0] <= size_threshold] # threshold may differ across inference images
indexes_to_remove = list(set(indexes_to_remove))
indexes_to_keep = set(range(crowns.shape[0])) - set(indexes_to_remove)
print("Removed",len(indexes_to_remove),"very small crowns!")
output_gdf = crowns.take(list(indexes_to_keep))
output_gdf = output_gdf.reset_index(drop=True)
return output_gdf


def load_geopandas_dataframes(folder):
"""Load all GeoPackage files in a folder into a list of GeoDataFrames."""
Expand Down