0% found this document useful (0 votes)
86 views37 pages

CHAPTER7

This document discusses working with raster data in QGIS. It covers loading raster data, creating a virtual raster catalog to combine multiple rasters, reprojecting and merging rasters, and changing raster symbology. Specifically, it discusses loading DEM raster data, examining its default grayscale style, and exploring options for contrast enhancement and setting the minimum and maximum values used for stretching pixel values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
86 views37 pages

CHAPTER7

This document discusses working with raster data in QGIS. It covers loading raster data, creating a virtual raster catalog to combine multiple rasters, reprojecting and merging rasters, and changing raster symbology. Specifically, it discusses loading DEM raster data, examining its default grayscale style, and exploring options for contrast enhancement and setting the minimum and maximum values used for stretching pixel values.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 37

CHAPTER

SEVEN

MODULE: RASTERS

We’ve used rasters for digitizing before, but raster data can also be used directly. In this module, you’ll see how it’s
done in QGIS.

7.1 Lesson: Working with Raster Data

Raster data is quite different from vector data. Vector data has discrete features with geometries constructed out
of vertices, and perhaps connected with lines and/or areas. Raster data, however, is like any image. Although it
may portray various properties of objects in the real world, these objects don’t exist as separate objects. Rather,
they are represented using pixels with different values.
During this module you are going to use raster data to supplement your existing GIS
analysis.
The goal for this lesson: To learn how to work with raster data in
QGIS.

7.1.1 Follow Along: Loading Raster


Data

Raster data can be loaded with the same methods we used for vector data. However we suggest to use the
Browser
Panel.
1. Open the Browser Panel and expand the exercise_data/raster folder.
2. Load all the data in this folder:
• 3320C_2010_314_RGB_LATLNG.tif
• 3320D_2010_315_RGB_LATLNG.tif
• 3420B_2010_328_RGB_LATLNG.tif
• 3420C_2010_327_RGB_LATLNG.tif
You should see the following
map:

219
QGIS Training Manual

There we have it - four aerial images covering our study


area.

7.1.2 Follow Along: Create a Virtual


Raster

Now as you can see from this, your solution layer lies across all four images. What this means is that you are
going to have to work with four rasters all the time. That’s not ideal. It would be better to have one file to work
with.
Luckily, QGIS allows you to do exactly this, and without needing to actually create a new raster file. You can
create a Virtual Raster. This is also often called a Catalog, which explains its function. It’s not really a new
raster. Rather, it is a way to organize your existing rasters into one catalog: one file for easy access.
To make a catalog we will use the Processing ►
Toolbox.
1. Open the Build virtual raster algorithm from the GDAL ► Raster miscellaneous;
2. In the dialog that appears, click on the … button next to the Input layers parameter and check all the layers
or use the Select All button;
3. Uncheck Place each input file into a separate band. Notice the text field below. What this dialog is
actually doing is that it is writing that text for you. It is a long command that QGIS is going to run.

Note: Keep in mind that you can copy and paste the text in the OSGeo Shell (Windows user) or Ter-
minal (Linux and OSX users) to run the command. You can also create a script for each GDAL
command. This is very handy when the procedure is taking a long time or when you want to schedule
specific tasks. Use the Help button to get more help on the syntax of the command.

4. Finally click on Run.

Note: As you know from the previous modules, Processing creates temporary layers by default. To save the file
click on the … button.

2 Chapter 7. Module:
QGIS Training

You can now remove the original four rasters from the Layers Panel and leave only the output virtual catalog
raster.

7.1. Lesson: Working with Raster 2


QGIS Training Manual

7.1.3 Transforming Raster Data

The above methods allow you to virtually merge datasets using a catalog, and to reproject them “on the fly”.
However, if you are setting up data that you’ll be using for quite a while, it may be more efficient to create new
rasters that are already merged and reprojected. This improves performance while using the rasters in a map, but
it may take some time to set up initially.

Reprojecting rasters

Open Warp (reproject) from GDAL ► Raster


projections.
You can also reproject virtual rasters (catalogs), enable multithreaded processing, and
more.

