Difference between revisions of "Creating a Heatmap using QGIS"

From CUOSGwiki
Jump to navigationJump to search
Line 48: Line 48:
 
# The fastest way to import data is by going through the '''Browser Panel''' (on the left of your new project screen) and navigating to where you stored the 2 shapefiles from your working directory. In my case, I stored the shapefiles in the Download folder. Drag both shapefiles to the '''Layers''' panel. You would need to transform the Neighbourhood Data to match the Projects Projections. Simply click '''Ok'''.
 
# The fastest way to import data is by going through the '''Browser Panel''' (on the left of your new project screen) and navigating to where you stored the 2 shapefiles from your working directory. In my case, I stored the shapefiles in the Download folder. Drag both shapefiles to the '''Layers''' panel. You would need to transform the Neighbourhood Data to match the Projects Projections. Simply click '''Ok'''.
 
[[File:Hvhjv.jpg|frame|center|Figure 2. Import both shapefiles and transform the projection of neighbourhood shapefile by clicking ok on the pop up.]]
 
[[File:Hvhjv.jpg|frame|center|Figure 2. Import both shapefiles and transform the projection of neighbourhood shapefile by clicking ok on the pop up.]]
  +
:2. Next, add the basemap that is provided by QGIS. In the browser panel, you should see a directory called "'''XYZ Tiles'''". In there is the “OpenStreetMap”. Drag that down to the Layer panel.
+
<br>2. Next, add the basemap that is provided by QGIS. In the browser panel, you should see a directory called "'''XYZ Tiles'''". In there is the “OpenStreetMap”. Drag that down to the Layer panel.
 
:At this point your screen should look like this (Figure 3 below). You might have to rearrange the layers in this order: MCI layer, Neighbourhood layer and OpenStreetMap layer. Feel free to rename the layers, and change the symbology for easy visualization.
 
:At this point your screen should look like this (Figure 3 below). You might have to rearrange the layers in this order: MCI layer, Neighbourhood layer and OpenStreetMap layer. Feel free to rename the layers, and change the symbology for easy visualization.
 
[[File:C.jpg|thumb|center|950px|Figure 3. The QGIS screen with all layers added.]]
 
[[File:C.jpg|thumb|center|950px|Figure 3. The QGIS screen with all layers added.]]

Revision as of 19:22, 1 October 2021

Introduction

Heatmaps are of one the best visualization tools for dense point data. Heatmap is an interpolation technique that is useful in determining density of input features. Heatmaps are most commonly used to visualize crime data, traffic incidents, housing density etc. The density is calculated based on the number of points in a location, with larger numbers of clustered points resulting in larger values. Heatmaps allow easy identification of “hotspots” and clustering of points. QGIS has a heatmap renderer that can be used to style a point layer and a Processing algorithm Heatmap (Kernel Density Estimation) that can be used to create a raster from a point layer.

Purpose

The purpose of the tutorial is to provide users with a step by step process of how to create a heatmap on QGIS. This tutorial will contain detailed explanations on how to download and use the QGIS software, how to get open source data and how to use the Heatmap feature in Symbology, make analysis and inferences from the data and how to create a map in QGIS. The tutorial will also cover the limitations of QGIS for making Heatmaps. For the tutorial, we would be making a Toronto Crime Heatmap with Data from the Toronto Police.
This tutorial covers themes of Open source Data, Spatial Pattern Analysis and Spatial Interpolation.

Disclaimer: This Tutorial is for Educational Purposes only. The result of the Tutorial shows real crime committed in the city of Toronto. Due to the sensitivity of the data to be used, we would only be making heat maps for Property related crimes.
In accordance with the Municipal Freedom of Information and Protection of Privacy Act, the Toronto Police Service has taken the necessary measures to protect the privacy of individuals involved in the reported occurrences. No personal information related to any of the parties involved in the occurrence will be released as open data. The location of crime occurrences have been deliberately offset to the nearest road intersection node to protect the privacy of parties involved in the occurrence. All location data must be considered as an approximate location of the occurrence and users are advised not to interpret any of these locations as related to a specific address or individual.

Materials

About QGIS

Quantum GIS, better known and QGIS, is arguably the most powerful and commonly used free open source geographic information system software available. It allows the user to create, edit, visually represent, analyze and publish information. It is available for download on a variety of operating systems from Windows, Mac, Linux and BSD, with recent efforts working towards compatibility with Android. QGIS is licensed under the Creative Commons Attribution-ShareAlike 3.0 license (CC BY-SA).

Download QGIS

