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 2 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
27 changes: 21 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 [GetFeature](https://docs.geoserver.org/stable/en/user/services/wfs/reference.html#getfeature).
annakrystalli marked this conversation as resolved.
Show resolved Hide resolved
#' 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 @@ -91,8 +101,11 @@ emodnet_get_layers <- function(wfs = NULL, service = NULL, service_version = "2.
# get features -------------------------------------------------------------
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 +182,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 +193,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 +202,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.

11 changes: 11 additions & 0 deletions tests/testthat/test-vendor-params.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
test_that("vendor param count works", {
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 there new fixtures, and what is their size?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Because I'm testing a new request. Small because it only returns the first feature

Copy link
Collaborator

Choose a reason for hiding this comment

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

but there is no httptest::with_mock_dir in this test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Oh yesh. Actually it doesn't seem like there is fixture for it but there probably should be

skip_if_offline()
wfs <- create_biology_wfs()
l_data <- emodnet_get_layers(
wfs = wfs,
layers = "mediseh_zostera_m_pnt",
count = 1, reduce_layers = TRUE)

expect_true(nrow(l_data) == 1)

})
Loading