Spatial joins

Visualizing Geospatial Data in Python

Mary van Valkenburg

Data Science Program Manager, Nashville Software School

Council districts and school districts

plot of 35 council districts

plot of 9 school districts

Visualizing Geospatial Data in Python

The .sjoin() predicate argument

import geopandas as gpd

gpd.sjoin(blue_region_gdf, black_point_gdf, predicate = <how to build>)

predicate can be intersects, contains, and within

Visualizing Geospatial Data in Python

Using .sjoin()

plot of blue region with black points

Visualizing Geospatial Data in Python

predicate = 'intersects'

gpd.sjoin(blue_region_gdf, black_point_gdf, predicate = 'intersects')

light blue polygon with black points

Visualizing Geospatial Data in Python

predicate = 'contains'

gpd.sjoin(blue_region_gdf, black_point_gdf, predicate = 'contains')

light blue polygon with black points

Visualizing Geospatial Data in Python

predicate = 'within'

gpd.sjoin(black_point_gdf, blue_region_gdf, predicate = 'within')

light blue polygon with black points

Visualizing Geospatial Data in Python

The .sjoin() predicate argument - within

# find council districts within school districts

within_gdf = gpd.sjoin(council_districts, school_districts, predicate = 'within')

print('council districts within school districts: ', within_gdf.shape[0])
council districts within school districts:  11
Visualizing Geospatial Data in Python

The .sjoin() predicate argument - contains

# find school districts that contain council districts

contains_gdf = pd.sjoin(school_districts, council_districts, predicate = 'contains')

print('school districts contain council districts: ', contains_gdf.shape[0])
school districts contain council districts:  11
Visualizing Geospatial Data in Python

The .sjoin() predicate argument - intersects

# find council districts that intersect with school districts

intersect_gdf = gpd.sjoin(council_districts, school_districts, predicate = 'intersects')

print('council districts intersect school districts: ', intersect.shape[0])
council districts intersect school districts:  100
Visualizing Geospatial Data in Python

Columns in a spatially joined GeoDataFrame

within_gdf = gpd.sjoin(council_districts, school_districts, predicate = 'within')
within_gdf.head()
  first_name_left  last_name_left district_left  index_right    
0 Nick             Leonardo            1              0              
1 DeCosta          Hastings            2              0 
2 Nancy            VanReece            8              1    
3 Bill             Pridemore           9              1    
9 Doug             Pardue             10              1    
Visualizing Geospatial Data in Python
# Aggregate council districts by school district - rename district_left and district_right
within_gdf.district_left = council_district
within_gdf.district_right = school_district
within_gdf[['council_district', 'school_district']
          ].groupby('school_district'
                   ).agg('count'
                        ).sort_values('council_district', ascending = False)
                   council_district 
school_district    
       3                   3
       1                   2
       9                   2
       2                   1
       5                   1
       6                   1
       8                   1
Visualizing Geospatial Data in Python

Let's Practice!

Visualizing Geospatial Data in Python

Preparing Video For Download...