2 Chapter 7. Module:
QGIS Training

Merging rasters

If you need to create a new raster layer and save it to disk you can use the merge algorithm.

Note: Depending on how many raster files you are merging and their resolution, the new raster file created can
be really big. Consider instead to create a raster catalog as described in the Create a Virtual Raster section.

1. Click on the Merge algorithm from the GDAL ► Raster miscellaneous menu.
2. As we did for the Create a Virtual raster, use the … button to choose which layers you want to merge.

7.1. Lesson: Working with Raster 2


QGIS Training Manual

You can also specify a Virtual raster as input, and then all of the rasters that it consists of will be processed.
3. If you know the GDAL library, you can also add your own options by opening the Advanced parameters
menu.

7.1.4 In Conclusion

QGIS makes it easy to include raster data into your existing projects.

2 Chapter 7. Module:
QGIS Training

7.1.5 What’s Next?

Next, we’ll use raster data that isn’t aerial imagery, and see how symbolization is useful in the case of rasters as well.

7.2 Lesson: Changing Raster Symbology

Not all raster data are aerial photos. There are many other forms of raster data, and in many of those cases, it is
essential to symbolize the them so that they becomes properly visible and useful.
The goal for this lesson: To change the symbology for a raster layer.

7.2.1 Try Yourself

1. Use the Browser Panel to load srtm_41_19.tif, found under exercise_data/raster/SRTM/


2. Zoom to the extent of this layer by right-clicking on it in the Layers panel and selecting Zoom to Layer.
This dataset is a Digital Elevation Model (DEM). It is a map of the elevation (altitude) of the terrain, allowing
us to see where the mountains and valleys are, for example.
While each pixel of the dataset of the previous section contained color information, in a DEM, each pixel
contains elevation values.
Once the DEM is loaded, you will notice that it is a grayscale representation:

7.2. Lesson: Changing Raster 2


QGIS Training Manual

QGIS has automatically applied a stretch to the pixel values of the image for visualization purposes, and we will learn
more about how this works as we continue.

7.2.2 Follow Along: Changing Raster Layer Symbology

You have two different options to change the raster symbology:


1. Within the Layer Properties dialog, by right-clicking on the layer in the Layer tree and selecting the
Properties
option. Then switch to the Symbology tab

Open the Layer Styling panel


2. By clicking on the button right above the Layers panel (shortcut F7). This will
Symbology
open the Layer Styling panel, where you can switch to the tab.
Choose the method you prefer to work with.

7.2.3 Follow Along: Singleband gray

When you load a raster file, if it is not a photo image like the ones of the previous section, the default style is set
to a grayscale gradient.
Let’s explore some of the features of this renderer.

2 Chapter 7. Module:
QGIS Training

The default Color gradient is set to Black to white, meaning that low pixel values are black and while
high values are white. Try to invert this setting to White to black and see the results.
Very important is the Contrast enhancement parameter: by default it is set to Stretch to MinMax meaning
that the pixel values are stretched to the minimum and maximum values.
Look at the difference with the enhancement (left) and without (right):

7.2. Lesson: Changing Raster 2


QGIS Training Manual

But what are the minimum and maximum values that should be used for the stretch? The ones that are
currently under Min / Max Value Settings. There are many ways to calculate the minimum and maximum values
and use them for the stretch:
1. User Defined: you enter the Min and Max values manually
2. Cumulative count cut: this is useful when you have some extreme low or high values. It cuts the 2% (or
the value you choose) of these values
3. Min / max: the Real or Estimated minimum and maximum values of the raster
4. Mean +/- standard deviation: the values will be calculated according to the mean value and the
standard deviation

7.2.4 Follow Along: Singleband pseudocolor

Grayscales are not always great styles for raster layers. Let’s try to make the DEM more
colorful.
• Change the Render type to Singleband pseudocolor. If you don’t like the default colors loaded, select another
Color ramp
• Click the Classify button to generate a new color classification
• If it is not generated automatically click on the OK button to apply this classification to the DEM

2 Chapter 7. Module:
QGIS Training

