load_tdat: Load and format imagery labels as a data.frame

Description Usage Arguments Value Examples

View source: R/load_tdat.R

Description

Provide load_tdat with two directories: one containing spatial data produced in the pre-processing pipeline, the other containing data labels generated with pt2. The function then assembles these data into a data.frame for model training. Parallel processing is available on Unix systems.

Usage

1
load_tdat(preproc_dir, label_dir, ncores = 1)

Arguments

preproc_dir

Path to pre-processing tiles.

label_dir

Path to label data .tifs.

ncores

Cores to use for parallel implementation.

Value

A data.frame in which the first column contains data labels and subsequent columns contain potential predictors with order corresponding to the band order of tiles in the preproc_dir.

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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
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'
umap_dir <- 'umap_tiles'
lab_dir <- 'label_tiles'
dir.create(preproc_dir)
dir.create(umap_dir)
dir.create(lab_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) 
umap_cores <- parallel::detectCores() - 1                  
   
                   
tile_at_coords(coords = coord_mat,
 len_side = ls,
 buffer = buff,
 out_dir = preproc_dir,
 img = image_dir,
 ncores = cores)

preproc_pipeline <- function(t, fs, b){
 ndvi_msavi(tile = t, r_band = 1, nir_band = 4)
 sobel(t, axes = 3, fill_na = TRUE)
 mean_var(t, axes = 3, f_width = fs, fill_na = TRUE)
 remove_buffer(tile = t, b = b)
}

targ_tiles <- list.files(preproc_dir, full.names = TRUE)

mclapply(FUN = preproc_pipeline, 
 X = targ_tiles, 
 mc.cores = cores, 
 fs = c(0.5, 1),
 b = buff)
 
lapply(FUN = umap_tile,
 X = targ_tiles,
 out_dir = umap_dir,
 n_threads = umap_cores, #args passed to umap
 n_sgd_threads = umap_cores, #args passed to umap
)

label_key <- list(Unknown = 0,
        `Not woody` = 1,
        `Woody` = 2)
#Establish the color  for each class for app visualization
pal <- c('royalblue',
        'tan',
        'green')

# Start the app, note that work will be saved every time the 
# label, filter, fill buttons are clicked within the app.
# Prior work saved in the label_dir will be loaded to resume labeling
p2t(umap_dir = umap_dir, 
   label_dir = lab_dir, 
   label_key = label_key, 
   label_col = pal)

train_dat <- load_tdat(preproc_dir = preproc_dir, label_dir = lab_dir, ncores = cores)

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