Unit 09 - Model tuning

Let’s improve our NDVI model created in Unit 08 - Modeler. Current model operates in a current computation region, it would be better to define region based on user input. Then NDVI would be computed only within user defined area.

NDVI values range from +1.0 to -1.0. Areas of barren rock, sand, or snow usually show very low NDVI values (for example, 0.1 or less). Sparse vegetation such as shrubs and grasslands or senescing crops may result in moderate NDVI values (approximately 0.2 to 0.5). High NDVI values (approximately 0.6 to 0.9) correspond to dense vegetation such as that found in temperate and tropical forests or crops at their peak growth stage. Let’s classify NDVI into 3 major classes:

  • Class 1: from -1.0 to 0.2
  • Class 2: from 0.2 to 0.6
  • Class 3: from 0.6 to 1.0

The desired output will be vector map with NDVI classes. Let’s also eliminate too small areas.

From GRASS perspective computation will be performed by several steps/modules:

  1. Erase cloud mask in input region (v.overlay: not operator)
  2. Set computation region based on modified input region (g.region)
  3. Set mask (r.mask)
  4. Compute NDVI values (i.vi)
  5. Reclassify NDVI values into classes (r.recode)
  6. Set nice color table for raster map classes (r.colors)
  7. Convert raster classes into vector areas (r.to.vect)
  8. Remove small areas (join them with adjacent areas by v.clean)

Overview of commands below:

v.overlay --overwrite ainput=oslo binput=MaskFeature operator=not output=region_mask
g.region --overwrite vector=region_mask align=L2A_T32VNM_20170705T105031_B04_10m
r.mask --overwrite vector=region_mask
i.vi --overwrite red=L2A_T32VNM_20170705T105031_B04_10m output=ndvi nir=L2A_T32VNM_20170705T105031_B08_10m
r.recode --overwrite input=ndvi output=ndvi_class rules=reclass.txt
r.colors map=ndvi_class rules=colors.txt
r.to.vect -s -v --overwrite input=ndvi_class output=ndvi_class type=area
v.clean --overwrite input=ndvi_class output=ndvi_vector tool=rmarea threshold=1600

New modules can be added to the existing model by grass-module-add Add command (GRASS module) to the model. Note that new commands are added to the end of a computation workflow which is not desired in this case. Commands (items in model terminology) can be reorder in Items tab.


Fig. 67 Reorder model items (commands). In this case move v.overlay up to the first position.


Be aware of correct computation region, don’t forget to align region bounds to input raster data (g.region with align option).

Reclassification of floating point raster maps can be done in GRASS by r.recode. Here is reclassification table for our case:


Beside predefined color tables r.colors (see Color table section) also allows to use user-defined color table by rules option. In our case color table can be quite simple:

1 grey
2 yellow
3 green


Reclassification and color table is recommended to store into files otherwise it can be lost when opening model next time: reclass.txt and colors.txt


Fig. 68 Extended model.

Sample model to download: ndvi-v2.gxm (note: don’t forget to fix path to reclass and colors file for r.recode and r.colors modules)


Our models have all parameters hard-coded, there is nothing which can be influenced by a user when launching the model.

In Graphical Modeler user input can be defined by two mechanisms:

  • parametrization of module options
  • using self-defined variables (ideal when more modules are sharing the same user input value)

Let’s start with parametrization of module options, it’s simple. We would like to change our model in order to provide the user ability to:

  • define own region area (option ainput in v.overlay)
  • set threshold for small areas (option threshold in v.clean)

For each command that we want to parameterize a properties dialog by double-click on command item must be open. Then desired option to be parameterized found and enabled by Parameterized in model checkbox below. That’s all.


Fig. 69 Parametrization of ainput option in v.overlay command.


Parameterized commands are highlighted in the model by bold border.

After pressing grass-execute Run model the model is not run automatically. Instead of that a GUI dialog is shown to allow user defining inputs.


Fig. 70 Model is run after defining user input parameters. Parameterized options are organized into tabs based on the modules.

After setting the input parameters the model can be Run.


Saved models can be run directly from Layer Manager File ‣ Run model without opening Graphical Model itself.

Let’s test our model with different settings…


Fig. 71 NDVI vector class without small area reduction.


Fig. 72 NDVI classes smaller than 2000m 2 (so 20 pixel) removed.

… and different region, eg. by buffering our current region area using v.buffer module.

v.buffer input=oslo output=oslo_5km distance=5000

Fig. 73 NDVI vector classes computed in 5km buffer around Oslo region.

Sample model to download: ndvi-v3.gxm