Warning
A new version of this tutorial is available at Automating Complex Workflows using Processing Modeler (QGIS3)
GIS Workflows typically involve many steps - with each step generating intermediate output that is used by the next step. If you change the input data or want to tweak a parameter, you will need to run through the entire process again manually. Fortunately, QGIS has a graphical modeler built-in that can help you define your workflow and run it with a single invocation. You can also run these workflows as a batch over a large number of inputs.
This tutorial shows how to build a model to extract areas for a particular class from a classified land use raster.
We will use the Global Mosaics of the standard MODIS land cover type data product from Global Land Cover Facility (GLCF) as an example.
Warning
As of 31 December 2018, GLCF has shut down its services and the files needed for this tutorial are no longer accessible.
You may directly download an archival copy of both the datasets from the links below if you wish to work on this tutorial:
Data Source [GLCF_MODIS]
Our workflow for this exercise will have the following steps.
Majority Filter
algorithm to the input landcover raster. This
will reduce noise in our output by eliminating isolated pixels.The following steps outline the process to code the above process into a model and run it on the downloaded datasets.
Input
as
the Parameter name and mark Yes
to Required.
Click OK.Majority filter
algorithm. Switch to the
Algorithm tab from the bottom-left corner. Search for the
algorithm and you will find it listed under SAGA provider. Drag
it to the canvas.Note
If you do not see this algorithm or any of the subsequent algorithms mentioned in thi tutorial, you may be using the Simplified Interface of the Processing Toolbox. Switch to the Advanced Interface by using the dropdown at the bottom of the Processing Toolbox in the main QGIS window.
Polygonize (raster to vector)
algorithm and drag it to the canvas.Note
The boxes can be moved and arranged by clicking on it and dragging it while holding the left mouse button. You can also use the scroll-wheel to zoom in and out in the model canvas.
Extract by
attribute
algorithm and drag it the canvas.DN
as the
Selection attribute and 12
as the value. As the
output of this operation will be the final result, we need to name the
output. Enter vectorized class
as the Output.vectorize
and Group
name as raster
. Click the Save button.vectorize
and click Save.LC_hd_global_2001.tif.gz
file and click
Open. Once the raster is loaded, go to
.LC_hd_global_2001
as the Input and click
Run.vectorized_class
will be added to
QGIS. Let’s improve the model a little bit. Right-click on the
vectorize
model and select Edit model.12
as the class value. Instead, we
can specify it as a input parameter which the user can change. To add this,
switch to the Inputs tab and drag the + String to
the model.Class
. Enter 12
as the
Default value.Extract by attribute
algorithm to use this input
instead of the hard-coded value. Click the Edit button next to
the Extract by attribute box.Class
. Click
OK.16
as the Class value and click
Run.LC_hd_global_2012.tif.gz
file by going to
. Click the
vectorize` model from the Processing Toolbox panel.LC_hd_global_2012
layer as the Input and click
Run.vectorize
model and select
Edit model.Models can be a great timesaver and allow you to write your workflow once and
run it multiple times. You can even share your model with other users. The
model files are saved in the .qgis2
directory. You can send the .model
file to another user who can copy it to the appropriate directory on their
computer and it will appear in the Processing toolbox. The models
directory location will depend on the platform as follows: (Replace
username
with your login name)
Windows
c:\Users\username\.qgis2\processing\models\
Mac
/Users/username/.qgis2/processing/models/
Linux
/home/username/.qgis2/processing/models/
This work is licensed under a Creative Commons Attribution 4.0 International License