Performing Spatial Joins¶
Warning
This tutorial is now obsolete. A new and updated version is available at Performing Spatial Joins (QGIS3)
Spatial Join is a classic GIS problem - transferring attributes from one layer to another based on their spatial relationship. In QGIS, this functionality is available through the Join Attributes by Location tool.
Overview of the task¶
We will use 2 layers - A shapefile of burough boundaries of New York city and another shapefile of nursing home locations in New York city. We will use spatial join technique to find the total nursing home capacity for each of the buroughs.
Other skills you will learn¶
Deleting columns from the attribute table of a layer.
Get the data¶
NYC Open Data Portal is an excellent source of free data for New York city.
Download the Borough Boundaries zip file using the Export option on the portal.
Download the Nursing Homes zip file using the Export option on the portal.
For convenience, you may directly download a copy of the datasets from the links below:
Procedure¶
Go to
. Browse to the downloaded zip filenybb_12c.zip
and select Open.
Select the layer
nybb.shp
and click OK.
Repeat the steps 3 and 4 for the another file
OEM_NursingHomes_001.zip
and load theOEM_NursingHomes_001.shp
layer. Once both the layers are loaded, right-click on theOEM_NursingHomes_001
layer and select Open Attribute Table.
Examine the attributes available for each feature. Since our task is to calculate the total nursing home capacity for each borough, we can use the attribute Capacity which can join to the boundaries layer.
Go to
.
The Target vector layer is the one we want to add the attributes to. In our case, this will be the boroughs boundary
nybb
layer. The Join vector layer will be the nursing homesOEM_NursingHomes_001
layer. As we want to sum the capacity of nursing homes, select Take summary of intersecting features and choose Sum. Name the output file asnyc_borough_join.shp
. In the Output table select Keep all records`.
Once the process finishes, select Yes when asked if you want to add the layer to TOC. The new layer
nyc_borough_join
would have the features fromnybb
layer along with spatially joined attributes fromOEM_NursingHomes_001
layer. Right-click on the layer and select Open Attribute Table.
You will see a column SUMCapacit in the attribute table. This is the sum of the Capacity attribute for the nursing homes that fall within each borough feature.
This is the answer we are looking for. But there are extra columns that we do not need in our output. Let’s clean up our output. Click on the Toggle editing button and then the Delete column button.
Press Control-A to select all columns in the Delete Attributes dialog. Next hold the Control key and de-select the columns you want to keep. Click OK.
In the attribute table, click Toggle editing button again to save the changes.
Back in the QGIS Canvas, use the Identify tool to verify that the output file has the desired attributes for each burough feature.
If you want to give feedback or share your experience with this tutorial, please comment below. (requires GitHub account)