Skip to content

Commit

Permalink
Vectorized version of osm_get_metadata_gpx and updated tests after se…
Browse files Browse the repository at this point in the history
…rver's API change
  • Loading branch information
jmaspons committed Dec 22, 2023
1 parent 7ea5014 commit 47e5cb7
Show file tree
Hide file tree
Showing 14 changed files with 119 additions and 45 deletions.
2 changes: 1 addition & 1 deletion NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export(osm_diff_upload_changeset)
export(osm_download_changeset)
export(osm_feed_notes)
export(osm_get_data_gpx)
export(osm_get_metadata_gpx)
export(osm_get_gpx_metadata)
export(osm_get_objects)
export(osm_get_points_gps)
export(osm_get_preferences_user)
Expand Down
56 changes: 56 additions & 0 deletions R/osm_get_gpx_metadata.R
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# Vectorized version of osm_get_metadata_gpx()

#' Download GPS Track Metadata
#'
#' Use this to access the metadata about GPX files. Available without authentication if the file is marked public.
#' Otherwise only usable by the owner account and requires authentication.
#'
#' @param gpx_id A vector of track ids represented by a numeric or a character value.
#' @param format Format of the output. Can be `R` (default) or `xml`.
#'
#' @return
#' If `format = "R"`, returns a data frame with one trace per row. If `format = "xml"`, returns a
#' [xml2::xml_document-class] with the following format:
#' ```xml
#' <?xml version="1.0" encoding="UTF-8"?>
#' <osm version="0.6" generator="OpenStreetMap server">
#' <gpx_file id="836619" name="track.gpx" lat="52.0194" lon="8.51807" uid="1234" user="Hartmut Holzgraefe" visibility="public" pending="false" timestamp="2010-10-09T09:24:19Z">
#' <description>PHP upload test</description>
#' <tag>test</tag>
#' <tag>php</tag>
#' </gpx_file>
#' <gpx_file>
#' ...
#' </gpx_file>
#' </osm>
#' ```
#' @family get GPS' functions
#' @export
#'
#' @examples
#' \dontrun{
#' trk_meta <- osm_get_gpx_metadata(gpx_id = 3498170)
#' trk_meta
#' }
osm_get_gpx_metadata <- function(gpx_id, format = c("R", "xml")) {
format <- match.arg(format)

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

if (format == "R") {
out <- do.call(rbind, outL)
} else if (format == "xml") {
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]]))
}
}
}

