Skip to content

Commit 3abc630

Browse files
committed
fix #184 fix print.occdatind to not throw tibble column warnings when col doesnt exist
fix #182 fix stand_date internal fxn to convert ALA timestamps correctly and ecoengine format had changed too fix #181 added date param to occ() fxn to do date range searches across the data sources bump patch version for the new parameter added for changes to some data sources, now need dev versions of rbison, rvernet, and rgbif for date stuff to work for spocc_inat_obs: added d1 and d2 params to do date range search fixed some egs in occ_egs file
1 parent a252b92 commit 3abc630

15 files changed

+218
-83
lines changed

DESCRIPTION

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ Description: A programmatic interface to many species occurrence data sources,
88
System ('OBIS'), and Atlas of Living Australia ('ALA'). Includes
99
functionality for retrieving species occurrence data, and combining
1010
those data.
11-
Version: 0.7.0.9411
11+
Version: 0.7.2.9100
1212
License: MIT + file LICENSE
1313
Authors@R: c(
1414
person("Scott", "Chamberlain", role = c("aut", "cre"),
@@ -23,10 +23,10 @@ VignetteBuilder: knitr
2323
Roxygen: list(markdown = TRUE)
2424
Imports:
2525
utils,
26-
rgbif (>= 0.9.8),
27-
rbison (>= 0.5.0),
26+
rgbif (>= 0.9.9.9412),
27+
rbison (>= 0.5.5.9210),
2828
rebird (>= 0.3.0),
29-
rvertnet (>= 0.6.3.9110),
29+
rvertnet (>= 0.6.3.9112),
3030
ridigbio (>= 0.3.5),
3131
lubridate (>= 1.5.0),
3232
crul (>= 0.3.4),
@@ -40,5 +40,5 @@ Suggests:
4040
testthat,
4141
knitr,
4242
taxize (>= 0.8.4)
43-
Remotes: ropensci/rvertnet
43+
Remotes: ropensci/rvertnet, ropensci/rbison, ropensci/rgbif
4444
RoxygenNote: 6.0.1

R/inat.R

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
spocc_inat_obs <- function(query=NULL, taxon = NULL, quality=NULL, geo=TRUE,
22
year=NULL, month=NULL, day=NULL, bounds=NULL,
3+
date_start = NULL, date_end = NULL,
34
maxresults=100, page=NULL, callopts) {
45

56
# input parameter checks
@@ -45,7 +46,8 @@ spocc_inat_obs <- function(query=NULL, taxon = NULL, quality=NULL, geo=TRUE,
4546

4647
args <- sc(list(q = query, quality_grade = quality, taxon_name = taxon,
4748
`has[]` = if (!is.null(geo) && geo) "geo" else NULL,
48-
year = year, month = month, day = day))
49+
year = year, month = month, day = day,
50+
d1 = date_start, d2 = date_end))
4951
bounds <- list(swlat = bounds[1], swlng = bounds[2], nelat = bounds[3],
5052
nelng = bounds[4])
5153
args <- sc(c(args, bounds))

R/methods.r

+1
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ occinddf <- function(obj) {
154154

155155
cat(sprintf("First 10 rows of [%s]\n\n", nms))
156156

157+
if (NROW(z) == 0) return(data_frame())
157158
df <- data.frame(name = z$name, longitude = z$longitude,
158159
latitude = z$latitude, prov = z$prov,
159160
stringsAsFactors = FALSE)

R/occ.r

+30-21
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@
77
#' @template occtemp
88
#' @template occ_egs
99
occ <- function(query = NULL, from = "gbif", limit = 500, start = NULL,
10-
page = NULL, geometry = NULL, has_coords = NULL, ids = NULL, callopts=list(),
10+
page = NULL, geometry = NULL, has_coords = NULL, ids = NULL, date = NULL,
11+
callopts=list(),
1112
gbifopts = list(), bisonopts = list(), inatopts = list(),
1213
ebirdopts = list(), ecoengineopts = list(), antwebopts = list(),
1314
vertnetopts = list(), idigbioopts = list(), obisopts = list(),
@@ -43,37 +44,44 @@ occ <- function(query = NULL, from = "gbif", limit = 500, start = NULL,
4344
stop(
4445
sprintf(
4546
"Woops, the following are not supported or spelled incorrectly: %s",
46-
from[!from %in% sources]))
47+
from[!from %in% sources]),
48+
call. = FALSE)
4749
}
4850

49-
loopfun <- function(x, y, s, p, z, hc, w) {
51+
if (!is.null(date)) {
52+
if (!inherits(date, c('character', 'Date'))) {
53+
stop("'date' must be of class character or Date", call. = FALSE)
54+
}
55+
}
56+
57+
loopfun <- function(x, y, s, p, z, hc, d, w) {
5058
# x = query; y = limit; s = start; p = page;
51-
# z = geometry; hc = has_coords; w = callopts
52-
gbif_res <- foo_gbif(sources, x, y, s, z, hc, w, gbifopts)
53-
bison_res <- foo_bison(sources, x, y, s, z, w, bisonopts)
54-
inat_res <- foo_inat(sources, x, y, p, z, hc, w, inatopts)
59+
# z = geometry; hc = has_coords; d = date; w = callopts
60+
gbif_res <- foo_gbif(sources, x, y, s, z, hc, d, w, gbifopts)
61+
bison_res <- foo_bison(sources, x, y, s, z, d, w, bisonopts)
62+
inat_res <- foo_inat(sources, x, y, p, z, hc, d, w, inatopts)
5563
ebird_res <- foo_ebird(sources, x, y, w, ebirdopts)
56-
ecoengine_res <- foo_ecoengine(sources, x, y, p, z, hc, w, ecoengineopts)
57-
antweb_res <- foo_antweb(sources, x, y, s, z, hc, w, antwebopts)
58-
vertnet_res <- foo_vertnet(sources, x, y, hc, w, vertnetopts)
59-
idigbio_res <- foo_idigbio(sources, x, y, s, z, hc, w, idigbioopts)
60-
obis_res <- foo_obis(sources, x, y, s, z, hc, w, obisopts)
61-
ala_res <- foo_ala(sources, x, y, s, z, hc, w, alaopts)
64+
ecoengine_res <- foo_ecoengine(sources, x, y, p, z, hc, d, w, ecoengineopts)
65+
antweb_res <- foo_antweb(sources, x, y, s, z, hc, d, w, antwebopts)
66+
vertnet_res <- foo_vertnet(sources, x, y, hc, d, w, vertnetopts)
67+
idigbio_res <- foo_idigbio(sources, x, y, s, z, hc, d, w, idigbioopts)
68+
obis_res <- foo_obis(sources, x, y, s, z, hc, d, w, obisopts)
69+
ala_res <- foo_ala(sources, x, y, s, z, hc, d, w, alaopts)
6270
list(gbif = gbif_res, bison = bison_res, inat = inat_res, ebird = ebird_res,
6371
ecoengine = ecoengine_res, antweb = antweb_res, vertnet = vertnet_res,
6472
idigbio = idigbio_res, obis = obis_res, ala = ala_res)
6573
}
6674

67-
loopids <- function(x, y, s, p, z, hc, w) {
75+
loopids <- function(x, y, s, p, z, hc, d, w) {
6876
classes <- class(x)
6977
if (!all(classes %in% c("gbifid", "tsn")))
7078
stop("Currently, taxon identifiers have to be of class gbifid or tsn",
7179
call. = FALSE)
7280
if (class(x) == 'gbifid') {
73-
gbif_res <- foo_gbif(sources, x, y, s, z, hc, w, gbifopts)
81+
gbif_res <- foo_gbif(sources, x, y, s, z, hc, d, w, gbifopts)
7482
bison_res <- list(time = NULL, data = data_frame())
7583
} else if (class(x) == 'tsn') {
76-
bison_res <- foo_bison(sources, x, y, s, z, w, bisonopts)
84+
bison_res <- foo_bison(sources, x, y, s, z, d, w, bisonopts)
7785
gbif_res <- list(time = NULL, data = data_frame())
7886
}
7987
list(gbif = gbif_res,
@@ -106,6 +114,7 @@ occ <- function(query = NULL, from = "gbif", limit = 500, start = NULL,
106114
p = page,
107115
x = query[[i]],
108116
hc = has_coords,
117+
d = date,
109118
w = callopts)
110119
})
111120

@@ -133,7 +142,7 @@ occ <- function(query = NULL, from = "gbif", limit = 500, start = NULL,
133142
}
134143
} else {
135144
tmp <- lapply(query, loopfun, y = limit, s = start, p = page,
136-
z = geometry, hc = has_coords, w = callopts)
145+
z = geometry, hc = has_coords, d = date, w = callopts)
137146
}
138147
} else if (is.null(query) && is.null(geometry) && !is.null(ids)) {
139148
unlistids <- function(x) {
@@ -164,19 +173,19 @@ occ <- function(query = NULL, from = "gbif", limit = 500, start = NULL,
164173
# ids can only be passed to gbif and bison for now
165174
# so don't pass anything on to ecoengine, inat, or ebird
166175
tmp <- lapply(ids, loopids, y = limit, s = start, p = page,
167-
z = geometry, hc = has_coords, w = callopts)
176+
z = geometry, hc = has_coords, d = date, w = callopts)
168177
} else if (is.null(query) && is.null(geometry) && is.null(ids)) {
169178
tmp <- list(loopfun(x = query, y = limit, s = start, p = page,
170-
z = geometry, hc = has_coords, w = callopts))
179+
z = geometry, hc = has_coords, d = date, w = callopts))
171180
} else {
172181
type <- 'geometry'
173182
if (is.numeric(geometry) || is.character(geometry)) {
174183
tmp <- list(loopfun(z = geometry, y = limit, s = start, p = page,
175-
x = query, hc = has_coords, w = callopts))
184+
x = query, hc = has_coords, d = date, w = callopts))
176185
} else if (is.list(geometry)) {
177186
tmp <- lapply(geometry, function(b) {
178187
loopfun(z = b, y = limit, s = start, p = page,
179-
x = query, hc = has_coords, w = callopts)
188+
x = query, hc = has_coords, d = date, w = callopts)
180189
})
181190
}
182191
}

R/plugin_helpers.R

+8-2
Original file line numberDiff line numberDiff line change
@@ -42,16 +42,22 @@ stand_dates <- function(dat, from){
4242
bison = as_date(ydm_hm(dat[[var]], truncated = 6, quiet = TRUE)),
4343
inat = as_date(ymd_hms(dat[[var]], truncated = 3, quiet = TRUE)),
4444
ebird = as_date(ymd_hm(dat[[var]], truncated = 3, quiet = TRUE)),
45-
ecoengine = as_date(ymd(dat[[var]], truncated = 3, quiet = TRUE)),
45+
ecoengine = as_date(ymd_hms(dat[[var]], truncated = 3, quiet = TRUE)),
4646
vertnet = as_date(ymd(dat[[var]], truncated = 3, quiet = TRUE)),
4747
idigbio = as_date(ymd_hms(dat[[var]], truncated = 3, quiet = TRUE)),
4848
obis = as_date(ymd_hms(dat[[var]], truncated = 3, quiet = TRUE)),
49-
ala = as_date(ymd_hms(dat[[var]], truncated = 3, quiet = TRUE))
49+
ala = as_date(date_ala(dat[[var]]))
5050
)
5151
if (from == "bison") rename(dat, stats::setNames('date', var)) else dat
5252
}
5353
}
5454

