空間連接 (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 Processing algorithm.

內容說明

We will use 2 layers - A shapefile of Borough boundaries of New York city and another shapefile of Street Pavement Rating for all streets in New York city. The first task will be to find the average rating of streets in each of the borough using a spatial join with summary algorithm. The second task will be to add the name of the borough to the street features through a one-to-many spatial join.

你還會學到這些

  • Creating filters to temporarily exclude certain features from computation.

取得資料

NYC Open Data Portal 是紐約市不錯的免費資料來源。

你可以使用介面上的 Export 鈕下載 Borough Boundaries 的 zip 檔。

../../_images/data116.png

Download the Street Pavement Rating zip file using the Export option on the portal.

../../_images/data26.png

為了方便起見,你也可以直接用下面的連結下載:

nybb_19a.zip

V_SSS_SEGMENTRATING_1.zip

資料來源 [CITYOFNY]

操作流程

  1. Locate the nybb_19a.zip file in the QGIS Browser and expand it. Select the nybb_19a/nybb.shp layer and drag it to the canvas. This is a polygon layer representing the borough boundaries in New York city.

../../_images/1135.png
  1. Next, locate the V_SSS_SEGMENTRATING_1.zip file and expand it. Select the dot_V_SSS_SEGMENTRATING_1_20190129.shp layer and add it to the canvas. This is a line layer of all streets in the city.

../../_images/2107.png
  1. Let’s examine the attributes available for each feature of the dot_V_SSS_SEGMENTRATING_1_20190129 layer. Right-click and select Open Attribute Table.

../../_images/350.png
  1. You will notice the attribute called Rating_B which has values in the range 0-10 representing the street segment’s rating. The attribute RatingWord has descriptive rating. We can use the Rating_B field to calculate the average rating.

../../_images/427.png
  1. You may have notice some features have a rating of NR. These are the segments that were not rated. Including them in our analysis will not be correct. Before we do the spatial join, let’s set up a Filter to exclude these records. Right-click the dot_V_SSS_SEGMENTRATING_1_20190129 layer and select Filter.

../../_images/525.png
  1. In the Query Builder, type the following expression to select all records that are not rated NR. You can also build the expression interactively by clicking on Field, Operator and selecting the appropriate Value. Click OK.

"RatingWord" != 'NR'
../../_images/625.png
  1. You will notice the dot_V_SSS_SEGMENTRATING_1_20190129 layer now has a filter icon indicating that there is an active filter applied to this layer. Now we can do a spatial join using this layer. Go to Processing ‣ Toolbox.

../../_images/724.png
  1. Search and locate the Vector general ‣ Join attribute by location (summary) algorithm. Double-click to launch it.

../../_images/824.png
  1. In the Join attribute by location (summary) dialog, select nybb as the Input layer. The street layer dot_V_SSS_SEGMENTRATING_1_20190129 will be the Join layer. You can leave the Geometry predicate to the default Intersects. Click the button next to Fields to sumarize.

../../_images/924.png

備註

A tip to help you select the correct input and join layers: The input layer is the one that will be modified will new attributes in the spatial join. As we want the average rating field to be added to the borough layer, it will be the input layer.

  1. Select Rating_B and click OK.

../../_images/1031.png
  1. Similarly, click the button next to Summaries to calculate.

../../_images/1136.png
  1. Select mean as the summary operator and click OK. Now we are ready to start the processing. Click Run.

../../_images/1232.png
  1. The processing algorithm will work through the features and apply the spatial join. Verify that the processing job was successful and click Close.

../../_images/1330.png
  1. Back in the main QGIS window, you will see a new Joined layer layer added to canvas. Open the attribute table for this layer. You will see a new column Rating_B_mean is added to the input borough layer with the average rating of all streets that are interesecting with that feature.

../../_images/1428.png
  1. Now we can perform a reverse operation. Sometimes your analysis requires getting attributes from another layer based on the spatial relationship but not compute any summary. We can use the Join attribute by location algorithm for such analysis. The task is to add the name of the borough to each feature in the streets layer based on which borough polygon it intersects with. Before we run this algorithm, let’s remove the filter from the dot_V_SSS_SEGMENTRATING_1_20190129 layer. Click the filter icon and press the Clear in the Query Builder. Click OK.

../../_images/1523.png
  1. Turn of the Joined layer in the Layers panel. Find the Vector general ‣ Join attribute by location algorithm in the Processing Toolbox and double-click it to launch.

../../_images/1622.png
  1. Select dot_V_SSS_SEGMENTRATING_1_20190129 as the Input layer and nybb as the Join layer. You can leave the Geometry predicate to the default Intersects. Click the button next to Fields to add and select BoroName. Click OK.

../../_images/1723.png
  1. The line segment may cross a borough boundary, so we choose the Join type as Crate separate feature for each located feature (one-to-many). Click Run.

../../_images/1820.png
  1. Once the processing finishes, open the attribute table of the newly added Joined layer. You will see that there is a new BoroName attribute added to each street feature.

../../_images/1917.png

If you want to give feedback or share your experience with this tutorial, please comment below. (requires GitHub account)