Skip to contents

Convert a single-layer terra::rast() object that contains integer values into a multi-layer terra::rast() object with pixel values denote the presence/absence of a given integer value. This is methodology is also known as "one-hot encoding".

Usage

binary_stack(x, keep_all = TRUE)

# S3 method for class 'Raster'
binary_stack(x, keep_all = TRUE)

# S3 method for class 'SpatRaster'
binary_stack(x, keep_all = TRUE)

Arguments

x

terra::rast() object with a single layer that contains integer values.

keep_all

logical value indicating if all integers should be kept in the output. If TRUE, the output will contain a layer for each sequential integer between 1 and the maximum value in x. If FALSE, the output will only contain layers for integer values present in x. Defaults to TRUE.

Value

A terra::rast() object.

Details

This function is provided to help manage data that encompass multiple management zones. For instance, this function may be helpful for preparing raster data for add_locked_in_constraints() and add_locked_out_constraints() since they require binary rasters as input arguments. It is essentially a wrapper for terra::segregate(). Note that this function assumes x contains integer values.

Examples

# create raster with categorical values
x <- terra::rast(matrix(c(1, 2, 4, 0, NA, 1), nrow = 3))

# plot the raster
# \dontrun{
plot(x, main = "x")

# }

# convert to binary stack
y <- binary_stack(x)

# plot result
# \dontrun{
plot(y)

# }