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.

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. The bands have been already imported into GRASS in Unit 03 - Data Management.

\[NDVI = (NIR - VIS) / (NIR + VIS)\]

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. 37 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. 38 NDVI computed by r.mapcalc map algebra module (Jena city 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).

A mask will defined base on cloud mask vector map in inverse manner. Note that mask as other raster processing is created only within the current computation region.


Fig. 39 Creating mask from an input vector map.

r.mask -i vector=MaskFeature


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

There is also specialized module for computing various vegetation indices including NDVI - i.vi. This module makes NDVI computing even simpler.

Let’s recompute ndvi map using i.vi (map names shorten):

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


Also a simple legend can be added to map display by grass-overlay-add Add map elements (Add raster legend) from Map Display toolbar.


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