binary_stack: Binary stack

View source: R/binary_stack.R

binary_stackR Documentation

Binary stack

Description

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.

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.

Value

A terra::rast() object.

See Also

category_layer(), terra::segregate().

Examples

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

# plot the raster
## Not run: 
plot(x, main = "x")

## End(Not run)

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

# plot result
## Not run: 
plot(y)

## End(Not run)

prioritizr/prioritizr documentation built on Sept. 17, 2024, 2:33 p.m.