#' Combine abiotic and biotic layers to create suitability raster
#'
#' Combine biotic (and abiotic) layers into a single suitability raster to be
#' used to scale arrival rates.
#'
#' @param x One of: a `RasterStack`; a `RasterBrick`; a list of
#' `RasterLayer` objects, or a vector of file paths to raster files.
#' @param outfile Character. Name of geotiff where output will be saved. If not
#' provided, raster object will be returned to R.
#' @param return_rast Logical. Should the raster object be returned to R?
#' Ignored if `outfile` is not provided.
#' @return If `outfile` is specified, the resulting raster is saved as a
#' geotiff to that path. If `return_rast` is `TRUE` or
#' `outfile` is not specified the resulting raster is returned, otherwise
#' `NULL` is returned invisibly.
#' @importFrom raster stack setMinMax minValue maxValue writeRaster
#' @export
suitability <- function(x, outfile, return_rast = FALSE) {
if(is.list(x)) {
out <- raster::stack(x)
} else if(is.character(x)) {
out <- raster::stack(x)
} else {
out <- x
}
out <- raster::setMinMax(out)
# Check layers are all between zero and 1
if(min(raster::minValue(out)) < 0 || max(raster::maxValue(out)) > 1) {
stop("Not all layers are normalised to be within 0 and 1")
}
if(class(out) %in% c("RasterStack", "RasterBrick") &&
raster::nlayers(out) > 1) {
out <- prod(out)
}
if(!missing(outfile)) {
# Create directory if it does not exist
if(!dir.exists(dirname(outfile))) {
dir.create(dirname(outfile), recursive = TRUE)
}
# write out raster
raster::writeRaster(out, outfile, overwrite = TRUE)
}
if(isTRUE(return_rast) || missing(outfile)) {
out
} else {
invisible(NULL)
}
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.