R/eooAoo.R

Defines functions eooAoo

Documented in eooAoo

#' @title Area of occupancy (AOO) and extent of occurrence (EOO) in km2 (Auxiliar functions)
#' @name eooAoo
#' @author Dan Carver
#' @description Calculates Area of occupancy (AOO) Area and extent of occurrence (EOO) in km2
#'  using a species distribution model, an occurrences CSV file and categorize the species conservation status according IUCN parameters
#'
#' @param species A name species compiled using '_'  to call occurrences files from Workspace/parameter/occurrences folder
#' @param Workspace A forder where the pipeline will be executed
#' @param  run_version The version of the analysis used (e.g 'v1')
#'
#'
#'
#' @return It returns a data frame file saved at gap_analysis folder with six columns:
#'
#' \tabular{lcc}{
#'  taxon \tab Species name \cr
#'  EOO Area km2 \tab Area of occupancy in km^2 (AOO) for a given species \cr
#'  EOO Status \tab IUCN conservation using (AOO)  \cr
#'  AOO \tab extent of occurrence (EOO) in km2 for a given species \cr
#'  AOO adjusted Minimum \tab Minimum adjusted polygon for AOO for a given species \cr
#'  AOO Status \tab IUCN conservation using (AOO) \cr
#' }
#'
#' @examples eooAoo('Cucurbita_digitata',Workspace,'v1')
#'
#' Workspace  <-  'E:/CIAT/workspace/Workspace_test/workspace'
#' run_version  <- 'v1'
#' species_list <- c('Cucurbita_cordata',
#'  'Cucurbita_digitata',
#'  'Cucurbita_foetidissima',
#'  'Cucurbita_palmata')
#'
#'  run_version <-'v1'
#
#' lapply(1:length(species_list),function(i){
#'    species <- species_list[[i]]
#'    x <- eooAoo(species,Workspace,run_version)
#'    print(paste0(species,' DONE!'))
#' })
#'
#' @export

eooAoo <-function(species_list, occurrenceData){

  suppressMessages(require(redlistr))
  suppressMessages(require(sp))


  df <- data.frame(matrix(ncol=3, nrow = length(species_list)))
  colnames(df) <- c("species", "EOO Status", "AOO Status")

  # loop over species list
  for(i in 1:length(species_list)){

    wgs84 <- raster::crs("+proj=longlat +ellps=WGS84 +datum=WGS84 +no_defs")
    worldEqualArea <- raster::crs("+proj=cea +lon_0=0 +lat_ts=0 +x_0=0 +y_0=0 +ellps=WGS84 +units=m +no_defs ")

    #filter out species occurrence data with coordinates
    ocd <-occurrenceData %>%
      dplyr::filter(taxon == species_list[i])
    ocd <- ocd[complete.cases(ocd),]
    coords <- ocd[,c("longitude","latitude")]
    cleanPoints2 <- sp::SpatialPoints(coords = coords, proj4string = wgs84)
    spAllPro <- sp::spTransform(cleanPoints2, worldEqualArea)
    EOO.polygon <- redlistr::makeEOO(spAllPro)
    # then calcualte the area of the bounding box
    EOO.area <- redlistr::getAreaEOO(EOO.polygon)
    #determine status based on area
    if (EOO.area >= 45000) {blo <- "Least Concern (LC)"}
    if (EOO.area < 45000) {blo <- "Possible Near Threatened (NT)"}
    if (EOO.area < 20000) {blo <- "Vulnerable (VU)"} # 20000
    if (EOO.area < 5000) {blo <- "Endangered (EN)"} # 5000
    if (EOO.area < 100) {blo <- "Critically Endangered (CR)"} # 100
    if (EOO.area == "NA") {blo <- "Critically Endangered (CR)"}

    #EOO.area
    # this value is then use in the develop of other criteria in the sebcriterion B1

    ### Subcriterion B2 (calculating AOO)
    # create a 10 x 10 grid of to overlay on distribution.

    AOO.grid <- redlistr::makeAOOGrid(spAllPro, grid.size = 10000,
                                      min.percent.rule = F)
    #plot(AOO.grid)
    n.AOO <- length(AOO.grid)
    AOOarea <- n.AOO* 100
    if (AOOarea >= 4500) {AOO_cat <- "Least Concern (LC)"} # <
    if (AOOarea < 4500) {AOO_cat <- "Possible Near Threatened (NT)"}
    if (AOOarea < 2000) {AOO_cat <- "Vulnerable (VU)"} # < 2000
    if (AOOarea < 500) {AOO_cat <- "Endangered (EN)"}# < 500
    if (AOOarea < 10) {AOO_cat  <- "Critically Endangered (CR)"}# < 10
    if (AOOarea == "NA") {AOO_cat <- "Critically Endangered (CR)"}


    #n.AOO
    # so the length is just the number of grid cells that overlay this environment
    # because the position of the grid cells can potential change the number of cells
    # a randomized process is used to determine a minimun number of grids.

    gU.results <- redlistr::gridUncertainty(spAllPro, 10000,
                                            n.AOO.improvement = 5,
                                            min.percent.rule = F)

    df$species[i] <- as.character(species_list[i])
    df$`EOO Status`[i] <- blo
    df$`AOO Status`[i] <- AOO_cat
  }
  return(df)
}

#Workspace = "E:/CIAT/workspace/Workspace_test/workspace"
#run_version="v1"
#species_list <- c(
#  "Cucurbita_cordata",
#  "Cucurbita_digitata",
#  "Cucurbita_foetidissima",
#  "Cucurbita_palmata"
#)
#run_version <-"v1"

#lapply(1:length(species_list),function(i){
#  species <- species_list[[i]]
#  x <- eooAoo(species,Workspace,run_version)
#  cat(paste0(species," DONE!"),"\n")
#})
dcarver1/gapAnalysisR documentation built on Feb. 29, 2020, 12:13 p.m.