Last active
April 20, 2022 06:12
-
-
Save davidclarance/6cc1300596380a34c1a6d94311a0fc11 to your computer and use it in GitHub Desktop.
Get occurrence (presence) data for multiple species (including adhoc records) from the africabirdmap API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function adapted from https://github.com/AfricaBirdData/ABAP/ | |
# Only pulls presence data | |
library("tidyverse") | |
library("purrr") | |
jsonToTibble <- function(jsonfile) { | |
out <- jsonfile %>% | |
lapply(function(x) { | |
x[sapply(x, is.null)] <- NA | |
x <- unlist(x) | |
x[x == "N/A"] <- NA | |
cnames <- names(x) | |
df <- data.frame() | |
df <- rbind(df, x) | |
names(df) <- cnames | |
return(df) | |
}) %>% | |
data.table::rbindlist(fill = TRUE) %>% | |
dplyr::as_tibble() | |
return(out) | |
} | |
getAbapData <- function(.spp_code, | |
.region_type = c("country", "province", "pentad"), | |
.region, | |
.years = NULL, | |
.adhoc = FALSE) { | |
if (is.null(.region_type)) { | |
.region_type <- "country" | |
} else if (!.region_type %in% c("country", "province", "pentad")) { | |
stop(".region_type must be one of 'country', 'province', 'pentad'") | |
} | |
if (!is.null(.years)) { | |
.years <- paste(.years, collapse = ",") | |
.years <- paste0("&year=", .years) | |
} | |
.region <- tolower(.region) | |
.region <- gsub(" ", "", .region) | |
url <- paste0( | |
"https://api.birdmap.africa/sabap2/v2/cards/species/85ee37929696cba93e1cdda4dbb3f93a/", | |
.spp_code, | |
"/", | |
.region_type, | |
"/", | |
.region, | |
"?format=csv", | |
.years | |
) | |
if (.adhoc) { | |
url <- paste0(url, "&adhoc=1") | |
} | |
# Extract data | |
myfile <- httr::RETRY("GET", url) %>% | |
httr::content(as = "text", encoding = "UTF-8") | |
if (myfile == "") { | |
return(NULL) | |
} | |
# Format | |
out <- myfile %>% | |
textConnection() %>% | |
utils::read.csv(header = TRUE) %>% | |
dplyr::as_tibble() | |
# If there are multiple regions (e.g. multiple pentads), then multiple headers | |
# are produced and inserted as data. Eliminate these | |
out <- out %>% | |
dplyr::filter(Pentad != "Pentad") | |
out <- out %>% | |
readr::type_convert( | |
col_types = readr::cols( | |
.default = readr::col_integer(), | |
CardNo = readr::col_character(), | |
StartDate = readr::col_date(format = ""), | |
EndDate = readr::col_date(format = ""), | |
StartTime = readr::col_character(), | |
Pentad = readr::col_character(), | |
Spp = readr::col_character(), | |
Sequence = readr::col_character(), | |
Common_name = readr::col_character(), | |
Taxonomic_name = readr::col_character() | |
) | |
) | |
return(out) | |
} | |
# species_list can be any arbitrary list of species IDs | |
# examples species_list = c(1, 196) | |
# In this case we've used the Kenya bird list to get all the species IDs | |
species_list <- read_csv("Birds_of_Kenya_2019.csv") %>% | |
select(ADU) %>% | |
na.omit() %>% | |
pull() | |
occurence_data <- species_list %>% | |
map_df( | |
~ getAbapData( | |
.spp_code = ., | |
.region_type = "country", | |
.region = "kenya", | |
.adhoc = TRUE | |
), | |
.default = data_frame( | |
CardNo = NA, | |
StartDate = NA, | |
EndDate = NA, | |
StartTime = NA, | |
Pentad = NA, | |
ObserverNo = NA, | |
TotalHours = NA, | |
Hour1 = NA, | |
Hour2 = NA, | |
Hour3 = NA, | |
Hour4 = NA, | |
Hour5 = NA, | |
Hour6 = NA, | |
Hour7 = NA, | |
Hour8 = NA, | |
Hour9 = NA, | |
Hour10 = NA, | |
TotalSpp = NA, | |
InclNight = NA, | |
AllHabitats = NA, | |
Spp = NA, | |
Sequence = NA, | |
Common_name = NA, | |
Taxonomic_name = NA | |
) | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment