ndvi_msavi: Calculate NDVI and MSAVI

Description Usage Arguments Value Examples

View source: R/preproc_funs.R

Description

Takes a multi-band raster stack and calculates NDVI and MSAVI vegetation indices. These indices are appended as the last two bands of the input tile.

Usage

1
ndvi_msavi(tile, r_band = 1, nir_band = 4)

Arguments

tile

File path to a mulit-band raster stack.

r_band

Stack index of the red band.

nir_band

Stack index of the near-infrared band.

Value

None. Appends bands to tile and overwrites appended stack to tile file path.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
library(paint2train)

image_dir <- tempfile()
image_url <- 'https://storage.googleapis.com/mpgranch_data/sample_4band.tif'
download.file(url = image_url, destfile = image_dir)
tdir <- tempdir()
setwd(tdir) 
preproc_dir <- 'preproc_tiles'
dir.create(preproc_dir)

#some test coordinates
xcoords <- c(727495,
             727919)

ycoords <- c(5175339,
             5175408)

coord_mat <- cbind(xcoords, ycoords)

ls <- 30 #how big should the tiles be, this is the side length (in units of data, meters here)
buff <- 5  #buffer in native units of CRS
cores <- ifelse(.Platform$OS.type == 'unix', #how many cores to use for preprocessing
                   parallel::detectCores() - 1,
                   1) 
                   
tile_at_coords(coords = coord_mat,
 len_side = ls,
 buffer = buff,
 out_dir = preproc_dir,
 img = image_dir,
 ncores = cores)
 
t <- list.files(preproc_dir, full.names = TRUE)[1]
ndvi_msavi(tile = t, r_band = 1, nir_band = 4)
 
t_ndvi <- raster(t, band = 5)
t_msavi <- raster(t, band = 6)
veg_inds <- stack(t_ndvi, t_msavi)
names(veg_inds) <- c('NDVI','MSAVI')
plot(veg_inds)

mosscoder/paint2train documentation built on Jan. 21, 2022, 11 a.m.