Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

42
qgis.spatialthoughts.com htt p:/ /qgis.spatialthoughts.com/2012/07/ tut orial- making-heatmaps -us ing-qgis- and.ht ml?pfstyle=wp Tutorial: Making Heatmaps using QGIS and GRASS Please s ee Creating Heatmaps for an updated version of this tutorial. Heatmaps are one o f the best visuali zation t oo ls f or dense po int data. Heatmaps are used to easil y identif y f ind c lusters where there is a hi gh concentrat ion o f activi ty. I n this t uto rial we will expl o re two dif f erent method of creating heatmaps in QGIS and work with cri me related data to find crime hot spo ts with cluster analysis. QGIS 1. 8 intr o duced a ‘Heatmap’ plugin which allows yo u to creat e heat maps directly f rom QGI S. Prior to that , one could use variety o f GRA SS algorithms via the GRA SS plugi n in QGI S to create t he heatmap. We wi ll use both these methods and obt ain the heatmap. The data f or this tutorial comes f rom London datastore . There is a wealth of open data avai lable and fo r this t uto rial I downl oaded the 2011 Crim e dat a for Surrey. Part 1: Heat map Pl ugi n 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 st art, unzip the data to a f older . The data is in a CSV f ormat. Use the st eps outlined in this tutorial to import CSV data to QGIS. Y ou have to specif y Nort hing’ as X and ‘East ing’ as Y f ields. The CRS f o r this data is OSGB 1936 Bri tish National Grid.

Transcript of Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

Page 1: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 1/42

qgis.spatialthoughts.com

http://qgis.spatialthoughts.com/2012/07/tutorial-making-heatmaps -us ing-qgis- and.html?pfstyle=wp

Tutorial: Making Heatmaps using QGIS and GRASS

Please see Creating Heatmaps for an updated version of this tuto rial.

Heatmaps are one o f the best visualization t oo ls f or dense po int data. Heatmaps are used to easily

identif y f ind clusters where there is a high concentrat ion o f activity. In this tuto rial we will explore two

dif f erent method o f creating heatmaps in QGIS and work with crime related data to f ind crime hotspo ts 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 o f GRASS algorithms via the GRASS plugin in QGIS to create the heatmap. We

will use both these methods and obtain the heatmap. The data f or t his tuto rial comes f romLondon

datastore . There is a wealth of open data available and fo r this tutorial I downloaded the2011 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 f older. The 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 ‘East ing’ as Y f ields. The

CRS f or this data is OSGB 1936 British National Grid.

Page 2: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 2/42

Save the layer as a shapef ile so it is easier to reuse without import ing again. Right click on the layer 

and select ‘Save As’.

Page 3: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 3/42

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

Page 4: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 4/42

The new vector ;ayer will be added to the table of content. Remove the older import ed layer by right

clicking and selecting ‘Remove’.

Page 5: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 5/42

To use the heapmap plugin, we must enable it f irst . Click on Plugins → Manage Plugins. Scroll down

and make sure the box next t o ‘Heatmap’ is checked.

Page 6: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 6/42

Open the Heatmap plugin by go ing 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 po int will inf luence o ther pixels.

Clicking on ‘Help’ will bring up a more detailed explanation o f 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 o f the output

raster to be 100. Click OK.

Page 7: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 7/42

We have more than 100,000 points in our layer, so the heatmap process ing map take a while. ( In my

case about 30 minutes ). Once the heatmap raster is created, you will get a prompt to select t he CRS.

Select OSGB 1936 British National Grid and click OK.

Page 8: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 8/42

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.

Page 9: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 9/42

Right click on the crime_heatmap layer and select Properties.

Page 10: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 10/42

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

Page 11: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 11/42

Now you will see something resembling a heatmap.

Page 12: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 12/42

Page 13: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 13/42

Let’s do so me styling adjustments to view our heatmap better. Right click on t he 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 t ransperant, so click ‘Add Values Manually’ next to the

Transparent pixel list. Add the value 0 to the list. Click OK.

Page 14: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 14/42

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

Page 15: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 15/42

Now let’s identify clusters of ‘crime hotspo ts ’. Say you wanted to identif y areas with high crime

concentrat ion and deploy more patro l 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 calculato r.

Page 16: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 16/42

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 no t. In Raster calculato r dialog, name the output layer as

crime_heatmap_100 and check the bo x 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.

Page 17: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 17/42

Page 18: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 18/42

Now you will be able to see clusters in white pixels. We will convert t hese to vectors . Click on Raster 

→ Conversion → Polygonize. ( Enable GdalToo ls plugin if you do not see this menu ).

Page 19: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 19/42

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 not ice that t his 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’.

Page 20: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 20/42

Click ‘Advanced Search’ at the bot to m of the Attribute Table.

Page 21: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 21/42

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

Page 22: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 22/42

You will see t he polygons with 0 values will be selected. Click ‘Toggle Editing’ button at the bo tt om.

Once editing is enabled click ‘Delete Selected Features’ button. Click ‘Toggle Editing’ again and Save.

Page 23: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 23/42

Go back to the main QGIS canvas and you will see the po lygon layer for your clusters. You can nowuse this po lygon layer in your f urther analysis.

Page 24: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 24/42

Part 2: GRASS v.kernel module

The region set ting that we want change is the Resolution. This set ting will determine the size and

reso lution o f our heatmap. To match the set ting with the previous method, put 100 as the Cell width

and height. Click OK.

Page 25: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 25/42

Now let us get t o the second method using GRASS. GRASS is another plugin that is built in and has

powerf ul analytical capability. I will quickly go through sett ing up a locat ion and mapset in GRASS. You

can refer to this tutorial f or more details on what they mean. Click on Plugins → GRASS → New

mapset.

Page 26: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 26/42

Select a Database directory and click Next.

Page 27: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 27/42

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

Page 28: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 28/42

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

Page 29: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 29/42

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

Page 30: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 30/42

Enter the mapset name as ‘crime’. Click Next.

Page 31: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 31/42

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

GRASS → Edit Current Grass Region.

Page 32: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 32/42

Now click on Plugins → GRASS → Open GRASS tools.

Page 33: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 33/42

There are hundreds of analytical modules in GRASS. They are organized in diff erent sections . Click

on the ‘Modules Tree’ tab and f ind the v.in.ogr.qgis module.

Page 34: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 34/42

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.

Page 35: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 35/42

Open the GRASS tools again and f ind the module v.kernel.

Page 36: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 36/42

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 t his module f rom running

correctly. This will be fixed in later versions, but if you encounter an error, f o llow the alternate

metho d below )

Page 37: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 37/42

From the GRASS tools, select shell from the top.

Page 38: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 38/42

This will open a windows commandline interf ace with relevant variables already set . Type v.kernel andpress Enter. This will launch the native GRASS interf ace for the module.

Enter the same values and click Run.

Page 39: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 39/42

Once the process ing is f inished, click Plugins → GRASS → Add GRASS raster layer.

Page 40: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 40/42

Select the crime_heatmap_grass layer and click OK.

You will see a grayscale heatmap. Right click and select

‘Properties’.

Page 41: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 41/42

Select ‘Pseudoco lor ’ as t he Colormap and click OK. Now the raster will be st yled in a f amiliar heatmap

style. You can export this raster f rom GRASS to any format you like and use it f urther in your 

analysis.

Page 42: Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

7/21/2019 Qgis.spatialthoughts.com-Tutorial Making Heatmaps Using QGIS and GRASS

http://slidepdf.com/reader/full/qgisspatialthoughtscom-tutorial-making-heatmaps-using-qgis-and-grass 42/42