Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable passing Vendor parameters to ows4R::GetFeature() #88

Merged
merged 9 commits into from
Apr 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Package: EMODnetWFS
Title: Access EMODnet Web Feature Service data through R
Version: 2.0.1.9000
Version: 2.0.1.9001
Authors@R: c(
person("Anna", "Krystalli", , "[email protected]", role = "aut",
comment = c(ORCID = "0000-0002-2378-4915")),
Expand Down
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# EMODnetWFS (development version)

* Added ability to pass vendor parameter to `EMODnetWFS::emodnet_get_layers()` queries (#88).
* Added memoising (caching during each R session) of the functions getting services
and layers information (#52).

Expand Down
30 changes: 24 additions & 6 deletions R/layers.R
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
#' @param reduce_layers whether to reduce output layers to a single `sf` object.
#' @param suppress_warnings logical. Whether to suppress messages of layer
#' download failures.
#' @param ... additional vendor parameter arguments passed to [`ows4R::GetFeature()`](https://docs.geoserver.org/stable/en/user/services/wfs/reference.html#getfeature).
#' 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
#' unsuccessful will be NULL. If `reduce_layers = TRUE`, all layers are
Expand Down Expand Up @@ -49,10 +51,18 @@
#' cql_filter = "sitename='Territory sea (12 nm)'",
#' reduce_layers = TRUE
#' )
#' # Usage of vendor parameter
#' emodnet_get_layers(
#' service = "human_activities",
#' layers = "maritimebnds",
#' count = 1,
#' reduce_layers = TRUE
#' )
#' }
emodnet_get_layers <- function(wfs = NULL, service = NULL, service_version = "2.0.0",
layers, crs = NULL, cql_filter = NULL,
reduce_layers = FALSE, suppress_warnings = FALSE) {
reduce_layers = FALSE, suppress_warnings = FALSE,
...) {

# check wfs ----------------------------------------------------------------

Expand Down Expand Up @@ -89,10 +99,16 @@ emodnet_get_layers <- function(wfs = NULL, service = NULL, service_version = "2.


# get features -------------------------------------------------------------
# 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
out <- purrr::map2(
.x = layers, .y = cql_filter,
~ews_get_layer(.x, wfs, cql_filter = .y),
wfs, suppress_warnings
.f = function(x, y, wfs, suppress_warnings, ...){
ews_get_layer(x, wfs = wfs, cql_filter = y,
suppress_warnings = suppress_warnings, ...)
},
wfs, suppress_warnings, ...
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why are the dots passed here too?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

purrr::map does funny stuff with dots so this was the only way to get it to work: https://stackoverflow.com/questions/48215325/passing-ellipsis-arguments-to-map-function-purrr-package-r

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add this as code comment so we don't forget? That sounds good to know 😅

) %>%
stats::setNames(layers)

Expand Down Expand Up @@ -169,7 +185,7 @@ standardise_crs <- function(out, crs = NULL) {
}
}

ews_get_layer <- function(x, wfs, suppress_warnings = FALSE, cql_filter = NULL) {
ews_get_layer <- function(x, wfs, suppress_warnings = FALSE, cql_filter = NULL, ...) {

# check and namespace layers -----------------------------------------------
namespaced_x <- namespace_layer_names(wfs, x)
Expand All @@ -180,7 +196,7 @@ ews_get_layer <- function(x, wfs, suppress_warnings = FALSE, cql_filter = NULL)
# get layer without cql_filter
tryCatch(

layer <- wfs$getFeatures(namespaced_x) %>%
layer <- wfs$getFeatures(namespaced_x, ...) %>%
check_layer_crs(layer = x, wfs = wfs),
error = function(e) {
usethis::ui_warn("Download of layer {usethis::ui_value(x)} failed: {usethis::ui_field(e)}")
Expand All @@ -189,7 +205,9 @@ ews_get_layer <- function(x, wfs, suppress_warnings = FALSE, cql_filter = NULL)
} else {
# 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),
...) %>%
check_layer_crs(layer = x, wfs = wfs),
error = function(e) {
usethis::ui_warn("Download of layer {usethis::ui_value(x)} failed: {usethis::ui_field(e)}")
Expand Down
17 changes: 14 additions & 3 deletions man/emodnet_get_layers.Rd

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

4 changes: 2 additions & 2 deletions man/emodnet_get_wfs_info.Rd

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

15 changes: 8 additions & 7 deletions man/emodnet_init_wfs_client.Rd

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

4 changes: 2 additions & 2 deletions man/layer_attribute_descriptions.Rd

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

4 changes: 2 additions & 2 deletions man/layer_attribute_inspect.Rd

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

4 changes: 2 additions & 2 deletions man/layer_attributes_get_names.Rd

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

4 changes: 2 additions & 2 deletions man/layer_attributes_summarise.Rd

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

4 changes: 2 additions & 2 deletions man/layer_attributes_tbl.Rd

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

2 changes: 1 addition & 1 deletion tests/testthat/_snaps/layers.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,5 @@

# emodnet_get_layers errors well when bad layer

'arg' should be one of "mediseh_cor_abs_pnt", "mediseh_cor_abs_poly", "mediseh_cymodocea_pnt", "Species_gridded_abundances_10year", "Species_gridded_abundances_2year", "Species_gridded_abundances_3year", "Species_gridded_abundance_all", "mediseh_halophila_pnt", "mediseh_maerl_pnt", "mediseh_maerl_poly", "mediseh_posidonia_model", "mediseh_coral_model", "mediseh_maerl_model", "OOPS_errors", "OOPS_metadata", "OOPS_products", "OOPS_products_vliz", "OOPS_regions", "OOPS_summaries", "mediseh_cor_pnt", "mediseh_cor_poly", "mediseh_posidonia_abs", "mediseh_posidonia_nodata", "mediseh_posidonia_current_pnt", "mediseh_posidonia_current_shape", "mediseh_posidonia_historical_shape", "mediseh_posidonia_historical_pnt", "mediseh_ruppia_c_pnt", "mediseh_ruppia_m_pnt", "mediseh_zostera_m_pnt", "mediseh_zostera_n_pnt"
'arg' should be one of "mediseh_cor_abs_pnt", "mediseh_cor_abs_poly", "mediseh_cymodocea_pnt", "Species_gridded_abundances_10year", "Species_gridded_abundances_2year", "Species_gridded_abundances_3year", "Species_gridded_abundance_all", "mediseh_halophila_pnt", "mediseh_maerl_pnt", "mediseh_maerl_poly", "mediseh_posidonia_model", "mediseh_coral_model", "mediseh_maerl_model", "OOPS_errors", "OOPS_metadata", "OOPS_products", "OOPS_products_vliz", "OOPS_regions", "OOPS_summaries", "mediseh_cor_pnt", "mediseh_cor_poly", "mediseh_posidonia_abs", "mediseh_posidonia_nodata", "mediseh_posidonia_current_pnt", "mediseh_posidonia_current_shape", "mediseh_posidonia_historical_shape", "mediseh_posidonia_historical_pnt", "mediseh_ruppia_c_pnt", "mediseh_ruppia_m_pnt", "mediseh_zostera_m_pnt", "mediseh_zostera_n_pnt", "grey_seal", "harbour_seal"

Loading