Search code examples
pythonalgorithmgisarcgisshapely

union overlapping polygons until no overlap


I have a list of geometries(could be a point or polygon) and I need to union the geometries that overlap(or contained within another geometry) in to one geometry. Any ideas on how to do this efficiently using shapely? I believe I should be using rtree but not sure how and where exactly I should use it.

Ex:

from shapely import geometry as gs
geometries = [gs.Polygon(((0,0), (0,10), (10, 10), (10, 0))),
              gs.Point((5,5)), gs.Point((30, 30))]

#output should be :
Polygon((0,0), (0,10), (10, 10), (10, 0)), Point((30, 30))

Solution

  • shapely.ops.unary_union will perform the union of any geometry types and produce a shapely.geometry.GeometryCollection (set of geometries). This is the function you should be using (docs here).