3

I have two GeoDataFrames. One contains Points, another contains Polygons.

I need to get all Points that are inside any Polygon from GeoDataFrame.

I tried iterate through all Polygons and check if Point inside this polygon or not. This solution works but it is very slow.

I wonder if there is another way to solve this task.

edit: My solution looks like this:

for i in range(len(Poly_gdf.index)):
    inter = Points_gdf[Points_gdf.intersects(Poly_gdf.loc[i,'geometry'])]
    if not inter.empty:
        for i in inter['geometry'].values:
            points.append(i)
Liquid Cat
  • 35
  • 1
  • 5
  • Have you checked this: https://gis.stackexchange.com/questions/230494/intersecting-two-shape-problem-using-geopandas – Anwarvic May 27 '20 at 09:33
  • 1
    Also see: [GeoPandas: check if point is in polygon](https://stackoverflow.com/q/58513123/7851470), [return list of points within polygon geopandas](https://stackoverflow.com/q/60136349/7851470), and [Check for points within polygon from geodataframe](https://stackoverflow.com/q/61055792/7851470). – Georgy May 27 '20 at 09:42

1 Answers1

10

Use spatial join, which is optimised and should be fast.

See documentation for details.

points_within = gpd.sjoin(Points_gdf, Poly_gdf, how='inner', predicate='within')

how='inner' might be "optional" (depending on the case).

Manuel F
  • 145
  • 2
  • 7
martinfleis
  • 7,124
  • 2
  • 22
  • 30
  • I had error "AttributeError: 'NoneType' object has no attribute 'intersection'". I solved it by installing Rtree. Thanks. This is exectly what I needed. – Liquid Cat May 27 '20 at 09:57