Last active
December 28, 2015 12:09
-
-
Save johnbaums/7498648 to your computer and use it in GitHub Desktop.
Get plant species occurrence records from the AVH hub of ALA (ala.org.au)
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
getAVH <- function(species, sleepInterval=0, progress=TRUE, verbose=FALSE, outpath=NULL) { | |
require(httr) | |
require(plyr) | |
avh <- handle('http://biocache.ala.org.au') | |
.getAVH <- function(x) { | |
sp <- gsub('\\s+', '+', x) | |
cfg <- c(accept_json()) | |
if (isTRUE(verbose)) cfg <- c(cfg, verbose()) | |
tt1 <- GET(handle=avh, path='ws/occurrences/search', | |
config=cfg, | |
query=list(q=sprintf('raw_name:%s', sp), | |
pageSize=0, | |
qc='data_hub_uid:dh2', | |
fq='geospatial_kosher:true', | |
facet='off', | |
fl='year')) | |
.count <- content(tt1)$totalRecords | |
if (.count > 1) { | |
tt2 <- GET(handle=avh, path='ws/occurrences/search', | |
config=cfg, | |
query=list(q=sprintf('raw_name:%s', sp), | |
pageSize=.count, | |
qc='data_hub_uid:dh2', | |
fq='geospatial_kosher:true', | |
facet='off')) | |
res <- content(tt2) | |
res$requestTime <- Sys.time() | |
if (length(res$occurrences) != .count) { | |
warning('Not all occurrences were downloaded for species ', x, | |
call.=FALSE) | |
res <- 'Error' | |
} | |
} else { | |
warning('No records for ', x, call.=FALSE) | |
res <- 'No records' | |
} | |
if (!is.null(outpath)) { | |
save(res, file=paste0(outpath, '/', gsub('\\s+', '_', x), '.rda')) | |
} | |
Sys.sleep(sleepInterval) | |
res | |
} | |
RES <- llply(species, function(x) tryCatch(.getAVH(x), error=function(e) { | |
warning(x, ' failed.', call.=FALSE) | |
return('Error') | |
}), | |
.progress=if (isTRUE(progress)) { | |
create_progress_bar(name = "text") | |
} else 'none') | |
names(RES) <- species | |
RES | |
} | |
# Examples | |
# out1 <- getAVH('banksia ornata', 'callitris rhomboidea', 'Ballota nigra subsp. foetida') | |
# str(out1$'banksia ornata', max=1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment