Efficiently merge all the polygons in a list in python

Viewed 73

I have a list of shapely polygons present in a list (around 12k). Many of the polygons in this list overlap with each other. The task I need to perform is to create a dictionary with the indexes of polygon which satisfy a certain overlapping threshold. For e.g. if polygon1 overlaps with polygon5 and polygon6 and polygon2 overlaps with polygon10 and polygon3, polygon4 does not overlap with any other polygon then the dictionary should be like

{1: [5, 6],
 2: [3, 10],
 3: [],
}

The overlapping criteria used to determine if the polygons should be merged is intersection of the polygons divided by the area of the smaller polygon. Also, getting a list of all the merged polygons will also help.

2 Answers

I have got a similar problem. Find duplicate geometries and mark them out, solved by geopandas accessor duplicated_geometry_groups.

    >>> import dtoolkit.geoaccessor
    >>> import geopandas as gpd
    >>> from shapely.geometry import Polygon
    >>> df = gpd.GeoDataFrame(
    ...     geometry=[
    ...         Polygon([(0,0), (1,0), (1,1), (0,1)]),
    ...         Polygon([(1,1), (2,1), (2,2), (1,2)]),
    ...         Polygon([(2,2), (3,2), (3,3), (2,3)]),
    ...         Polygon([(2, 0), (3, 0), (3, 1)]),
    ...     ],
    ... )
    >>> df
                                                geometry
    0  POLYGON ((0.00000 0.00000, 1.00000 0.00000, 1....
    1  POLYGON ((1.00000 1.00000, 2.00000 1.00000, 2....
    2  POLYGON ((2.00000 2.00000, 3.00000 2.00000, 3....
    3  POLYGON ((2.00000 0.00000, 3.00000 0.00000, 3....

    - 0 and 1 are intersecting.
    - 1 and 2 are intersecting.
    - 3 is alone.

    So there are two groups: ``(0, 1, 2)`` and ``(3,)``.

    >>> df.duplicated_geometry_groups()
    0    0
    1    0
    2    0
    3    1
    dtype: int64

    ``index`` is the index of inputting, ``values`` is the labels of groups.
    And labels are natural numbers.
Related