SpaDES (version 1.2.0)

splitRaster: Split a RasterLayer into multiple tiles

Description

Divides up a raster into an arbitrary number of pieces (tiles). Split rasters can be recombined using do.call(merge, y) or mergeRaster(y), where y <- splitRaster(x).

Usage

splitRaster(r, nx, ny, buffer, path)
"splitRaster"(r, nx, ny, buffer, path)
"splitRaster"(r, nx, ny, buffer, path)
"splitRaster"(r, nx, ny, buffer)
"splitRaster"(r, nx, ny, buffer, path)
"splitRaster"(r, nx, ny, buffer)
"splitRaster"(r, nx, ny, path)
"splitRaster"(r, nx, ny)
"splitRaster"(r, nx, ny, path)
"splitRaster"(r, nx, ny)

Arguments

r
The raster to be split.
nx
The number of tiles to make along the x-axis.
ny
The number of tiles to make along the y-axis.
buffer
Numeric vector of length 2 giving the size of the buffer along the x and y axes. If these values less than or equal to 1 are used, this is interpreted as the number of pixels (cells) to use as a buffer. Values between 0 and 1 are interpreted as proportions of the number of pixels in each tile (rounded up to an integer value). Default is c(0, 0), which means no buffer.
path
Character specifying the directory to which the split tiles will be saved. If missing, the function creates a subfolder in the current working directory based on the raster's name (i.e., using names(x)).

Value

A list (length nx*ny) of cropped raster tiles.

Details

This function is parallel-aware, using the same mechanism as used in the raster package. Specifically, if you start a cluster using beginCluster, then this function will automatically use that cluster. It is always a good idea to stop the cluster when finished, using endCluster.

See Also

do.call, merge, mergeRaster.

Examples

Run this code
require(raster)

# an example with dimensions:
# nrow = 77
# ncol = 101
# nlayers = 3
b <- brick(system.file("external/rlogo.grd", package = "raster"))
r <- b[[1]] # use first layer only
nx <- 3
ny <- 4

tmpdir <- file.path(tempdir(), "splitRaster-example")

y0 <- splitRaster(r, nx, ny, path = file.path(tmpdir, "y0")) # no buffer

# buffer: 10 pixels along both axes
y1 <- splitRaster(r, nx, ny, c(10, 10), path = file.path(tmpdir, "y1"))

# buffer: half the width and length of each tile
y2 <- splitRaster(r, nx, ny, c(0.5, 0.5), path = file.path(tmpdir, "y2"))

# parallel cropping
if (interactive()) {
  n <- pmin(parallel::detectCores(), 4) # use up to 4 cores
  beginCluster(n)
  y3 <- splitRaster(r, nx, ny, c(0.7, 0.7), path = file.path(tmpdir, "y3"))
  endCluster()
}

# the original raster:
if (interactive()) plot(r) # may require a call to `dev()` if using RStudio

# the split raster:
layout(mat = matrix(seq_len(nx*ny), ncol = nx, nrow = ny))
plotOrder <- c(4, 8, 12, 3, 7, 11, 2, 6, 10, 1, 5, 9)
if (interactive()) invisible(lapply(y0[plotOrder], plot))

# can be recombined using `raster::merge`
m0 <- do.call(merge, y0)
all.equal(m0, r) ## TRUE

m1 <- do.call(merge, y1)
all.equal(m1, r) ## TRUE

m2 <- do.call(merge, y2)
all.equal(m2, r) ## TRUE

# or recombine using SpaDES::mergeRaster
n0 <- mergeRaster(y0)
all.equal(n0, r) ## TRUE

n1 <- mergeRaster(y1)
all.equal(n1, r) ## TRUE

n2 <- mergeRaster(y2)
all.equal(n2, r) ## TRUE

unlink(tmpdir, recursive = TRUE)

Run the code above in your browser using DataCamp Workspace