Spatial Query is selection of features that satisfies a certain condition which relates to other features in a space. Some example of spatial queries such as features that intersect with other features (i.e. parcel that intersect with road network), features within other features (i.e. hotspot within a forest area), features with a distance from another feature (i.e. buildings 20 meter from main road), etc. In QGIS 2.x we can perform spatial query with spatial query plugin, but for QGIS 3, I can't find the plugin in the plugin repository. So how to do spatial queries in QGIS 3.0?
In QGIS 3.0, the spatial queries operation can be performed with a tool called Select by Location. The tool can be found under the Vector Selection tool in the Processing Toolbox (See figure 1).Let see some spatial queries example using this tool.
Figure 1. Select by Location and Select by Expression tool |
For spatial query example, I used some Vancouver dataset such as: crime point 2016, parks polygon and public streets. Those data can be downloaded from Vancouver City data catalogue. Those data can be seen as in figure 2.
Based on the data we will do some spatial queries below.
1. Select crime events that happened in the city park
2. Which park that crimes took place?
3. Find crime event that took place on the street.
4. Find roads which cross city park
5. Find crime that occurred at radius 100 meter from main street.
For first query we will select all crimes that took place in the city park. We can do the query with the following steps:
1. Select crime_shp as the selected features.
2. Check are within option below geometric predicate.
3. Select park_polygon as comparing features.
4. Select creating new selection, because we make a new selection (not to select from selected features).
5. Run the the query with the selected option as in figure 3. All crimes that happened in the city park will be selected. I captured a part of the query result in figure 4.
Based on the data we will do some spatial queries below.
1. Select crime events that happened in the city park
2. Which park that crimes took place?
3. Find crime event that took place on the street.
4. Find roads which cross city park
5. Find crime that occurred at radius 100 meter from main street.
For first query we will select all crimes that took place in the city park. We can do the query with the following steps:
1. Select crime_shp as the selected features.
2. Check are within option below geometric predicate.
3. Select park_polygon as comparing features.
4. Select creating new selection, because we make a new selection (not to select from selected features).
5. Run the the query with the selected option as in figure 3. All crimes that happened in the city park will be selected. I captured a part of the query result in figure 4.
Figure 3. Query option to select all crimes event in the city park |
Figure 4. Spatial query result: Crimes within park |
For the second question, we use contain. The logic is we select any park that contains crime point. The query is set as in figure 5. Figure 6 shows the result.
Figure 5. Spatial query to find any park where crime took place |
Figure 6. The selected park where crimes took place |
In the fourth query we want to select any road in a park. For this case we use intersect option. There is also a cross option but it will not select a street segment that completely within a park. The figure 7 gives the difference between cross and intersect.
Figure 7. Cross vs Intersect |
1. Make a definition query to select arterial road only.
2. Buffer 100 m the arterial road.
3. Use Select by location tool to find any crime within the buffer area.
A definition query can be made through street's layer properties in the source option. In the option select Query Builder and make a query expression to select feature by its attribute. For this case the expression is USE='Arterial'. See figure 8.
Figure 8. Make a definition query |
Figure 9. Arterial road buffer |
Figure 10. Crime event 100 m from main road |