From 572b4e5e5537e478f6900e5cd594265ec1933c96 Mon Sep 17 00:00:00 2001 From: Joan Maspons Date: Sun, 17 Nov 2024 13:43:15 +0100 Subject: [PATCH] Add new parameters --- NEWS.md | 5 +++- R/osm_query_changesets.R | 25 ++++++++++++---- R/osmapi_changesets.R | 29 ++++++++++++++----- man/osm_query_changesets.Rd | 15 ++++++++-- tests/testthat/_snaps/changesets.md | 18 ++++++++++++ .../osm.org/api/0.6/changesets-b87e70.xml | 19 ++++++++++++ tests/testthat/test-changesets.R | 6 ++++ 7 files changed, 99 insertions(+), 18 deletions(-) create mode 100644 tests/testthat/mock_query_changesets/osm.org/api/0.6/changesets-b87e70.xml diff --git a/NEWS.md b/NEWS.md index 030273d8..ba04eaa8 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,7 +1,10 @@ # osmapiR (development version) * Use the new function `httr2::oauth_cache_clear()` from httr2 1.0.6 (#58 by @hadley). -* Update documentation and code for server-side changes documented in OSMWikiVersion 2775892 (#60). +* Update documentation and code for server-side changes documented in OSMWikiVersion + [2711808 -> 2775892](https://wiki.openstreetmap.org/w/index.php?title=API_v0.6&type=revision&diff=2775892&oldid=2711808) + (#60). + * Add new parameters to `osm_query_changesets(..., from, to)`. # osmapiR 0.2.1 diff --git a/R/osm_query_changesets.R b/R/osm_query_changesets.R index 09197c9f..8a2fb853 100644 --- a/R/osm_query_changesets.R +++ b/R/osm_query_changesets.R @@ -6,7 +6,11 @@ #' @param user Find changesets by the user with the given user id (numeric) or display name (character). #' @param time Find changesets **closed** after this date and time. See details for the valid formats. #' @param time_2 find changesets that were **closed** after `time` and **created** before `time_2`. In other words, any -#' changesets that were open **at some time** during the given time range `time` to `time_2`. +#' changesets that were open **at some time** during the given time range `time` to `time_2`. See details for the +#' valid formats. +#' @param from Find changesets **created** at or after this value. See details for the valid formats. +#' @param to Find changesets **created** before this value. `to` requires `from`, but not vice-versa. If `to` is +#' provided alone, it has no effect. See details for the valid formats. #' @param open If `TRUE`, only finds changesets that are still **open** but excludes changesets that are closed or have #' reached the element limit for a changeset (10,000 at the moment `osm_capabilities()$api$changesets`). #' @param closed If `TRUE`, only finds changesets that are **closed** or have reached the element limit. @@ -32,8 +36,9 @@ #' – see the [current state](https://github.com/openstreetmap/openstreetmap-website/blob/master/app/controllers/api/changesets_controller.rb#L174)). #' Reverse ordering cannot be combined with `time`. #' -#' Te valid formats for `time` and `time_2` parameters are anything that -#' [`Time.parse` Ruby function](https://ruby-doc.org/stdlib-2.7.0/libdoc/time/rdoc/Time.html#method-c-parse) will parse. +#' Te valid formats for `time`, `time_2`, `from` and `to` parameters are [POSIXt] values or characters with anything +#' that [`Time.parse` Ruby function](https://ruby-doc.org/stdlib-2.7.0/libdoc/time/rdoc/Time.html#method-c-parse) will +#' parse. #' #' @return #' If `format = "R"`, returns a data frame with one OSM changeset per row. If `format = "sf"`, returns a `sf` object @@ -111,7 +116,8 @@ #' closed = TRUE #' ) #' chsts2 -osm_query_changesets <- function(bbox, user, time, time_2, open, closed, changeset_ids, order = c("newest", "oldest"), +osm_query_changesets <- function(bbox, user, time, time_2, from, to, open, closed, changeset_ids, + order = c("newest", "oldest"), limit = getOption("osmapir.api_capabilities")$api$changesets["default_query_limit"], format = c("R", "sf", "xml", "json"), tags_in_columns = FALSE) { format <- match.arg(format) @@ -140,6 +146,13 @@ osm_query_changesets <- function(bbox, user, time, time_2, open, closed, changes } stopifnot(is.null(time) && is.null(time_2) || !is.null(time)) + if (missing(from)) { + from <- NULL + } + if (missing(to)) { + to <- NULL + } + if (missing(open)) { open <- NULL } else { @@ -179,7 +192,7 @@ osm_query_changesets <- function(bbox, user, time, time_2, open, closed, changes if (limit <= getOption("osmapir.api_capabilities")$api$changesets["maximum_query_limit"]) { # no batch needed out <- .osm_query_changesets( - bbox = bbox, user = user, time = time, time_2 = time_2, open = open, closed = closed, + bbox = bbox, user = user, time = time, time_2 = time_2, from = from, to = to, open = open, closed = closed, changeset_ids = changeset_ids, order = order, limit = limit, format = .format, tags_in_columns = tags_in_columns ) @@ -209,7 +222,7 @@ osm_query_changesets <- function(bbox, user, time, time_2, open, closed, changes ## TODO: simplify and split in different functions ---- while (n_out < limit && n > 0) { outL[[i]] <- .osm_query_changesets( - bbox = bbox, user = user, time = time, time_2 = time_2, open = open, closed = closed, + bbox = bbox, user = user, time = time, time_2 = time_2, from = from, to = to, open = open, closed = closed, changeset_ids = changeset_ids, order = order, limit = min(limit - n_out, getOption("osmapir.api_capabilities")$api$changesets["maximum_query_limit"]), format = .format, tags_in_columns = FALSE diff --git a/R/osmapi_changesets.R b/R/osmapi_changesets.R index 4923f58a..b594a506 100644 --- a/R/osmapi_changesets.R +++ b/R/osmapi_changesets.R @@ -557,9 +557,14 @@ osm_download_changeset <- function(changeset_id, format = c("R", "osc", "xml")) #' #' @param bbox Find changesets within the given bounding box coordinates (`left,bottom,right,top`). #' @param user Find changesets by the user with the given user id (numeric) or display name (character). -#' @param time Find changesets **closed** after this date and time. See details for the valid formats. -#' @param time_2 find changesets that were **closed** after `time` and **created** before `time_2`. In other words, any -#' changesets that were open **at some time** during the given time range `time` to `time_2`. +#' @param time Find changesets **closed** after this date and time. Compare with `from=T1` which filters by creation +#' time instead. See details for the valid formats. +#' @param time_2 Find changesets that were **closed** after `time` and **created** before `time_2`. In other words, any +#' changesets that were open **at some time** during the given time range `time` to `time_2`. See details for the +#' valid formats. +#' @param from Find changesets **created** at or after this value. See details for the valid formats. +#' @param to Find changesets **created** before this value. `to` requires `from`, but not vice-versa. If `to` is +#' provided alone, it has no effect. See details for the valid formats. #' @param open If `TRUE`, only finds changesets that are still **open** but excludes changesets that are closed or have #' reached the element limit for a changeset (10,000 at the moment `osm_capabilities()$api$changesets`). #' @param closed If `TRUE`, only finds changesets that are **closed** or have reached the element limit. @@ -586,8 +591,9 @@ osm_download_changeset <- function(changeset_id, format = c("R", "osc", "xml")) #' – see the [current state](https://github.com/openstreetmap/openstreetmap-website/blob/master/app/controllers/api/changesets_controller.rb#L174)). #' Reverse ordering cannot be combined with `time`. #' -#' Te valid formats for `time` and `time_2` parameters are anything that -#' [`Time.parse` Ruby function](https://ruby-doc.org/stdlib-2.7.0/libdoc/time/rdoc/Time.html#method-c-parse) will parse. +#' Te valid formats for `time`, `time_2`, `from` and `to` parameters are [POSIXt] values or characters with anything +#' that [`Time.parse` Ruby function](https://ruby-doc.org/stdlib-2.7.0/libdoc/time/rdoc/Time.html#method-c-parse) will +#' parse. #' #' @return #' If `format = "R"`, returns a data frame with one OSM changeset per row. @@ -661,8 +667,8 @@ osm_download_changeset <- function(changeset_id, format = c("R", "osc", "xml")) #' closed = "true" #' ) #' chsts2 -.osm_query_changesets <- function(bbox = NULL, user = NULL, time = NULL, time_2 = NULL, open = NULL, closed = NULL, - changeset_ids = NULL, order = NULL, +.osm_query_changesets <- function(bbox = NULL, user = NULL, time = NULL, time_2 = NULL, from = NULL, to = NULL, + open = NULL, closed = NULL, changeset_ids = NULL, order = NULL, limit = getOption( "osmapir.api_capabilities" )$api$changesets["default_query_limit"], @@ -688,6 +694,13 @@ osm_download_changeset <- function(changeset_id, format = c("R", "osc", "xml")) time <- paste0(time, ",", time_2) } + if (!is.null(from) && inherits(from, "POSIXt")) { + from <- format(from, "%Y-%m-%dT%H:%M:%SZ") + } + if (!is.null(to) && inherits(to, "POSIXt")) { + to <- format(to, "%Y-%m-%dT%H:%M:%SZ") + } + if (format == "json") { ext <- "changesets.json" } else { @@ -700,7 +713,7 @@ osm_download_changeset <- function(changeset_id, format = c("R", "osc", "xml")) req <- httr2::req_url_query(req, bbox = bbox, user = user, display_name = display_name, - time = time, + time = time, from = from, to = to, open = open, closed = closed, changesets = changeset_ids, order = order, diff --git a/man/osm_query_changesets.Rd b/man/osm_query_changesets.Rd index a0133ebe..bec1bfd1 100644 --- a/man/osm_query_changesets.Rd +++ b/man/osm_query_changesets.Rd @@ -9,6 +9,8 @@ osm_query_changesets( user, time, time_2, + from, + to, open, closed, changeset_ids, @@ -26,7 +28,13 @@ osm_query_changesets( \item{time}{Find changesets \strong{closed} after this date and time. See details for the valid formats.} \item{time_2}{find changesets that were \strong{closed} after \code{time} and \strong{created} before \code{time_2}. In other words, any -changesets that were open \strong{at some time} during the given time range \code{time} to \code{time_2}.} +changesets that were open \strong{at some time} during the given time range \code{time} to \code{time_2}. See details for the +valid formats.} + +\item{from}{Find changesets \strong{created} at or after this value. See details for the valid formats.} + +\item{to}{Find changesets \strong{created} before this value. \code{to} requires \code{from}, but not vice-versa. If \code{to} is +provided alone, it has no effect. See details for the valid formats.} \item{open}{If \code{TRUE}, only finds changesets that are still \strong{open} but excludes changesets that are closed or have reached the element limit for a changeset (10,000 at the moment \code{osm_capabilities()$api$changesets}).} @@ -120,8 +128,9 @@ This call returns latest changesets matching criteria. The default ordering is n – see the \href{https://github.com/openstreetmap/openstreetmap-website/blob/master/app/controllers/api/changesets_controller.rb#L174}{current state}). Reverse ordering cannot be combined with \code{time}. -Te valid formats for \code{time} and \code{time_2} parameters are anything that -\href{https://ruby-doc.org/stdlib-2.7.0/libdoc/time/rdoc/Time.html#method-c-parse}{\code{Time.parse} Ruby function} will parse. +Te valid formats for \code{time}, \code{time_2}, \code{from} and \code{to} parameters are \link{POSIXt} values or characters with anything +that \href{https://ruby-doc.org/stdlib-2.7.0/libdoc/time/rdoc/Time.html#method-c-parse}{\code{Time.parse} Ruby function} will +parse. } \examples{ chst_ids <- osm_query_changesets(changeset_ids = c(137627129, 137625624)) diff --git a/tests/testthat/_snaps/changesets.md b/tests/testthat/_snaps/changesets.md index 2c643e04..17f230c7 100644 --- a/tests/testthat/_snaps/changesets.md +++ b/tests/testthat/_snaps/changesets.md @@ -200,6 +200,24 @@ 2 6 tags: changesets_count=155 | comment=--- | created_by=iD 2.25.2 | host=https:/... 3 6 tags: changesets_count=154 | comment=--- | created_by=iD 2.25.2 | host=https:/... +--- + + Code + print(x) + Output + id created_at closed_at open user + 1 137626916 2023-06-22 02:05:19 2023-06-22 02:05:19 FALSE Mementomoristultus + 2 137626898 2023-06-22 02:03:57 2023-06-22 02:03:57 FALSE Mementomoristultus + uid min_lat min_lon max_lat max_lon comments_count + 1 19648429 38.9100720 1.4302823 38.9101137 1.4304540 1 + 2 19648429 38.8835173 1.3845938 38.9299968 1.4634214 0 + changes_count + 1 1 + 2 1 + tags + 1 6 tags: changesets_count=146 | comment=23 | created_by=iD 2.25.2 | host=https://... + 2 6 tags: changesets_count=145 | comment=112 | created_by=iD 2.25.2 | host=https:/... + --- Code diff --git a/tests/testthat/mock_query_changesets/osm.org/api/0.6/changesets-b87e70.xml b/tests/testthat/mock_query_changesets/osm.org/api/0.6/changesets-b87e70.xml new file mode 100644 index 00000000..fa62e26b --- /dev/null +++ b/tests/testthat/mock_query_changesets/osm.org/api/0.6/changesets-b87e70.xml @@ -0,0 +1,19 @@ + + + + + + + + + + + + + + + + + + + diff --git a/tests/testthat/test-changesets.R b/tests/testthat/test-changesets.R index b9e918e0..613c13d0 100644 --- a/tests/testthat/test-changesets.R +++ b/tests/testthat/test-changesets.R @@ -267,6 +267,12 @@ test_that("osm_query_changesets works", { time = "2023-06-22T02:23:23Z", time_2 = "2023-06-22T00:38:20Z" ) + chaset$from_to <- osm_query_changesets( + bbox = c(-1.241112, 38.0294955, 8.4203171, 42.9186456), + user = "Mementomoristultus", + from = as.POSIXlt("2023-06-22T00:38:20Z", tz = "GMT", format = "%Y-%m-%dT%H:%M:%S"), + to = as.POSIXlt("2023-06-22T02:23:23Z", tz = "GMT", format = "%Y-%m-%dT%H:%M:%S") + ) chaset$closed <- osm_query_changesets( bbox = "-9.3015367,41.8073642,-6.7339533,43.790422", user = "Mementomoristultus",