This R Markdown document presents the functions and the steps used to validate species’ Area of Habitat (AOH) as described by Dória & Dobrovolski (2020) in the work “Improving post-2020 conservation of terrestrial vertebrates in Caatinga”.
install.packages('rgdal')
install.packages('dismo')
install.packages('sp')
install.packages('raster')
install.packages('plyr')
library(rgdal)
library(dismo)
library(sp)
library(raster)
library(plyr)
f.gbif <- function (spp){
for (i in 1:length(spp)) {
p.occ=gbif(spp, ext=ex, geo=T, download=T)
return(as.data.frame(cbind(p.occ$species, p.occ$lon, p.occ$lat))) # returning only coordinates and name of species
}}
#Notes:
#'spp' in this function is list with specie's names (as.character)
#'ex' in this function is the spatial extension (xmin, xmax, ymin, ymax) that should be considered to delimit the search (vector)
ATTENTION: run this function with ‘lapply’ to generate a list containing the results of downloaded data for each spp “lista.gbif <- lapply(1:length(spp), function(f0) f.gbif(spp[f0]))”
f.clean <- function(lista.gbif){
if (length(lista.gbif[[1]]) != 0){
DF=NULL
for (i in 1:length(lista.gbif)) {
DF=rbind(DF, lista.gbif[[i]])
x=as.numeric(as.matrix(DF[,2]))
y=as.numeric(as.matrix(DF[,3]))
coord=cbind(x, y)
}
spp=as.data.frame(DF[,1])
all=as.data.frame(cbind(spp, coord))
list.clean=split(all, all$`DF[, 1]`)
return(list.clean)}
if (length(lista.gbif[[1]]) == 0) {
DF=NULL
for (i in 2:length(lista.gbif)){
DF=rbind(DF, lista.gbif[[i]])
x=as.numeric(as.matrix(DF[,2]))
y=as.numeric(as.matrix(DF[,3]))
coord=cbind(x, y)
}
spp=as.data.frame(DF[,1])
all=as.data.frame(cbind(spp, coord))
list.clean=split(all, all$`DF[, 1]`)
return(list.clean)}}
f.ras <- function (list.clean){
for (i in 1:length(list.clean)){
r <- rasterize(as.data.frame(list.clean[[i]][,2:3]), r0.05, background=0, fun='count') # each coordinate point present in each cell is counted
r[r > 1] <- 1 # to remove duplicates considering as one all coordinates that is present in the same cell
c <- crop(r, caatinga)
m <- mask(c, caatinga)
e <- extent(-44.8, -34.8, -16.6, -2.2)
r.occu.caat <- extend(m,e)
nome <- names(list.clean[i])
writeRaster(r.occu.caat,paste(nome,"GBIFPoints.asc",sep = ""),format="ascii", overwrite=T)
}}
evaldata <- "https://github.com/thaisdoria/TD_Thesis_Chapter1_RData/blob/master/DoriaTAF_AOHsVALIDATION.RData?raw=true"
load(url(evaldata)) # RData file created on 2017/2018 containing the files listed bellow
### STEP 1 - Running function 1 (f.gbif) to download occurrences records and generate a list with all results gathered from GBIF for each species inside the extension specified (including species with no records)
lista.gbif.amph <- lapply(1:length(amph.aoh), function(f0) f.gbif(amph.aoh[f0]))
### STEP 2 - Running function 2 (f.clean) to clean the first object and obtain a list only with species for which there are records
lista.clean.amph <- f.clean(lista.gbif.amph)
### STEP 3 - Running the function 3 (f.ras) to rasterize the occurrences downloaded for each species and save the raster file GBIFPoints
f.ras(lista.clean.amph)
# Identifying the spp. with occurrence in, at least, 5 cells
amphs.gbif <- raster::stack(list.files(path=getwd(), pattern = "GBIFPoints.asc$", full.names = F))
amphs.5=sort(cellStats(amphs.gbif, sum)>=5)
amphs.5 # return the species that not meet this criteria
### STEP 1 - Running function 1 (f.gbif) to download occurrences records and generate a list with all results gathered from GBIF for each species inside the extension specified (including species with no records)
lista.gbif.repg <- lapply(1:length(repg.aoh), function(f0) f.gbif(repg.aoh[f0]))
### STEP 2 - Running function 2 (f.clean) to clean the first object and obtain a list only with species for which there are records
lista.clean.repg <- f.clean(lista.gbif.repg)
### STEP 3 - Running the function 3 (f.ras) to rasterize the occurrences downloaded for each species and save the raster file GBIFPoints
f.ras(lista.clean.repg)
# Identifying the spp. with occurrence in, at least, 5 cells
reps.gbif <- raster::stack(list.files(path=getwd(), pattern = "GBIFPoints.asc$", full.names = F))
reps.5=sort(cellStats(reps.gbif, sum)>=5)
reps.5 # return the species that not meet this criteria
### STEP 1 - Running function 1 (f.gbif) to download occurrences records and generate a list with all results gathered from GBIF for each species inside the extension specified (including species with no records)
lista.gbif.aves <- lapply(1:length(avi.aoh), function(f0) f.gbif(avi.aoh[f0]))
### STEP 2 - Running function 2 (f.clean) to clean the first object and obtain a list only with species for which there are records
lista.clean.aves <- f.clean(lista.gbif.aves)
### STEP 3 - Running the function 3 (f.ras) to rasterize the occurrences downloaded for each species and save the raster file GBIFPoints
f.ras(lista.clean.aves)
# Identifying the spp. with occurrence in, at least, 5 cells
aves.gbif <- raster::stack(list.files(path=getwd(), pattern = "GBIFPoints.asc$", full.names = F))
aves.5=sort(cellStats(aves.gbif, sum)>=5)
aves.5 # return the species that not meet this criteria
### STEP 1 - Running function 1 (f.gbif) to download occurrences records and generate a list with all results gathered from GBIF for each species inside the extension specified (including species with no records)
lista.gbif.mams <- lapply(1:length(mam.aoh), function(f0) f.gbif(mam.aoh[f0]))
### STEP 2 - Running function 2 (f.clean) to clean the first object and obtain a list only with species for which there are records
lista.clean.mams <- f.clean(lista.gbif.mams)
### STEP 3 - Running the function 3 (f.ras) to rasterize the occurrences downloaded for each species and save the raster file GBIFPoints
f.ras(lista.clean.mams)
# Identifying the spp. with occurrence in, at least, 5 cells
mams.gbif <- raster::stack(list.files(path=getwd(), pattern = "GBIFPoints.asc$", full.names = F))
mams.5=sort(cellStats(mams.gbif, sum)>=5)
mams.5 # return the species that not meet this criteria