Faster way of polygon intersection with shapely

前端 未结 2 1925
时光取名叫无心
时光取名叫无心 2020-11-28 05:20

I have a large number of polygons (~100000) and try to find a smart way of calculating their intersecting area with a regular grid cells.

Currently, I am creating th

2条回答
  •  日久生厌
    2020-11-28 05:37

    Consider using Rtree to help identify which grid cells that a polygon may intersect. This way, you can remove the for loop used with the array of lat/lons, which is probably the slow part.

    Structure your code something like this:

    from shapely.ops import cascaded_union
    from rtree import index
    idx = index.Index()
    
    # Populate R-tree index with bounds of grid cells
    for pos, cell in enumerate(grid_cells):
        # assuming cell is a shapely object
        idx.insert(pos, cell.bounds)
    
    # Loop through each Shapely polygon
    for 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)
    

提交回复
热议问题