如何从多边形中提取多边形?

时间:2016-08-13 07:07:17

标签: python shapely

我试图从多边形中提取多边形。我可以使用MultiPolygon从形状上将多边形列表转换为多边形。

>>Multi = MultiPolygon([shape(pol['geometry']) for pol in fiona.open('data.shp')]) 

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

有人知道我该如何扭转这个过程,即如果给定多面,我怎样才能将它转换为单独的多边形?

2 个答案:

答案 0 :(得分:11)

简单地做

Polygons = list(Multi)

这会提取多边形并将它们放在一个列表中。

答案 1 :(得分:1)

根据集合中的documentation,其中包括MultiPointMultiLineStringMultiPolygon之类的类,可以通过{{1 }}属性或使用geomsin “的迭代器协议:

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

将它们切片会为您提供一个集合:

print(multipolygon[0])
POLYGON ((0 0, 1 1, 1 0, 0 0))