Tutorial: Performing Spatial Queries in QGIS

Tutorial moved to http://www.qgistutorials.com/en/docs/performing_spatial_queries.html 

Spatial queries are core to many GIS analysis. In ArcGIS, you probably use ‘Select by Location’ all the time. In Quantum GIS, this functionality is available via the ‘Spatial Query’ plugin.
The example I am showing you here is a pretty trivial one, but demonstrates how to do spatial queries. As usual, the data is from Natural Earth public domain dataset. The question we will try to answer is, ‘What cities in Europe are within 10 kms of a river?’. 

  • Open QGIS and load the layers ‘ne_10m_rivers_europe’ and ‘10m_populated places’.
  • These layers need to be re-projected to a projected coordinate system, so we could run our queries in metres instead of lat/long. The re-projection mechanism has been changed from QGIS 1.7 onwards. Previously, you would use Vector → Data Management Tools → Export to a new projection. But now, re-projection is part of the ‘Save As’ menu. This menu can be brought up by right clicking on a layer.
  • In the ‘Save as...’ dialog, select ‘World Equidistance Cylindrical (Sphere)’ as theCRS and save the output file as 10m_populated_places_reprojected.shp’. Similarly save the other layer as ‘ne_10m_rivers_europe_reprojected.shp’
  • Once the reprojection is done. Remove the existing layers and add the re-projected layers to your project.
  • Now, we will buffer both the layers by 10,000 metres (10km). Our populated places layer represent the city center, so to simulate the entire city, we would consider the 10km buffer as the city limit. You could be smart and use the population as an indicator to buffer each city proportional to its population. But we will keep it simple here. Also, to find out which cities are within 10kms or a river, we must buffer our rivers line layer also by 10,000 metres. Use the ‘Vector → Geoprocessing Tools → Buffer’ for this operation.
  • Use 10,000 as the buffer distance since our projection uses ‘metres’ as units. Save the layer by adding a ‘_buffer’ to the original name. Do this for both the layers.
  • Once the buffering is done, add both of these layers to your project.
  • Now we are ready to run the spatial query to find out our answer. Open the plugin from ‘Plugins → Spatial Query’. If you don’t see the plugin, use this tutorial to find and add the plugin. Spatial Query has moved to being a 'core' plugin from version 1.8. That means it is part of the standard QGIS installation. You need to enable it from Plugins → Manage Plugins. After that you can access from Vector → Spatial Query.
  • We want to select all populated regions that intersect with the buffered river polygon. Select the options as shown below and click Apply.

  • The new selection will highlight the regions that match the query. This is the answer we are looking for. Click Close and see the highlighted features from the populated places layer.

  • Save the selected features into a new file. I usually like to verify my results to ensure the analysis is not flawed. One way to verify the results is to save this selection as a KML file and load it up in Google Earth. You can check if the areas you found really are within 10kms of a river. As you can see, the results indeed show the existence of rivers in close proximity of the city.

    Hope you enjoyed this tutorial. You can also use various other spatial operatives such as ‘Contains’, ‘Equals’, Is Disjoint’, ‘Overlaps’, ‘Touches’ or ‘Within’ as appropriate for your analysis. Do leave a comment on what type of analysis you are doing and how this tutorial helped you.