Skip to content

Commit

Permalink
Merge pull request #42 from ropensci/sf_list_gpxs
Browse files Browse the repository at this point in the history
Implement format = sf for osm_list_gpxs()
  • Loading branch information
jmaspons committed Jul 5, 2024
2 parents 44db407 + 21a88cf commit 8ca8b4c
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 15 deletions.
5 changes: 3 additions & 2 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,12 @@

* Upgrade logo by @atarom
* Add inst/CITATION
* Improve tests and fix bugs (#35, 08fb4b10abf0270d8bea2473b02b2520ba341521)
* Improve tests and fix bugs (#35, [08fb4b1](https://github.com/ropensci/osmapiR/commit/08fb4b10abf0270d8bea2473b02b2520ba341521))
* Add format = "sf" for functions returning objects of class `osmapi_map_notes` (#36)
* Add format = "sf" for functions returning objects of class `osmapi_changesets` (#37)
* Add format = "sf" for functions returning objects of class `osmapi_gpx_metadata` (#38)
* Add format = "sf" for `osm_get_gpx_metadata()` (#38)
* Updated links to the new osmapiR home at rOpenSci (#40)
* Add format = "sf" for `osm_list_gpxs()` (#42)

# osmapiR 0.1.0

Expand Down
20 changes: 14 additions & 6 deletions R/osmapi_gps_traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -470,11 +470,12 @@ osm_get_data_gpx <- function(gpx_id, format) {
#'
#' Use this to get a list of GPX traces owned by the authenticated user. Requires authentication.
#'
#' @param format Format of the output. Can be `"R"` (default) or `"xml"`.
#' @param format Format of the output. Can be `"R"` (default), `"sf"` or `"xml"`.
#'
#' @return
#' If `format = "R"`, returns a data frame with one trace per row. If `format = "xml"`, returns a
#' [xml2::xml_document-class] similar to [osm_get_gpx_metadata()]. Example:
#' Results with the same format as [osm_get_gpx_metadata()]. If `format = "R"`, returns a data frame with one trace
#' per row. If `format = "sf"`, returns a `sf` object from \pkg{sf}. If `format = "xml"`, returns a
#' [xml2::xml_document-class]. Example:
#' ``` xml
#' <?xml version="1.0" encoding="UTF-8"?>
#' <osm version="0.6" generator="OpenStreetMap server">
Expand All @@ -498,18 +499,25 @@ osm_get_data_gpx <- function(gpx_id, format) {
#' traces <- osm_list_gpxs()
#' traces
#' }
osm_list_gpxs <- function(format = c("R", "xml")) {
osm_list_gpxs <- function(format = c("R", "sf", "xml")) {
format <- match.arg(format)
if (format == "sf" && !requireNamespace("sf", quietly = TRUE)) {
stop("Missing `sf` package. Install with:\n\tinstall.package(\"sf\")")
}

req <- osmapi_request(authenticate = TRUE)
req <- httr2::req_method(req, "GET")
req <- httr2::req_url_path_append(req, "user", "gpx_files")

resp <- httr2::req_perform(req)
obj_xml <- httr2::resp_body_xml(resp)

if (format == "R") {
if (format %in% c("R", "sf")) {
out <- gpx_meta_xml2DF(obj_xml)
} else {
if (format == "sf") {
out <- sf::st_as_sf(out, coords = c("lon", "lat"), crs = sf::st_crs(4326))
}
} else { # format == "xml"
out <- obj_xml
}

Expand Down
9 changes: 5 additions & 4 deletions man/osm_list_gpxs.Rd

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

10 changes: 7 additions & 3 deletions tests/testthat/test-gps_traces.R
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,18 @@ test_that("osm_get_data_gpx works", {
test_that("osm_list_gpxs works", {
with_mock_dir("mock_list_gpxs", {
traces <- osm_list_gpxs()
sf_traces <- osm_list_gpxs(format = "sf")
xml_traces <- osm_list_gpxs(format = "xml")
})

expect_s3_class(traces, "data.frame")
expect_named(traces, column_meta_gpx)

expect_s3_class(traces, class = "data.frame", exact = TRUE)
expect_s3_class(sf_traces, class = c("sf", "data.frame"), exact = TRUE)
expect_s3_class(xml_traces, "xml_document")

expect_named(traces, column_meta_gpx)
expect_named(sf_traces, column_meta_gpx_sf)

# Compare xml & R
expect_equal(nrow(traces), nrow(sf_traces))
expect_equal(nrow(traces), xml2::xml_length(xml_traces))
})

0 comments on commit 8ca8b4c

Please sign in to comment.