Reputation: 3386
I'm trying to extract the polygons from multipolygons in Shapely. I can transform a list of polygons into multipolygons using MultiPolygon
from Shapely.
>>> Multi = MultiPolygon([shape(pol['geometry']) for pol in fiona.open('data.shp')])
And,
>>> Multi.wkt
'MULTIPOLYGON (((249744.2315302934148349 142798.1643468967231456, 250113.7910872535139788 142132.9571443685272243, 250062.6213024436729029 141973.7622582934272941, 249607.7787708004761953 141757.7120557629095856, 249367.7742475979903247 142304.6840291862317827, 249367.7742475979903247 142304.6840291862317827, 249744.2315302934148349 142798.1643468967231456)),
((249175.7899173096520826 142292.5352640640921891, 249367.7742475979903247 142304.6840291862317827, 249607.7787708004761953 141757.7120557629095856, 249014.4539607730694115 141876.1348429077770561, 249175.7899173096520826 142292.5352640640921891)))'
Does anybody know how can I reverse the process i.e. given a multipolygon how can I convert it to separate polygons?
Upvotes: 29
Views: 68556
Reputation: 2057
shapely.get_parts()
docs is the fastest way:
>>> print(multipolygon)
<MULTIPOLYGON (((19.652 39.714, 19.652 39.714, 19.652 39.714, 19.652 39.714,...>
>>> shapely.get_num_geometries(mmm)
Out[186]: np.int32(5457)
>>> %timeit list(multipolygon.geoms)
32.5 ms ± 388 μs per loop (mean ± std. dev. of 7 runs, 10 loops each)
runtime: 2.64 s
>>> %timeit shapely.get_parts(multipolygon)
1.9 ms ± 12.8 μs per loop (mean ± std. dev. of 7 runs, 1,000 loops each)
Upvotes: 0
Reputation: 7852
To avoid Deprecation Warning when using this othwerwise ok answer, do:
Polygons = list(Multipolygon.geoms)
Upvotes: 33
Reputation: 71
As other have already noted from shapely 1.8 onwards iterating upon the Multipolygon class is deprecated. The following code will work in 1.8 but won't in 2.0.
for poly in multipolygon:
print(poly)
The correct way of doing it since 2.0 as follows:
for poly in multipolygon.geoms:
print(poly)
Also if you are not specifically iterating through the polygons it might have to do with other libraries being outdated and calling a shapely 2.0 version.
Upvotes: 1
Reputation: 13697
According to documentation on collections, which include such classes as MultiPoint
, MultiLineString
and MultiPolygon
, their members can be "accessed via the geoms
property or via the iterator protocol using in
or list()
":
from shapely.geometry import MultiPolygon, Polygon
multipolygon = MultiPolygon([Polygon([(0, 0), (1, 1), (1, 0)]),
Polygon([(0, 0), (1, 1), (0, 1)])])
polygons = list(multipolygon)
print(*polygons)
# POLYGON ((0 0, 1 1, 1 0, 0 0)) POLYGON ((0 0, 1 1, 0 1, 0 0))
polygons = list(multipolygon.geoms)
print(*polygons)
# POLYGON ((0 0, 1 1, 1 0, 0 0)) POLYGON ((0 0, 1 1, 0 1, 0 0))
for polygon in multipolygon: # same for multipolygon.geoms
print(polygon)
# POLYGON ((0 0, 1 1, 1 0, 0 0))
# POLYGON ((0 0, 1 1, 0 1, 0 0))
You can also extract individual geometries by their index:
print(multipolygon[0])
POLYGON ((0 0, 1 1, 1 0, 0 0))
Slicing them will give you a collection though:
print(multipolygon[:1])
MULTIPOLYGON (((0 0, 1 1, 1 0, 0 0)))
Upvotes: 22
Reputation: 761
Simply do
Polygons = list(Multi)
This extracts the polygons and puts them in a list.
Upvotes: 38