R/readWDPA.R

Defines functions readWDPA

Documented in readWDPA

## read WDPA protected areas:
# with the option to rasterize for a given resolution (then latlong), or according to a given raster file



#' Read World Database on Protected Areas (WDPA) data
#' 
#' Reads the World Database on Protected Areas, so polygons of the worldwide
#' protected areas. Output can also be converted to raster data.
#' 
#' 
#' @usage readWDPA(rasterize=F, rast=NULL, res=NULL, getCover=F, rCache=T,
#' wCache=T)
#' @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 WDPA polygons or alternatively a raster dataset if rasterize is
#' set to TRUE
#' @author Ulrich Kreidenweis
#' @seealso \code{\link{readBH}}, \code{\link{readFF}}, \code{\link{readCPD}}
#' @examples
#' 
#' \dontrun{readWDPA()}
#' 
#' @export readWDPA
readWDPA <- function(rasterize=F, rast=NULL, res=NULL, getCover=F, rCache=T, wCache=T) {
  
  dat <- NULL
  if(rCache) dat <- readCache(); fromcache=T
  
  if(is.null(dat)) {
    dat <- rgdal::readOGR(paste0(geodata$config$mainfolder, "/WDPA/", "WDPA_July2016-shapefile-polygons.shp"), layer="WDPA_July2016-shapefile-polygons")  
    fromcache=F
    
    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(x = dat, y = r, getCover=getCover)  
    }
  }
  
  if (wCache & !fromcache) writeCache(dat)
  
  return(dat)
  
}
pik-piam/geodata documentation built on Nov. 5, 2019, 12:21 a.m.