I have a list of Shapely polygons which I need to find the intersection of each with a bounding Shapely polygon.
Not all the polygons in the list cross the boundary of the bounding polygon so I only need to calculate the intersection for those that do but my method is rather slow as I'm currently iterating over each polygon in the list.
My current code is below:
def intersect_polygons(poly, boundary): if poly.overlaps(boundary): poly = poly.intersection(boundary) return poly # polygons is a list of shapely Polygon objects # boundary is a shapely Polygon object new_polygons = [intersect_polygons(p, boundary) for p in polygons]
The limiting step is checking to see if the polygons overlap as the intersection only happens for a small percentage of the polygons. Is there anyway to do this step without checking every polygon to speed this up? I've seen an rtree suggested as a solution for similar problems but I'm not sure whether one could be used here.
And here is what I want the output to look like, once the overlapping polygons have been swapped for their intersections with the boundary.