From 728329dfa7d4714374f9c6088d59e283800126b9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ma=C3=ABlle=20Salmon?= Date: Mon, 14 Mar 2022 14:26:04 +0100 Subject: [PATCH] add memoising for layer info (#52) --- NAMESPACE | 1 + NEWS.md | 3 ++ R/info.R | 105 +++++++++++++++++++----------------- man/emodnet_get_wfs_info.Rd | 23 ++++---- 4 files changed, 74 insertions(+), 58 deletions(-) diff --git a/NAMESPACE b/NAMESPACE index 4ebb5fa..d8603a9 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -13,5 +13,6 @@ export(layer_attributes_get_names) export(layer_attributes_summarise) export(layer_attributes_tbl) importFrom(magrittr,"%>%") +importFrom(memoise,memoise) importFrom(rlang,.data) importFrom(rlang,`%||%`) diff --git a/NEWS.md b/NEWS.md index d5617a2..5bebd38 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,5 +1,8 @@ # EMODnetWFS (development version) +* Added memoising (caching during each R session) of the functions getting services + and layers information (#52). + # EMODnetWFS 2.0.1 * Introduced better handling of server response errors. diff --git a/R/info.R b/R/info.R index c7236e8..e7605d0 100644 --- a/R/info.R +++ b/R/info.R @@ -1,21 +1,43 @@ -#' Get WFS available layer information -#' -#' @param wfs A `WFSClient` R6 object with methods for interfacing an OGC Web Feature Service. -#' @inheritParams emodnet_init_wfs_client -#' @importFrom rlang .data `%||%` -#' @return a tibble containg metadata on each layer available from the service. +.emodnet_get_layer_info <- function(wfs, layers) { + + check_wfs(wfs) + + layers <- namespace_layer_names(wfs, layers) + + capabilities <- wfs$getCapabilities() + + wfs_layers <- purrr::map(layers, capabilities$findFeatureTypeByName) %>% + unlist(recursive = FALSE) + + tibble::tibble( + data_source = "emodnet_wfs", + service_name = wfs$getUrl(), + service_url = get_service_name(wfs$getUrl()), + layer_name = purrr::map_chr(wfs_layers, ~.x$getName()), + title = purrr::map_chr(wfs_layers, ~.x$getTitle()), + abstract = purrr::map_chr(wfs_layers, ~getAbstractNull(.x)), + class = purrr::map_chr(wfs_layers, ~.x$getClassName()), + format = "sf" + ) %>% + tidyr::separate( + .data$layer_name, + into = c("layer_namespace", "layer_name"), + sep = ":" + ) %>% + unique() +} + +#' @describeIn emodnet_get_wfs_info Get metadata for specific layers. Requires a +#' `wfs` object as input. +#' @inheritParams emodnet_get_layers +#' @importFrom memoise memoise +#' @details To minimize the number of requests sent to webservices, +#' these functions use `memoise` to cache results inside the active R session. +#' To clear the cache, re-start R or run `memoise::forget(emodnet_get_wfs_info)`/`memoise::forget(emodnet_get_layer_info)`. #' @export -#' @describeIn emodnet_get_wfs_info Get info on all layers from am EMODnet WFS service. -#' @examples -#' emodnet_get_wfs_info(service = "bathymetry") -#' # Query a wfs object -#' wfs_cml <- emodnet_init_wfs_client("chemistry_marine_litter") -#' emodnet_get_wfs_info(wfs_cml) -#' # Get info for specific layers from wfs object -#' layers <- c("bl_fishing_monitoring", -#' "bl_beacheslocations_monitoring") -#' emodnet_get_layer_info(wfs = wfs_cml, layers = layers) -emodnet_get_wfs_info <- function(wfs = NULL, service = NULL, service_version = "2.0.0") { +emodnet_get_layer_info <- memoise::memoise(.emodnet_get_layer_info) + +.emodnet_get_wfs_info <- function(wfs = NULL, service = NULL, service_version = "2.0.0") { if(is.null(wfs) & is.null(service)){ usethis::ui_stop("Please provide a valid {usethis::ui_field('service')} name or {usethis::ui_field('wfs')} object. @@ -42,39 +64,24 @@ emodnet_get_wfs_info <- function(wfs = NULL, service = NULL, service_version = " sep = ":") } - -#' @describeIn emodnet_get_wfs_info Get metadata for specific layers. Requires a -#' `wfs` object as input. -#' @inheritParams emodnet_get_layers +#' Get WFS available layer information +#' +#' @param wfs A `WFSClient` R6 object with methods for interfacing an OGC Web Feature Service. +#' @inheritParams emodnet_init_wfs_client +#' @importFrom rlang .data `%||%` +#' @return a tibble containg metadata on each layer available from the service. #' @export -emodnet_get_layer_info <- function(wfs, layers) { - - check_wfs(wfs) - - layers <- namespace_layer_names(wfs, layers) - - capabilities <- wfs$getCapabilities() - - wfs_layers <- purrr::map(layers, capabilities$findFeatureTypeByName) %>% - unlist(recursive = FALSE) - - tibble::tibble( - data_source = "emodnet_wfs", - service_name = wfs$getUrl(), - service_url = get_service_name(wfs$getUrl()), - layer_name = purrr::map_chr(wfs_layers, ~.x$getName()), - title = purrr::map_chr(wfs_layers, ~.x$getTitle()), - abstract = purrr::map_chr(wfs_layers, ~getAbstractNull(.x)), - class = purrr::map_chr(wfs_layers, ~.x$getClassName()), - format = "sf" - ) %>% - tidyr::separate( - .data$layer_name, - into = c("layer_namespace", "layer_name"), - sep = ":" - ) %>% - unique() -} +#' @describeIn emodnet_get_wfs_info Get info on all layers from am EMODnet WFS service. +#' @examples +#' emodnet_get_wfs_info(service = "bathymetry") +#' # Query a wfs object +#' wfs_cml <- emodnet_init_wfs_client("chemistry_marine_litter") +#' emodnet_get_wfs_info(wfs_cml) +#' # Get info for specific layers from wfs object +#' layers <- c("bl_fishing_monitoring", +#' "bl_beacheslocations_monitoring") +#' emodnet_get_layer_info(wfs = wfs_cml, layers = layers) +emodnet_get_wfs_info <- memoise::memoise(.emodnet_get_wfs_info) #' @describeIn emodnet_get_wfs_info Get metadata on all layers and all available #' services from server. diff --git a/man/emodnet_get_wfs_info.Rd b/man/emodnet_get_wfs_info.Rd index 397dfa1..f3bf780 100644 --- a/man/emodnet_get_wfs_info.Rd +++ b/man/emodnet_get_wfs_info.Rd @@ -1,27 +1,27 @@ % Generated by roxygen2: do not edit by hand % Please edit documentation in R/info.R -\name{emodnet_get_wfs_info} -\alias{emodnet_get_wfs_info} +\name{emodnet_get_layer_info} \alias{emodnet_get_layer_info} +\alias{emodnet_get_wfs_info} \alias{emodnet_get_all_wfs_info} \title{Get WFS available layer information} \usage{ -emodnet_get_wfs_info(wfs = NULL, service = NULL, service_version = "2.0.0") - emodnet_get_layer_info(wfs, layers) +emodnet_get_wfs_info(wfs = NULL, service = NULL, service_version = "2.0.0") + emodnet_get_all_wfs_info() } \arguments{ \item{wfs}{A \code{WFSClient} R6 object with methods for interfacing an OGC Web Feature Service.} +\item{layers}{a character vector of layer names. To get info on layers, including +\code{layer_name} use \code{\link[=emodnet_get_wfs_info]{emodnet_get_wfs_info()}}.} + \item{service}{the EMODnet OGC WFS service name. For available services, see \code{emodnet_wfs}.} \item{service_version}{the WFS service version. Defaults to "2.0.0".} - -\item{layers}{a character vector of layer names. To get info on layers, including -\code{layer_name} use \code{\link[=emodnet_get_wfs_info]{emodnet_get_wfs_info()}}.} } \value{ a tibble containg metadata on each layer available from the service. @@ -29,13 +29,18 @@ a tibble containg metadata on each layer available from the service. \description{ Get WFS available layer information } +\details{ +To minimize the number of requests sent to webservices, +these functions use \code{memoise} to cache results inside the active R session. +To clear the cache, re-start R or run \code{memoise::forget(emodnet_get_wfs_info)}/\code{memoise::forget(emodnet_get_layer_info)}. +} \section{Functions}{ \itemize{ -\item \code{emodnet_get_wfs_info}: Get info on all layers from am EMODnet WFS service. - \item \code{emodnet_get_layer_info}: Get metadata for specific layers. Requires a \code{wfs} object as input. +\item \code{emodnet_get_wfs_info}: Get info on all layers from am EMODnet WFS service. + \item \code{emodnet_get_all_wfs_info}: Get metadata on all layers and all available services from server. }}