Skip to content

Commit 1bc38ef

Browse files
authored
Merge pull request #339 from ropensci/bb-poly
getbb with sf_polygon for #338
2 parents d60e833 + 1f489c7 commit 1bc38ef

File tree

9 files changed

+121
-52
lines changed

9 files changed

+121
-52
lines changed

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# R specific hooks: https://github.com/lorenzwalthert/precommit
33
repos:
44
- repo: https://github.com/lorenzwalthert/precommit
5-
rev: v0.4.1
5+
rev: v0.4.2
66
hooks:
77
- id: style-files
88
args: [--style_pkg=spaceout, --style_fun=spaceout_style]
@@ -73,7 +73,7 @@ repos:
7373
files: '\.(Rhistory|RData|Rds|rds)$'
7474
# `exclude: <regex>` to allow committing specific files
7575
- id: description version
76-
name: Version has been incremented in DESCRIPTION
76+
name: Version has been incremeneted in DESCRIPTION
7777
entry: .hooks/description
7878
language: script
7979

DESCRIPTION

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
Package: osmdata
22
Title: Import 'OpenStreetMap' Data as Simple Features or Spatial Objects
3-
Version: 0.2.5.009
3+
Version: 0.2.5.016
44
Authors@R: c(
55
person("Mark", "Padgham", , "[email protected]", role = c("aut", "cre")),
66
person("Bob", "Rudis", role = "aut"),

NEWS.md

+5
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
- Implemented `c.osmdata_sc` method to join `osmdata_sc` objects (#333)
88

9+
## Minor changes
10+
11+
- Improved `get_bb(..., format_out = "sf_polygon")` to return full metadata
12+
along with geometries (#338 thanks to @RegularnaMatrica)
13+
914

1015
0.2.5
1116
===================

R/getbb.R

+26-2
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,14 @@ getbb <- function (place_name,
278278
}
279279

280280
if (format_out == "sf_polygon") {
281-
ret <- bb_as_sf_poly (gt_p, gt_mp, place_name)
281+
ret_poly <- bb_as_sf_poly (gt_p, gt_mp, place_name)
282+
obj_index <- as.integer (c (names (gt_p), names (gt_mp)))
283+
ret_data <- obj [obj_index, which (!names (obj) %in% c ("boundingbox", "geotext"))]
284+
ret <- cbind (ret_data, ret_poly)
285+
# Then restore sf attributes:
286+
nms <- names (ret)
287+
attributes (ret) <- attributes (ret_poly)
288+
names (ret) <- nms
282289
}
283290

284291
return (ret)
@@ -318,7 +325,7 @@ get_nominatim_query <- function (place_name,
318325
}
319326

320327
if (!silent) {
321-
print (req$url)
328+
message (req$url)
322329
}
323330

324331
req <- httr2::req_retry (req, max_tries = 10L)
@@ -360,9 +367,12 @@ get_geotext_poly <- function (obj) {
360367

361368
. <- NULL # suppress R CMD check note
362369

370+
index_final <- seq_len (nrow (obj))
371+
363372
indx_multi <- grep ("MULTIPOLYGON", obj$geotext)
364373
gt_p <- NULL
365374
indx <- which (!(seq (nrow (obj)) %in% indx_multi))
375+
index_final <- index_final [indx]
366376
gt_p <- obj$geotext [indx] %>%
367377
gsub ("POLYGON\\(\\(", "", .) %>%
368378
gsub ("\\)\\)", "", .) %>%
@@ -371,6 +381,9 @@ get_geotext_poly <- function (obj) {
371381
for (i in indx_na) {
372382
gt_p [[i]] <- NULL
373383
}
384+
if (length (indx_na) > 0L) {
385+
index_final <- index_final [-indx_na]
386+
}
374387

375388
# points and linestrings may be present in result, and will be prepended
376389
# by sf-standard prefixes, while (multi)polygons will have been stripped
@@ -384,11 +397,15 @@ get_geotext_poly <- function (obj) {
384397
))
385398
if (length (indx) > 0) {
386399
gt_p <- gt_p [-indx]
400+
index_final <- index_final [-indx]
387401
}
388402

389403
if (length (gt_p) > 0) {
390404
gt_p <- lapply (gt_p, function (i) get1bdypoly (i))
405+
lens <- vapply (gt_p, length, integer (1))
406+
index_final <- rep (index_final, times = lens)
391407
gt_p <- do.call (c, gt_p)
408+
names (gt_p) <- as.character (index_final)
392409
}
393410

394411
return (gt_p)
@@ -407,6 +424,7 @@ get_geotext_multipoly <- function (obj) {
407424

408425
indx_multi <- grep ("MULTIPOLYGON", obj$geotext)
409426
gt_mp <- NULL
427+
index_final <- seq_len (nrow (obj)) [indx_multi]
410428

411429
# nocov start
412430
# TODO: Test this
@@ -419,11 +437,17 @@ get_geotext_multipoly <- function (obj) {
419437
for (i in indx_na) {
420438
gt_mp [[i]] <- NULL
421439
}
440+
if (length (indx_na) > 0L) {
441+
index_final <- index_final [-indx_na]
442+
}
422443
}
423444
# nocov end
424445

425446
if (length (gt_mp) > 0) {
426447
gt_mp <- lapply (gt_mp, function (i) get1bdypoly (i))
448+
lens <- vapply (gt_mp, length, integer (1))
449+
index_final <- rep (index_final, times = lens)
450+
names (gt_mp) <- as.character (index_final)
427451
}
428452

429453
return (gt_mp)

codemeta.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"codeRepository": "https://github.com/ropensci/osmdata/",
1212
"issueTracker": "https://github.com/ropensci/osmdata/issues",
1313
"license": "https://spdx.org/licenses/GPL-3.0",
14-
"version": "0.2.5.009",
14+
"version": "0.2.5.016",
1515
"programmingLanguage": {
1616
"@type": "ComputerLanguage",
1717
"name": "R",

tests/testthat/mock_bb_poly/nominatim/search-c1f6d2.json

+40-17
Large diffs are not rendered by default.

tests/testthat/mock_bb_sf/nominatim/search-c1f6d2.json

+40-17
Large diffs are not rendered by default.

tests/testthat/test-getbb.R

+2-1
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ test_that ("getbb-place_name", {
3838
range2 <- apply (res2, 1, function (i) diff (range (i)))
3939
expect_true (all (range2 >= range0))
4040

41-
expect_output (
41+
expect_message (
4242
res0 <- with_mock_dir ("mock_bb", {
4343
getbb (place_name = "Salzburg", silent = FALSE)
4444
})
@@ -132,6 +132,7 @@ test_that ("getbb-polygon", {
132132
expect_is (res, "sf")
133133
expect_is (res$geometry, "sfc_POLYGON")
134134
expect_true (length (res$geometry) > 1)
135+
expect_true (ncol (res) > 1)
135136
})
136137

137138
test_that ("bbox-to-string", {

tests/testthat/test-sf-construction.R

+4-11
Original file line numberDiff line numberDiff line change
@@ -47,21 +47,14 @@ make_sfc <- function (x, type) {
4747
class (x) <- c (paste0 ("sfc_", class (x [[1L]]) [2L]), "sfc")
4848
attr (x, "bbox") <- bb
4949

50-
if (packageVersion ("sf") < 0.9) {
51-
NA_crs_ <- structure (list ( # nolint
52-
epsg = NA_integer_, # nolint
53-
proj4string = NA_character_
54-
), # nolint
55-
class = "crs"
56-
)
57-
} else {
58-
NA_crs_ <- structure (list ( # nolint
50+
NA_crs_ <- structure (
51+
list ( # nolint
5952
input = NA_character_, # nolint
6053
wkt = NA_character_
6154
), # nolint
6255
class = "crs"
63-
)
64-
}
56+
)
57+
6558
attr (x, "crs") <- NA_crs_
6659

6760
return (x)

0 commit comments

Comments
 (0)