GeoPandas QGIS – Looking for Equivalent of ArcGIS ‘Select by Location’ Tool

geopandaspoint-in-polygonpythonselect-by-locationshapely

I have two GeoDataFrames:

  1. A single multi-part polygon
  2. Many points

I have read them in with the following:

import geopandas as gpd

poly = gpd.read_file('C:\\Users\\srcha\\Desktop\\folder\\poly.shp')
points = gpd.read_file('c:\\Users\\srcha\\Desktop\\folder\\points.shp')

I want to run the equivalent of "Select by location" in ArcGIS with GeoPandas and shapely tools to select points that are within the polygon.

How should I go about this?

Best Answer

If poly is a GeoDataFrame with a single geometry, extract this:

polygon = poly.geometry[0]

Then, you can use the within method to check which points are within the polygon:

points.within(polygon)

returning a boolean True/False values which can be used to filter the original dataframe:

subset = points[points.within(polygon)]
Related Question