Skip to content

Commit

Permalink
Merge pull request #299 from jmaspons/add_osm_feature
Browse files Browse the repository at this point in the history
Allow to chain  add_osm_feature/s function calls
  • Loading branch information
mpadge authored Feb 2, 2023
2 parents 83c4ad9 + d44ecb6 commit cbbb59e
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 11 deletions.
26 changes: 18 additions & 8 deletions R/opq.R
Original file line number Diff line number Diff line change
Expand Up @@ -339,8 +339,13 @@ add_osm_feature <- function (opq,
key, value, bind_key_pre$key_pre, bind_key_pre$bind,
match_case, value_exact
)
feature<- paste (feature, collapse = " ")

opq$features <- paste0 (c (opq$features, feature), collapse = "")
if (is.null (opq$features)) {
opq$features <- feature
} else {
opq$features <- paste (opq$features, feature)
}

if (any (w <- !grepl("\\[(\\\"|~)", opq$features))) {
warning(
Expand Down Expand Up @@ -527,19 +532,24 @@ add_osm_features <- function (opq,
key_exact = key_exact
)

features <-
paste0 (
bind_key_pre$key_pre, '\"', names (features), '\"',
bind_key_pre$bind, '\"', features, '\"'
)
features <- mapply (function (key, value, key_pre, bind) {
paste_features (key, value, key_pre = key_pre, bind = bind,
match_case = TRUE, value_exact = value_exact)
},
key = names (features), value = features,
key_pre = bind_key_pre$key_pre, bind = bind_key_pre$bind,
SIMPLIFY = FALSE
)
features <- as.character (features)

}

index <- which (!grepl ("^\\[", features))
features [index] <- paste0 (" [", features [index])
features [index] <- paste0 ("[", features [index])
index <- which (!grepl ("\\]$", features))
features [index] <- paste0 (features [index], "]")

opq$features <- features
opq$features <- unique (c (opq$features, features))

opq
}
Expand Down
6 changes: 3 additions & 3 deletions tests/testthat/test-osmdata.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ test_that ("add feature", {
add_osm_feature (key = "natural", value = "peak"),
"The query will request objects whith only a negated key "
)
expect_identical(qry7$features, "[\"name\"][!\"name:ca\"]")
expect_identical(qry8$features, "[\"natural\"=\"peak\"][!\"ele\"]")
expect_identical(qry9$features, "[!\"ele\"][\"natural\"=\"peak\"]")
expect_identical(qry7$features, "[\"name\"] [!\"name:ca\"]")
expect_identical(qry8$features, "[\"natural\"=\"peak\"] [!\"ele\"]")
expect_identical(qry9$features, "[!\"ele\"] [\"natural\"=\"peak\"]")
})

test_that ("query_errors", {
Expand Down

0 comments on commit cbbb59e

Please # to comment.