geopandas-point-in-polygon
How to determine which points fall inside a specific polygon using GeoPandas.
- License 许可
- MIT
- 最近更新
- 2026-10-02
- Stars
- ★ 92
- Forks
- 7
01 前言 Front Matter
- name
- geopandas-point-in-polygon
- description
- How to determine which points fall inside a specific polygon using GeoPandas.
03 SKILL.md 正文
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
# 1. Load the polygon data
polygons_gdf = gpd.read_file('polygons.geojson')
# 2. Extract the specific polygon you want to test against
# Example: Get the polygon for a specific region
target_polygon = polygons_gdf[polygons_gdf['name'] == 'Target Region'].geometry.unary_union
# 3. Create or load your points data
points_data = [
{'id': 1, 'lat': 34.0, 'lon': -118.2},
{'id': 2, 'lat': 40.7, 'lon': -74.0}
]
# Convert to GeoDataFrame
geometry = [Point(xy['lon'], xy['lat']) for xy in points_data]
points_gdf = gpd.GeoDataFrame(points_data, geometry=geometry, crs="EPSG:4326")
# 4. Perform the spatial filter
# This creates a boolean mask of points that are within the target polygon
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_unionon the filtered polygon GeoDataFrame is useful if the target region consists of multiple polygon geometries (like a multipolygon).
04 同仓库更多 Skills
object_counter
Count occurrences of an object in the image using computer vision algorithm.
travel-planning-workflow
Invoke this skill when performing travel planning. It defines a structured workflow for generating itineraries. Following this workflow is essential to ensure the final plan satisfies user requirements.
bundled-dataset-search-tool
invoke this skill when you need to perform database search for travel planning. This skill provides some useful pre-packaged tools to look up accommodations, attractions, cities, driving distance, flights, and restaurants from the bundled dataset.
glm-output
Output-processing guidance for GLM tasks. Especially useful after glm-basics and glm-calibration when you need verifier-matching metrics from output.nc and a final /root/metrics.json.
glm-calibration
Calibration guidance for GLM tasks. Often most effective after glm-basics has clarified the setup; glm-output is the companion skill for exact final metric computation.
glm-basics
A good starting point for GLM calibration tasks. Use to inspect glm3.nml, confirm how GLM runs, and identify the relevant files before moving on to calibration and output evaluation.