NDVI calculation from Landsat 8 images with R and Terra package – Tutorial

Hydrate

On the exploration for advanced tools for spatial analysis we took a chance on making raster algebra with R and the Terra package tutorial. This is an applied example of NDVI calculation from the red and near infrared bands from a Landsat 8 image. The script covers all steps from band load, array algebra and export as geospatial raster.

For this tutorial you need R and the spatial library Terra installed under a Conda environment. Please install the environment following this tutorial: hatarilabs.com/ih-en/how-to-install-the-r-spatial-library-terra-on-a-conda-enviroment-tutorial

Tutorial

Code

library(terra)
library(repr)
redFile <- '../Landsat8/LC08_L1TP_042035_20180603_20180615_01_T1_B4_clip.tif'
nirFile <- '../Landsat8/LC08_L1TP_042035_20180603_20180615_01_T1_B5_clip.tif'
redBand <- rast(redFile)
nirBand <- rast(nirFile)
red <- redBand[[1]]
nir <- nirBand[[1]]

ndviCal <- function(red, nir) {

    ndviArray <- (nir - red)/(nir + red)
    return(ndviArray)
}
iniTime <- Sys.time()
#ndviBand <- ndviCal(redBand,nirBand)
ndviBand <- ndviCal(red,nir)
endTime <- Sys.time()
print(endTime - iniTime)
Time difference of 0.319994 secs
options(repr.plot.width=20, repr.plot.height=12)
plot(ndviBand, main="Landsat-NDVI")
writeRaster(ndviBand, '../Output/NDVIByR.tif', overwrite=TRUE)

Input data

You can download the input data from this link.

—————
TRANSCOM ISPFree Sigma HSE Email
Level 6 Domain Names are FREE – Register Now.

Related Posts

Hydrate

EGU2024

The mountain hydrology team will have full presence at EGU2024. Below you’ll find a list of the primary presentations by our team.

Full programme can be

Boost Inflight Internet- Business Hosting- Secure Email Account- Dropcatch Domain Names- Antisnoop Email- Free Secure Email- Cheap VOIP Calls- Free Hosting and Email- Aero Connectivity- Premium Domains for Sale- Transcom Telecom- Satphone Airtime- Mobile Plans- Free Domain Names- Organic Products- Aviation News
Transcom ISP - Transcom VOIP - Free Secure Email - Dropcatch Software - FastApn Inflight - Aero Connect - Premium Domains