mirror of
https://github.com/sensebox/opensensmapr
synced 2025-06-09 03:06:08 +02:00
auto paging for osem_measurements() (#2)
This commit is contained in:
parent
7efa6d4f8a
commit
dde4b17e3a
2 changed files with 28 additions and 18 deletions
|
@ -62,8 +62,10 @@ osem_measurements.bbox = function (x, phenomenon, exposure = NA,
|
||||||
...,
|
...,
|
||||||
endpoint = 'https://api.opensensemap.org') {
|
endpoint = 'https://api.opensensemap.org') {
|
||||||
bbox = x
|
bbox = x
|
||||||
query = parse_get_measurements_params(as.list(environment()))
|
environment() %>%
|
||||||
do.call(get_measurements_, query)
|
as.list() %>%
|
||||||
|
parse_get_measurements_params() %>%
|
||||||
|
paged_measurements_req()
|
||||||
}
|
}
|
||||||
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
|
@ -84,8 +86,10 @@ osem_measurements.sensebox = function (x, phenomenon, exposure = NA,
|
||||||
...,
|
...,
|
||||||
endpoint = 'https://api.opensensemap.org') {
|
endpoint = 'https://api.opensensemap.org') {
|
||||||
boxes = x
|
boxes = x
|
||||||
query = parse_get_measurements_params(as.list(environment()))
|
environment() %>%
|
||||||
do.call(get_measurements_, query)
|
as.list() %>%
|
||||||
|
parse_get_measurements_params() %>%
|
||||||
|
paged_measurements_req()
|
||||||
}
|
}
|
||||||
|
|
||||||
# ==============================================================================
|
# ==============================================================================
|
||||||
|
@ -114,9 +118,9 @@ parse_get_measurements_params = function (params) {
|
||||||
if (!is.null(params$bbox)) query$bbox = paste(params$bbox, collapse = ',')
|
if (!is.null(params$bbox)) query$bbox = paste(params$bbox, collapse = ',')
|
||||||
|
|
||||||
if (!is.na(params$from) && !is.na(params$to)) {
|
if (!is.na(params$from) && !is.na(params$to)) {
|
||||||
dates = parse_dateparams(params$from, params$to)
|
parse_dateparams(params$from, params$to) # only for validation sideeffect
|
||||||
query$`from-date` = dates[1]
|
query$`from-date` = utc_date(params$from)
|
||||||
query$`to-date` = dates[2]
|
query$`to-date` = utc_date(params$to)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!is.na(params$exposure)) query$exposure = params$exposure
|
if (!is.na(params$exposure)) query$exposure = params$exposure
|
||||||
|
@ -129,23 +133,29 @@ parse_get_measurements_params = function (params) {
|
||||||
}
|
}
|
||||||
|
|
||||||
paged_measurements_req = function (query) {
|
paged_measurements_req = function (query) {
|
||||||
if (is.na(query$from) && is.na(query$to))
|
# no paged requests when no dates are provided
|
||||||
|
if (is.na(query$`from-date`) && is.na(query$`to-date`))
|
||||||
return(do.call(get_measurements_, query))
|
return(do.call(get_measurements_, query))
|
||||||
|
|
||||||
# auto paging: make a request for one 31day interval each.
|
# auto paging: make a request for one 31day interval each (max supprted length)
|
||||||
|
# generate a list 31day intervals
|
||||||
from = query$from
|
from = query$from
|
||||||
to = query$to
|
to = query$to
|
||||||
|
dates = list()
|
||||||
dates = data.frame()
|
|
||||||
while (from < to) {
|
while (from < to) {
|
||||||
in31days = from + as.difftime(31, units = 'days')
|
in31days = from + as.difftime(31, units = 'days')
|
||||||
# TODO: how to do append to a vector / list instead?
|
dates = append(dates, list(list(from = from, to = min(in31days, to))))
|
||||||
dates = rbind(dates, data.frame(from = from, to = min(in31days, to)))
|
|
||||||
from = in31days + as.difftime(1, units = 'secs')
|
from = in31days + as.difftime(1, units = 'secs')
|
||||||
}
|
}
|
||||||
|
|
||||||
print(dates)
|
# use the dates as pages for multiple requests
|
||||||
|
lapply(dates, function(page) {
|
||||||
# TODO: iterate over all those dates and call get_measurements_
|
query$`from-date` = date_as_isostring(page$from)
|
||||||
|
query$`to-date` = date_as_isostring(page$to)
|
||||||
|
res = do.call(get_measurements_, query)
|
||||||
|
cat(paste(query$`from-date`, query$`to-date`, sep = ' - '))
|
||||||
|
cat('\n')
|
||||||
|
res
|
||||||
|
}) %>%
|
||||||
|
dplyr::bind_rows()
|
||||||
}
|
}
|
||||||
|
|
|
@ -121,7 +121,7 @@ berlin = st_point(c(13.4034, 52.5120)) %>%
|
||||||
pm25 = osem_measurements(
|
pm25 = osem_measurements(
|
||||||
berlin,
|
berlin,
|
||||||
phenomenon = 'PM2.5',
|
phenomenon = 'PM2.5',
|
||||||
from = now() - days(7), # defaults to 2 days, maximum 31 days
|
from = now() - days(7), # defaults to 2 days
|
||||||
to = now()
|
to = now()
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue