R/perc2area.R

Defines functions perc2area

Documented in perc2area

### convert classified data to areas per gridcell
## this function should be able to convert classified data 

## to do:
# adapte to the input of lists. Currently it only supports to put in singluar land-cover types
# also has to check whether projection is the same as assumed for the stored area file


# example to convert
# x <- readEsaCci(types="tree.avg")
# x <- raster::crop(CCI, extent(-10,10,40,60))
# tmp <- perc2area(x)

# x <- raster::crop(dat, raster::extent(-10,10,40,60))



# either a target resolution or a factor for aggregation have to be specified
# types are specific land-use classes for which the fractional information should be calculated (e.g. crop)


# somehow it also has to be specified which type of data is converted (e.g. ESA CCI, so that the function knows that it has to use the conversion table of EsaCci)
# this should best already be specified somewhere in the attributes of the file. However, if the file is for instance cropped, than attributes are lost




#' Convert percentage maps to area maps
#' 
#' This function converts raster datasets with shares of one single land use
#' classes (0 to 1) into absolute areas (km2). Areas are usually calculated on
#' coarser raster resolution, therefore either an aggregation factor or a
#' target resolution have to be specified. Function is for instance used to
#' convert remote sensing derived land-cover data (e.g ESA CCI) to area values
#' of individual land-cover classes per grid cell.
#' 
#' 
#' @usage perc2area(x, fact=NULL, res=NULL, na.rm=T)
#' @param x Raster dataset that represents shares of one land-cover/land-use
#' class between 0 and 1.
#' @param fact Spatial aggregation factor
#' @param res Target resolution
#' @param na.rm Sets NA values to 0 while aggregating
#' @return Raster stack containing km2 values (and possibly several years) for
#' individual land-cover class
#' @author Ulrich Kreidenweis
#' @seealso \code{\link{readEsaCci}}
#' @examples
#' 
#' \dontrun{perc2area()}
#' 
#' @export perc2area
perc2area <- function(x, fact=NULL, res=NULL, na.rm=T){

## there should be some check whether the data is likely in percentage values between 0 and 1
# as it is currently it is not working because if there is not attr this fails
#  if(attr(x, "unit")!= "share") warning("Unit is not specified or not share. Are you sure this is a dataset that contains shares between 1 and 0?")
 
  if ((is.null(fact) & is.null(res)) | (is.numeric(fact) & is.numeric(res)))  stop("Either fact or res have to be specified")
  if(is.null(fact)) {
    if (res(x)[1] != res(x)[2]) stop("Resolution of y and x have to agree")
    fact <- res/res(x)[1] # calculate fact from specified target resolution
  }
  
  
  # if the target resolution is coarser then the initial resolution first aggregate
  if (fact > 1){
    
    # if rm.na is TRUE then all NA values have to be set to 0. Just setting na.rm to true in aggregate would overestimate the area
    # maybe not all values have to be set to 0, those close to the land masses would be enough
    if (na.rm==T) {
      x[is.na(x)] <- 0
    }
    
    x <- raster::aggregate(x, fact=fact, fun=mean, na.rm=F)
  }
  
  ## area
  A <- calcArea(x)
  
  out <- x * A
  attr(out, "unit") <- "km2"
  
  return(out)
}
pik-piam/geodata documentation built on Nov. 5, 2019, 12:21 a.m.