You’ll see the raster looking like this:

7.2. Lesson: Changing Raster 2


QGIS Training Manual

This is an interesting way of looking at the DEM. You will now see that the values of the raster are again
properly displayed, going from blue for the lower areas to red for the higher ones.

7.2.5 Follow Along: Changing the transparency

Sometimes changing the transparency of the whole raster layer can help you to see other layers covered by the
raster itself and better understand the study area.
To change the transparency of the whole raster switch to the Transparency tab and use the slider of the Global Opacity
to lower the opacity:

2 Chapter 7. Module:
QGIS Training

More interesting is changing the transparency for some pixel values. For example in the raster we used you can
see a homogeneous color at the corners. To set these pixels as transparent, go to Custom Transparency Options in
the Transparency tab.

Add values manually


• By clicking on the button, you can add a range of values and set their transparency
per- centage

Add values from display


• For single values the button is more useful

Add values from display


• Click on the button. The dialog disappears, and you can interact with the map.
• Click on the homogeneous color in a corner of the DEM
• You will see that the transparency table will be filled with the clicked values:

7.2. Lesson: Changing Raster 2


QGIS Training Manual

• Click on OK to close the dialog and see the changes.

See? The corners are now 100% transparent.

2 Chapter 7. Module:
QGIS Training

7.2.6 In Conclusion

These are some the basic functions to get you started with raster symbology. QGIS also gives you many
other options, such as symbolizing a layer using paletted/unique values, representing different bands with
different colors in a multispectral image, or making an automatic hillshade effect (useful only with DEM raster
files).

7.2.7 Reference

The SRTM dataset was obtained from


http://srtm.csi.cgiar.org/

7.2.8 What’s Next?

Now that we can see our data displayed properly, let’s investigate how we can analyze it
further.

7.3 Lesson: Terrain Analysis

Certain types of rasters allow you to gain more insight into the terrain that they represent. Digital Elevation
Models (DEMs) are particularly useful in this regard. In this lesson you will use terrain analysis tools to find out
more about the study area for the proposed residential development from earlier.
The goal for this lesson: To use terrain analysis tools to derive more information about the
terrain.

7.3.1 Follow Along: Calculating a


Hillshade

We are going to use the same DEM layer as in the previous lesson. If you are starting this chapter from scratch,
use the Browser panel and load the raster/SRTM/srtm_41_19.tif.
The DEM layer shows you the elevation of the terrain, but it can sometimes seem a little abstract. It contains all
the 3D information about the terrain that you need, but it doesn’t look like a 3D object. To get a better
impression of the terrain, it is possible to calculate a hillshade, which is a raster that maps the terrain using light
and shadow to create a 3D-looking image.
We are going to use algorithms in the Raster ► Raster terrain analysis
menu.
1. Click on the Hillshade menu
2. The algorithm allows you to specify the position of the light source: Azimuth has values from 0 (North)
through 90 (East), 180 (South) and 270 (West), while the Vertical angle sets how high the light source
is (0 to 90 degrees).
3. We will use the following values:
• Z factor at 1.0
• Azimuth (horizontal angle) at 300.0°
• Vertical angle at 40.0°

7.3. Lesson: Terrain 2


QGIS Training Manual

4. Save the file in a new folder exercise_data/raster_analysis/ with the name hillshade
5. Finally click on Run
You will now have a new layer called hillshade that looks like this:

2 Chapter 7. Module:
QGIS Training

That looks nice and 3D, but can we improve on this? On its own, the hillshade looks like a plaster cast. Can’t
we use it together with our other, more colorful rasters somehow? Of course we can, by using the hillshade as
an overlay.

7.3.2 Follow Along: Using a Hillshade as an


Overlay

A hillshade can provide very useful information about the sunlight at a given time of day. But it can also be
used for aesthetic purposes, to make the map look better. The key to this is setting the hillshade to being mostly
transparent.
1. Change the symbology of the original srtm_41_19 layer to use the Pseudocolor scheme as in the
previous exercise
2. Hide all the layers except the srtm_41_19 and hillshade layers
3. Click and drag the srtm_41_19 to be beneath the hillshade layer in the Layers panel
4. Set the hillshade layer to be transparent by clicking on the Transparency tab in the layer properties
5. Set the Global opacity to 50%.
You’ll get a result like this:

6. Switch the hillshade layer off and back on in the Layers panel to see the difference it makes.
Using a hillshade in this way, it’s possible to enhance the topography of the landscape. If the effect doesn’t
seem strong enough to you, you can change the transparency of the hillshade layer; but of course, the brighter
the hillshade becomes, the dimmer the colors behind it will be. You will need to find a balance that works for

7.3. Lesson: Terrain 2


QGIS Training
you.
Remember to save the project when you are
done.

7.3. Lesson: Terrain 2


QGIS Training Manual

7.3.3 Follow Along: Finding the best areas

Think back to the estate agent problem, which we last addressed in the Vector Analysis lesson. Let us imagine
that the buyers now wish to purchase a building and build a smaller cottage on the property. In the Southern
Hemisphere, we know that an ideal plot for development needs to have areas on it that:
• are north-facing
• with a slope of less than 5 degrees
• But if the slope is less than 2 degrees, then the aspect doesn’t
matter. Let’s find the best areas for them.

7.3.4 Follow Along: Calculating the


Slope

Slope informs about how steep the terrain is. If, for example, you want to build houses on the land there, then you
need land that is relatively flat.
To calculate the slope, you need to use the Slope algorithm of the Processing ► Raster terrain
analysis.
1. Open the algorithm
2. Choose srtm_41_19 as the Elevation layer
3. Keep the Z factor at 1.0
4. Save the output as a file with the name slope in the same folder as the hillshade
5. Click on Run
Now you’ll see the slope of the terrain, each pixel holding the corresponding slope value. Black pixels show flat
terrain and white pixels, steep terrain:

2 Chapter 7. Module:
QGIS Training

7.3.5 Try Yourself Calculating the aspect

Aspect is the compass direction that the slope of the terrain faces. An aspect of 0 means that the slope is North-
facing, 90 East-facing, 180 South-facing, and 270 West-facing.
Since this study is taking place in the Southern Hemisphere, properties should ideally be built on a north-facing
slope so that they can remain in the sunlight.
Use the Aspect algorithm of the Processing ► Raster terrain analysis to get the aspect layer saved along with
the
slope.
Check your results

7.3.6 Follow Along: Finding the north-facing aspect

Now, you have rasters showing you the slope as well as the aspect, but you have no way of knowing where ideal
conditions are satisfied at once. How could this analysis be done?
The answer lies with the Raster calculator.
QGIS has different raster calculators
available:
• Raster ► Raster Calculator
• In processing:

7.3. Lesson: Terrain 2


QGIS Training
– Raster Analysis ► Raster calculator

7.3. Lesson: Terrain 2


QGIS Training Manual

– GDAL ► Raster miscellaneous ► Raster calculator


– SAGA ► Raster calculus ► Raster calculator
Each tool is leading to the same results, but the syntax may be slightly different and the availability of operators may
vary.
We will use Raster Analysis ► Raster calculator in the Processing Toolbox
1. Open the tool by double clicking on it.
• The upper left part of the dialog lists all the loaded raster layers as name@N, where name is the
name of the layer and N is the band.
• In the upper right part you will see a lot of different operators. Stop for a moment to think that a
raster is an image. You should see it as a 2D matrix filled with numbers.
2. North is at 0 (zero) degrees, so for the terrain to face north, its aspect needs to be greater than 270 degrees
or less than 90 degrees. Therefore the formula is:
aspect@1 <= 90 OR aspect@1 >= 270

3. Now you have to set up the raster details, like the cell size, extent and CRS. This can be done manually or
it can be automatically set by choosing a Reference layer. Choose this last option by clicking on the
… button next to the Reference layer(s) parameter.
4. In the dialog, choose the aspect layer, because we want to obtain a layer with the same resolution.
5. Save the layer as aspect_north.
The dialog should look like:

2 Chapter 7. Module:
QGIS Training

6. Finally click on
Run. Your result will be
this:

The output values are 0 or 1. What does it mean? For each pixel in the raster, the formula we wrote returns
whether it matches the conditions or not. Therefore the final result will be False (0) and True (1).

7.3.7 Try Yourself More criteria

Now that you have done the aspect, create two new layers from the DEM.
• The first shall identify areas where the slope is less than or equal to 2 degrees
• The second is similar, but the slope should be less than or equal to 5 degrees.
• Save them under exercise_data/raster_analysis as slope_lte2.tif and
slope_lte5. tif.
Check your results

7.3. Lesson: Terrain 2


QGIS Training Manual

7.3.8 Follow Along: Combining Raster Analysis Results

Now you have generated three raster layers from the DEM:
• aspect_north: terrain facing north
• slope_lte2: slope equal to or below 2 degrees
• slope_lte5: slope equal to or below 5 degrees
Where the condition is met, the pixel value is 1. Elsewhere, it is 0. Therefore, if you multiply these rasters, the
pixels that have a value of 1 for all of them will get a value of 1 (the rest will get 0).
The conditions to be met are:
• at or below 5 degrees of slope, the terrain must face north
• at or below 2 degrees of slope, the direction that the terrain faces does not matter.
Therefore, you need to find areas where the slope is at or below five degrees AND the terrain is facing north, OR
the slope is at or below 2 degrees. Such terrain would be suitable for development.
To calculate the areas that satisfy these criteria:
1. Open the Raster calculator again
2. Use this expression in Expression:

( aspect_north@1 = 1 AND slope_lte5@1 = 1 ) OR slope_lte2@1 = 1

3. Set the Reference layer(s) parameter to aspect_north (it does not matter if you choose another -
they have all been calculated from srtm_41_19)
4. Save the output under exercise_data/raster_analysis/ as all_conditions.tif
5. Click Run
The result:

2 Chapter 7. Module:
QGIS Training

Hint: The previous steps could have been simplified using the following command:

((aspect@1 <= 90 OR aspect@1 >= 270) AND slope@1 <= 5) OR slope@1 <= 2

7.3.9 Follow Along: Simplifying the


Raster

As you can see from the image above, the combined analysis has left us with many, very small areas where
the conditions are met (in white). But these aren’t really useful for our analysis, since they are too small to build
anything on. Let us get rid of all these tiny unusable areas.
1. Open the Sieve tool (GDAL ► Raster Analysis in the Processing Toolbox)
2. Set the Input file to all_conditions, and the Sieved to all_conditions_sieve.tif (under
exercise_data/raster_analysis/).
3. Set the Threshold to 8 (minimum eight contiguous pixels), and check Use 8-connectedness.

7.3. Lesson: Terrain 2


QGIS Training Manual

Once processing is done, the new layer will be loaded.

2 Chapter 7. Module:
QGIS Training

What is going on? The answer lies in the new raster file’s metadata.
4. View the metadata under the Information tab of the Layer Properties dialog. Look the STATIS-
TICS_MINIMUM value:

7.3. Lesson: Terrain 2


QGIS Training Manual

This raster, like the one it is derived from, should only feature the values 1 and 0, but it has also a very
large negative number. Investigation of the data shows that this number acts as a null value. Since we are
only after areas that weren’t filtered out, let us set these null values to zero.
5. Open the Raster Calculator, and build this expression:

(all_conditions_sieve@1 <= 0) = 0

This will maintain all non-negative values, and set the negative numbers to zero, leaving all the areas with
value
1 intact.
6. Save the output under exercise_data/raster_analysis/ as
all_conditions_simple. tif.
Your output looks like
this:

This is what was expected: a simplified version of the earlier results. Remember that if the results you get from
a tool aren’t what you expected, viewing the metadata (and vector attributes, if applicable) can prove essential to
solving the problem.

7.3.10 Follow Along: Reclassifying the


Raster