return(out)
}
7 changes: 3 additions & 4 deletions R/osmapi_gps_traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -300,8 +300,8 @@ osm_delete_gpx <- function(gpx_id) {
#' </gpx_file>
#' </osm>
#' ```
#' @family get GPS' functions
#' @export
# @family get GPS' functions
#' @noRd
#'
#' @examples
#' \dontrun{
Expand Down Expand Up @@ -436,8 +436,7 @@ osm_get_data_gpx <- function(gpx_id, format) {
#'
#' @return
#' If `format = "R"`, returns a data frame with one trace per row. If `format = "xml"`, returns a
#' [xml2::xml_document-class] similar to the one of [osm_get_metadata_gpx()], except with multiple possible `<gpx_file>`
#' elements. Example:
#' [xml2::xml_document-class] similar to [osm_get_gpx_metadata()]. Example:
#' ``` xml
#' <?xml version="1.0" encoding="UTF-8"?>
#' <osm version="0.6" generator="OpenStreetMap server">
Expand Down
2 changes: 1 addition & 1 deletion README.Rmd
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ Get GPX data:
``` r
## Requires authentication
usr_traces <- osm_list_gpxs()
osm_get_metadata_gpx(gpx_id = 3790367)
osm_get_gpx_metadata(gpx_id = 3790367)
osm_get_data_gpx(gpx_id = 3790367, format = "R")
```
```{r gpx}
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ Get GPX data:
``` r
## Requires authentication
usr_traces <- osm_list_gpxs()
osm_get_metadata_gpx(gpx_id = 3790367)
osm_get_gpx_metadata(gpx_id = 3790367)
osm_get_data_gpx(gpx_id = 3790367, format = "R")
```

Expand Down
2 changes: 2 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ OsmChange
OsmChange's
osmdata
osmexctract
outL
packageStartupMessage
packageVersion
pageNumber
Expand Down Expand Up @@ -318,6 +319,7 @@ uvAy
vals
vapply
VAZUTwcmHJLwDEX
Vectorized
Visca
VORx
vt
Expand Down
2 changes: 1 addition & 1 deletion man/osm_get_data_gpx.Rd

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

17 changes: 10 additions & 7 deletions man/osm_get_metadata_gpx.Rd → man/osm_get_gpx_metadata.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_get_points_gps.Rd

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

5 changes: 2 additions & 3 deletions man/osm_list_gpxs.Rd

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

Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?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/">
<gpx_file id="3458743" name="not_saved" uid="11725140" user="jmaspons" visibility="identifiable" pending="false" timestamp="2020-10-12T11:08:58Z" lat="41.71107572503388" lon="2.236165450885892">
<description>Puiggiró</description>
<tag>explorant</tag>
<tag>Cingles de Bertí</tag>
</gpx_file>
</osm>
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?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/">
<gpx_file id="3790367" name="Airoto_Marimanha.gpx" user="jmaspons" visibility="public" pending="false" timestamp="2021-08-20T10:30:15Z" lat="42.69660229794681" lon="1.0419843904674053">
<gpx_file id="3790367" name="Airoto_Marimanha.gpx" uid="11725140" user="jmaspons" visibility="public" pending="false" timestamp="2021-08-20T10:30:15Z" lat="42.69660229794681" lon="1.0419843904674053">
<description>Airoto Marimanha Oriental</description>
<tag>camp</tag>
<tag>a</tag>
Expand Down
35 changes: 19 additions & 16 deletions tests/testthat/mock_list_gpxs/osm.org/api/0.6/user/gpx_files.xml
Original file line number Diff line number Diff line change
@@ -1,55 +1,58 @@
<?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/">
<gpx_file id="3431956" name="StJuli_DeRibelles_HostalDeLaMuga_Talaix__Sadernes.gpx" user="jmaspons" visibility="identifiable" pending="false" timestamp="2020-09-28T09:23:52Z" lat="42.333541251719" lon="2.5914327520877123">
<gpx_file id="3431956" name="StJuli_DeRibelles_HostalDeLaMuga_Talaix__Sadernes.gpx" uid="11725140" user="jmaspons" visibility="identifiable" pending="false" timestamp="2020-09-28T09:23:52Z" lat="42.333541251719" lon="2.5914327520877123">
<description>StJuliàDeRibelles_HostalDeLaMuga_Talaixà_Sadernes</description>
</gpx_file>
<gpx_file id="3472797" name="not_saved" user="jmaspons" visibility="identifiable" pending="false" timestamp="2020-10-25T12:06:03Z" lat="42.313132993876934" lon="2.5896873883903027">
<gpx_file id="3472797" name="not_saved" uid="11725140" user="jmaspons" visibility="identifiable" pending="false" timestamp="2020-10-25T12:06:03Z" lat="42.313132993876934" lon="2.5896873883903027">
<description>St. Aniol - Passant del Gamarús - cova dels Trabucaires - Passant d'en Llebre</description>
</gpx_file>
<gpx_file id="3494162" name="not_saved" user="jmaspons" visibility="identifiable" pending="false" timestamp="2020-11-07T18:18:15Z" lat="41.49835919961333" lon="2.1166683454066515">
<gpx_file id="3494162" name="not_saved" uid="11725140" user="jmaspons" visibility="identifiable" pending="false" timestamp="2020-11-07T18:18:15Z" lat="41.49835919961333" lon="2.1166683454066515">
<description>camins de Cerdanyola</description>
<tag>trailrun</tag>
</gpx_file>
<gpx_file id="3498170" name="bruc.gpx" user="jmaspons" visibility="identifiable" pending="false" timestamp="2020-11-11T09:58:39Z" lat="41.58796912059188" lon="1.7747229896485806">
<gpx_file id="3498170" name="bruc.gpx" uid="11725140" user="jmaspons" visibility="identifiable" pending="false" timestamp="2020-11-11T09:58:39Z" lat="41.58796912059188" lon="1.7747229896485806">
<description>Corriols per les rodalies del Bruc</description>
</gpx_file>
<gpx_file id="3789949" name="Roca_de_la_Pena_corrent.gpx" user="jmaspons" visibility="public" pending="false" timestamp="2021-08-19T21:47:50Z" lat="42.18059817329049" lon="1.4281276892870665">
<gpx_file id="3789949" name="Roca_de_la_Pena_corrent.gpx" uid="11725140" user="jmaspons" visibility="public" pending="false" timestamp="2021-08-19T21:47:50Z" lat="42.18059817329049" lon="1.4281276892870665">
<description>Roca de la Pena circular</description>
</gpx_file>
<gpx_file id="3789948" name="Roca_de_la_Pena_des_de_Llobera.gpx" user="jmaspons" visibility="public" pending="false" timestamp="2021-08-19T21:47:07Z" lat="42.18116671778262" lon="1.4277103543281555">
<gpx_file id="3789948" name="Roca_de_la_Pena_des_de_Llobera.gpx" uid="11725140" user="jmaspons" visibility="public" pending="false" timestamp="2021-08-19T21:47:07Z" lat="42.18116671778262" lon="1.4277103543281555">
<description>Roca de la Pena des de Llobera</description>
</gpx_file>
<gpx_file id="3790367" name="Airoto_Marimanha.gpx" user="jmaspons" visibility="public" pending="false" timestamp="2021-08-20T10:30:15Z" lat="42.69660229794681" lon="1.0419843904674053">
<gpx_file id="3790367" name="Airoto_Marimanha.gpx" uid="11725140" user="jmaspons" visibility="public" pending="false" timestamp="2021-08-20T10:30:15Z" lat="42.69660229794681" lon="1.0419843904674053">
<description>Airoto Marimanha Oriental</description>
<tag>camp</tag>
<tag>a</tag>
<tag>través</tag>
</gpx_file>
<gpx_file id="3901854" name="Castell_Canig__refugi_d_Arag_.gpx" user="jmaspons" visibility="identifiable" pending="false" timestamp="2021-11-02T18:22:25Z" lat="42.53348727710545" lon="2.395844478160143">
<gpx_file id="3901854" name="Castell_Canig__refugi_d_Arag_.gpx" uid="11725140" user="jmaspons" visibility="identifiable" pending="false" timestamp="2021-11-02T18:22:25Z" lat="42.53348727710545" lon="2.395844478160143">
<description>Castell - Canigó - refugi d'Aragó</description>
</gpx_file>
<gpx_file id="4073819" name="Bisaroques.gpx" user="jmaspons" visibility="identifiable" pending="false" timestamp="2022-01-24T07:51:46Z" lat="42.188827358186245" lon="2.5100998766720295">
<gpx_file id="4062097" name="cingles.gpx" uid="11725140" user="jmaspons" visibility="trackable" pending="false" timestamp="2022-01-19T15:45:51Z" lat="41.70677346177399" lon="2.2133453004062176">
<description>Turó de les 11 hores</description>
</gpx_file>
<gpx_file id="4073819" name="Bisaroques.gpx" uid="11725140" user="jmaspons" visibility="identifiable" pending="false" timestamp="2022-01-24T07:51:46Z" lat="42.188827358186245" lon="2.5100998766720295">
<description>Volta circular amb corriols per traçar</description>
</gpx_file>
<gpx_file id="4118006" name="Perles_Figols.gpx" user="jmaspons" visibility="identifiable" pending="false" timestamp="2022-02-13T19:21:43Z" lat="42.17918783426285" lon="1.3936991896480322">
<gpx_file id="4118006" name="Perles_Figols.gpx" uid="11725140" user="jmaspons" visibility="identifiable" pending="false" timestamp="2022-02-13T19:21:43Z" lat="42.17918783426285" lon="1.3936991896480322">
<description>Camins pendents d'agegir entre Perles i Fígols pel Collet de la Canal d'en Joan</description>
</gpx_file>
<gpx_file id="4431250" name="activity_8969751253.gpx" user="jmaspons" visibility="identifiable" pending="false" timestamp="2022-06-07T10:19:52Z" lat="42.23573558963835" lon="1.6616315860301256">
<gpx_file id="4431250" name="activity_8969751253.gpx" uid="11725140" user="jmaspons" visibility="identifiable" pending="false" timestamp="2022-06-07T10:19:52Z" lat="42.23573558963835" lon="1.6616315860301256">
<description>Gósol - Vulturó per Josa del Cadí</description>
</gpx_file>
<gpx_file id="4431415" name="activity_8969751704.gpx" user="jmaspons" visibility="identifiable" pending="false" timestamp="2022-06-07T10:49:34Z" lat="42.28576574474573" lon="1.6366010438650846">
<gpx_file id="4431415" name="activity_8969751704.gpx" uid="11725140" user="jmaspons" visibility="identifiable" pending="false" timestamp="2022-06-07T10:49:34Z" lat="42.28576574474573" lon="1.6366010438650846">
<description>Vulturó i fins més avall del refugi de St. Jordi</description>
</gpx_file>
<gpx_file id="4432183" name="activity_8969752023.gpx" user="jmaspons" visibility="identifiable" pending="false" timestamp="2022-06-07T16:34:34Z" lat="42.27627390995622" lon="1.818500580266118">
<gpx_file id="4432183" name="activity_8969752023.gpx" uid="11725140" user="jmaspons" visibility="identifiable" pending="false" timestamp="2022-06-07T16:34:34Z" lat="42.27627390995622" lon="1.818500580266118">
<description>Tornada a Gòsol</description>
</gpx_file>
<gpx_file id="7797939" name="activity_11093847135.gpx" user="jmaspons" visibility="identifiable" pending="false" timestamp="2023-06-04T09:17:06Z" lat="31.136192930862308" lon="-7.927299840375781">
<gpx_file id="7797939" name="activity_11093847135.gpx" uid="11725140" user="jmaspons" visibility="identifiable" pending="false" timestamp="2023-06-04T09:17:06Z" lat="31.136192930862308" lon="-7.927299840375781">
<description>Imlil - Tamsoult</description>
</gpx_file>
<gpx_file id="7797955" name="activity_11103300874.gpx" user="jmaspons" visibility="identifiable" pending="false" timestamp="2023-06-04T09:21:17Z" lat="31.063376488164067" lon="-7.937062913551927">
<gpx_file id="7797955" name="activity_11103300874.gpx" uid="11725140" user="jmaspons" visibility="identifiable" pending="false" timestamp="2023-06-04T09:21:17Z" lat="31.063376488164067" lon="-7.937062913551927">
<description>Toubkal - Imlil</description>
</gpx_file>
<gpx_file id="7797946" name="activity_11102996506.gpx" user="jmaspons" visibility="identifiable" pending="false" timestamp="2023-06-04T09:19:07Z" lat="31.097403075546026" lon="-7.967628613114357">
<gpx_file id="7797946" name="activity_11102996506.gpx" uid="11725140" user="jmaspons" visibility="identifiable" pending="false" timestamp="2023-06-04T09:19:07Z" lat="31.097403075546026" lon="-7.967628613114357">
<description>Tamsoult - Refuge Toubkal</description>
</gpx_file>
</osm>
22 changes: 13 additions & 9 deletions tests/testthat/test-gps_traces.R
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
column_meta_gpx <- c("id", "name", "user", "visibility", "pending", "timestamp", "lat", "lon", "description", "tags")
column_meta_gpx <- c("id", "name", "uid", "user", "visibility", "pending", "timestamp", "lat", "lon", "description", "tags")
column_gpx <- c("lat", "lon", "ele", "time")
column_pts_gps <- c("lat", "lon", "time")

class_columns <- list(
id = "character", name = "character", user = "character", visibility = "character", pending = "logical",
timestamp = "POSIXct", lat = "character", lon = "character", description = "character", tags = "list",
ele = "character", time = "POSIXct"
id = "character", name = "character", uid = "character", user = "character", visibility = "character",
pending = "logical", timestamp = "POSIXct", lat = "character", lon = "character", description = "character",
tags = "list", ele = "character", time = "POSIXct"
)


Expand Down Expand Up @@ -87,17 +87,21 @@ test_that("edit gpx works", {
## Download Metadata: `GET /api/0.6/gpx/#id/details` ----

test_that("osm_get_metadata_gpx works", {
trk_meta <- list()
with_mock_dir("mock_get_metadata_gpx", {
trk_meta <- osm_get_metadata_gpx(gpx_id = 3790367)
trk_meta$track <- osm_get_gpx_metadata(gpx_id = 3790367)
trk_meta$tracks <- osm_get_gpx_metadata(gpx_id = c(3790367, 3458743))
})

expect_s3_class(trk_meta, "data.frame")
expect_named(trk_meta, column_meta_gpx)
lapply(trk_meta, function(x) expect_s3_class(x, "data.frame"))
lapply(trk_meta, function(x) expect_named(x, column_meta_gpx))

mapply(function(x, cl) expect_true(inherits(x, cl)), x = trk_meta, cl = class_columns[names(trk_meta)])
lapply(trk_meta, function(trk) {
mapply(function(x, cl) expect_true(inherits(x, cl)), x = trk, cl = class_columns[names(trk)])
})

# Check that time is extracted, otherwise it's 00:00:00 in local time
expect_false(strftime(as.POSIXct(trk_meta$timestamp), format = "%M:%S") == "00:00")
lapply(trk_meta, function(x) expect_false(unique(strftime(as.POSIXct(x$timestamp), format = "%M:%S") == "00:00")))
})


Expand Down

0 comments on commit 47e5cb7

Please sign in to comment.