R/resample_categorical.R

Defines functions resample_categorical

Documented in resample_categorical

#' Apply minimum mapping unit to categorical raster
#'
#' @param x Categorical raster map
#' @param y Minimum mapping unit
#' @param filename Optional. filename for writing raster to disk.
#'
#' @return A raster layer with resolution of y with one band for each category of x showing the pixel-wise proportion
#' @export


resample_categorical <- function(x, y, filename = NULL) {

  pixels <- raster::reclassify(x, matrix(c(raster::cellStats(x, "min"), raster::cellStats(x, "max"), 1), ncol = 3))
  x_res <- raster::layerize(x, y)
  npixel <- raster::layerize(pixels, y)
  out <- x_res / npixel

  if(!is.null(filename)) {
    raster::writeRaster(out, filename, overwrite = TRUE)
  }

  return(out)

}
juoe/spatialtools documentation built on May 25, 2019, 6:25 p.m.