We have used the Raster calculator to do calculations on raster layers. There is another powerful tool that we can
use to extract information from existing layers.
Back to the aspect layer. We know now that it has numerical values within a range from 0 through 360. What
we want to do is to reclassify this layer to other discrete values (from 1 to 4), depending on the aspect:
• 1 = North (from 0 to 45 and from 315 to 360);

2 Chapter 7. Module:
QGIS Training Manual
• 2 = East (from 45 to 135)
• 3 = South (from 135 to 225)

2 Chapter 7. Module:
QGIS Training

• 4 = West (from 225 to 315)


This operation can be achieved with the raster calculator, but the formula would become very very large.
The alternative tool is the Reclassify by table tool in Raster analysis in the Processing Toolbox.
1. Open the tool
2. Choose aspect as the Input raster layer
3. Click on the … of Reclassification table. A table-like dialog will pop up, where you can choose the
minimum, maximum and new values for each class.
4. Click on the Add row button and add 5 rows. Fill in each row as the following picture and click OK:

The method used by the algorithm to treat the threshold values of each class is defined by the Range
boundaries.
5. Save the layer as reclassified.tif in the exercise_data/raster_analysis/ folder

7.3. Lesson: Terrain 2


QGIS Training Manual

6. Click on Run
If you compare the native aspect layer with the reclassified one, there are not big differences. But by looking at
the legend, you can see that the values go from 1 to 4.
Let us give this layer a better style.
1. Open the Layer Styling panel
2. Choose Paletted/Unique values, instead of Singleband gray

2 Chapter 7. Module:
QGIS Training

3. Click on the Classify button to automatically fetch the values and assign them random colors:

The output should look like this (you can have different colors given that they have been randomly generated):

7.3. Lesson: Terrain 2


QGIS Training Manual

With this reclassification and the paletted style applied to the layer, you can immediately differentiate the aspect
areas.

7.3.11 Follow Along: Querying the raster

Unlike vector layers, raster layers don’t have an attribute table. Each pixel contains one or more numerical values
(singleband or multiband rasters).
All the raster layers we used in this exercise consist of just one band. Depending on the layer, pixel values may
represent elevation, aspect or slope values.

Identify Features
How can we query the raster layer to get the value of a pixel? We can use the button!
1. Select the tool from the Attributes toolbar.
2. Click on a random location of the srtm_41_19 layer. Identify Results will appear with the value of the
band at the clicked location:

2 Chapter 7. Module:
QGIS Training

3. You can change the output of the Identify Results panel from the current tree mode to a table one
by selecting Table in the View menu at the bottom of the panel:

Clicking each pixel to get the value of the raster could become annoying after a while. We can use the Value Tool
plugin to solve this problem.
1. Go to Plugins ► Manage/Install Plugins…
2. In the All tab, type value t in the search box
3. Select the Value Tool plugin, press Install Plugin and then Close the dialog.

7.3. Lesson: Terrain 2


QGIS Training Manual

The new Value Tool panel will appear.

Tip: If you close the panel you can reopen it by enabling it in the View ► Panels ► Value Tool or by
clicking on the icon in the toolbar.

4. To use the plugin just check the Enable checkbox and be sure that the srtm_41_19 layer is active
(checked) in the Layers panel.
5. Move the cursor over the map to see the value of the pixels.

2 Chapter 7. Module:
QGIS Training

6. But there is more. The Value Tool plugin allows you to query all the active raster layers in the Layers
panel.
Set the aspect and slope layers active again and hover the mouse on the map:

7.3.12 In Conclusion

You’ve seen how to derive all kinds of analysis products from a DEM. These include hillshade, slope and
aspect calculations. You’ve also seen how to use the raster calculator to further analyze and combine these results.
Finally you learned how to reclassify a layer and how to query the results.

7.3.13 What’s Next?

Now you have two analyses: the vector analysis which shows you the potentially suitable plots, and the raster
analysis that shows you the potentially suitable terrain. How can these be combined to arrive at a final result for this
problem? That’s the topic for the next lesson, starting in the next module.

7.3. Lesson: Terrain 2


QGIS Training Manual

2 Chapter 7. Module:

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy