Skip to content

Commit

Permalink
add memoising for layer info (#52)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle authored Mar 14, 2022
1 parent 35933ba commit 728329d
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 58 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -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,`%||%`)
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
105 changes: 56 additions & 49 deletions R/info.R
Original file line number Diff line number Diff line change
@@ -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.
Expand All @@ -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.
Expand Down
23 changes: 14 additions & 9 deletions man/emodnet_get_wfs_info.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 728329d

Please sign in to comment.