55+
date_ala <- function(x) {
56+
x <- as.numeric(substr(x, 1, 10))
57+
x <- as.POSIXct(x, origin = "1970-01-01", tz = "UTC")
58+
sub("\\sUTC$", "", x)
59+
}
60+
5561
is_null <- function(...) {
5662
xx <- tryCatch(..., error = function(e) e)
5763
inherits(xx, "error") || is.null(xx)

R/plugins.r

+63-10
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## the plugins
33
#' @noRd
44
foo_gbif <- function(sources, query, limit, start, geometry, has_coords,
5-
callopts, opts) {
5+
date, callopts, opts) {
66
if (any(grepl("gbif", sources))) {
77

88
opts$hasCoordinate <- has_coords
@@ -24,6 +24,10 @@ foo_gbif <- function(sources, query, limit, start, geometry, has_coords,
2424
} else {
2525
query_use <- NULL
2626
}
27+
if (!is.null(date)) {
28+
if (length(date) != 2) stop("'date' for GBIF must be length 2")
29+
opts$eventDate <- paste0(date, collapse = ",")
30+
}
2731

2832
if (is.null(query_use) && is.null(geometry) && length(opts) == 0) {
2933
warning(sprintf("No records found in GBIF for %s", query), call. = FALSE)
@@ -88,7 +92,7 @@ foo_gbif <- function(sources, query, limit, start, geometry, has_coords,
8892

8993
#' @noRd
9094
foo_ecoengine <- function(sources, query, limit, page, geometry, has_coords,
91-
callopts, opts) {
95+
date, callopts, opts) {
9296
if (any(grepl("ecoengine", sources))) {
9397
opts <- limit_alias(opts, "ecoengine")
9498
time <- now()
@@ -105,6 +109,11 @@ foo_ecoengine <- function(sources, query, limit, page, geometry, has_coords,
105109
geometry
106110
}
107111
}
112+
if (!is.null(date)) {
113+
if (length(date) != 2) stop("'date' for Ecoengine must be length 2")
114+
opts$min_date <- date[1]
115+
opts$max_date <- date[2]
116+
}
108117
# This could hang things if request is super large. Will deal with this issue
109118
# when it arises in a usecase
110119
# For now default behavior is to retrive one page.
@@ -141,7 +150,7 @@ foo_ecoengine <- function(sources, query, limit, page, geometry, has_coords,
141150

142151
#' @noRd
143152
foo_antweb <- function(sources, query, limit, start, geometry, has_coords,
144-
callopts, opts) {
153+
date, callopts, opts) {
145154
if (any(grepl("antweb", sources))) {
146155
time <- now()
147156
opts$georeferenced <- has_coords
@@ -158,8 +167,15 @@ foo_antweb <- function(sources, query, limit, start, geometry, has_coords,
158167
opts$scientific_name <- NULL
159168
}
160169

170+
if (!is.null(date)) {
171+
if (length(date) != 2) stop("'date' for Ecoengine must be length 2")
172+
opts$min_date <- date[1]
173+
opts$max_date <- date[2]
174+
}
175+
161176
if (!'limit' %in% names(opts)) opts$limit <- limit
162177
if (!'offset' %in% names(opts)) opts$offset <- start
178+
if (length(callopts) > 0) opts$callopts <- callopts
163179
out <- tryCatch(do.call(aw_data2, opts), error = function(e) e)
164180

165181
if (is.null(out) || inherits(out, "simpleError")) {
@@ -181,7 +197,9 @@ foo_antweb <- function(sources, query, limit, start, geometry, has_coords,
181197
}
182198

183199
#' @noRd
184-
foo_bison <- function(sources, query, limit, start, geometry, callopts, opts) {
200+
foo_bison <- function(sources, query, limit, start, geometry, date,
201+
callopts, opts) {
202+
185203
if (any(grepl("bison", sources))) {
186204
opts <- limit_alias(opts, "bison", geometry)
187205
if (class(query) %in% c("ids","tsn")) {
@@ -190,10 +208,12 @@ foo_bison <- function(sources, query, limit, start, geometry, callopts, opts) {
190208
} else {
191209
opts$TSNs <- query
192210
}
211+
if (!is.null(date)) opts$eventDate <- date
193212
bisonfxn <- "bison_solr"
194213
} else {
195214
if (is.null(geometry)) {
196-
opts$scientificName <- query
215+
opts$ITISscientificName <- query
216+
if (!is.null(date)) opts$eventDate <- date
197217
bisonfxn <- "bison_solr"
198218
} else {
199219
opts$species <- query
@@ -250,7 +270,8 @@ foo_bison <- function(sources, query, limit, start, geometry, callopts, opts) {
250270

251271
#' @noRd
252272
foo_inat <- function(sources, query, limit, page, geometry, has_coords,
253-
callopts, opts) {
273+
date, callopts, opts) {
274+
254275
if (any(grepl("inat", sources))) {
255276
opts <- limit_alias(opts, "inat")
256277
opts$geo <- has_coords
@@ -268,6 +289,11 @@ foo_inat <- function(sources, query, limit, page, geometry, has_coords,
268289
c(geometry[2], geometry[1], geometry[4], geometry[3])
269290
}
270291
}
292+
if (!is.null(date)) {
293+
if (length(date) != 2) stop("'date' for Inaturalist must be length 2")
294+
opts$date_start <- date[1]
295+
opts$date_end <- date[2]
296+
}
271297
opts$callopts <- callopts
272298
out <- tryCatch(do.call("spocc_inat_obs", opts), error = function(e) e)
273299
if (!is.data.frame(out$data) || inherits(out, "simpleError")) {
@@ -325,14 +351,25 @@ foo_ebird <- function(sources, query, limit, callopts, opts) {
325351
}
326352

327353
#' @noRd
328-
foo_vertnet <- function(sources, query, limit, has_coords, callopts, opts) {
354+
foo_vertnet <- function(sources, query, limit, has_coords, date, callopts, opts) {
329355
if (any(grepl("vertnet", sources))) {
330356
time <- now()
331357
if (!is.null(has_coords)) {
332358
opts$mappable <- has_coords
333359
}
334360
opts$query <- query
335361
opts$messages <- FALSE
362+
if (!is.null(date)) {
363+
if (length(date) != 2) stop("'date' for Vertnet must be length 2")
364+
date <- tryCatch(as.Date(date), error = function(e) e)
365+
if (inherits(date, "error")) stop("'date' values do not appear to be dates")
366+
opts$year <- c(paste0('>=', format(date[1], "%Y")),
367+
paste0('<=', format(date[2], "%Y")))
368+
opts$month <- c(paste0('>=', as.numeric(format(date[1], "%m"))),
369+
paste0('<=', as.numeric(format(date[2], "%m"))))
370+
opts$day <- c(paste0('>=', as.numeric(format(date[1], "%d"))),
371+
paste0('<=', as.numeric(format(date[2], "%d"))))
372+
}
336373
if (!'limit' %in% names(opts)) opts$limit <- limit
337374
opts$callopts <- callopts
338375
out <- tryCatch(do.call(rvertnet::searchbyterm, opts),
@@ -363,7 +400,7 @@ foo_vertnet <- function(sources, query, limit, has_coords, callopts, opts) {
363400

364401
#' @noRd
365402
foo_idigbio <- function(sources, query, limit, start, geometry, has_coords,
366-
callopts, opts) {
403+
date, callopts, opts) {
367404
if (any(grepl("idigbio", sources))) {
368405
time <- now()
369406

@@ -375,6 +412,11 @@ foo_idigbio <- function(sources, query, limit, start, geometry, has_coords,
375412
list(type = "missing")
376413
}
377414

415+
if (!is.null(date)) {
416+
if (length(date) != 2) stop("'date' for IdigBio must be length 2")
417+
opts$rq$datecollected <- list(type = "range", gte = date[1], lte = date[2])
418+
}
419+
378420
if (!is.null(geometry)) {
379421
if (grepl('POLYGON', paste(as.character(geometry), collapse = " "))) {
380422
geometry <- unlist(unname(c(wkt2bbox(geometry))))
@@ -429,7 +471,7 @@ foo_idigbio <- function(sources, query, limit, start, geometry, has_coords,
429471

430472
#' @noRd
431473
foo_obis <- function(sources, query, limit, start, geometry, has_coords,
432-
callopts, opts) {
474+
date, callopts, opts) {
433475

434476
if (any(grepl("obis", sources))) {
435477
time <- now()
@@ -444,6 +486,12 @@ foo_obis <- function(sources, query, limit, start, geometry, has_coords,
444486
}
445487
}
446488

489+
if (!is.null(date)) {
490+
if (length(date) != 2) stop("'date' for OBIS must be length 2")
491+
opts$startdate <- date[1]
492+
opts$enddate <- date[2]
493+
}
494+
447495
if (!"limit" %in% names(opts)) opts$limit <- limit
448496
if (!'offset' %in% names(opts)) opts$offset <- start
449497

@@ -475,10 +523,15 @@ foo_obis <- function(sources, query, limit, start, geometry, has_coords,
475523

476524
#' @noRd
477525
foo_ala <- function(sources, query, limit, start, geometry, has_coords,
478-
callopts, opts) {
526+
date, callopts, opts) {
527+
479528
if (any(grepl("ala", sources))) {
480529
time <- now()
481530
opts$taxon <- sprintf('taxon_name:"%s"', query)
531+
if (!is.null(date)) {
532+
if (length(date) != 2) stop("'date' for ALA must be length 2")
533+
opts$taxon <- paste0(opts$taxon, sprintf(" occurrence_date:[%s TO %s]", date[1], date[2]))
534+
}
482535

483536
if (!is.null(geometry)) {
484537
opts$wkt <- if (grepl('POLYGON', paste(as.character(geometry),

0 commit comments

Comments
 (0)