4

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))
Prash
  • 43
  • 1
  • 6
  • 2
    this seems pretty broad, have you made any attempts to do this yourself? How -specifically- are your geometries defined? Can you easily detect where two shapes intersect? – Tadhg McDonald-Jensen Mar 14 '17 at 16:24
  • yes, I can do that easily. – Prash Mar 14 '17 at 16:27
  • You should probably ask this at the [gis.se] Stack Exchange. – PolyGeo Mar 14 '17 at 19:24
  • in case you are willing to use PostGIS, then `ST_ClusterDBSCAN` seems to do what you are after, i.e., it constructs clusters of geometries on top of which you can then call `ST_Union` to merge them... – ewcz Mar 15 '17 at 08:45

1 Answers1

0

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).

Logan Byers
  • 1,454
  • 12
  • 19
  • I think that OP is probably more interested in how to efficiently construct the individual groups of overlapping geometries (the union of which is then calculated) – ewcz Mar 15 '17 at 08:44
  • @ewcz I am confident that my answer serves the original question. `unary_union` performs the union of any number of geometries. It produces the minimal result and returns disjoint geometries when necessary. – Logan Byers Mar 15 '17 at 12:39
  • The link is now incorrect, can it be updated please to: https://shapely.readthedocs.io/en/stable/manual.html#shapely.ops.unary_union – Thirst for Knowledge Jul 23 '19 at 11:06