R/readIFL.R

Defines functions readIFL

Documented in readIFL

# function to read Intact Forest Landscapes



#' Read Intact Forest Landscapes
#' 
#' Reads the Intact Forest Landscapes shapefile. Output can also be converted
#' to raster data.
#' 
#' 
#' @usage readIFL(year=2000, rasterize=F, rast=NULL, res=NULL, getCover=F,
#' rCache=T, wCache=T)
#' @param year data for the year 2000 or 2013
#' @param rasterize if TRUE, polygons are rasterized
#' @param rast specify a raster here that should be used for rasterizing the
#' polygons accordingly
#' @param res alternatively to rast the raster resolution can be set
#' @param getCover if TRUE, returns the share of each cell covered by protected
#' areas
#' @param rCache TRUE: read the data from cache if available
#' @param wCache write the data to cache if it has been newly created
#' @return The Intact Forest Landscapes polygons or alternatively a raster
#' dataset if rasterize is set to TRUE
#' @author Ulrich Kreidenweis
#' @seealso \code{\link{readWDPA}}, \code{\link{readBH}},
#' \code{\link{readCPD}}, \code{\link{readFF}}
#' @examples
#' 
#' \dontrun{readIFL()}
#' 
#' @export readIFL
readIFL <- function(year=2000, rasterize=F, rast=NULL, res=NULL, getCover=F, rCache=T, wCache=T) {
  
  dat <- NULL
  if(rCache) dat <- readCache(); fromcache=T
  
  if(is.null(dat)) {
    fromcache=F
    if(year==2000){
      dat <- rgdal::readOGR(paste0(geodata$config$mainfolder, "/IFL/", "ifl_2000.shp"), layer="ifl_2000")    
    } else if (year==2013) {
      dat <- rgdal::readOGR(paste0(geodata$config$mainfolder, "/IFL/", "ifl_2013.shp"), layer="ifl_2013")
    } else {
      stop("No data available for the specified year")
    }
    
    if(rasterize) {
      if ( (is.null(rast) & is.null(res)) | (!is.null(rast) & !is.null(res)) ) stop("Either 'rast' or 'res' have to be specified")
      
      if(!is.null(rast)) {
        if (!attr(class(r), "package") == "raster") stop("'rast' has to be a raster object")
        r <- rast
      } else if (!is.null(res)) {
        r <- raster::raster()
        raster::res(r) <- res
      }
      
      dat <- raster::rasterize(dat, r, getCover=getCover)
    }
  }
  
  if (wCache & !fromcache) writeCache(dat)
  
  return(dat)
}
pik-piam/geodata documentation built on Nov. 5, 2019, 12:21 a.m.