Skip to contents

Helper function to make a binary raster layer from input target.

Usage

binary_layer(target, fun = "equals", value, layer = 1)

Arguments

target

SpatRaster target (see terra::rast())

fun

character indicating which function to use to compare layer to value. One of 'equals', 'gt', 'gte', 'lt', 'lte' or 'in'. Default: 'equals'.

value

numeric value in target. see Details.

layer

default 1, see terra::extract()

Value

A binary raster SpatRaster (see terra::rast()) with two values:

  • TRUE if pixel matches 'value' provided given the provided function

  • FALSE if pixel does not match 'value' provided given the provided function

Details

Find all pixels given the function ('fun') matching the value ('value') and return a binary raster.

value' may only be length 1 if 'fun' is one of: 'equals', 'gt', 'gte', 'lt', 'lte'.

'value' may be greater than length 1 only if 'fun' is: 'in'.

Examples

library(terra)
#> terra 1.7.78
clearwater_lc_path <- system.file("extdata",
  "clearwater_lake_land_cover.tif", package = "camtrapmonitoring")
clearwater_lake_land_cover <- rast(clearwater_lc_path)

bin <- binary_layer(target = clearwater_lake_land_cover,
  fun = 'equals', value = 18)

plot(bin)


# fun = 'in'
bin <- binary_layer(target = clearwater_lake_land_cover,
  fun = 'in', value = c(1, 2))

plot(bin)