You are on page 1of 42

qgis.spat ialt ho ught s.co m http://qgis.spatialtho ughts.co m/2012/07/tuto rial-making-heatmaps-using-qgis-and.html?

pfstyle=wp

Tutorial: Making Heatmaps using QGIS and GRASS


Please see Creating Heatmaps f or an updated version of this tutorial. Heatmaps are one of the best visualization tools f or dense point data. Heatmaps are used to easily identif y f ind clusters where there is a high concentration of activity. In this tutorial we will explore two dif f erent method of creating heatmaps in QGIS and work with crime related data to f ind crime hotspots with cluster analysis. QGIS 1.8 introduced a Heatmap plugin which allows you to create heatmaps directly f rom 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. T he data f or this tutorial comes f rom London datastore . T here is a wealth of open data available and f or this tutorial I downloaded the 2011 Crime data f or Surrey.

Part 1: Heat map 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 f older. T he data is in a CSV f ormat. Use the steps outlined in this tutorial to import CSV data to QGIS. You have to specif y Northing as X and Easting as Y f ields. T he CRS f or this data is OSGB 1936 British National Grid.

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

Name the shapef ile as crime.shp. Check the box next to Add saved f ile to map and click OK.

T he 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 f irst. 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. T he Radius is the area around each point that will be used to calculate the heat a pixel received. T he Decay ratio is how f ar a point will inf luence other pixels. Clicking on Help will bring up a more detailed explanation of the algorithm. For our analysis we would like to have a pixels 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. T his is the heatmap raster, but it doesnt 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.

Z oom in closer to the area where the points are. Click on the Identif y Features to activate the Identif y tool. Click anywhere on the raster. You will see the Identif y Results dialog pop up with the value of the pixel at that location. T his value represents the total number of points in the 1000 pixel radius that we had specif ied.

Lets 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 f or 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 lets identif y clusters of crime hotspots. Say you wanted to identif y areas with high crime concentration and deploy more patrol there. Or we may want to use these clusters in some f urther analysis. We will use Raster Calculator to identif y 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 identif y 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 f ile. Name the output f ile as crime_clusters.shp. Check the box next to Load into canvas when f inished and click OK.

You will see the vector polygons f rom the crime_clusters layer. You will also notice that this layer contains polygons f or 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 f or your clusters. You can now use this polygon layer in your f urther analysis.

Part 2: GRASS v.kernel module


T he region setting that we want change is the Resolution. T his 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 powerf ul analytical capability. I will quickly go through setting up a location and mapset in GRASS. You can ref er to this tutorial f or 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.

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

For Def ault GRASS Region, click Set current QGIS extent. Click Next.

Enter the mapset name as crime. 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.

T here are hundreds of analytical modules in GRASS. T hey are organized in dif f erent sections. Click on the Modules Tree tab and f ind 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 f inished, click View Output to load the layer in QGIS canvas.

Open the GRASS tools again and f ind 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 f rom running correctly. T his will be f ixed in later versions, but if you encounter an error, f ollow the alternate method below )

From the GRASS tools, select shell f rom the top.

T his will open a windows commandline interf ace with relevant variables already set. Type v.kernel and press Enter. T his will launch the native GRASS interf ace f or the module.

Enter the same values and click Run.

Once the processing is f inished, click Plugins GRASS Add GRASS raster layer.

Select the crime_heatmap_grass layer and click OK. 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 f amiliar heatmap style. You can export this raster f rom GRASS to any f ormat you like and use it f urther in your analysis.

You might also like