Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Merging a list of Polygons to Multipolygons

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

like image 334
Stophface Avatar asked Apr 21 '16 15:04

Stophface


1 Answers

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

like image 63
Mike T Avatar answered Oct 06 '22 15:10

Mike T