| name | geopandas-point-in-polygon |
| description | How to determine which points fall inside a specific polygon using GeoPandas. |
Point in Polygon with GeoPandas
This skill demonstrates how to filter a set of points (like earthquakes or cities) to find only those that fall within a specific polygon (like a country or tectonic plate) using GeoPandas.
Prerequisites
pip install geopandas shapely
Basic Usage
When you have a set of points and a polygon, you can use the .within() spatial predicate to filter the points.
import geopandas as gpd
from shapely.geometry import Point
polygons_gdf = gpd.read_file('polygons.geojson')
target_polygon = polygons_gdf[polygons_gdf['name'] == 'Target Region'].geometry.unary_union
points_data = [
{'id': 1, 'lat': 34.0, 'lon': -118.2},
{'id': 2, 'lat': 40.7, 'lon': -74.0}
]
geometry = [Point(xy['lon'], xy['lat']) for xy in points_data]
points_gdf = gpd.GeoDataFrame(points_data, geometry=geometry, crs="EPSG:4326")
points_inside = points_gdf[points_gdf.geometry.within(target_polygon)]
print(f"Found {len(points_inside)} points inside the polygon.")
Tips
- Always ensure both the points and the polygon share the same Coordinate Reference System (CRS) before doing spatial operations.
- Using
.unary_union on the filtered polygon GeoDataFrame is useful if the target region consists of multiple polygon geometries (like a multipolygon).