绘制圆圈并测试与多边形的重叠形状
Draw circles and test overlap with polygons in shapely
我按照 创建了一个可以有多个多边形的图形。
我计划创建横跨图中整个网格的实心圆。我想测试圆和多边形之间是否 重叠 以确定每个圆重叠的多边形。为此,我查看了 this, but the circles here are cascaded whereas I want separate circles like this.
如何创建圆圈并测试重叠?
任何建议都会有所帮助。
你可以做类似的事情(即使总是有比嵌套循环更好的解决方案):
首先初始化一个DataFrame,然后我们将用相交的rectangles/circles.
的组合填充它
然后我们将遍历存储在your_dict
中的矩形并根据positions
创建圆圈。
对于每个圆,检查它是否与给定的矩形相交。如果是这样,将圆几何保存在列表中。
检查完所有圆之后,创建一个包含两列的 DataFrame,其中矩形几何被复制的次数与相交圆的数量相同,而圆则存储与矩形相交的圆几何。
最后,将此 DataFrame 附加到 results
。
results = pd.DataFrame()
for key,your_polygon in your_dict.items():
intersected_circles = []
for x in positions:
for y in positions:
sampleCircle = Point(x,y).buffer(1)
intersect = sampleCircle.intersects(your_polygon)
if intersect:
intersected_circles.append(sampleCircle)
to_append = pd.DataFrame({'rectangle': np.repeat(your_polygon, len(intersected_circles)),
'circle': intersected_circles})
results = results.append(to_append, ignore_index = True)
results
前 10 行的片段:
rectangle circle
0 POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7)) POLYGON ((2 7, 1.995184726672197 6.90198285967...
1 POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7)) POLYGON ((2 9, 1.995184726672197 8.90198285967...
2 POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7)) POLYGON ((4 7, 3.995184726672197 6.90198285967...
3 POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7)) POLYGON ((4 9, 3.995184726672197 8.90198285967...
4 POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7)) POLYGON ((6 9, 5.995184726672197 8.90198285967...
5 POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1)) POLYGON ((8 1, 7.995184726672197 0.90198285967...
6 POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1)) POLYGON ((8 3, 7.995184726672197 2.90198285967...
7 POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1)) POLYGON ((8 5, 7.995184726672197 4.90198285967...
8 POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1)) POLYGON ((8 7, 7.995184726672197 6.90198285967...
9 POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1)) POLYGON ((8 9, 7.995184726672197 8.90198285967...
10 POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1)) POLYGON ((10 1, 9.995184726672196 0.9019828596...
我按照
我计划创建横跨图中整个网格的实心圆。我想测试圆和多边形之间是否 重叠 以确定每个圆重叠的多边形。为此,我查看了 this, but the circles here are cascaded whereas I want separate circles like this.
如何创建圆圈并测试重叠?
任何建议都会有所帮助。
你可以做类似的事情(即使总是有比嵌套循环更好的解决方案):
首先初始化一个DataFrame,然后我们将用相交的rectangles/circles.
的组合填充它然后我们将遍历存储在your_dict
中的矩形并根据positions
创建圆圈。
对于每个圆,检查它是否与给定的矩形相交。如果是这样,将圆几何保存在列表中。
检查完所有圆之后,创建一个包含两列的 DataFrame,其中矩形几何被复制的次数与相交圆的数量相同,而圆则存储与矩形相交的圆几何。
最后,将此 DataFrame 附加到 results
。
results = pd.DataFrame()
for key,your_polygon in your_dict.items():
intersected_circles = []
for x in positions:
for y in positions:
sampleCircle = Point(x,y).buffer(1)
intersect = sampleCircle.intersects(your_polygon)
if intersect:
intersected_circles.append(sampleCircle)
to_append = pd.DataFrame({'rectangle': np.repeat(your_polygon, len(intersected_circles)),
'circle': intersected_circles})
results = results.append(to_append, ignore_index = True)
results
前 10 行的片段:
rectangle circle
0 POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7)) POLYGON ((2 7, 1.995184726672197 6.90198285967...
1 POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7)) POLYGON ((2 9, 1.995184726672197 8.90198285967...
2 POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7)) POLYGON ((4 7, 3.995184726672197 6.90198285967...
3 POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7)) POLYGON ((4 9, 3.995184726672197 8.90198285967...
4 POLYGON ((0 7, 4 7, 4 12, 0 12, 0 7)) POLYGON ((6 9, 5.995184726672197 8.90198285967...
5 POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1)) POLYGON ((8 1, 7.995184726672197 0.90198285967...
6 POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1)) POLYGON ((8 3, 7.995184726672197 2.90198285967...
7 POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1)) POLYGON ((8 5, 7.995184726672197 4.90198285967...
8 POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1)) POLYGON ((8 7, 7.995184726672197 6.90198285967...
9 POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1)) POLYGON ((8 9, 7.995184726672197 8.90198285967...
10 POLYGON ((7 1, 10 1, 10 8, 7 8, 7 1)) POLYGON ((10 1, 9.995184726672196 0.9019828596...