Heatmaps are one of the best visualization tools for dense point data. Heatmap is an interpolation technique that useful in determining density of input features. Heatmaps are most commonly used to visualize crime data, traffic incidents, housing density etc. 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 an raster from a point layer.
We will work with a dataset of crime locations in Surrey, UK and create a heatmap to visualize regions with high density of crime.
data.police.uk provides street-level crime, outcome, and stop and search data in simple CSV format. Download the data for Surrey Police and unzip the downloaded archive to extract the CSV file.
For convenience, you may directly download a copy of the dataset from the link below:
Data Source [POLICEUK]
2019-02-surrey-street.csv
file. The X field and Y field in the Geometry Definition section to be auto-populated with the Longitude
and Latitude
columns. The Geometry CRS should be left to default EPSG:4326 - WGS 84
definition. Make sure the data looks correct in the Sample data panel and click Add, followed by Close.OpenStreetMap
and 2019-02-surrey-street
loaded in the QGIS Layers panel. Right-click the 2019-02-surrey-street
layer and select Zoom to Layer.2019-02-surrey-street
layer and click the Open the Layer Styling panel button.Heatmap
as the renderer in the dropbox menu. The Layer Styling panel is interactive and you can see the effect of your changes reflected in the canvas immediately. The layer will now be displayed in the default grayscale color-ramp.Reds
color-ramp.EPSG: 3857
in the bottom-right corner. This CRS has a unit of meter, so we should specify 5000
meters as the radius. Another parameter that is hidden from this menu is the Kernel shape. This is a function that determines how the influence of a point should be spread out over the given radius. The Heatmap renderer uses the Quartic
function for this calculation. There are other types of kernels such as Triangular
, Uniform
, Triweight
and Epanechnikov
that can be specified in when using a different heatmap creation method described later in this tutorial. See this post for a good explanation and guidance for select the right radius and kernel shape.60 %
so you can see the basemap along with the heatmap.2019-02-surrey-street
layer and select Open Attribute Table.Crime type
in the input data that describes the type of crime. We can use these to categorize the different types of crimes and assign a higher weight to more violent crimes.Crime type
and determines the weight value. QGIS has a handy way to add such computed fields using Virtual Fields. The virtual field is saved in the QGIS project and doesn’t modify the source data. It is also dynamically computed and can be used anywhere in QGIS just like any other attribute value. Enter weight
as the Output field name and set the Output field type to Whole number (integer)
. Enter the following expression in the Expression editor. Here we are using CASE statement to assign different values based on different conditions. Click OK.CASE WHEN "Crime type" LIKE 'Violence%' THEN 10 WHEN "Crime type" LIKE 'Criminal%' THEN 5 ELSE 1 END
weight
field.EPSG:27700 OSGB 1936 / British National Grid
CRS. This projected CRS is a good choice for data in the UK. Click Run.Reprojected
will be added to the Layers panel. Un-check the box next to the old 2019-02-surrey-street
layer to hide it.5000
meters and Weight from field as weight
. Set the Pixel size X and Pixel size Y to 50
meters. Let the Kernel shape to the default value of Quartic
. Click Run.Note
The Radius from field parameter allows you to specify a dynamic search radius for each point. This can be used along with Weight from field to have fine grainer control on how each point’s influence is spread.
OUTPUT
will be loaded. The default visualization is ugly since it uses the Singleband gray
renderer. Click the Open the Layer Styling panel button.Singleband Pseudocolor
and select the Reds
color ramp. The layer now looks like the heatmap visualization that we had created earlier.Note
Notice that OUTPUT
layer in the Layers panel has a legend but the 2019-02-surrey-street
layer does not. A common problem with using a heatmap layer created with the Heatmap renderer is the lack of a legend. Say you want use the heatmap in the Print Layout and add a legend. A raster heatmap created with the Heatmap processing algorithm method makes this possible.
This work is licensed under a Creative Commons Attribution 4.0 International License