使用Shapely有效地计算列表中每个多边形与另一个列表中多边形的交点

时间:2020-08-14 12:07:13

标签: python shapely

如何通过列表理解简化for循环并提高速度?

import numpy as np
import shapely

#buffer_original_points --> shapely polygons in a list
#raster --> shapely polygons in a list


raster_array = np.zeros((len(raster)))

for i in range(0, len(buffer_original_points)):
    for j in range(0, len(raster)):
        if buffer_original_points[i].intersects(raster[j]) or raster[j].contains(buffer_original_points[i]):
            raster_array[j] += 1

raster_array = raster_array.reshape(rows, columns)

我尝试了这个,但是raster_array结果是错误的,并且被一个填充了:

raster_array=[raster_array[j]+1 for i in range(0, len(buffer_original_points)) for j in range(0, len(raster)) if (buffer_original_points[i].intersects(raster[j]) or raster[j].contains(buffer_original_points[i]))]

1 个答案:

答案 0 :(得分:0)

您可能可以尝试以下操作:

raster_array = [sum([buffer_original_points[i].intersects(raster[j]) or raster[j].contains(buffer_original_points[i])) for i in range(0, len(buffer_original_points))] for j in range(0, len(raster))] 

但是由于不确定性降低,我不确定这不是一种好的编码方法。