tile_at_coords: Generate tiles from imagery and spatial data

Description Usage Arguments Value Examples

View source: R/tile_at_coords.R

Description

Accepts a two-column matrix of coordinates and a path to a multi-band raster. Generates tiles of specified side length in CRS units of the data with an optional buffer to avoid edge effects in subsequent preocessing. Save tiles to an output directory for further preprocessing.

Usage

1
tile_at_coords(coords, img, len_side, out_dir, buffer = 0, ncores = 1)

Arguments

coords

A two column matrix containing x and y coordinates.

img

The path to the source raster stack.

len_side

Desired side length of tiles in img CRS units.

out_dir

Directory in which to place tile output.

buffer

Distance in native CRS units to buffer around tiles.

ncores

The number of cores to use for parallel tiling.

Value

None. Tiles are saved to out_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
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)
 
#show tiling outcome
par(mfrow = c(1,2))              
for(i in seq_along(xcoords)){plotRGB(stack(list.files(preproc_dir, full.names = T)[i])[[1:3]])}
par(mfrow = c(1,1))
 

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