mean_var: Calculate mean and variance for a target neighborhood

Description Usage Arguments Value Examples

View source: R/preproc_funs.R

Description

mean_var takes a multi-band raster stack, conducts principal components analysis, then applies mean and variance calculations within a specified neighborhood or list of neighborhoods for the range of retained PCA axes.

Usage

1
mean_var(tile, axes = 3, f_width, fill_na = TRUE)

Arguments

tile

File path to a mulit-band raster stack.

axes

How many PCA axes to retain for neighborhood calculations.

f_width

Focal neighborhood radius or radii (if a list) in which to perform calculations. Units correspond to that of the tile CRS.

fill_na

Whether to impute NA cells with mean of neighboring cells.

Value

None. Appends i PCA axes by j f_widths for both mean and variance (axes X neighborhoods X 2) to tile and overwrites appended stack to tile file path. Appended bands are sorted hierarchically by summary type (mean first), then neighborhood, and finally PCA axis.

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
41
42
43
44
45
46
47
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)
sobel(t, axes = 3, fill_na = TRUE)

fs <- c(0.5, 1)

mean_var(t, axes = 3, f_width = fs, fill_na = TRUE)
t_full <- stack(t)
t_mv <- t_full[[10:nlayers(t_full)]]
names(t_mv) <- paste(c(rep('mean',6), rep('var',6)),
                     paste0('ngb_', c(rep(0.5, 3), rep(1, 3))),
                     rep(paste0('axis_',1:3), 4),
                     sep = '_')
#mean_var output
plot(t_mv)

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