Skip to content

Commit

Permalink
Add new parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaspons committed Nov 17, 2024
1 parent 9524254 commit 572b4e5
Show file tree
Hide file tree
Showing 7 changed files with 99 additions and 18 deletions.
5 changes: 4 additions & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
25 changes: 19 additions & 6 deletions R/osm_query_changesets.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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
)

Expand Down Expand Up @@ -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
Expand Down
29 changes: 21 additions & 8 deletions R/osmapi_changesets.R
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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.
Expand Down Expand Up @@ -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"],
Expand All @@ -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 {
Expand All @@ -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,
Expand Down
15 changes: 12 additions & 3 deletions man/osm_query_changesets.Rd

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

18 changes: 18 additions & 0 deletions tests/testthat/_snaps/changesets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?xml version="1.0" encoding="UTF-8"?>
<osm version="0.6" generator="OpenStreetMap server" copyright="OpenStreetMap and contributors" attribution="http://www.openstreetmap.org/copyright" license="http://opendatacommons.org/licenses/odbl/1-0/">
<changeset id="137626916" created_at="2023-06-22T02:05:19Z" open="false" comments_count="1" changes_count="1" closed_at="2023-06-22T02:05:19Z" min_lat="38.9100720" min_lon="1.4302823" max_lat="38.9101137" max_lon="1.4304540" uid="19648429" user="Mementomoristultus">
<tag k="changesets_count" v="146"/>
<tag k="comment" v="23"/>
<tag k="created_by" v="iD 2.25.2"/>
<tag k="host" v="https://www.openstreetmap.org/edit"/>
<tag k="imagery_used" v="PNOA Spain"/>
<tag k="locale" v="es"/>
</changeset>
<changeset id="137626898" created_at="2023-06-22T02:03:57Z" open="false" comments_count="0" changes_count="1" closed_at="2023-06-22T02:03:57Z" min_lat="38.8835173" min_lon="1.3845938" max_lat="38.9299968" max_lon="1.4634214" uid="19648429" user="Mementomoristultus">
<tag k="changesets_count" v="145"/>
<tag k="comment" v="112"/>
<tag k="created_by" v="iD 2.25.2"/>
<tag k="host" v="https://www.openstreetmap.org/edit"/>
<tag k="imagery_used" v="PNOA Spain"/>
<tag k="locale" v="es"/>
</changeset>
</osm>
6 changes: 6 additions & 0 deletions tests/testthat/test-changesets.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 572b4e5

Please sign in to comment.