Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Spatial Joins with Geopandas | Spatial Analysis Techniques
Geospatial Analysis with Python

Spatial Joins with Geopandas

Swipe to show menu

Spatial joins are a powerful way to combine geospatial datasets based on their spatial relationships rather than just matching attribute values. With geopandas, you can easily perform spatial joins to answer questions like "Which points fall within which polygons?" or "Which features are nearest to each other?" The core tool for this in geopandas is the sjoin() method, which merges two GeoDataFrame objects by evaluating their spatial relationships.

Suppose you have two spatial datasets: one containing locations of schools (as points) and another containing neighborhood boundaries (as polygons). By performing a spatial join, you can determine which school is located in which neighborhood, or count the number of schools per neighborhood.

The sjoin() method in geopandas allows you to specify the type of spatial relationship to use for the join, such as within, contains, or intersects. Additionally, you can choose the type of join — left, right, or inner — which determines how records from each dataset are matched and retained in the result.

123456789101112131415161718192021222324252627282930313233343536
import geopandas as gpd from shapely.geometry import Point, Polygon # Create GeoDataFrame of points (e.g., schools) points = gpd.GeoDataFrame({ "school": ["A", "B", "C"], "geometry": [ Point(1, 1), Point(2, 2), Point(3, 3) ] }) # Create GeoDataFrame of polygons (e.g., neighborhoods) polygons = gpd.GeoDataFrame({ "neighborhood": ["Red", "Blue"], "geometry": [ Polygon([(0,0), (0,2), (2,2), (2,0)]), # Red neighborhood Polygon([(2,2), (2,4), (4,4), (4,2)]) # Blue neighborhood ] }) # Spatial join: which school is in which neighborhood? left_join = gpd.sjoin(points, polygons, how="left", predicate="within") right_join = gpd.sjoin(points, polygons, how="right", predicate="within") inner_join = gpd.sjoin(points, polygons, how="inner", predicate="within") print("Left Join Result:") print(left_join[["school", "neighborhood"]]) print("\nRight Join Result:") print(right_join[["school", "neighborhood"]]) print("\nInner Join Result:") print(inner_join[["school", "neighborhood"]])

In the example above, you see three types of joins:

  • Left join: keeps all points (schools), adding neighborhood information where available;
  • Right join: keeps all polygons (neighborhoods), adding schools contained within them;
  • Inner join: keeps only points that are within a neighborhood.

Choosing the correct join type depends on your analysis goals. For instance, if you want all schools regardless of whether they fall inside a neighborhood, use a left join. If you want to see all neighborhoods and the schools they contain, use a right join. If you only care about schools that are inside neighborhoods, use an inner join.

question mark

Which statement about spatial joins in geopandas is correct?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 2. Chapter 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 2. Chapter 1
some-alt