Unit 05 - Simple computation

Computation of NDVI is a well known “discipline” in remote sensing. It is a simple task which requires basic knowledge of map algebra. The key GRASS module related to map algebra is r.mapcalc.

NDVI = (NIR - VIS) / (NIR + VIS)

To compute NDVI, the red (VIS) and near-infrared (NIR) channels are required. In the case of Sentinel-2, it refers to 4th and 8th band. First of bands has been already imported into GRASS in Unit 03. 8th band can be imported similarly from sentinel/sample directory.

A GUI tool for map algebra can be launched from Raster ‣ Raster map calculator or by grass-raster-calculator Raster map calculator from a main toolbar.


Be aware that r.mapcalc as other raster processing modules are sensitive to computational region which must to be set before any computation, see Computational region section for details.


Fig. 41 Compute NDVI using GUI raster map calculator by Expression. The result is defined by Output section.

Corresponding command for CLI (map names shorten):

r.mapcalc expression="ndvi = float(B08_10m - B04_10m) / ( B08_10m + B04_10m )"


Because all input raster maps are CELL type (integer) at least one sub-result must be converted to floating-point (here by float() function). Otherwise result would be also CELL map (integer: -1, 0, 1).


Fig. 42 NDVI computed by r.mapcalc map algebra module (Oslo region in red color).

Result is not perfect:

  • default color table viridis is not suitable for NDVI values
  • NDVI has been computed also in cloudy areas

The first issue can be easily fixed by r.colors (Raster ‣ Manage colors ‣ Color tables), see Color table section. The second issue requires setting a mask to ignore cloudy areas in computation. This operation can be done by r.mask module (Raster ‣ Mask).

There is also specialized module for computing various vegetation indices including NDVI - i.vi. This module makes NDVI computing even simpler, and it also solves first issue since the module sets ndvi color table automatically.

At first let’s define a mask based on cloud mask vector map (note that mask is created only within current computation region).

r.mask -i vector=oslo_clouds


Cloud mask provided by Sentinel products is not perfect, but it is a good starting point for us.

Then let’s recompute ndvi map with i.vi (map names shorten):

i.vi --overwrite red=B04_10m output=ndvi viname=ndvi nir=B08_10m

We can also add simple legend to map display by grass-overlay-add Add map elements (Add raster legend) from Map Display toolbar.


Fig. 43 Final NDVI result with cloud mask and simple legend.