Skip to content

Commit

Permalink
refactor: shorten lines (#149)
Browse files Browse the repository at this point in the history
  • Loading branch information
maelle authored Apr 7, 2023
1 parent cf137e9 commit b2fe4c7
Show file tree
Hide file tree
Showing 27 changed files with 348 additions and 158 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
^Meta$
^attic$
^codemeta\.json$
^vignettes/EMODnetWFS\.Rmd\.orig$
24 changes: 19 additions & 5 deletions R/EMODnetWFS-package.R
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,33 @@ emodnetwfs_user_agent <- function() {
version <- as.character(utils::packageVersion("EMODnetWFS"))

if (nzchar(Sys.getenv("EMODNETWFS_CI"))) {
return(sprintf("EMODnetWFS R package %s CI https://github.com/EMODnet/EMODnetWFS", version))
return(
sprintf(
"EMODnetWFS R package %s CI https://github.com/EMODnet/EMODnetWFS",
version
)
)
}

gh_username <- try(whoami::gh_username(), silent = TRUE)
if (!inherits(gh_username, "try-error") && gh_username %in% emodnetwfs_collaborators()) {
return(sprintf("EMODnetWFS R package %s DEV https://github.com/EMODnet/EMODnetWFS", version))
if (!inherits(gh_username, "try-error") &&
gh_username %in% emodnetwfs_collaborators()) {
return(
sprintf(
"EMODnetWFS R package %s DEV https://github.com/EMODnet/EMODnetWFS",
version
)
)
}

sprintf("EMODnetWFS R package %s https://github.com/EMODnet/EMODnetWFS", version)
sprintf(
"EMODnetWFS R package %s https://github.com/EMODnet/EMODnetWFS",
version
)
}

globalVariables(c("layer_name", "n"))

release_bullets <- function() {
c('update vignette with knitr::knit("vignettes/EMODnetWFS.Rmd.orig", output = "vignettes/EMODnetWFS.Rmd")')
c('update vignette with knitr::knit("vignettes/EMODnetWFS.Rmd.orig", output = "vignettes/EMODnetWFS.Rmd")')
}
32 changes: 23 additions & 9 deletions R/client.R
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,28 @@
#'
#' @param service the EMODnet OGC WFS service name.
#' For available services, see [`emodnet_wfs()`].
#' @param service_version `r lifecycle::badge('deprecated')` the WFS service version. Now always "2.0.0".
#' @param logger the logger. Either `NULL` (no logging info), `"INFO"` (log about ows4R requests)
#' @param service_version `r lifecycle::badge('deprecated')`
#' the WFS service version. Now always "2.0.0".
#' @param logger the logger. Either `NULL` (no logging info), `"INFO"`
#' (log about ows4R requests)
#' or `"DEBUG"` (including curl details).
#'
#' @return An [`ows4R::WFSClient`] R6 object with methods for interfacing an OGC Web Feature Service.
#' @return An [`ows4R::WFSClient`] R6 object with methods for interfacing an
#' OGC Web Feature Service.
#' @export
#'
#' @seealso `WFSClient` in package `ows4R`.
#' @examples
#' \dontrun{
#' wfs <- emodnet_init_wfs_client(service = "bathymetry")
#' }
emodnet_init_wfs_client <- function(service, service_version = NULL, logger = NULL) {
deprecate_message_service_version(service_version, "deprecate_message_service_version")
emodnet_init_wfs_client <- function(service,
service_version = NULL,
logger = NULL) {
deprecate_message_service_version(
service_version,
"deprecate_message_service_version"
)

check_service_name(service)

Expand Down Expand Up @@ -97,12 +105,18 @@ check_service <- function(request) {
if (httr::http_error(request)) {
cli_alert_danger("HTTP Status: {httr::http_status(request)$message}")

is_monitor_up <- !is.null(curl::nslookup("monitor.emodnet.eu", error = FALSE))
is_monitor_up <- !is.null(
curl::nslookup("monitor.emodnet.eu", error = FALSE)
)
if (interactive() && is_monitor_up) {
browse_monitor <- utils::askYesNo("Browse the EMODnet OGC monitor?", FALSE, prompts = "yes/no/cancel")
browse_monitor <- utils::askYesNo(
"Browse the EMODnet OGC monitor?",
FALSE,
prompts = "yes/no/cancel"
)
if (is.na(browse_monitor)) browse_monitor <- FALSE
if (browse_monitor) {
utils::browseURL("https://monitor.emodnet.eu/resources?lang=en&resource_type=OGC:WFS")
utils::browseURL("https://monitor.emodnet.eu/resources?lang=en&resource_type=OGC:WFS") # nolint
}
}

Expand All @@ -115,7 +129,7 @@ check_service <- function(request) {
cli::cli_abort(
c(
"An exception has occurred.",
i = "Please raise an issue in {packageDescription('EMODnetWFS')$BugReports}"
i = "Please raise an issue in {packageDescription('EMODnetWFS')$BugReports}" # nolint
)
)
}
Expand Down
22 changes: 16 additions & 6 deletions R/info.R
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@
#' @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)`.
#' To clear the cache, re-start R or
#' run `memoise::forget(emodnet_get_wfs_info)`/
#' `memoise::forget(emodnet_get_layer_info)`.
#' @export
emodnet_get_layer_info <- memoise::memoise(.emodnet_get_layer_info)

.emodnet_get_wfs_info <- function(wfs = NULL, service = NULL, service_version = NULL) {
.emodnet_get_wfs_info <- function(wfs = NULL,
service = NULL,
service_version = NULL) {
deprecate_message_service_version(service_version, "emodnet_get_wfs_info")

if (is.null(wfs) && is.null(service)) {
Expand All @@ -59,7 +63,10 @@ emodnet_get_layer_info <- memoise::memoise(.emodnet_get_layer_info)
service_url = capabilities$getUrl(),
layer_name = purrr::map_chr(capabilities$getFeatureTypes(), ~ .x$getName()),
title = purrr::map_chr(capabilities$getFeatureTypes(), ~ .x$getTitle()),
abstract = purrr::map_chr(capabilities$getFeatureTypes(), ~ get_abstract_null(.x)),
abstract = purrr::map_chr(
capabilities$getFeatureTypes(),
~ get_abstract_null(.x)
),
class = purrr::map_chr(capabilities$getFeatureTypes(), ~ .x$getClassName()),
format = purrr::map_chr(capabilities$getFeatureTypes(), guess_layer_format)
) %>%
Expand All @@ -71,11 +78,14 @@ emodnet_get_layer_info <- memoise::memoise(.emodnet_get_layer_info)
}
#' Get WFS available layer information
#'
#' @param wfs A `WFSClient` R6 object with methods for interfacing an OGC Web Feature Service.
#' @param wfs A `WFSClient` R6 object with methods for interfacing an
#' OGC Web Feature Service.
#' @inheritParams emodnet_init_wfs_client
#' @return a tibble containing metadata on each layer available from the service.
#' @return a tibble containing metadata on each layer available from the
#' service.
#' @export
#' @describeIn emodnet_get_wfs_info Get info on all layers from am EMODnet WFS service.
#' @describeIn emodnet_get_wfs_info Get info on all layers from
#' an EMODnet WFS service.
#' @examples
#' \dontrun{
#' emodnet_get_wfs_info(service = "bathymetry")
Expand Down
64 changes: 49 additions & 15 deletions R/layer_attributes.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,25 @@
#' @param layer character sting of layer name. To get info on layers, including
#' `layer_name` use [emodnet_get_wfs_info()].
#'
#' @return output of `summary()` on the attributes (variables) in a given layer for a given service.
#' @return output of `summary()` on the attributes (variables) in a given layer
#' for a given service.
#' @export
#'
#' @examples
#' \dontrun{
#' layer_attributes_summarise(service = "human_activities", layer = "maritimebnds")
#' layer_attributes_summarise(
#' service = "human_activities",
#' layer = "maritimebnds"
#' )
#' }
layer_attributes_summarise <- function(wfs = NULL,
service = NULL,
service_version = NULL,
layer) {
deprecate_message_service_version(service_version, "layer_attributes_summarise")
deprecate_message_service_version(
service_version,
"layer_attributes_summarise"
)
summary(
layer_attributes_tbl(
wfs = wfs,
Expand All @@ -37,12 +44,18 @@ layer_attributes_summarise <- function(wfs = NULL,
#'
#' @examples
#' \dontrun{
#' layer_attribute_descriptions(service = "human_activities", layer = "maritimebnds")
#' layer_attribute_descriptions(
#' service = "human_activities",
#' layer = "maritimebnds"
#' )
#' }
layer_attribute_descriptions <- function(wfs = NULL,
service = NULL,
service_version = NULL, layer) {
deprecate_message_service_version(service_version, "layer_attribute_descriptions")
deprecate_message_service_version(
service_version,
"layer_attribute_descriptions"
)

wfs <- wfs %||% emodnet_init_wfs_client(service)
check_wfs(wfs)
Expand All @@ -62,13 +75,19 @@ layer_attribute_descriptions <- function(wfs = NULL,
#'
#' @examples
#' \dontrun{
#' layer_attributes_get_names(service = "human_activities", layer = "maritimebnds")
#' layer_attributes_get_names(
#' service = "human_activities",
#' layer = "maritimebnds"
#' )
#' }
layer_attributes_get_names <- function(wfs = NULL,
service = NULL,
service_version = NULL,
layer) {
deprecate_message_service_version(service_version, "layer_attributes_get_names")
deprecate_message_service_version(
service_version,
"layer_attributes_get_names"
)

layer_attribute_descriptions(
wfs = wfs,
Expand All @@ -85,7 +104,8 @@ layer_attributes_get_names <- function(wfs = NULL,
#' @inheritParams emodnet_init_wfs_client
#' @inheritParams emodnet_get_wfs_info
#'
#' @return Detailed summary of individual attribute (variable). Particularly useful for inspecting
#' @return Detailed summary of individual attribute (variable). Particularly
#' useful for inspecting
#' factor or character variable levels or unique values.
#' @export
#'
Expand Down Expand Up @@ -116,7 +136,10 @@ layer_attribute_inspect <- function(wfs = NULL,
choices = layer_attributes_get_names(wfs, layer = layer)
)

attribute_vector <- wfs$getFeatures(namespaced_layer, PROPERTYNAME = attribute)[[attribute]]
attribute_vector <- wfs$getFeatures(
namespaced_layer,
PROPERTYNAME = attribute
)[[attribute]]

if (inherits(attribute_vector, "sfc")) {
attribute_type <- "geometry"
Expand All @@ -141,9 +164,12 @@ layer_attribute_inspect <- function(wfs = NULL,
#' @inheritParams emodnet_get_wfs_info
#' @inheritParams layer_attributes_summarise
#'
#' @return tibble of layer attribute (variable) values with geometry column removed.
#' @details Request excluding spatial information can be significantly faster. Can be
#' useful for inspecting attribute values and constructing feature filters for more
#' @return tibble of layer attribute (variable) values
#' with geometry column removed.
#' @details Request excluding spatial information can be significantly faster.
#' Can be
#' useful for inspecting attribute values and constructing feature filters
#' for more
#' targeted and faster layer download.
#' @export
#'
Expand All @@ -168,7 +194,10 @@ layer_attributes_tbl <- function(wfs = NULL,
attributes <- layer_attributes_get_names(wfs, layer = layer)
attributes <- attributes[attributes != get_layer_geom_name(layer, wfs)]

wfs$getFeatures(namespaced_layer, PROPERTYNAME = paste(attributes, collapse = ",")) %>%
wfs$getFeatures(
namespaced_layer,
PROPERTYNAME = paste(attributes, collapse = ",")
) %>%
sf::st_drop_geometry() %>%
tibble::as_tibble()
}
Expand Down Expand Up @@ -196,7 +225,9 @@ get_layer_geom_name <- function(layer, wfs) {
desc$name[desc$type == "geometry"]
}

get_layer_default_crs <- function(layer, wfs, output = c("crs", "epsg.text", "epsg.num")) {
get_layer_default_crs <- function(layer,
wfs,
output = c("crs", "epsg.text", "epsg.num")) {
check_wfs(wfs)
output <- match.arg(output, several.ok = FALSE)

Expand All @@ -211,7 +242,10 @@ get_layer_default_crs <- function(layer, wfs, output = c("crs", "epsg.text", "ep
return(crs)
}

epsg.text <- regmatches(crs$input, regexpr("epsg\\:[[:digit:]]{4}", crs$input))
epsg.text <- regmatches(
crs$input,
regexpr("epsg\\:[[:digit:]]{4}", crs$input)
)
if (output == "epsg.text") {
return(epsg.text)
}
Expand Down
38 changes: 25 additions & 13 deletions R/layers.R
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
#' of layer features can also be handled via ECQL language filters.
#' @inheritParams emodnet_init_wfs_client
#' @inheritParams emodnet_get_wfs_info
#' @param layers a character vector of layer names. To get info on layers, including
#' @param layers a character vector of layer names. To get info on layers,
#' including
#' `layer_name` use [emodnet_get_wfs_info()].
#' @param crs integer. EPSG code for the output crs. If `NULL` (default), layers
#' are returned with original crs.
#' @param cql_filter character. Features returned can be filtered using valid
#' Extended Common Query Language (ECQL) filtering statements
#' (<https://docs.geoserver.org/stable/en/user/filter/ecql_reference.html>). Should be one of:
#' (<https://docs.geoserver.org/stable/en/user/filter/ecql_reference.html>).
#' Should be one of:
#' \itemize{
#' \item{character string or character vector of length 1.
#' Filter will be recycled across all layers requested}
Expand All @@ -24,7 +26,8 @@
#' Layers without corresponding filters are returned whole }
#' }
#' @param reduce_layers whether to reduce output layers to a single `sf` object.
#' @param ... additional vendor parameter arguments passed to [`ows4R::GetFeature()`](https://docs.geoserver.org/stable/en/user/services/wfs/reference.html#getfeature).
#' @param ... additional vendor parameter arguments passed to
#' [`ows4R::GetFeature()`](https://docs.geoserver.org/stable/en/user/services/wfs/reference.html#getfeature).# nolint
#' For example, including `count=1` returns the first available feature.
#' @return If `reduce_layers = FALSE` (default), a list of `sf`
#' objects, one element for each layer. Any layers for which download was
Expand Down Expand Up @@ -64,8 +67,12 @@
#' reduce_layers = TRUE
#' )
#' }
emodnet_get_layers <- function(wfs = NULL, service = NULL, service_version = NULL,
layers, crs = NULL, cql_filter = NULL,
emodnet_get_layers <- function(wfs = NULL,
service = NULL,
service_version = NULL,
layers,
crs = NULL,
cql_filter = NULL,
reduce_layers = FALSE,
...) {
deprecate_message_service_version(service_version, "emodnet_get_layers")
Expand Down Expand Up @@ -96,7 +103,7 @@ emodnet_get_layers <- function(wfs = NULL, service = NULL, service_version = NUL
cli::cli_abort(
c(
"Can't reduce layers when one is a data.frame",
i = 'data.frame layer(s): {.val {toString(layers[formats == "data.frame"])}}'
i = 'data.frame layer(s): {.val {toString(layers[formats == "data.frame"])}}' # nolint
)
)
}
Expand All @@ -119,9 +126,10 @@ emodnet_get_layers <- function(wfs = NULL, service = NULL, service_version = NUL


# get features -------------------------------------------------------------
# unnamed function and explicit passing of ellipses used because of idiosyncratic use of ...
# unnamed function and explicit passing of ellipses used
# because of idiosyncratic use of ...
# within purrr::map2 function.
# See: https://stackoverflow.com/questions/48215325/passing-ellipsis-arguments-to-map-function-purrr-package-r
# See: https://stackoverflow.com/questions/48215325/passing-ellipsis-arguments-to-map-function-purrr-package-r # nolint
out <- purrr::map2(
.x = layers, .y = cql_filter,
.f = function(x, y, wfs, ...) {
Expand Down Expand Up @@ -164,14 +172,14 @@ check_layer_crs <- function(layer_sf, layer, wfs) {
wfs_crs <- get_layer_default_crs(layer, wfs)

if (is.na(wfs_crs) || is.null(wfs_crs)) {
# If full crs object not available, try to get epsg number from identifier of
# the default CRS for this feature type in service description CRS
# If full crs object not available, try to get epsg number from identifier
# ofthe default CRS for this feature type in service description CRS
wfs_crs <- get_layer_default_crs(layer, wfs, output = "epsg.num")
}

if (!is.na(wfs_crs) && !is.null(wfs_crs)) {
# If full crs object not available, try to get epsg number from identifier of
# the default CRS for this feature type in service description CRS
# If full crs object not available, try to get epsg number from identifier
# ofthe default CRS for this feature type in service description CRS
sf::st_crs(layer_sf) <- wfs_crs
}

Expand Down Expand Up @@ -239,7 +247,11 @@ ews_get_layer <- function(x, wfs, cql_filter = NULL, ...) {
# get layer using cql_filter
tryCatch(
{
layer <- wfs$getFeatures(namespaced_x, cql_filter = utils::URLencode(cql_filter), ...)
layer <- wfs$getFeatures(
namespaced_x,
cql_filter = utils::URLencode(cql_filter),
...
)

if (inherits(layer, "sf")) {
layer <- check_layer_crs(layer, layer = x, wfs = wfs)
Expand Down
Loading

0 comments on commit b2fe4c7

Please sign in to comment.