This question already has an answer here:
I'm trying to determine which of about 300,000 points fall within any of 215 closed polygons. My code right now loops through each point and then through each shape to see if it the point is contained within a polygon.
def in_shapes(proj): for i in range(len(shapes)): if Point(proj).within(shape(shapes[i])): return records[i] return 'None' df['ContainedIn'] = df.apply(lambda b: in_shapes(b.Projection), axis=1)
This works, but it is extremely time-consuming. Is there a more efficient way of determining:
- Which of a set of polygons a point falls within?
- Which of a list of points falls within a polygon?
- Or anything better?