Spatial Query in QGIS 3.0

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.
Select by Location and Select by Expression 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.
Crimes, city park and street dataset of Vancouver
Figure 2. Crimes, city park and street dataset of Vancouver
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.

Query option to select all crimes event in the city park
Figure 3. Query option to select all crimes event in the city park
 Spatial query result Crimes within 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.
Spatial query to find any park where crime took place in it
Figure 5. Spatial query to find any park where crime took place

The selected park where crimes took place in it
Figure 6. The selected park where crimes took place
The third query is to find any crime that happened on the street. For this we can use touch or overlap. But I think touch is more correct than overlap, because overlap is more suits with polygon features.   While street is a line/polyline features, so in this query we just need to find a point that touch the line of a street. If there is any crime point that touch a street then it will be selected. But I got none.

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.

Spatial query cross and intersect
Figure 7. Cross vs Intersect
The last spatial query is a little bit complex, we want to find any crime that happened within a radius of 100 m from main/arterial road. In this query there is a dynamic variable, radius length. For this case we can't use Select by location tool right away because there is no option to set define a radius. Because of that, firstly we have to make a buffer for arterial road with a distance of 100 m, and then we select any crime points within the buffer area. The query can be done with the following steps:
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.
Make a definition query
Figure 8. Make a definition query
Then using Buffer tool, buffer the arterial road 100 m. The result can be seen as in figure 9.

Arterial road buffer
Figure 9. Arterial road buffer
Finally using Select by location tool select any crime point within the buffer area. Figure 10 shows the result crime that took place 100 m from main/arterial road.
Crime event 100 m from main road
Figure 10. Crime event 100 m from main road
That's all the tutorial how to perform spatial query in QGIS 3.0. With the example above hopefully you can get an idea how to do spatial query for you own case. If you want to explore more, QGIS 3.0 documentation provides more explanation about vector query.   

Related Posts

Disqus Comments