I am running into an issue where my function for downloading occurrences fails when there is a species with no data, or there is a spelling error in my AOI. Maybe someone knows a way to avoid this.
I am new to R but have a fair amount of experience. What I wanted to do was pass on two columns with the common name and scientific name for the species I am trying to pull occ data for, but there is too many issues with spelling errors, name errors, etc. I am also open to other workflows. I tried to download by taxa within my polygon but im not sure how to handle that large dataset now that I have it.
This is what I have, I apologies if this is not a standard practice for sharing code, I am learning!
`#make a string of species names to use in the 'occ_data' function
species <- c("Grus americana", "Egretta rufescens", "Plegadis chihi","Mycteria americana", "Pelecanus erythrorhynchos", "Pelecanus occidentalis", "Peucaea botterii", "Platalea ajaja", "Vireo atricapilla Woodhouse", "Setophaga chrysoparia", "Sternula antillarum", "Tympanuchus cupido subsp. attwateri", "Falco femoralis", "Dryobates borealis", "Empidonax traillii", "Falco peregrinus", "Charadrius melodus", "Buteo nitidus", "Glaucidium brasilianum","Haliaeetus leucocephalus","Camptostoma imberbe","Pachyramphus aglaiae","Elanoides forficatus","Coccyzus americanus","Elanus leucurus","Peucaea aestivalis","Buteogallus anthracinus","Strix occidentalis","Buteo albicaudatus") *****#function fails when not found in the bounding box or species name is incorrect/capitalization****
#also make a string of common names
common_name <- c("Whooping Crane", "Reddish Egret", "White-faced Ibis","Wood Stork", "American White Pelican", "Brown Pelican", "Botteri's Sparrow", "Roseate Spoonbill", "Black-capped Vireo", "Golden-cheeked Warbler", "Least Tern", "Attwater's Greater Prairie Chicken", "Aplomado Falcon", "Red-cockaded Woodpecker", "Traill'S Flycatcher", "Peregrine Falcon", "Piping Plover", "Gray Hawk", "Ferruginous Pygmy Owl","American Bald Eagle", "Northern Beardless Tyrannulet","Rose-throated Becard","American Swallow-Tailed Kite","Yellow-Billed Cuckoo","White-Tailed Kite","Bachman's Sparrow","Common Black Hawk","Spotted Owl","White-tailed Hawk")
emptylist <- vector("list", length = length(species))
commonemplist <- vector("list", length = length(common_name))
#funtion for pulling data
crawl <- function(year){
for (i in 1:length(species)) { #this function can pull data for multiple species
occ <- occ_data( #parameters outlined by the package to pull species specific data
scientificName = species[[i]],
hasCoordinate = TRUE, #spatial coordinates are an important feature for the observations
geometry = st_bbox(AOI), #identifying the AOI to get observation within texas
year = year,
) %>%
.$data #
# add species name column as ID to use later
occ$ID <- common_name [[i]]
#clean by removing duplicate occurrences
emptylist[[i]] <-
occ %>% distinct(decimalLatitude, decimalLongitude, .keep_all = TRUE) %>%
dplyr::select(Species = ID,
decimalLatitude,
decimalLongitude,
year,
month,
basisOfRecord) #grabbing geographic coordinates, year, month, and the type of record. For this data set, all are "Human Observations"
}
whoop <- bind_rows(emptylist)
}
years <- c(2013:2023) #assigning the years to pull data from
whoop <- map_dfr(years, crawl) #using our function and inputting years to pull species data
# Giving each observation a unique ID
whoopunique <- rowid_to_column(whoop) %>%
st_as_sf(coords = c(x ="decimalLongitude", y ="decimalLatitude"), crs = 4269)`
`
thank you!