Merging a list of Polygons to Multipolygons

Stophface picture Stophface · Apr 21, 2016 · Viewed 7.1k times · Source

I have a list of shapely polygons

myList = [[<shapely.geometry.polygon.Polygon object at 0x110e09d90>], [<shapely.geometry.polygon.Polygon object at 0x110e09f90>], [<shapely.geometry.polygon.Polygon object at 0x110ec9150>]]

How would I create a MultiPolygon out of them? I cannot get my head around it

Answer

Mike T picture Mike T · Apr 27, 2016

`It looks like you have a list of lists (each with one item). Before you do anything, make a flat list of geometries:

myGeomList = [x[0] for x in myList]

There are actually a few options to combine them. The best is to do a unary union on a list of geometries, which may result in different geometry types, such as MultiPolygon, but it not always.

from shapely.ops import unary_union
cu = unary_union(myGeomList)

Or you could pass the list to MultiPolgyon() or GeometryCollection(), but these might present issues (invalid, inability to use overlay ops, etc.)