Skip to content

Commit

Permalink
Vectorized version of osm_read_note()
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaspons committed Dec 27, 2023
1 parent 34411f0 commit 3df9362
Show file tree
Hide file tree
Showing 8 changed files with 63 additions and 18 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ export(osm_feed_notes)
export(osm_get_changesets)
export(osm_get_data_gpx)
export(osm_get_gpx_metadata)
export(osm_get_notes)
export(osm_get_objects)
export(osm_get_points_gps)
export(osm_get_preferences_user)
Expand All @@ -47,7 +48,6 @@ export(osm_list_gpxs)
export(osm_permissions)
export(osm_query_changesets)
export(osm_read_bbox_notes)
export(osm_read_note)
export(osm_redaction_object)
export(osm_relations_object)
export(osm_reopen_note)
Expand Down
45 changes: 45 additions & 0 deletions R/osm_get_notes.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# Vectorized version of osm_read_note

#' Get notes
#'
#' Returns the existing note with the given ID.
#'
#' @param note_id Note id represented by a numeric or a character value.
#' @param format Format of the output. Can be `R` (default), `xml`, `rss`, `json` or `gpx`.
#'
#' @return
#' If `format = "R"`, returns a data frame with one map note per row. If `format = "json"`, returns a list with the json
#' structure. For `format` in `xml`, `rss`, and `gpx`, a [xml2::xml_document-class] with the corresponding format.
#' @family get notes' functions
#' @export
#'
#' @examples
#' \dontrun{
#' note <- osm_get_notes(note_id = "2067786")
#' note
#' }
osm_get_notes <- function(note_id, format = c("R", "xml", "rss", "json", "gpx")) {
format <- match.arg(format)

if (length(note_id) == 1) {
out <- osm_read_note(note_id = note_id, format = format)
} else {
outL <- lapply(note_id, function(id) {
osm_read_note(note_id = id, format = format)
})

if (format == "R") {
out <- do.call(rbind, outL)
} else if (format %in% c("xml", "rss", "gpx")) {
out <- xml2::xml_new_root(outL[[1]])
for (i in seq_len(length(outL) - 1)) {
xml2::xml_add_child(out, xml2::xml_child(outL[[i + 1]]))
}
} else if (format == "json") {
out <- outL
}
}

return(out)
}

4 changes: 2 additions & 2 deletions R/osmapi_map_notes.R
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ osm_read_bbox_notes <- function(bbox, limit = 100, closed = 7, format = c("R", "
#' @return
#' If `format = "R"`, returns a data frame with one map note per row. If `format = "json"`, returns a list with the json
#' structure. For `format` in `xml`, `rss`, and `gpx`, a [xml2::xml_document-class] with the corresponding format.
#' @family get notes' functions
#' @export
# @family get notes' functions
#' @noRd
#'
#' @examples
#' \dontrun{
Expand Down
2 changes: 1 addition & 1 deletion man/osm_feed_notes.Rd

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

12 changes: 6 additions & 6 deletions man/osm_read_note.Rd → man/osm_get_notes.Rd

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

2 changes: 1 addition & 1 deletion man/osm_read_bbox_notes.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/osm_search_notes.Rd

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

10 changes: 5 additions & 5 deletions tests/testthat/test-map_notes.R
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,11 @@ test_that("osm_read_bbox_notes works", {
test_that("osm_read_note works", {
read_note <- list()
with_mock_dir("mock_read_note", {
read_note$df <- osm_read_note(note_id = "2067786")
read_note$xml <- osm_read_note(note_id = 2067786, format = "xml")
read_note$rss <- osm_read_note(note_id = 2067786, format = "rss")
read_note$json <- osm_read_note(note_id = 2067786, format = "json")
read_note$gpx <- osm_read_note(note_id = 2067786L, format = "gpx")
read_note$df <- osm_get_notes(note_id = "2067786")
read_note$xml <- osm_get_notes(note_id = 2067786, format = "xml")
read_note$rss <- osm_get_notes(note_id = 2067786, format = "rss")
read_note$json <- osm_get_notes(note_id = 2067786, format = "json")
read_note$gpx <- osm_get_notes(note_id = 2067786L, format = "gpx")
})

mapply(function(x, class) expect_true(inherits(x, class)), x = read_note, class = classes)
Expand Down

0 comments on commit 3df9362

Please sign in to comment.