考虑使用Rtree帮助确定多边形可以相交的网格单元。这样,您可以删除经纬度数组使用的for循环,这可能是最慢的部分。
构造代码如下:
from shapely.ops import cascaded_unionfrom rtree import indexidx = index.Index()# Populate R-tree index with bounds of grid cellsfor pos, cell in enumerate(grid_cells): # assuming cell is a shapely object idx.insert(pos, cell.bounds)# Loop through each Shapely polygonfor poly in polygons: # Merge cells that have overlapping bounding boxes merged_cells = cascaded_union([grid_cells[pos] for pos in idx.intersection(poly.bounds)]) # Now do actual intersection print(poly.intersection(merged_cells).area)



