You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
121 lines
4.7 KiB
R
121 lines
4.7 KiB
R
7 years ago
|
# ==============================================================================
|
||
|
#
|
||
|
#' Get the Measurements of a Phenomenon on opensensemap.org
|
||
|
#'
|
||
|
#' Measurements can be retrieved either for a set of boxes, or through a spatial
|
||
|
#' bounding box filter. To get all measurements, the \code{default} function applies
|
||
|
#' a bounding box spanning the whole world.
|
||
|
#'
|
||
7 years ago
|
#' @param x Depending on the method, either
|
||
|
#' \enumerate{
|
||
|
#' \item a \code{chr} specifying the phenomenon, see \code{phenomenon}
|
||
|
#' \item a \code{\link[sf]{st_bbox}} to select sensors spatially,
|
||
|
#' \item a \code{sensebox data.frame} to select boxes from which
|
||
|
#' measurements will be retrieved,
|
||
|
#' }
|
||
|
#' @param ... see parameters below
|
||
7 years ago
|
#' @param phenomenon The phenomenon to retrieve measurements for
|
||
|
#' @param exposure Filter sensors by their exposure ('indoor', 'outdoor', 'mobile')
|
||
|
#' @param from A \code{POSIXt} like object to select a time interval
|
||
|
#' @param to A \code{POSIXt} like object to select a time interval
|
||
|
#' @param columns Select specific column in the output (see oSeM documentation)
|
||
|
#' @param endpoint The URL of the openSenseMap API
|
||
|
#'
|
||
7 years ago
|
#' @return An \code{osem_measurements data.frame} containing the
|
||
7 years ago
|
#' requested measurements
|
||
|
#'
|
||
|
#' @export
|
||
7 years ago
|
#' @seealso \href{https://docs.opensensemap.org/#api-Measurements-getDataMulti}{openSenseMap API documentation (web)}
|
||
|
#' @seealso \code{\link{osem_boxes}}
|
||
7 years ago
|
osem_measurements = function (x, ...) UseMethod('osem_measurements')
|
||
|
|
||
7 years ago
|
# ==============================================================================
|
||
|
#
|
||
7 years ago
|
#' @describeIn osem_measurements Get measurements from \strong{all} senseBoxes.
|
||
7 years ago
|
#' @export
|
||
|
#' @examples
|
||
7 years ago
|
#' # get measurements from all boxes
|
||
|
#' \dontrun{
|
||
7 years ago
|
#' osem_measurements('PM2.5')
|
||
7 years ago
|
#' }
|
||
|
#'
|
||
|
osem_measurements.default = function (x, ...) {
|
||
7 years ago
|
bbox = structure(c(-180, -90, 180, 90), class = 'bbox')
|
||
7 years ago
|
osem_measurements(bbox, x, ...)
|
||
7 years ago
|
}
|
||
|
|
||
7 years ago
|
# ==============================================================================
|
||
|
#
|
||
7 years ago
|
#' @describeIn osem_measurements Get measurements by a spatial filter.
|
||
7 years ago
|
#' @export
|
||
|
#' @examples
|
||
7 years ago
|
#' # get measurements from sensors within a bounding box
|
||
|
#' bbox = structure(c(7, 51, 8, 52), class = 'bbox')
|
||
|
#' osem_measurements(bbox, 'Temperatur')
|
||
7 years ago
|
#'
|
||
7 years ago
|
#' points = sf::st_multipoint(x = matrix(c(7,8,51,52),2,2))
|
||
|
#' bbox2 = sf::st_bbox(points)
|
||
|
#' osem_measurements(bbox2, 'Temperatur', exposure = 'outdoor')
|
||
7 years ago
|
#'
|
||
7 years ago
|
osem_measurements.bbox = function (x, phenomenon, exposure = NA,
|
||
|
from = NA, to = NA, columns = NA,
|
||
|
...,
|
||
7 years ago
|
endpoint = 'https://api.opensensemap.org') {
|
||
7 years ago
|
bbox = x
|
||
7 years ago
|
query = parse_get_measurements_params(as.list(environment()))
|
||
|
do.call(get_measurements_, query)
|
||
|
}
|
||
|
|
||
7 years ago
|
# ==============================================================================
|
||
|
#
|
||
7 years ago
|
#' @describeIn osem_measurements Get measurements from a set of senseBoxes.
|
||
7 years ago
|
#' @export
|
||
|
#' @examples
|
||
7 years ago
|
#' # get measurements from a set of boxes
|
||
|
#' b = osem_boxes(grouptag = 'ifgi')
|
||
|
#' osem_measurements(b, phenomenon = 'Temperatur')
|
||
7 years ago
|
#'
|
||
7 years ago
|
#' # ...or a single box
|
||
7 years ago
|
#' b = osem_box('593bcd656ccf3b0011791f5a')
|
||
7 years ago
|
#' osem_measurements(b, phenomenon = 'Temperatur')
|
||
|
#'
|
||
|
osem_measurements.sensebox = function (x, phenomenon, exposure = NA,
|
||
|
from = NA, to = NA, columns = NA,
|
||
|
...,
|
||
7 years ago
|
endpoint = 'https://api.opensensemap.org') {
|
||
7 years ago
|
boxes = x
|
||
7 years ago
|
query = parse_get_measurements_params(as.list(environment()))
|
||
|
do.call(get_measurements_, query)
|
||
|
}
|
||
|
|
||
7 years ago
|
# ==============================================================================
|
||
|
#
|
||
7 years ago
|
#' Validates and parses the Parameters for use in \code{osem_measurements()}
|
||
7 years ago
|
#'
|
||
|
#' @param params A named \code{list} of parameters
|
||
|
#' @return A named \code{list} of parsed parameters.
|
||
|
#' @noRd
|
||
7 years ago
|
parse_get_measurements_params = function (params) {
|
||
|
if (is.null(params$phenomenon) | is.na(params$phenomenon))
|
||
|
stop('Parameter "phenomenon" is required')
|
||
|
if (!is.na(params$from) && is.na(params$to)) stop('specify "from" only together with "to"')
|
||
|
if (
|
||
|
(!is.null(params$bbox) && !is.null(params$boxes)) ||
|
||
|
(is.null(params$bbox) && is.null(params$boxes))
|
||
|
) stop('Specify either "bbox" or "boxes"')
|
||
|
|
||
|
query = list(endpoint = params$endpoint, phenomenon = params$phenomenon)
|
||
|
|
||
|
if (!is.null(params$boxes)) query$boxIds = paste(params$boxes$X_id, collapse = ',')
|
||
|
if (!is.null(params$bbox)) query$bbox = paste(params$bbox, collapse = ',')
|
||
|
|
||
7 years ago
|
if (!is.na(params$from))
|
||
|
query$`from-date` = utc_date(params$from) %>% date_as_isostring()
|
||
|
if (!is.na(params$to))
|
||
|
query$`to-date` = utc_date(params$to) %>% date_as_isostring()
|
||
7 years ago
|
if (!is.na(params$exposure)) query$exposure = params$exposure
|
||
|
if (!is.na(params$columns)) query$columns = paste(params$columns, collapse = ',')
|
||
|
|
||
|
query
|
||
|
}
|