Tutorial: Basic Raster Analysis and Styling in QGIS

Tutorial has moved to http://www.qgistutorials.com/en/docs/raster_styling_and_analysis.html

A lot of scientific observations and research produces raster datasets. Rasters are essentially grids of pixels that have a specific value assigned to them. By doing mathematical operations on these values, one can do some interesting analysis. QGIS has some basic analysis capabilities built-in via Raster Calculator. In this tutorial, we will explore basics on using Raster Calculator and options available for styling rasters.

For this tutorial, we will use the Gridded Population of the World (GPW), v3 dataset from Columbia University. Specifically, I downloaded the Population Density Grid for the entire globe in ASCII format and for the year 1990 and 2000. Our task will be to find and visualize areas of the world that have seen dramatic population density change between year 1990 and 2000.

Open QGIS. Locate the 2 zip files for year 1990 and 2000 in QGIS Browser. See this tutorial on using QGIS Browser and opening zip files in QGIS.

There are 2 grid files for each of the years. The ‘a’ in the filename suggests that the population counts were adjusted to match the UN totals. We will use the adjusted grids for this tutorial. Expand and double-click on the glds90ag.asc layer to load it in QGIS. The layer doesn’t have a CRS defined, and since the grids are in lat/long, choose EPSG:4326 as the coordinate reference system.
Each pixel in the raster has a value assigned. This value is the population density for that grid. Click on ‘Identify Features’ button to select the tool and click anywhere on the raster to see the value of that pixel.
You must have noticed that the pixel values differ from place to place, but our raster is just plain grey everywhere. To better visualize the pattern of population density, we would need to style it. Double-click on the layer name in the TOC to bring up the Layer Properties dialog. You could also right-click on the layer name and select Properties. Under the ‘Style’ tab, change the ‘Color map’ to Pseudocolor. Click OK.
Now you will see a little more variation on the colors and can see how the population density is distributed.
Now locate and load the year 2000 grid glds90ag.asc by double-clicking or dragging the layer from the browser.
You can apply the same Pseudocolor style to this layer as well.
For our analysis, we would like to find areas with largest population change between 1990 and 2000. The way to accomplish this is by finding the difference between each grid’s pixel value in both the layers. Select Raster → Raster calculator.
In the ‘Raster bands’ section, you can select the layer by double-clicking on them. The bands are named after the raster name followed by @ and band number. Since each of our rasters have only 1 band, you will see only 1 entry per raster. The raster calculator can apply mathematical operations on the raster pixels.In this case we want to enter a simple formula to subtract the 1990 population density from 2000. Enter “glds00ag@1 - glds90ag@1” as the formula. Name your output layer as ‘density_change’ and check the box next to ‘Add result to project’. Click Ok.
Once the operation is complete, you will see the new layer load in the TOC.
Right-click on the layer and select ‘Properties’. Under the Style tab, select Custom min/max values. We want to style the layer using the actual min/max values. In the section ‘Load min / max values from band’, select Actual and click Load. Change the ‘Contrast enhancement’ to ‘Stretch to MinMax’. Click Ok.

Now the layer will be styled so the minimum values are black and the maximum values are white. The values in-between are shades of grey.
This visualization is useful, but we can create a much more informative output. Go back to the Properties menu. Choose Colormap as the ‘Color map’ in Single band properties.

We want to style the layer so pixel values in certain ranges get the same color. Before we dive in to that, go to the Metadata tab and look at the properties of the raster. Notice that the No Data value. It is usually a large negative value that is reserved for areas of the raster where the pixels are empty. We want to keep that in mind when styling our layer. Also note the minimum and maximum values of this layer.
Go to the Colormap tab. Select ‘Color interpolation’ as Discrete. Click the ‘Add entry’ button 4 times to create 4 unique classes. Click on an entry to change the values. The way color map works is that all values lower than the value entered will be given the color of that entry. Since the minmum value in our raster is just above -3600, we choose -3600 as the first entry. This will be for the No Data values. Enter the values and Labels for other entries as below and click OK.

Now you will see a much more powerful visualization where you can see areas which has seen positive and negative population density changes.
Now let’s take this analysis one-step further and find areas with only ‘negative’ population density change. Open Raster calculator. Enter the expression as ‘density_change@1 < -10’. What this expression will do is set the value of the pixel to 1 is if matches the expression and 0 if it doesn't.  So we will get a raster with pixel value of 1 where there was negative change and 0 where there wasn't  Name the Output layer as ‘negative_change’ and check the box next to ‘Add result to project’. Click OK.

Once the layer is loaded, right-click and select Properties.
Since our layer has only 2 unique pixel values - 0 and 1, we can use Grayscale colormap. Load the min / max values and select ‘Stretch to Min/Max’ as the contrast stretch. Click OK.

Now you will see the areas of negative population density change in white.

Hope this tutorial gave you an insight into how raster analysis works and styling options available in QGIS to display rasters.