library(rgbif)
library(dplyr)
library(readr)
library(CoordinateCleaner)
#Make a vector of species or upload from file. Must have space between Genus and epithet. example below
splist<-c("Clarkia rhomboidea", "Dryopteris arguta", "Calochortus monophyllus", "Dianthus armeria")
#Query GBIF API for taxon keys. These are unique codes for each species needed later to get the ocurrences
keys <- sapply(splist, function(x) name_backbone_verbose(x)$data$usageKey, USE.NAMES=FALSE)
#Direct download ocurrences -- queues a download from your account on gbif. (You need to make an account on gbif website)
#In the second code chunk (occ_download), you can add additional filters if you decide they are necessary -- check the
#documentation for rgbif to see the options
user="MY_USERNAME" #enter your gbif username
pwd="MY_PASSWORD" #enter your gbif password
email="MY_EMAIL" #enter your gbif email
occ_download( #this chunk will gather your ocurrences and print a download key in the console
pred_in("taxonKey",keys),
pred_in("basisOfRecord", c('PRESERVED_SPECIMEN','HUMAN_OBSERVATION','OBSERVATION')),
pred_in("continent", c("NORTH_AMERICA")), #choose a continent
pred("hasCoordinate", TRUE), #filter out records without coordinates
pred("hasGeospatialIssue", FALSE), #filter out records with flagged geospatial issues
pred_lte("coordinateUncertaintyInMeters",1000), #choose an upper threshold of coordinate uncertainty in meters
format = "SIMPLE_CSV",
user=user,pwd=pwd,email=email
)
#Load the data by entering the download key from previous chunk
d <- occ_download_get('ENTER DWNLD KEY HERE') %>% #replace the number here with the download key generated by previous chunk
occ_download_import()
#Simplify the dataset down to the important stuff. check how many records of each
occs<-d %>% select(species,decimalLatitude,decimalLongitude)
occs %>% group_by(species) %>% summarise(n())
#Use coordinate cleaner to flag occurences with any additional geospatial issues
flags<-clean_coordinates(occs,lon = "decimalLongitude",lat="decimalLatitude")
occs.clean<- flags %>% filter(.summary==T) %>% select(species,decimalLatitude,decimalLongitude)
occs.clean %>% group_by(species) %>% summarise(n())