R/gis.R

Defines functions rs_reclass

Documented in rs_reclass

#' @title Reclassify a raster object
#' @description Divide values of a raster object into bins of a given width.
#' @param x A raster object.
#' @param binwidth The width of the bins.
#' @param right Should bin intervals be closed on the right? See [`terra::classify()`] for details.
#' @param col_name Name of the classified variable.
#' @param ... Other arguments passed to [`terra::classify()`].
#' @export
rs_reclass <- function(x, binwidth = 100, right = FALSE, col_name = "zone", ...) {
  min <- round_nearest(terra::minmax(x)[1], -binwidth)
  max <- round_nearest(terra::minmax(x)[2], -binwidth)
  s <- seq(min, max, binwidth)
  new_values <- tibble(lower = s, upper = s + binwidth, new = s)
  x <- terra::classify(x, new_values, right = right, ...)
  setNames(x, col_name)
}
arnaudgallou/toolkit documentation built on Nov. 25, 2022, 5:42 p.m.