Pages

Tutorial: Making Heatmaps using QGIS and GRASS

Tutorial has moved to http://www.qgistutorials.com/en/docs/creating_heatmaps.htmlHeatmaps are one of the best visualization tools for dense point data. Heatmaps are used to easily identify find clusters where there is a high concentration of activity. In this tutorial we will explore two different method of creating heatmaps in QGIS and work with crime related data to find crime hotspots with cluster analysis.QGIS 1.8 introduced a ‘Heatmap’ plugin which allows you to create heatmaps directly from QGIS. Prior to that, one could use variety of GRASS algorithms via the GRASS plugin in QGIS to create the heatmap. We will use both these methods and obtain the heatmap. The data for this tutorial comes from London datastore . There is a wealth of open data available and for this tutorial I downloaded the 2011 Crime data for Surrey.

Part 1: Heatmap Plugin

Note that the plugin method is experimental and can be slow/unstable at times. If you are working with a large dataset or scientific analysis, GRASS method outlined in Part 2 is recommended. But the plugin method is simpler and suited for quick visualiaztions.

To start, unzip the data to a folder. The data is in a CSV format. Use the steps outlined in this tutorial to import CSV data to QGIS. You have to specify Northing’ as X and ‘Easting’ as Y fields. The CRS for this data is OSGB 1936 British National Grid.

Save the layer as a shapefile so it is easier to reuse without importing again. Right click on the layer and select ‘Save As’.

Name the shapefile as crime.shp. Check the box next to ‘Add saved file to map’ and click OK.

The new vector ;ayer will be added to the table of content. Remove the older imported layer by right clicking and selecting ‘Remove’.

To use the heapmap plugin, we must enable it first. Click on Plugins → Manage Plugins. Scroll down and make sure the box next to ‘Heatmap’ is checked.

Open the Heatmap plugin by going to Raster → Heatmap → Heatmap

In the Heatmap plugin dialog the crime layer would be selected as the Input Point Vector. Name the output raster as crime_heatmap. The Radius is the area around each point that will be used to calculate the ‘heat’ a pixel received. The Decay ratio is how far a point will influence other pixels. Clicking on ‘Help’ will bring up a more detailed explanation of the algorithm. For our analysis we would like to have a pixel’s value determined by how many points are within 1 km (1000m) of the location, so we select the radius at 1000 and Decay Ratio as 0. Also set the pixel resolution of the output raster to be 100. Click OK.

We have more than 100,000 points in our layer, so the heatmap processing map take a while. ( In my case about 30 minutes ). Once the heatmap raster is created, you will get a prompt to select the CRS. Select OSGB 1936 British National Grid and click OK.

You will see a gray rectangle covering your layer extents. This is the heatmap raster, but it doesn’t have any styles applied to it.

Right click on the crime_heatmap layer and select Properties.

Under the ‘Style’ tab, select the Pseudocolor as the Color map and click OK.

Now you will see something resembling a heatmap.

Zoom in closer to the area where the points are. Click on the ‘Identify Features’ to activate the Identify tool. Click anywhere on the raster. You will see the Identify Results dialog pop up with the value of the pixel at that location. This value represents the total number of points in the 1000 pixel radius that we had specified.

Let’s do some styling adjustments to view our heatmap better. Right click on the crime_heatmap layer and select ‘Properties’. In the ‘Transparency’ tab, move the slider for Global transparency to 20%. We also want to make the pixels with 0 values transperant, so click ‘Add Values Manually’ next to the Transparent pixel list. Add the value 0 to the list. Click OK.

You will now see the underlying points along with the heatmap.

Now let’s identify clusters of ‘crime hotspots’. Say you wanted to identify areas with high crime concentration and deploy more patrol there. Or we may want to use these clusters in some further analysis. We will use Raster Calculator to identify these clusters. Click on Raster → Raster calculator.

You will have to decide on a threshold value. For our example, we will use 100 as a threshold to identify what is a ‘cluster’ and what is not. In Raster calculator dialog, name the output layer as crime_heatmap_100 and check the box next to ‘Add result to project’. Input the expression crime_heatmap@1 >= 100. What this means is that all pixels will values below 100 will be assigned a value of 0 and all other pixels a value of 1. Click OK.

You will see a gray raster like earlier. Right click on the new layer and click ‘Properties’. Select ‘Stretch to MinMax under contrast enhancement and click OK.

Now you will be able to see clusters in white pixels. We will convert these to vectors. Click on Raster → Conversion → Polygonize. ( Enable GdalTools plugin if you do not see this menu ).

Select crime_heatmap_100 as the input file. Name the output file as crime_clusters.shp. Check the box next to ‘Load into canvas when finished’ and click OK.

You will see the vector polygons from the crime_clusters layer. You will also notice that this layer contains polygons for 0 value areas also. We will need to remove these to get a layer with our clusters. Right click on the layer and select ‘Open Attribute Table’.

Click ‘Advanced Search’ at the bottom of the Attribute Table.

Enter the expression ‘DN = 0’ and click OK.

You will see the polygons with 0 values will be selected. Click ‘Toggle Editing’ button at the bottom. Once editing is enabled click ‘Delete Selected Features’ button. Click ‘Toggle Editing’ again and Save.

Go back to the main QGIS canvas and you will see the polygon layer for your clusters. You can now use this polygon layer in your further analysis.

Part 2: GRASS v.kernel module

The region setting that we want change is the Resolution. This setting will determine the size and resolution of our heatmap. To match the setting with the previous method, put 100 as the Cell width and height. Click OK.

Now let us get to the second method using GRASS. GRASS is another plugin that is built in and has powerful analytical capability. I will quickly go through setting up a location and mapset in GRASS. You can refer to this tutorial for more details on what they mean. Click on Plugins → GRASS → New mapset.

Select a Database directory and click Next.

Select ‘Create new location’ and name it ‘uk’.

Define the projection as OSGB 1936 / British National Grid. Click Next.

Now that you set an active mapset, many more GRASS menu items will be enabled. Click Plugins → GRASS → Edit Current Grass Region.

Now click on Plugins → GRASS → Open GRASS tools.

There are hundreds of analytical modules in GRASS. They are organized in different sections. Click on the ‘Modules Tree’ tab and find the v.in.ogr.qgis module.

Select the ‘crime’ points layer as input and name the GRASS vector as ’crime’. Click Run. Once the import is finished, click ‘View Output’ to load the layer in QGIS canvas.

Open the GRASS tools again and find the module v.kernel.

Select the crime layer as input vector and enter 1000 as the Standard deviation. Name the output raster map as crime_heatmap_grass. Click Run. ( A bug in QGIS 1.8 prevents this module from running correctly. This will be fixed in later versions, but if you encounter an error, follow the alternate method below )

From the GRASS tools, select shell from the top.

This will open a windows commandline interface with relevant variables already set. Type v.kernel and press Enter. This will launch the native GRASS interface for the module.

You will see a grayscale heatmap. Right click and select ‘Properties’.

Select ‘Pseudocolor’ as the Colormap and click OK. Now the raster will be styled in a familiar heatmap style. You can export this raster from GRASS to any format you like and use it further in your analysis.