绘制圆形并测试与多边形的重叠

问题描述

我按照 Adrien's answer 创建了一个可以有多个多边形的图形。

我计划创建跨越图表中整个网格的填充圆圈。我想测试是否有圆和多边形之间的重叠,以确定每个圆的哪些多边形重叠。为此,我查看了 this,但这里的圆圈是级联的,而我想要像 this 这样的单独圆圈。

如何创建圆圈并测试重叠?

任何建议都会有所帮助。

解决方法

你可以这样做(即使总是有比嵌套循环更好的解决方案):

首先初始化一个 DataFrame,然后我们将用相交的矩形/圆形的组合填充它。

然后我们将遍历存储在 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 lignes 的片段:

 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,0 7))  POLYGON ((2 9,1.995184726672197 8.90198285967...
2     POLYGON ((0 7,0 7))  POLYGON ((4 7,3.995184726672197 6.90198285967...
3     POLYGON ((0 7,0 7))  POLYGON ((4 9,3.995184726672197 8.90198285967...
4     POLYGON ((0 7,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,7 1))  POLYGON ((8 3,7.995184726672197 2.90198285967...
7     POLYGON ((7 1,7 1))  POLYGON ((8 5,7.995184726672197 4.90198285967...
8     POLYGON ((7 1,7 1))  POLYGON ((8 7,7.995184726672197 6.90198285967...
9     POLYGON ((7 1,7 1))  POLYGON ((8 9,7.995184726672197 8.90198285967...
10    POLYGON ((7 1,7 1))  POLYGON ((10 1,9.995184726672196 0.9019828596...