From 7ea4f5d7f412ef8cf7691741b836cf45ddeb61f2 Mon Sep 17 00:00:00 2001 From: Joan Maspons Date: Mon, 15 Jul 2024 09:22:04 +0200 Subject: [PATCH] Implement NA bboxes in --- DESCRIPTION | 2 +- NEWS.md | 22 ++++++++++++---------- R/st_as_sf.R | 14 +++++++++++++- codemeta.json | 2 +- inst/WORDLIST | 2 ++ man/st_as_sf.Rd | 6 ++++++ tests/testthat/_snaps/changesets.md | 23 +++++++++++++++++++++++ tests/testthat/test-changesets.R | 4 ++++ 8 files changed, 62 insertions(+), 13 deletions(-) diff --git a/DESCRIPTION b/DESCRIPTION index 3d70637..8d31b83 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: osmapiR Title: 'OpenStreetMap' API -Version: 0.1.0.9011 +Version: 0.1.0.9012 Authors@R: c( person("Joan", "Maspons", , "joanmaspons@gmail.com", role = c("aut", "cre", "cph"), comment = c(ORCID = "0000-0003-2286-8727")), diff --git a/NEWS.md b/NEWS.md index 7c3cd90..a961e25 100644 --- a/NEWS.md +++ b/NEWS.md @@ -2,16 +2,18 @@ * Upgrade logo by @atarom * Add inst/CITATION -* 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 `osm_get_gpx_metadata()` (#38) -* Updated links to the new osmapiR home at rOpenSci (#40) -* Add format = "sf" for `osm_list_gpxs()` (#42) -* Split functions to parse gpx data from different API endpoints and different properties (#43) -* Add format = "sf" for functions returning objects of class `osmapi_gps_track` (#44) -* Add format = "sf" for functions returning objects of class `osmapi_gpx` (#45) -* Fix miscalculation of the nchar_url that trigger errors when many ids are requested in osm_fetch_objects() +* 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 `osm_get_gpx_metadata()` (#38). +* Updated links to the new osmapiR home at rOpenSci (#40). +* Add format = "sf" for `osm_list_gpxs()` (#42). +* Split functions to parse gpx data from different API endpoints and different properties (#43). +* Add format = "sf" for functions returning objects of class `osmapi_gps_track` (#44). +* Add format = "sf" for functions returning objects of class `osmapi_gpx` (#45). +* Fix miscalculation of the nchar_url that trigger errors when many ids are requested in osm_fetch_objects(). +* Fix changesets' bbox in `st_as_sf.osmapi_chagesets()` [84f16e7a](https://github.com/ropensci/osmapiR/commit/84f16e7adda087ab707cc2644c79ff1590cf307e)). +* Implement NA bboxes in `st_as_sf.osmapi_chagesets()`. # osmapiR 0.1.0 diff --git a/R/st_as_sf.R b/R/st_as_sf.R index 7f54a6a..8312c62 100644 --- a/R/st_as_sf.R +++ b/R/st_as_sf.R @@ -16,6 +16,12 @@ #' @family methods #' @seealso `st_as_sf()` from \pkg{sf} package. #' @examples +#' note <- osm_get_notes(note_id = "2067786") +#' sf::st_as_sf(note) +#' +#' chaset <- osm_get_changesets(changeset_id = 137595351, include_discussion = TRUE) +#' sf::st_as_sf(chaset) +#' #' gpx <- osm_get_points_gps(bbox = c(-0.3667545, 40.2153246, -0.3354263, 40.2364915)) #' sf::st_as_sf(gpx, format = "line") #' sf::st_as_sf(gpx, format = "points") @@ -62,7 +68,13 @@ st_as_sf.osmapi_changesets <- function(x, ...) { bbox <- apply(x[, c("min_lat", "min_lon", "max_lat", "max_lon")], 1, function(y) { sf::st_bbox(stats::setNames(as.numeric(y), nm = c("ymin", "xmin", "ymax", "xmax")), crs = sf::st_crs(4326)) }, simplify = FALSE) - geom <- lapply(bbox, sf::st_as_sfc) + geom <- lapply(bbox, function(bb) { + if (anyNA(bb[1:4])) { + sf::st_sfc(sf::st_polygon(), crs = sf::st_crs(4326)) + } else { + sf::st_as_sfc(bb) + } + }) out$geometry <- do.call(c, geom) out <- sf::st_as_sf(x = as.data.frame(out), crs = sf::st_crs(4326), ...) diff --git a/codemeta.json b/codemeta.json index fcdc8da..3bdcecd 100644 --- a/codemeta.json +++ b/codemeta.json @@ -8,7 +8,7 @@ "codeRepository": "https://github.com/ropensci/osmapiR", "issueTracker": "https://github.com/ropensci/osmapiR/issues", "license": "https://spdx.org/licenses/GPL-3.0", - "version": "0.1.0.9011", + "version": "0.1.0.9012", "programmingLanguage": { "@type": "ComputerLanguage", "name": "R", diff --git a/inst/WORDLIST b/inst/WORDLIST index 0cc5a03..1af7e89 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -4,6 +4,7 @@ af aff AGsWGO amz +anyNA api apiL apis @@ -18,6 +19,7 @@ Auth ba Backtrace barzzz +bb bbox bcb BEXER diff --git a/man/st_as_sf.Rd b/man/st_as_sf.Rd index 006d833..1fdb70d 100644 --- a/man/st_as_sf.Rd +++ b/man/st_as_sf.Rd @@ -36,6 +36,12 @@ available. Convert osmapiR objects to sf objects } \examples{ +note <- osm_get_notes(note_id = "2067786") +sf::st_as_sf(note) + +chaset <- osm_get_changesets(changeset_id = 137595351, include_discussion = TRUE) +sf::st_as_sf(chaset) + gpx <- osm_get_points_gps(bbox = c(-0.3667545, 40.2153246, -0.3354263, 40.2364915)) sf::st_as_sf(gpx, format = "line") sf::st_as_sf(gpx, format = "points") diff --git a/tests/testthat/_snaps/changesets.md b/tests/testthat/_snaps/changesets.md index 2a5dc47..2c643e0 100644 --- a/tests/testthat/_snaps/changesets.md +++ b/tests/testthat/_snaps/changesets.md @@ -516,6 +516,29 @@ 9 POLYGON ((-7.851801 43.4515... 10 POLYGON ((-7.852136 43.4517... +--- + + Code + print(chaset_empty_sf) + Output + Simple feature collection with 2 features and 9 fields (with 1 geometry empty) + Geometry type: POLYGON + Dimension: XY + Bounding box: xmin: 2.658056 ymin: 42.67996 xmax: 2.712578 ymax: 42.69368 + Geodetic CRS: WGS 84 + id created_at closed_at open user uid + 1 151819967 2024-05-25 16:49:04 2024-05-25 16:49:04 FALSE jmaspons 11725140 + 2 137595351 2023-06-21 09:09:18 2023-06-21 09:09:18 FALSE Quercinus 19641470 + comments_count changes_count + 1 0 0 + 2 4 1 + tags + 1 4 tags: comment=Afegeixo `name:ca` a objectes que apareixen a https://www.openst... + 2 8 tags: changesets_count=1 | comment=Correcció d'acord amb la toponímia oficial ... + geometry + 1 POLYGON EMPTY + 2 POLYGON ((2.658056 42.67996... + --- Code diff --git a/tests/testthat/test-changesets.R b/tests/testthat/test-changesets.R index 39ac2cd..b9e918e 100644 --- a/tests/testthat/test-changesets.R +++ b/tests/testthat/test-changesets.R @@ -259,6 +259,8 @@ test_that("osm_query_changesets works", { with_mock_dir("mock_query_changesets", { chaset$ids <- osm_query_changesets(changeset_ids = c(137627129, 137625624), order = "oldest") chaset$empty <- osm_query_changesets(changeset_ids = c(151819967, 137595351)) # empty & no empty + chaset_empty_sf <- osm_query_changesets(changeset_ids = c(151819967, 137595351), format = "sf") # empty & no empty + chaset$time <- osm_query_changesets( bbox = c(-1.241112, 38.0294955, 8.4203171, 42.9186456), user = "Mementomoristultus", @@ -312,6 +314,7 @@ test_that("osm_query_changesets works", { }) expect_s3_class(chaset_sf, class = c("sf_osmapi_changesets", "sf", "data.frame"), exact = TRUE) + expect_s3_class(chaset_empty_sf, class = c("sf_osmapi_changesets", "sf", "data.frame"), exact = TRUE) expect_s3_class(chaset_xml, class = "xml_document") expect_type(chaset_json, type = "list") @@ -325,6 +328,7 @@ test_that("osm_query_changesets works", { # methods lapply(chaset, function(x) expect_snapshot(print(x))) expect_snapshot(print(chaset_sf)) + expect_snapshot(print(chaset_empty_sf)) ## Empty results