diff --git a/R/osm_query_changesets.R b/R/osm_query_changesets.R index 09197c9f..71ba8a34 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. @@ -111,7 +115,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 +145,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 +191,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 +221,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..b7849e6b 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,7 +591,7 @@ 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 +#' Te valid formats for `time`, `time_2`, `from` and `to` 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. #' #' @return @@ -661,8 +666,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 +693,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 +712,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..95c80ed7 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}).} 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..0dd59c7b 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 = "2023-06-22T00:38:20Z", + to = "2023-06-22T02:23:23Z" + ) chaset$closed <- osm_query_changesets( bbox = "-9.3015367,41.8073642,-6.7339533,43.790422", user = "Mementomoristultus",