Unit 05 - Raster processing

Raster data processing is always limited to the current computational region.

Let’s demostrate raster processing on map algebra based computation of NDVI (Normalized difference vegetation index). The key GRASS module that allows a user to do the map algebra is r.mapcalc. A GUI tool for map algebra can be launched from Raster ‣ Raster map calculator or by grass-raster-calculator Raster map calculator from the main toolbar.

To compute NDVI, the red (VIS) and near-infrared (NIR) bands are required. In the case of Sentinel-2, it refers to 4th and 8th band. Missing Sentinel-2 8th band can be imported as explained in Unit 03 - Data Management from L2A_T32UPB_20170706T102021_B08_10m.jp2 file.

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

Fig. 36 GUI map algebra tool. Compute NDVI by Expression. Result of computation is defined by Output section.

Corresponding command (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 by float() function. Otherwise result would be also CELL map (integer: -1, 0, 1).


Fig. 37 NDVI computed by r.mapcalc map algebra module (Jena city in red color).

Result requires some improvements:

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

The first shortcoming can be easily solved by changing color table to ndvi as described in Color table section. The second requires setting a mask to ignore cloudy areas by the computation. Raster mask can be created by r.mask. Like other modules from the r.* family, it operates in the current computational region.

Let’s create raster mask from cloud mask vector map in inverse manner.


Fig. 38 Creating raster 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.


Fig. 39 Final NDVI result with cloud mask and simple legend added to map display by grass-overlay-add Add map elements (Add raster legend) from Map Display toolbar.

A mask can be deactivated r.mask -r command.