Spatial queries are core to many types of GIS analysis. Spatial queries allows you to select features in a layer by their spatial relationships (intersect, contain, touch etc.) with features from another layer. In QGIS, this functionality is available via the Select by Location and Extract by Location Processing tools.
We will be working with 2 data layers for the city of Melbourne, Australia. Given the data layers for the pubs and bars in the city and locations of all metro stations, we want to find out all bars and pubs within 500 meters of a metro station.
City of Melbourne’s Open Data Platform provides many GIS-ready datasets for the city.
Download the Metro Train Stations with Accessibility Information dataset by Metro Trains Melbourne. Export the data in the Original format.
Download the Bars and pubs, with patron capacity dataset by City of Melbourne’s Census of Land Use and Employment (CLUE). Export the data as a CSV.
For convenience, you may directly download a copy of datasets from the link below:
metro_stations_accessbility.zip
Bars_and_pubs__with_patron_capacity
Data Source: [CITYOFMELBOURNE]
metro_stations_accessbility.zip
file in the QGIS Browser and expand it. Select the metro_stations_accessbility.shp
file and drag it to the canvas. A new layer metro_stations_accessbility
will be loaded in the Layers panel.Bars_and_pubs__with_patron_capacity.csv
file as File name. The X field and Y field columns should be auto selected to x coordinate
and y coordinate
respectively. Click Add.Bars_and_pubs__with_patron_capacity
layer added to the Layers panel. Both of the input layers are in the Geograhpic Coordinate Reference System (CRS) EPSG:436 WGS84
. For performing spatial analysis, it is recommended to use a Projected Coordinate Reference System (CRS). So we will now re-project both the layers to an appropriate regional CRS that minimizes distortions and allows us to work in units of distance such as meters instead of degrees. Go to .Bars_and_pubs__with_patron_capacity
as the Input layer. Click the Select CRS button next to Target CRS.Note
If you are not sure of the a local CRS for the region that you are working in, selecting a CRS for the UTM zone based on the WGS84 datum is a safe choice. You can find out the UTM zone number of your region using UTM Grid Zones of the World.
Save to GeoPackage
. Geopackage is the recommended open data format spatial data and is the default data exchange format for QGIS3. A single GeoPackage .gpkg
file can contain multiple vector and raster layers.spatialquery
and click Save.bars_and_pubs
and click OK. Click Run to reproject the layer.bars_and_pubs
.metro_stations_accessbility
layer. Switch back to the Paramters tab in the Reproject layer window. Select metro_stations_accessbility
as the Input layer. Keep the same Target CRS. Next, click the ... button next to Reprojected and select Save to GeoPackage
. Select the same output file spatialquery
(Remember that a single geopackage file can contain multiple layers, so we will save the new layer to the same geopackage file). Enter metro_stations
as the Layer name. Click Run.bars_and_pubs
and metro_stations
. You may turn off the visibility for original layers. Now, we are ready to do the spatial query. As we are interested in selecting bars and pubs within 500m of the metro stations, the first step is to create a buffer around the metro stations that represents our search area. Search and locate the tool in the Processing Toolbox and double-click to launch it.metro_stations
as the Input layer. Set 500
meters as the Distance. Save the output to the same spatialquery
geopackage and enter metro_stations_buffers
as the Layer name. Click Run.metro_stations_buffers
layers loaded in the Layers panel. Now we can find out which points from the bars_and_pubs
layer falls within the polygons from the metro_stations_buffers
layer. Locate the tool from the Processing Toolbox and double-click to launch it.Note
Extract by location will create a new layer with the matching features from the spatial query. If you just want to select features, use the Select by location tool.
bars_and_pubs
as the Extract features from. Check Intersect
as the geometry predicate. Set metro_stations_buffers
as By comparing to the features from. Save the output to the spatialquery
geopackage as the layer selected
. Click Run.selected
layers added to the Layers panel. Note that this layer only contains points from the bars_and_pubs
that fall within the buffer polygons.GDA 2020 / MGA zone 55 EPSG:7855
which we used for the analysis. Once set to this CRS, the buffer will appear in the correct shape.This work is licensed under a Creative Commons Attribution 4.0 International License