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.
A GUI tool for map algebra can be launched from or by
Raster map calculator from a main toolbar.
Important
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 )"
Note
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 (), 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 ().
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
Note
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
