R/clip_modelLayers.R

Defines functions clip_variableLayers

Documented in clip_variableLayers

#' Crop model variable raster layers to accessible area.
#'
#' @description Crop all raster layers in a directory that are potential
#' predictor variables for a species distribution model to the accessible area.
#'
#' @param layerDir path to directory hosting raster layers
#'
#' @param accessibleArea spatial polygon denoting the species' accessible area
#' in which the predictor variable should be cropped to.
#'
#' @return raster stack of cropped predictor variables.
#'
#' @importFrom sp spTransform
#' @importFrom raster crop crs mask stack
#'
#' @export



# Read in past spp specific workflows
#' Function to clip model variable layers to accessible area

clip_variableLayers <- function(layerDir, accessibleArea){
  # read in list of files
  filelist <- list.files(layerDir, full.names = TRUE)
  rstack <- raster::stack(filelist)

    #crop file to extent of the buffer
    aa_proj <- sp::spTransform(accessibleArea, CRSobj = raster::crs(rstack))

    r_crop <- raster::crop(rstack, aa_proj)

    # mask the cropped file
    r_mask <- raster::mask(r_crop, mask = aa_proj)

    return(r_mask)

}
mbelitz/autoSDM documentation built on Dec. 21, 2021, 3:55 p.m.