Answers:
您需要进行某种程度的迭代。(更新:我已编辑删除了所有“ for”循环,除了一个列表理解)
# imports used throughout this example
from shapely.geometry import Point
from shapely.ops import cascaded_union
from itertools import combinations
# Here are your input shapes (circles A, B, C)
A = Point(3, 6).buffer(4)
B = Point(6, 2).buffer(4)
C = Point(1, 2).buffer(4)
# list the shapes so they are iterable
shapes = [A, B, C]
首先,需要使用每个形状的组合对,所有交叉点的并集(使用级联并集)。然后,从所有形状的并集中删除(通过)相交。difference
# All intersections
inter = cascaded_union([pair[0].intersection(pair[1]) for pair in combinations(shapes, 2)])
# Remove from union of all shapes
nonoverlap = cascaded_union(shapes).difference(inter)
如下nonoverlap
所示(通过JTS Test Builder):
几年后,似乎可以通过以下方法找到更好的解决方案shapely
:
# imports used throughout this example
from shapely.geometry import Point
from shapely.ops import polygonize, unary_union
# Here are your input shapes (circles A, B, C)
A = Point(3, 6).buffer(4)
B = Point(6, 2).buffer(4)
C = Point(1, 2).buffer(4)
...
# list the shapes so they are iterable
shapes = [A, B, C, ...]
# generate the overlay
list(polygonize(unary_union(list(x.exterior for x in shapes))))
它支持任何长度的几何图形,唯一的问题是计算时间,不支持带孔的多边形。