Skip to content

Commit

Permalink
closes #179
Browse files Browse the repository at this point in the history
  • Loading branch information
mpadge committed Mar 5, 2020
1 parent 7233883 commit fa1ae12
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 5 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Package: osmdata
Version: 0.1.3.001
Version: 0.1.3.002
Authors@R: c(
person("Mark", "Padgham", email="[email protected]", role=c("aut", "cre")),
person("Bob", "Rudis", role="aut"),
Expand Down
7 changes: 7 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
0.1.3.00x
===================

Major changes:

- `opq()` now has additional `datetime` and `datetime2` parameters which can be
used to extract historical data prior to `datetime`, or differences between
two datetimes by specifying `datetime2`; thanks to @neogeomat for the idea in
issue#179.

0.1.3
===================

Expand Down
52 changes: 51 additions & 1 deletion R/opq.R
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
#' will be passed to \link{getbb} to be converted to a numerical bounding
#' box. Can also be (iii) a matrix representing a bounding polygon as
#' returned from `getbb(..., format_out = "polygon")`.
#' @param datetime If specified, a date and time to extract data from the OSM
#' database as it was up to the specified date and time, as described at
#' \url{https://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_QL#date}.
#' This \emph{must} be in ISO8601 format ("YYYY-MM-DDThh:mm:ssZ"), where
#' both the "T" and "Z" characters must be present.
#' @param datetime2 If specified, return the \emph{difference} in the OSM
#' database between \code{datetime} and \code{datetime2}, where
#' \code{datetime2 > datetime}. See
#' \url{https://wiki.openstreetmap.org/wiki/Overpass_API/Overpass_QL#Delta_between_two_dates_.28.22diff.22.29}.
#' @param timeout It may be necessary to increase this value for large queries,
#' because the server may time out before all data are delivered.
#' @param memsize The default memory size for the 'overpass' server in *bytes*;
Expand Down Expand Up @@ -36,21 +45,62 @@
#' add_osm_feature("amenity", "pub")
#' c (osmdata_sf (q1), osmdata_sf (q2)) # all restaurants OR pubs
#' }
opq <- function (bbox = NULL, timeout = 25, memsize)
opq <- function (bbox = NULL, datetime = NULL, datetime2 = NULL,
timeout = 25, memsize)
{
timeout <- format (timeout, scientific = FALSE)
prefix <- paste0 ("[out:xml][timeout:", timeout, "]")
suffix <- ");\n(._;>;);\nout body;" # recurse down
if (!missing (memsize))
prefix <- paste0 (prefix, "[maxsize:", # nocov
format (memsize, scientific = FALSE), "]") # nocov
if (!is.null (datetime))
{
datetime <- check_datetime (datetime)
if (!is.null (datetime2))
{
datetime2 <- check_datetime (datetime2)
prefix <- paste0 ('[diff:\"', datetime,'\",\"', datetime2, '\"]',
prefix)
} else
{
prefix <- paste0 ('[date:\"', datetime,'\"]', prefix)
}
}

res <- list (bbox = bbox_to_string (bbox),
prefix = paste0 (prefix, ";\n(\n"),
suffix = suffix, features = NULL)
class (res) <- c (class (res), "overpass_query")
return (res)
}

check_datetime <- function (x)
{
if (nchar (x) != 20 &
substring (x, 5, 5) != "-" &
substring (x, 8, 8) != "-" &
substring (x, 11, 11) != "T" &
substring (x, 14, 14) != ":" &
substring (x, 17, 17) != ":" &
substring (x, 20, 20) != "Z")
stop ("x is not is ISO8601 format ('YYYY-MM-DDThh:mm:ssZ')")
YY <- substring (x, 1, 4)
MM <- substring (x, 6, 7)
DD <- substring (x, 9, 10)
hh <- substring (x, 12, 13)
mm <- substring (x, 15, 16)
ss <- substring (x, 18, 19)
if (formatC (as.integer (YY), width = 4, flag = "0") != YY |
formatC (as.integer (MM), width = 2, flag = "0") != MM |
formatC (as.integer (DD), width = 2, flag = "0") != DD |
formatC (as.integer (hh), width = 2, flag = "0") != hh |
formatC (as.integer (mm), width = 2, flag = "0") != mm |
formatC (as.integer (ss), width = 2, flag = "0") != ss)
stop ("x is not is ISO8601 format ('YYYY-MM-DDThh:mm:ssZ')")
invisible (x)
}

#' Add a feature to an Overpass query
#'
#' @param opq An `overpass_query` object
Expand Down
4 changes: 2 additions & 2 deletions codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
"codeRepository": "https://github.com/ropensci/osmdata",
"issueTracker": "https://github.com/ropensci/osmdata/issues",
"license": "https://spdx.org/licenses/GPL-3.0",
"version": "0.1.3.1",
"version": "0.1.3.2",
"programmingLanguage": {
"@type": "ComputerLanguage",
"name": "R",
Expand Down Expand Up @@ -352,7 +352,7 @@
"developmentStatus": "active",
"releaseNotes": "https://github.com/ropensci/osmdata/blob/master/NEWS.md",
"readme": "https://github.com/ropensci/osmdata/blob/master/README.md",
"fileSize": "1169.844KB",
"fileSize": "3531.16KB",
"citation": [
{
"@type": "ScholarlyArticle",
Expand Down
13 changes: 12 additions & 1 deletion man/opq.Rd

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

0 comments on commit fa1ae12

Please sign in to comment.