3. Creating a simple model

In this exercise we will be creating a simple model with /docs/user_manual/processing/modeler. In this exercise you will work with Model Builder yourself. This is an important tool since you will use it often in Chapter 3 of this practical. Here you will automate a series of operations so that they can be executed with just one click. It will also allow you to make changes to parameter settings and then quickly re-run the model. This allows you to compare changes in your model output and link them to the changes that you made earlier. In this exercise, you will create a model that determines the Topographic Position Index (TPI) of a DEM. The TPI is given by the equation:

\[TPI=DEM-\mathrm{focalmean}\left(DEM,annulus,r_i,r_o\right)\]

The TPI can be used to determine whether a location is on a ridge or valley. This can be useful when you are creating your own adaptation strategies!

As you can see in Fig. 3.1, the TPI can differ, based on the radius. This is useful so you can distinguish between large and small features.

https://landscapearchaeology.org/figures/TPI_diagram.png

Fig. 3.1 Topographic index depends on Neighborhood size. (https://landscapearchaeology.org/2019/tpi/)

3.1. basic implementing the model with grassLogo r.neighbors

Since saga is a bit difficult and has different versions, the preferred way to implement the TPI is to use grassLogor.neighbors. This is a focal statistics algorithm: it takes all cells in a neighborhood around a cell and uses them to calculate the value for a cell.

3.1.1. basic Follow Along: Adding the annulus mask script to the toolbox

The grassLogor.neighbors algorithm does not support an annulus as area by default. However, it provides the possibility to use a mask file. That is a .txt file that for \(r_i=1,r_o=3\) looks something like this:

0 0 0 1 0 0 0
0 1 1 1 1 1 0
0 1 1 1 1 1 0
1 1 1 0 1 1 1
0 1 1 1 1 1 0
0 1 1 1 1 1 0
0 0 0 1 0 0 0

Where all \(1\)’s are taken into account. The script that we need creates this file for us. In Follow Along: Creating a script for the mask file we will be making this script ourselves. You can also directly copy it from the convenience scripts.

  1. extract the convenience scripts to a location of your choice.

  2. In the processing toolbox, click the pythonFileAdd script to toolbox… and select the annulus_r_neighbors.py file.

    The script should now be added to your toolbox.

    the toolbox with annulus mask for r.neighbors added.

3.1.2. basic Follow Along: Getting the model inputs

  1. In the Processing toolbox, select processingModel► Create new Model

    ../../_images/create_model.png

    The following window will show:

    ../../_images/modeler_canvas.png

    Fig. 3.2 The canvas. Enter 01_update_landuse as name and pre-processing as group.

    Our TPI model needs three inputs:

    • A Digital Elevation Model (DEM) raster

    • The inner radius for the zonal statistics

    • The outer radius for the zonal statistics

  2. In the Inputs pane, scroll down until you see signPlusRaster Layer. drag it into the model view. The following dialog will show:

    ../../_images/raster_input_dialog.png

    Enter DEM as name and press OK.

    Note

    The checkboxes define how the inputs are shown when you open a model:

    • Your created model will not run without checkboxMandatory inputs

    • uncheckedadvanced inputs will be under a drop-down menu

  3. Drag a signPlusNumber into the modeler. Give it a:

    • Description: Outer radius

    • Number type: Integer

    • Minimum value: 1

    • Default value: 3

    Your modeler should now look like this:

    ../../_images/model_inputs.png

    Fig. 3.3 Model with only inputs

    Tip

    Snapping You can enable snapping by View ► Enable snapping

  4. |TY| Add another input for the inner radius.

  5. Name the model Topographic Position Index (TPI) and fileSave Save it with a logical name such as tpi.model3

  6. close the modeler for now

3.1.3. basic Adding the processes to the TPI model

Now we have all the processes we need, it is time to add them to our model!

  1. Click Algorithms (highlighted in Fig. 3.3).

  2. Search for your script and drag it into the modeler. Fill it in like this:

    ../../_images/grass_script_prompt.png

    You can change the type of variable by clicking the highlighted processingModel. You can then choose from:

    • fieldInteger Value A value

    • processingModel Input An input to your model (signPlus)

    • processingAlgorithm Algorithm output The output of an algorithm

    • expression Pre-calculated value An expression that will be evaluated when you run the model

  3. Next, drag in the grassLogor.neighbors algorithm. The mask option we are using is an advanced parameter. Click the Show avancedparameters button. Then fill it in like this:

    • Using model input: processingModelDEM

    • Neighborhood operation [optional]: fieldIntegeraverage

    • Neighborhood size (must be odd) [optional]: processingModelouter radius

    • File containing weights [optional]: processingAlgorithm"annular mask" from algorithm "annulus mask for r.neighbors"

    press OK

Note

For some reason, the logo native raster calculator does not work well with output of saga Saga or grassLogo Grass algorithms. Please use the gdal gdalrastercalculator for this exercise. Instructions are still added, because you may find it more intuitive to use than the GDAL version later on in the manual. However, I used the gdal gdalrastercalculator for everything.

  1. Drag the gdalgdalrastercalculator into the view. Fill in the dialog as follows:

    • Input layer A: processingModelDEM

    • Number of raster band for A: 1 This is for multi-band rasters. Since our raster only has one band, we want that to be \(1\).

    • Input layer B: processingAlgorithm"neighbors" from algorithm "r.neighbors"

    • Number of raster band for B: 1

    • Calculation in gdalnumeric syntax: A-B

    • modelOutputCalculated: TPI

    Your model should now look like this:

    ../../_images/grass_model_final.png
  2. Run your model with \(r_i=62,r_o=67\). Your output should look like this:

../../_images/grass_tpi_62_67.png

3.2. basic Implementing the algorithm with saga Focal statistics

Warning

The following section was written for win QGIS with saga SAGA 7.8. This version has different algorithms available than nix osx saga version 7.3. In that case, use the grassLogo :r.neighbors algorithm.

  1. In the Processing toolbox, select processingModel► Create new Model

    ../../_images/create_model.png

    The following window will show:

    ../../_images/modeler_canvas.png

    Our TPI model needs three inputs:

    • A Digital Elevation Model (DEM) raster

    • The inner radius for the zonal statistics

    • The outer radius for the zonal statistics

  2. In the Inputs pane, scroll down until you see signPlusRaster Layer. drag it into the model view. The following dialog will show:

    ../../_images/raster_input_dialog.png

    Enter DEM as name and press OK.

    Note

    The checkboxes define how the inputs are shown when you open a model:

    • Your created model will not run without checkboxMandatory inputs

    • uncheckedadvanced inputs will be under a drop-down menu

  3. Drag a signPlusNumber into the modeler. Give it a:

    • Description: Outer radius

    • Number type: Integer

    • Munimum value: 1

    • Default value: 3

    Your modeler should now look like this:

    ../../_images/model_inputs.png

    Fig. 3.4 Model with only inputs

    Tip

    Snapping You can enable snapping by View ► Enable snapping

  4. Now, we are going to include our first algorithm.

    1. Click Algorithms (highlighted in Fig. 3.4).

    2. Search for sagaFocal Statistics, drag it into the view and fill in the pop-up window as follows:

      • Under Grid, press the fieldInteger drop-down and select processingModelModel Input. It should be on DEM already since this is the only raster type model input.

      • Include Center Cell: fieldIntegerNo

      • Kernel Type: fieldInteger[1] Circle

      • Radius: processingModelOuter radius

      • the rest on default settings

    3. Press OK

      Your model should now look like this (with some rearranging):

      ../../_images/model_focal_statistics.png

Note

For some reason, the logo native raster calculator does not work well with output of saga Saga algorithms. Please use the gdal gdalrastercalculator for this exercise. Instructions are still added, because you may find it more intuitive to use than the GDAL later on in the manual

  1. Drag the gdalgdalrastercalculator into the view. Fill in the dialog as follows:

    • Input layer A: processingModelDEM

    • Number of raster band for A: 1 This is for multi-band rasters. Since our raster only has one band, we want that.

    • Input layer B: processingAlgorithm"Mean value" from algorithm "Focal Statistics"

    • Number of raster band for B: 1

    • Calculation in gdalnumeric syntax: A-B

    • modelOutputCalculated: TPI

  1. Press OK to add it to the model. It should now look like this:

    ../../_images/model_full.png
  2. Before we can save our model, we have to give it a name. Below Model Properties, give it the Name Topographic Position Index (TPI)

  3. Save your model by pressing the fileSave icon or Ctrl+S. Give it a descriptive name.

3.3. Running the model

Note

Currently (Oct 2021), layers from a GeoPackage cannot be selected as raster inputs in the Graphical Modeler. See the related Feature request and a Possible workaround . However, we will be working around this by loading our data into the project first.

  1. Load the Hadocha_dem layer into your map if it isn’t there yet.

  2. Now we have created the model, it is time to run it! There are two ways to do so:

    1. From within the Graphical Modeler:

      Press the play button or F5.

    2. From the Processing Toolbox:

      Notice that there is a new drop-down menu labeled processingModelModels. There, your model named processingModelTopographic Position Index is shown. Run it like any other tool!

  3. Either way, now select Hadocha_DEM as DEM. and outer radius 3. Run the model and your output should look like this:

    ../../_images/TPI.png

3.4. In Conclusion Wrapping up

Now, you have learned how to use the graphical modeler and to calculate the Topographic Position Index. Both are very useful. We will excessively use the Graphical Modeler later for the MMF erosion model, and you could use the TPI for determining where to apply specific measures.