Skip to content

Commit

Permalink
Implement NA bboxes in
Browse files Browse the repository at this point in the history
  • Loading branch information
jmaspons committed Jul 15, 2024
1 parent 84f16e7 commit 7ea4f5d
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 13 deletions.
2 changes: 1 addition & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
@@ -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")),
Expand Down
22 changes: 12 additions & 10 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 13 additions & 1 deletion R/st_as_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand Down Expand Up @@ -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), ...)
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
2 changes: 2 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ af
aff
AGsWGO
amz
anyNA
api
apiL
apis
Expand All @@ -18,6 +19,7 @@ Auth
ba
Backtrace
barzzz
bb
bbox
bcb
BEXER
Expand Down
6 changes: 6 additions & 0 deletions man/st_as_sf.Rd

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

23 changes: 23 additions & 0 deletions tests/testthat/_snaps/changesets.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions tests/testthat/test-changesets.R
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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")

Expand All @@ -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
Expand Down

0 comments on commit 7ea4f5d

Please sign in to comment.