I am trying to get a function to return True when a LineString intersects with any polygon in a list of polygons. Currently, I am using a for loop to iterate through all the polygons and check one by one:
line = [point1, point2]
shapely_line = shapely.geometry.LineString(line)
for polygon in self.polygon_list:
if(shapely_line.intersects(polygon)):
return True
This method works but it is not efficient. The problem is that the line is constantly changing in a simulation, so this method is called every time the line changes (once per step), which means that I have to iterate through the whole list of polygons a lot of times per second, which slows down the performance. There can also be multiple lines at a time, making it even slower.
I was wondering if there was any way of doing this in a more efficient way. I thought of turning the list of polygons into a MultiPolygon, but the intersects() function doesn't work for MultiPolygons. I also saw that I could maybe use STRtree, but I tried it and I couldn't manage to get any better performance.