This chapter covers how to prepare raster data for further processing with the GDAL menu tools and Processing Toolbox algorithms. There are specific considerations and tools for managing raster data. The topics that you will cover in this chapter are as follows:
Raster datasets often have hundreds or thousands of values. For an analysis, you may need to synthesize the data into meaningful categories. For example, elevation may be an important input in a habitat model for species X. However, you may only be interested in identifying several broad elevation thresholds that help to define the habitat. In the following example, you will use the elevation.tif
sample data. You will reclassify the elevation data into several categories: less than 2,000 meters, 2,000 to 2,500 meters, and greater than 2,500 meters. This will result in a raster with three values, one for each group of elevation values. The following steps outline how to use the r.recode GRASS tool (found in the Processing Toolbox) to accomplish this:
elevation.tif
and set the project's CRS to EPSG: 26912
.The Processing Toolbox is covered in more detail in Chapter 8, The Processing Toolbox.
recode
into the Processing Toolbox search bar and hit the Enter key. Double-click on the tool to open it.input_value_low:input_value_high:output_value_low:output_value_high input_value_low:input_value_high:output_value *:input_value:output_value input_value:*:output_value
2000
meters with a value of 1
in the output raster. The first asterisk is a wildcard for every value less than 2000
. The second line recodes the values greater than and equal to 2000
and less than 2500
as 2
in the output raster. The third line recodes all values greater than 2500
as 3
in the output raster:*:2000:1 2000:2500:2 2500:*:3
Elevation_rRecode_Rules.txt
.There is also a user developed plugin named Slicer that is very useful for reclassifying raster data. To duplicate the previously described reclassification of the elevation.tif
dataset, install the plugin and use the following steps:
0
for the first class.In addition to these options, there are ways to use GRASS to reclassify rasters. Simply choose the method that both meets your needs and that you feel most comfortable with.
There is a similar GRASS tool in the Processing Toolbox named r.reclass
. The r.reclass
tool is used when reclassifying integer and categorical rasters, while r.recode will reclassify floating-point and decimal value rasters. Both tools use the same format for the rules text file. More complete documentation for these tools can be found on the GRASS GIS help pages at http://grass.osgeo.org/grass65/manuals/r.reclass.html and http://grass.osgeo.org/grass65/manuals/r.recode.html.
The following figure shows the result of the reclassification. The original elevation raster with the original elevation values is on the left, and the reclassified raster with three values is on the right:
Raster datasets may have integer values or floating-point values with decimal points. The r.recode tool can also be used to convert raster datasets between these formats. The elevation.tif
sample data is a floating-point raster with values ranging from 1502.1
to 2898.49
meters above sea level.
To convert this raster to an integer raster, you will need to set up a rule text file with the following text:
1502.1:2898.49:1502:2898 (input_value_low:input_value_high:output_value_low: output_value_high)
Conversely, if you have an integer raster with values between 10
and 500
and want to convert it to floating point, you will need to set up a rule text file with this:
10:500:0.1:5.0
This will result in a raster with cell values ranging from 0.1
to 5.0
.
When an analysis requires that multiple raster datasets be combined or overlaid, their pixel resolutions should be equal. The spatial resolution or cell size of a raster can be increased or decreased by a process known as resampling. Although you can increase or decrease the resolution of a raster, it is considered a better practice to decrease the resolution of the finer datasets to match the resolution of the coarsest raster.
As an example, let's say you have two rasters: a 90-meter resolution elevation raster and a 30-meter vegetation raster. In this scenario, it would be best to resample the vegetation data to a 90-meter resolution to match the elevation data. This way, all the data will be matched accurately. Conversely, if you resample to match the finest resolution raster, you will introduce false accuracy. The elevation data has a 90-meter resolution because that was the smallest unit that could be differentiated from the neighboring pixels. If you increase the spatial resolution, each elevation pixel is converted to nine 30-meter pixels. You cannot assume that all nine resulting pixels actually have the same elevation value in the real world. It is more likely that only the center pixel would have the same value.
In QGIS, there are several tools that can be used to resample a raster. In this example, the GDAL Translate tool will be used. The resolution of the elevation.tif
sample data will be changed from 27.3526 meters to 100 meters. While the Translate tool can be found by navigating to Raster | Conversion | Translate, you will use the Processing Toolbox implementation of it here because it has better options for specifying the output pixel size. As you'll see in the following steps, this tool can be used for a variety of raster conversions during the resampling process:
The following figure shows the result of raster resampling. The original elevation raster with 27.3526-meter pixel resolution is on the left and the resampled raster with 100-meter pixel resolution is on the right.
There are two additional tools that can be used to resample raster data, and both are found in the Processing Toolbox.
Under GRASS commands | Raster tools there is the r.resample tool. Under SAGA | Grid-Tools there is the Resampling tool. Both these tools have similar options to the GDAL Translate tool and are included with most installations of QGIS.