- 1 What is the NDVI index?
- 2 Use R to calculate NDVI
- 3 R packages to calculate the NDVI
- 4 Analysis of the R code to calculate the NDVI
- 5 Interpretation of the NDVI calculated with R
How to calculate the NDVI index with R
In this article cartogeek.com will try to show how to calculate NDVI with R step by step. Calculation of NDVI index of vegetation through remote sensing It is very useful for multiple fields due to its multiple applications.
As we will see, the fact of calculating the vegetation index through theR programming languageIt is a great advantage due to the speed of the execution, as well as the simplicity of the language.
We will carry out the process based on a series of R commands that we will comment in detail. Also, for calculate the NDVI We will have to use multiple packages ( packages ) for this programming language. As a calculation base we will use satellite images from Sentinel-2.
Before that, we will give some theoretical brushes to put ourselves in context and understand the procedure used for the calculation of the NDVI index by scripts in R.
What is the NDVI index?
The NDVI is considered a vegetation index. A vegetation index is a unique value that quantifies the health or structure of the vegetation.
Mathematics associated with calculation of a vegetation index are derived from the physics of reflection and light absorption through spectral bands.
For example, healthy vegetation is known to reflect strong light in the NIR band ( near infrared, in English near Infrared) and much less in the visible part of the spectrum.
Of this mode, if a relationship is created between the light reflected in the NIR and the light reflected in the band of red, areas that potentially have healthy vegetation.
NDVI, vegetation and reflectance
The NDVI ( Normalized Difference Vegetation Index ) is a quantitative greening index ranging from -1 to 1, where -1 represents a minimum or non-existent greenery and 1 represents the maximum greenery.
This index is often used for an approximate measure of health, coverage, and phenology of the vegetation ( life cycle stage ) in large territorial areas.
Its use is also common in precision agriculture to estimate the health of plantations in certain plots.
The NDVI calculation it is made from the visible channel and the NIR reflected by the vegetation.
- Healthy vegetation ( image left ) absorbs most of the visible light, and reflects a large amount of near-infrared energy.
- Unhealthy or sparse vegetation ( image right ) reflects more visible light and less infrared light.
You can expand information in the NASA article about the NDVI, the calculation of this vegetation index and its applications. You can also find relevant information in the EOS website – Earth Observing System, from where to view and download satellite and NDVI images.
How to calculate the NDVI index with R,
Formula for calculating NDVI and interpreting
The formula for calculating the NDVI index is as follows:
- NIR is the reflectance of the Near Infrared ( b8 Sentinel, b5 in Landsat 8, b2 MODIS ).
- RED is the red reflectance ( b4 Sentinel, b4 in Landsat 8, b1 MODIS ).
- The range of output values will be between -1 and 1.
Use R to calculate NDVI
The language R is one of themost used programming languages in GISfor geoprocesses ( together with Python ) which has been booming for a few years due to its qualities.
R allows perform statistical and spatial calculations with both vector and raster layers comfortably using scripts, making use of different specific packages for this purpose.
The satellite image treatment with R It becomes a relatively easy task for users with certain knowledge. Under certain circumstances, the use of R may offer more advantages what drawbacks.
These advantages include, for example:
- the speed of calculation in geoprocesses and spatial analysis
- the ability to run geoprocessing tasks without an explicit graphical interface
- the ability to replicate the calculation task for multiple layers or images using the same script.
R packages to calculate the NDVI
In the following example we will calculate NDVI in an agricultural and forestry area typically Mediterranean. To perform such an exercise, we will need a series of R libraries or packages that allow us to read, calculate and visualize our results.
In this example we will use the libraries ( packages):
- RStoolbox, specific for the treatment of satellite images.
- raster, necessary for grid data processing.
- ggplot2, well known for its great versatility when viewing data.
- gridExtra, which will finally allow us to show two images in a single visualization.
Remember that if you have never used these libraries, you will first have to install them with the command install.packages ( “ library_name ” ). If you already have them installed, simply using the library ( ) command you will load the library for use. They are commented in the script shown below.
Analysis of the R code to calculate the NDVI
Notice that the code is simple. First, we use list.files ( ) to list all raster bands contained in our working folder, which we have previously configured with the setwd function.
Once listed, we stack these bands on a single RasterStack object, using the stack function of the raster library. For the NDVI calculation, We simply replicate the equation described at the beginning of the post, but with the Sentinel bands.
By last, what we do is create the “ plots ” representing the NDVI and the combination of false near infrared color – green – blue.
This representation is carried out with the ggR and ggRGB function, both contained in the RStoolbox library and which are fed directly from ggplot2 to display the results.. The grid.arrange function allows us to view both maps in one view.
Interpretation of the NDVI calculated with R
In the obtained result image, it is clearly perceived that the highest values of the index refer to cultivated fields and forests, while around 0 we find the urban fabric and, below, the water bodies.
However, we can also detect that crop fields are healthier ( more green ) than forests.
The data used in this exercise for calculate NDVI can be downloaded by clicking here.
Before any questions, leave a comment below in this post and we will respond as soon as possible.
How to calculate the NDVI index with R,