R/raster_dummy.R

Defines functions raster_dummy

Documented in raster_dummy

#' Converts Categorical Raster to Dummy Raster
#' @description Convert raster file containing n classes
#'     in n files containing one class
#'     type presence (1) and absence (0)
#'     Output a multiple raster (SpatRaster) from the terra package
#' @param r raster with categorical data
#' @return SpatRaster with n layers one for each factor level
#' @export
#' @importFrom terra rast ext res unique `add<-`
#' @examples
#' # dumr = raster_dummy(r)
raster_dummy <- function(r) {
  if (class(r) %in% c("RasterStack", "RasterLayer")) {
    r <- rast(r)
  } else {
    if (class(r) != "SpatRaster") {
      stop("Error : file must be in RasterStack or SpatRaster format")
    }
  }
  if (is.na(terra::crs(r))) {
    stop("r does not have a coordinate system")
  }

  rbin <- terra::rast()
  terra::ext(rbin) <- terra::ext(r)
  terra::res(rbin) <- terra::res(r)
  rnames <- vector()
  nr <- nrow(terra::unique(r))
  u <- terra::unique(r)
  u <- u[, 1]
  u <- as.numeric(u)
  for (j in 1:nr) {
    b <- r
    b[] <- ifelse(b[] == u[j], 1, 0)
    rbin <- terra::`add<-`(rbin, b)
    rnames <- append(rnames, paste(names(r),
      paste("level", u[j], sep = "."),
      sep = "_"
    ))
  }
  names(rbin) <- rnames
  return(rbin)
}
elpidiofilho/mdsFuncs documentation built on April 14, 2022, 5:40 p.m.