-
Notifications
You must be signed in to change notification settings - Fork 11
/
checklist.R
91 lines (80 loc) · 2.7 KB
/
checklist.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
#' Create a checklist.
#'
#' @usage checklist(scientificname = NULL, taxonid = NULL, datasetid = NULL,
#' nodeid = NULL, areaid = NULL, startdate = NULL, enddate = NULL,
#' startdepth = NULL, enddepth = NULL, geometry = NULL, redlist = NULL,
#' exclude = NULL, verbose = FALSE)
#' @param scientificname the scientific name.
#' @param taxonid the taxon identifier (WoRMS AphiaID).
#' @param datasetid the dataset identifier.
#' @param nodeid the OBIS node identifier.
#' @param areaid the OBIS area identifier.
#' @param startdate the earliest date on which occurrence took place.
#' @param enddate the latest date on which the occurrence took place.
#' @param startdepth the minimum depth below the sea surface.
#' @param enddepth the maximum depth below the sea surface.
#' @param geometry a WKT geometry string.
#' @param redlist include only IUCN Red List species.
#' @param exclude quality flags to be excluded from the results.
#' @param verbose logical. Optional parameter to enable verbose logging (default = \code{FALSE}).
#' @return The checklist.
#' @examples
#' taxa <- checklist(scientificname = "Tellinidae")
#' taxa <- checklist(geometry = "POLYGON ((2.3 51.8, 2.3 51.6, 2.6 51.6, 2.6 51.8, 2.3 51.8))")
#' taxa <- checklist(areaid = 10181)
#' @export
checklist <- function(
scientificname = NULL,
taxonid = NULL,
datasetid = NULL,
nodeid = NULL,
areaid = NULL,
startdate = NULL,
enddate = NULL,
startdepth = NULL,
enddepth = NULL,
geometry = NULL,
redlist = NULL,
exclude = NULL,
verbose = FALSE
) {
result_list <- list()
last_page <- FALSE
partition <- 0
fetched <- 0
while (!last_page) {
query <- list(
scientificname = handle_vector(scientificname),
taxonid = handle_vector(taxonid),
datasetid = handle_vector(datasetid),
nodeid = handle_vector(nodeid),
areaid = handle_vector(areaid),
startdate = handle_date(startdate),
enddate = handle_date(enddate),
startdepth = startdepth,
enddepth = enddepth,
geometry = geometry,
redlist = handle_logical(redlist),
exclude = handle_vector(exclude),
partition = partition
)
result <- http_request("GET", "checklist", query)
if (verbose) {
log_request(result)
}
stop_for_status(result)
text <- content(result, "text", encoding = "UTF-8")
res <- fromJSON(text, simplifyVector = TRUE)
total <- res$total
partition = partition + 1
if (partition >= res$partitions) {
last_page <- TRUE
}
result_list[[partition + 1]] <- res$results
fetched <- fetched + nrow(res$results)
log_progress(fetched, total)
}
data <- bind_rows(result_list)
data <- data[order(data$records, decreasing = TRUE),]
return(data)
}