R/readRas_function.R

Defines functions readRas

Documented in readRas

#' readRas: read raster files
#'
#' Read the spatial distribution raster files (.tif or .asc format) of species
#' from a specified folder and provide a list with the raster files.
#'
#' @usage readRas (path)
#' @param path Path for a folder with spatial distribution raster files
#' (.asc or .tif format).
#' @return \code{readRas} retrurns a list of 'RasterLayer' files or
#' a 'RasterLayer' file.
#' @encoding UTF-8
#' @author Thaís Dória & Daniel Gonçalves-Souza
#' @export readRas

readRas <- function(path){

  if(substr(path, nchar(path), nchar(path)) == '/'){
    path <- substr(path, 1, nchar(path) - 1)
  }

  files.sp <- list.files(path, pattern = ".tif$|.asc$",
                         full.names = T)
  names.sp <- list.files(path, pattern = ".tif$|.asc$", full.names=F)
  sps <- lapply(files.sp, raster)
  names(sps) <- gsub("*\\.asc|*\\.tif", '', names.sp)

  return(sps)
}
thaisdoria/habitaR documentation built on Dec. 18, 2020, 8:09 p.m.