2

Problem

I have a polygon dataset containing many features with holes of various sizes. For my downstream modeling workflow, I need polygons without any holes. The challenge is to remove these holes efficiently while maintaining reasonable geometric properties and computational performance.

Details

My dataset contains polygons with:

  • Small holes that could be filled without significant impact
  • Large holes that shouldn't simply be filled as they represent meaningful gaps
  • Complex polygons with multiple holes of mixed sizes
  • Varying exterior boundary complexity

Requirements Output polygons must have no holes; Preserve reasonable geometric integrity; Handle large datasets efficiently; Maintain topological consistency

What I Have Tried

Approach 1: Hybrid Fill/Split Strategy Fills small holes (≤500m²) directly Splits polygons along large holes by creating cutting lines through hole centroids Uses cuts extending from exterior to exterior example for approach 1

# Simplified version of the splitting logic

def process_holes_hybrid(polygon, area_threshold=500):
    if not polygon.interiors:
        return [polygon]
    
    small_holes = []
    large_holes = []
    
    for interior in polygon.interiors:
        hole_area = Polygon(interior).area
        if hole_area <= area_threshold:
            small_holes.append(interior)
        else:
            large_holes.append(interior)
    
    # Fill small holes by excluding them from new exterior
    # Split along large holes using cutting lines

Problems with Approach 1: Works well for simple cases but fails with complex polygons; Multiple large holes create overlapping cutting lines; Complex exterior boundaries lead to nonsensical results;
Difficult to ensure proper topology

Approach 2: Triangulation and Reaggregation Triangulate the polygon using constrained Delaunay triangulation. Reaggregate triangles iteratively based on compactness metrics

from shapely import constrained_delaunay_triangles
import math

def calculate_polsby_popper(polygon):
    area = polygon.area
    perimeter = polygon.length
    return (4 * math.pi * area) / (perimeter**2)

def explode_polygon_to_triangles(polygon):
    triangles = constrained_delaunay_triangles(polygon)
    return [geom for geom in triangles.geoms if isinstance(geom, Polygon)]

def optimize_polygon_compactness(polygons, max_iterations=3):
    # Iteratively merge neighboring triangles if compactness improves
    for iteration in range(max_iterations):
        merged_any = False
        # Logic to merge adjacent polygons based on:
        # - Neighborhood check: poly1.distance(poly2) < threshold
        # - Compactness improvement
        # - Area preservation

Problems with Approach 2: Computationally expensive even for small polygons, Results not geometrically meaningful, Difficult to control the final polygon characteristics

Question

What's the best way to remove holes from polygons without breaking the geometry? Are there existing tools or libraries that handle this better than writing custom code? I need a solution that:

  • Works with complex polygons having multiple holes
  • Doesn't drastically change the polygon shape or area
  • Performs reasonably well on large datasets

Environment: Python with GeoPandas/Shapely (but open to other tools and have access to ArcGIS Pro etc.)

10
  • Would it be possible if you share a sample of your data for testing purposes ? Commented Jul 15 at 7:38
  • Hey, thank you! The thread you sent me unfortunately does not address my problem. I do not want to fill polygons above a certain threshold but want to preserve the original geometry as much as possible. That is why I thought, splitting the polygons along the holes would be a good idea. Something like this: community.safe.com/transformers-9/… How could I provide some test data? I am pretty new to stack exchange and haven`t found an upload option yet Commented Jul 15 at 8:46
  • 2
    Why not simply Polygon(polygon.exterior.coords) ? Commented Jul 15 at 15:05
  • 1
    gis.stackexchange.com/questions/490838/… Commented Jul 15 at 19:37
  • 1
    I think I'm misunderstanding the question a bit so can you please clarify... You say that you "need polygons without any holes" but you also say that "Large holes that shouldn't simply be filled". To me this seems contradictory, but I assume I'm misunderstanding something. Commented Jul 21 at 4:39

1 Answer 1

0

The solution I found is removing holes in polygons while preserving both topology and area. The approach uses a minimum spanning tree strategy to create optimal bridge connections between holes, effectively converting them into "peninsulas" within the polygon rather than completely separate holes.

Implementation Features:

  1. Minimum Spanning Tree Approach:
  • Creates a graph where nodes are holes and the exterior boundary
  • Finds the minimum set of connections that link all holes
  • Minimizes the number of bridge connections needed
  1. Adaptive Buffer Sizing:
  • Calculates buffer sizes based on local geometry
  • Prevents unwanted intersections with other features
  • Creates clean connections that maintain polygon integrity
  1. Topology Preservation:
  • Validates geometries at intermediate processing steps
  • Ensures no new holes are created during processing
  • Maintains the original shape characteristics
  1. Connection Optimization:
  • Reuses shared paths between connections
  • Eliminates redundant connection lines
  • Creates the minimum number of cuts necessary
  1. Area Preservation:
  • Maintains the overall area with minimal loss (only ~0.006%)
  • Significantly better than naive approaches that can lose hole areas entirely

Technical Implementation: The algorithm is implemented in Python using Shapely and GeoPandas. It processes each polygon by:

  • Identifying all holes in the polygon
  • Building a graph representation of holes and exterior
  • Computing a minimum spanning tree to find optimal connections
  • Creating buffered lines along these connections
  • Cutting the polygon along these lines
  • Validating and cleaning the resulting geometries

Before and after & Bridge in zoom

enter image description here

enter image description here

4
  • Although this approach meets the letter of your requirements, it is creating contrived polygons that I guess will cause issues at some point down the road if/when additional geoprocessing is done on them. Why don't you leave the big holes as-is and remove the small ones instead? Commented Jul 29 at 13:50
  • What do you mean by contrived polygons? The polygons are to be used next for avalanche modelling. There must be no holes, as the programme cannot handle them. However, large holes must not be filled either, as this would significantly distort the modelling results. This approach is therefore the best approach for my use so far. Commented Jul 29 at 14:11
  • Accept your answer Commented Aug 30 at 7:57
  • Could you share the solution in code as well? Commented Nov 3 at 8:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.