Apply a Mask to a Land Cover Classification in QGIS

An updated tutorial is available here . This post is a brief tutorial about how to mask a classification raster in QGIS using the Orfeo Toolbox provided by the SEXTANTE plugin.
The masking process is useful to clip a classification raster to a certain study area, as for example a municipal boundary. Obviously, this masking process can be applied to any raster file.
It is worth mentioning the QGIS tool Clipper (Menu Raster > Extraction > Clipper that is based on gdalwarp), which allows for the masking of rasters. However, in this tutorial I illustrate some SEXTANTE functions, which can also be used in python scripts for automatic processing.
At the end of this post you can find the video of this tutorial.

We are going to create a mask raster from a shapefile (here is the shapefile used in this tutorial), and consequently to apply the mask to a classification raster. You can download the classification used in this tutorial from here, which is the result of my previous post about the classification of a Landsat 8 image available from the U.S. Geological Survey. 

  1. Load the classification and the mask shapefile in QGIS
    • Open the classification and the shapefile; the .qml files, included within the sample data, allow QGIS to load layers with the proper styles.

  1. Convert the mask shapefile from vector to raster
    The mask we are going to create is a binary raster whose pixel values are 0 if located outside the shapefile boundary, or 1 if located inside the boundary.
    • Open the SEXTANTE Toolbox (from the menu Analysis click  Sextante toolbox) and double click Orfeo Toolbox > Vector Data Manipulation > Rasterization;
    • Under Input vector dataset select the path to the mask shapefile;
    • Under Input reference image select the classification image loaded in QGIS;
    • Leave the default value for all the other parameters, except the Foreground value that we need to set to 1, and click OK;
    • After a while the mask will be loaded in QGIS; changing the layer style we can see that the boundary shape is converted to raster, and pixels have values 1 or 0.

The raster we have created is a temporary file. If you want to save the mask raster for future analyses, simply select where to save it under Output image.

  1. Apply the mask to the classification
    • Open the SEXTANTE Toolbox and double click Orfeo Toolbox > Miscellaneous > Band Math;
    • Under Input image list select classification and the mask raster previously created (the name thereof begins with otbrasterization);
    • Type the following Expression, which is the multiplication of the two rasters:
    • Under Output image select where to save the masked classification, and click OK; when the process is completed, the masked raster will load in QGIS; 
    • Open the properties of the masked classification, by double clicking the layer name in the Layers panel;
    • Select the Colormap tab and click the button Load Style ... ; select the file  classification.qml inside the classification directory;
    • Select Exact as Color interpolation; click the entry 0 Unclassified in the list and click the button Delete entry (this way, pixels having a value of 0 will be transparent); click OK.

The pixels of the masked raster have the same values of the original classification inside the study area boundary, while have a value of 0 outside the boundary.
Therefore, it is possible to calculate the land cover statistics only for the study area. Also, the masking process is useful for the representation of raster data and for the creation of layout maps.
If you want to convert the classification raster to vector format, see my previous post here.

Update 07/08/2013: as alternative to the above procedure, the command SAGA > Shapes - Grid > Clip grid with polygon allows for the masking of rasters in one go.

The following is the video of this tutorial.

Newer posts Older posts