← retour aux snippets

GeoPandas: jointure spatiale

faire une jointure spatiale (points dans polygones)

GeoPandas: jointure spatiale

objectif

Expliquer et montrer comment faire une jointure spatiale (points dans polygones).

code minimal

import geopandas as gpd
from shapely.geometry import Point

polys = gpd.GeoDataFrame(geometry=gpd.GeoSeries.from_wkt([
    "POLYGON((0 0, 0 2, 2 2, 2 0, 0 0))",
    "POLYGON((2 0, 2 2, 4 2, 4 0, 2 0))"
]), crs="EPSG:4326")
pts = gpd.GeoDataFrame(geometry=[Point(0.5,0.5), Point(3.0,1.0), Point(5.0,1.0)], crs="EPSG:4326")

joined = gpd.sjoin(pts, polys, how="left", predicate="within")
joined

utilisation

# compter les points par polygone
counts = gpd.sjoin(pts, polys, predicate="within").groupby("index_right").size()
counts

variante(s) utile(s)

# jointure 'intersects' pour segments/zones d'influence
# gpd.sjoin(pts, polys, predicate="intersects")

notes

  • Vérifiez les CRS identiques avant la jointure.
  • predicate peut être ‘within’, ‘contains’, ‘intersects’…