This tutorial was originally done with QGIS version 3.10.10 ‘A Coruña’ which was released in November 2019. However, any version after 3.8.1 works as well. QGIS is available for download on their Official Website. Click on this link to direct you to the page https://qgis.org/en/site/forusers/download.html. Download and install the version mentioned or the most recent version for your operating system in order to follow along with the following step-by-step tutorial.

Figure 1. On the Toronto Police Service Public Safety Data Portal, the third blue icon is the download button.

Data Acquisition

There are numerous resources available to acquire spatial data for use in QGIS including Open Data Catalogues, Scholars GeoPortal, Ontario GeoHub etc.
For this tutorial, we will be acquiring data from the “Toronto Police Service Public Safety Data Portal”. The Toronto Police documents and release Public Safety Data. “The Public Safety Data Portal is intended to improve the understanding of policing, improve transparency and enhance confidence through the creation and use of open data for public safety in Toronto.”
Dataset to be used:

  • Major Crime Indicators (MCI) 2014 - 2019 shapefile. Categories are Assault, Break and Enter, Auto Theft, Robbery and Theft Over (Excludes Sexual Assaults)
  • Toronto Neighbourhood Boundary shapefile. This also contains crime data but we just need the shapefile for its boundaries, to provide context for the MCI data.


Steps in data acquisition:

  1. Go to the MCI page by clicking the link (https://data.torontopolice.on.ca/datasets/mci-2014-to-2019) or type in 'major crime indicators' under the 'Open Data' tab.
  2. Click on Download (see Figure 2 for help), then under Full Dataset click on Shapefile
  3. Repeat the same process for the Neighbourhood Boundary page by clicking the link (https://data.torontopolice.on.ca/datasets/neighbourhood-crime-rates-boundary-file-) or type in 'neighbourhood boundary' under the 'Open Data' tab.
  4. Extract (Unzip) both folders and save in the working directory you will be using.


Tools and Methods

Getting started

For this tutorial, no changes are needed to the projection as we will be using the source projection.

Import Data

  1. The fastest way to import data is by going through the Browser Panel (on the left of your new project screen) and navigating to where you stored the 2 shapefiles from your working directory. In my case, I stored the shapefiles in the Download folder. Drag both shapefiles to the Layers panel. You would need to transform the Neighbourhood Data to match the Projects Projections. Simply click Ok.
Figure 2. Import both shapefiles and transform the projection of neighbourhood shapefile by clicking ok on the pop up.


2. Next, add the basemap that is provided by QGIS. In the browser panel, you should see a directory called "XYZ Tiles". In there is the “OpenStreetMap”. Drag that down to the Layer panel.

At this point your screen should look like this (Figure 3 below). You might have to rearrange the layers in this order: MCI layer, Neighbourhood layer and OpenStreetMap layer. Feel free to rename the layers, and change the symbology for easy visualization.
Figure 3. The QGIS screen with all layers added.

Process

In this section we would be altering the data to do what we need with it. Feel free to explore the Attribute Table for the MCI layer.
Because this shapefile contains a lot of data for ranging (Field name: occurren_1) from 2000 - 2019 (even though the shapefile says 2014 - 2019), we would only be working with 2019 data. Due to the sensitivity of the data we are using, we would only be making heat maps for Property related crimes according to the Department of Justice categorization: Break & Entry (all types), Theft of Motor Vehicle and Non-vehicle Theft (all of theft).

Methodology

Here we would be creating feature layers from the MCI layer and creating the heatmaps.
For Break and Entry, 2019 shapefile:
1. On the Toolbar, click on Select Features by Expression.

Fig 4. Select by expression


2. Click on “Fields and Values” and select “occurren_1”, then put an “=” and type 2019.

 (This means we are narrowing the data to 2019 only.) 
Fig 5. Select by Expression for 2019 B&E

3. Next type “And”, and click “offence”, then type “like” and finally type “ ‘B&E%’ ”.

 (The “And” joins the first statement with the second statement”. The second statement narrows down offence to Break and Entry. 
 The “%” is needed as the symbol acts like a wildcard. 
 Meaning it covers everything past “B&E”. ie. “B&E”, “B&E - M/Veh To Steal Firearm”, “B&E Out”, etc.)
 Your statement should look like this: “occurren_1” = 2019 AND “offence” like ‘B&E%’

After all this click Select Features. And you should see certain points on the map selected, though it may be hard to visualize.
Be careful of the syntax and formatting as the tool is case sensitive. For more information about how Expressions work, check out this link (https://docs.qgis.org/2.18/en/docs/user_manual/working_with_vector/expression.html)
4. Right click on the “MCI 2014 to 2019” layer and click on “Export” then “Save Selected Features As...”

Fig 6. Save selected features as..


5. Change the Format to “ESRI Shapefile” and set the File name as “BreakEntry2019” (or whatever you prefer) and then click Ok. Leave everything else the same.
6. Unselect the “MCI 2014 to 2019” layer and you should now see all Break and Entry cases for 2019 in Toronto.
7. Next, we would be making the heatmap. Right click on the “BreakEntry2019” layer and select “Properties”.
8. Click on “Layer styling Panel” and change the symbology from “Single symbol” to “Heatmap”.
9. Next you need to choose a Radius. This parameter determines the circular neighborhood around each point where that point will have an influence. This value will largely depend on the type of your input data. For our data, let’s assume a crime incident will have an influence upto 5 Kilometers from the location. Notice that the current project CRS is set to EPSG: 3857 in the bottom-right corner. This CRS has a unit of meter, so we should specify 5000 meters as the radius.
10. Change the colour ramp to “Reds” and under “Layer Rendering” change the Opacity to “55%” and click Ok.
You should see a change in the map.

Fig 7. Heatmap settings for b&e in 2019


11. Next, we are going to do some Spatial Interpolation. On the Toolbar click on Processing then Toolbox. Search “Heatmap”, and select “Heatmap (Kernel Density Estimation)”
Point Layer: BreakEntry2019 (or whatever you named your layer)
Radius: 5000 metres
Pixel Size: X: 50, Y: 50
Leave everything else as is.

Fig 8. Input for heat map


12. Your screen should look like this. Click on Layer Styling Panel and change “Singleband gray” to “Singleband pseudocolor”. Change colour ramp to “Reds”. Then Apply.

Fig 9. Interpolation Heatmap tool

Result

In this section we would be making 2 final maps. One with the output of the Heatmap Render and one with the output of the interpolation tool “Heatmap (Kernel Density Estimation)”.
For the first Map:

  1. Deselect the new Heatmap layer.
    1. Fig 10. Layers selected for map 1
  2. On the Toolbar Click on Project and then New Print Layout.
  3. Name the Layout and click Ok.
  4. On the Toolbar, click Add Item and select Add Map. Create the extent for the map on the layout.
  5. Add a North Arrow, Scale Bar, Title and Legend.
  6. Export as PDF or Image.

For the Second Map:

  1. Reselect the new Heatmap layer.
  2. Move the “Toronto Neighbourhood” layer above the new “Heatmap” layer and reduce its Opacity to “50%” by right-clicking and the properties and then “Layer Rendering”.
    1. Fig 11. Layers selected for map 2
  3. On the Toolbar Click on Project and then New Print Layout.
  4. Name the Layout and click Ok.
  5. On the Toolbar, click Add Item and select Add Map. Create the extent for the map on the layout.
  6. Add a North Arrow, Scale Bar, Title and Legend.
  7. Export as PDF or Image.
Fig 12. Heatmap Using GQIS Renderer
Fig 13. Heatmap using Heatmap (KDE) Interpolation tool

Limitations

Limitations to this tutorial include:

  • QGIS map creation isn't as good as ESRI’s ArcGIS Pro, or ArcMaps
  • Heatmaps can often be misinterpreted. Heat maps express clusters, and one can look at a map and think there is no crime except at the hotspot zone which is wrong
  • Using real crime data might alter one's perception of a city which may or may not be misleading.
  • The Heatmap Render does not produce a legend or values used to calculate for the heatmap. And the interpolation tool “Heatmap (Kernel Density Estimation)” produces a heatmap that is not visually appealing.

Conclusion

Heatmaps are one of the best visualization tools for dense point data and are used to visualize crime data, traffic incidents, housing density and more.The purpose of the tutorial was to provide users with a step by step process of how to create a heatmap on QGIS. This tutorial contained details on how to download and use the QGIS software, how to get open source data, how to create multiple Heatmaps, and how to make analysis and inferences from the data. The tutorial also covered the limitations of QGIS for. Due to the facts that we are working with crime data which is sensitive, the tutorial is for Educational Purposes only. I hope this tutorial will help future users have a better understanding of QGIS, Open source Data and Spatial Pattern Analysis.

Resources

1. Heatmap: https://www.crazyegg.com/blog/understanding-using-heatmaps-studies/
2. QGIS: https://qgis.org/en/site/forusers/download.html
3. Toronto Police SErvice PSDP: https://data.torontopolice.on.ca
4. Department of Justice: https://www.justice.gc.ca/eng/rp-pr/cj-jp/victim/rr05_4/p7.html#a5_2
5. QGIS Expressions: https://docs.qgis.org/2.18/en/docs/user_manual/working_with_vector/expression.html