Skip to content

Commit

Permalink
Merge pull request #283 from jmaspons/opq_osm_id-vectors
Browse files Browse the repository at this point in the history
Allow vectors of ids + types for opq_osm_id
  • Loading branch information
mpadge committed Nov 29, 2022
2 parents 30d65f7 + e13b252 commit 2605e00
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 8 deletions.
29 changes: 23 additions & 6 deletions R/opq.R
Original file line number Diff line number Diff line change
Expand Up @@ -502,7 +502,7 @@ check_features <- function (features) {
#' must be entered as either a character or *numeric* value (because R does not
#' support long-form integers). id can also be a character string prefixed with
#' the id type, e.g. "relation/11158003"
#' @param type Type of object; must be either `node`, `way`, or `relation`.
#' @param type Type of objects (recycled); must be either `node`, `way`, or `relation`.
#' Optional if id is prefixed with the type.
#' @param open_url If `TRUE`, open the OSM page of the specified object in web
#' browser. Multiple objects (`id` values) will be opened in multiple pages.
Expand Down Expand Up @@ -532,30 +532,42 @@ check_features <- function (features) {
#' osmdata_sf ()
#' dat2$osm_lines # the desired ways
#' dat <- c (dat1, dat2) # The node and way data combined
#' # All in one (same result as dat)
#' id <- c(1489221200, 1489221321, 1489221491, 136190595, 136190596)
#' type <- c("node", "node", "node", "way", "way")
#' datAiO<- opq_osm_id (id = id, type = type) %>%
#' opq_string () %>%
#' osmdata_sf ()
#' }
opq_osm_id <- function (id = NULL, type = NULL, open_url = FALSE) {
if (is.null (type)) {
if (is.null (id)) {
stop (
"type must be specified: one of node, way, or relation if id is 'NULL'"
)
} else if ((length (id) == 1L) && grepl ("^node/|^way/|^relation/", id)) {
} else if (all(grepl ("^node/|^way/|^relation/", id))) {
type <- dirname (id)
id <- basename (id)
}
}

type <- match.arg (tolower (type), c ("node", "way", "relation"))
type <- tolower(type)
if (!all(type %in% c("node", "way", "relation"))){
stop('type items must be "node", "way" or "relation".')
}

if (is.null (id)) {
stop ("id must be specified.")
}
if (!(is.character (id) | storage.mode (id) == "double")) {
stop ("id must be character or numeric.")
}
if (length (id) %% length (type) != 0 | length (type) > length (id)){
stop ("id length must be a multiple of type length.")
}

if (!is.character (id)) {
id <- as.character(id)
id <- as.character (id)
}

opq <- opq (1:4)
Expand Down Expand Up @@ -794,8 +806,13 @@ opq_string_intern <- function (opq, quiet = TRUE) {

} else if (!is.null (opq$id)) { # opq with opq_osm_id

id <- paste (opq$id$id, collapse = ",")
id <- sprintf (" %s(id:%s);\n", opq$id$type, id)
type_id <- data.frame (type=opq$id$type, id=opq$id$id)
type_id <- split(type_id, type_id$type)
id <- mapply (function(type, ids){
paste0 (" ", type, "(id:", paste (ids, collapse=","), ");\n")
}, type=names(type_id), ids=type_id)

id <- paste (id, collapse="")
res <- paste0 (opq$prefix, id, opq$suffix)

} else { # straight opq with neither features nor ID specified
Expand Down
8 changes: 7 additions & 1 deletion man/opq_osm_id.Rd

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

14 changes: 13 additions & 1 deletion tests/testthat/test-opq.R
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ test_that ("opq_osm_id", {
)
expect_error (
opq_osm_id (type = "a"),
"'arg' should be one of"
'type items must be "node", "way" or "relation".'
)
expect_error (
opq_osm_id (type = "node"),
Expand All @@ -133,6 +133,18 @@ test_that ("opq_osm_id", {
opq_osm_id (type = "node", id = 1:2 + 0.1),
"overpass_query"
)
expect_s3_class (
opq_osm_id (id = c(paste0("node/", 1:2), "way/1")),
"overpass_query"
)
expect_s3_class (
x <- opq_osm_id (type = c("node", "way"), id = 1:4 + 0.1),
"overpass_query"
)
expect_error (
x <- opq_osm_id (type = c("node", "way"), id = 1:3 + 0.1),
"id length must be a multiple of type length."
)
expect_identical(
opq_osm_id (type = "node", id = 123456),
opq_osm_id (id = "node/123456")
Expand Down

0 comments on commit 2605e00

Please sign in to comment.