NIVANorge / catchment_processing_workflows

Examples and documentation for common workflows used by Catchment Processes (Section 317)
https://nivanorge.github.io/catchment_processing_workflows/
0 stars 0 forks source link

Multipolygons? #2

Open LeahJB opened 9 months ago

LeahJB commented 9 months ago

Often multipolygons are returned rather than polygons. Is this a problem? I'm not sure, but it sometimes confuses people. I guess this happens at diagonal corners somehow. Could consider trimming these off and just picking the largest polygons based on area. E.g.:

from shapely.geometry import Polygon

Create a new GeoDataFrame to store the main polygons

delineated_scs_cleaned_gdf = gpd.GeoDataFrame(columns=delineated_scs_gdf.columns)

for idx, row in delineated_scs_gdf.iterrows(): if row.geometry.geom_type == 'MultiPolygon':

Find the largest polygon based on area

    main_polygon = max(row.geometry.geoms, key=lambda polygon: polygon.area)
    # Create a new row with the main polygon
    new_row = row.copy()
    new_row.geometry = Polygon(main_polygon)
    # Append the new row to the new GeoDataFrame
    delineated_scs_cleaned_gdf = delineated_scs_cleaned_gdf.append(new_row, ignore_index=True)
else:
    # If it's not a MultiPolygon, just append the row as is
    delineated_scs_cleaned_gdf = delineated_scs_cleaned_gdf.append(row, ignore_index=True)