Skip to content

Commit

Permalink
Fix problems and improve unnesting
Browse files Browse the repository at this point in the history
  • Loading branch information
dieghernan committed Apr 8, 2024
1 parent ed3a4aa commit 1658018
Show file tree
Hide file tree
Showing 11 changed files with 292 additions and 27 deletions.
1 change: 1 addition & 0 deletions .Rbuildignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@
^CODE_OF_CONDUCT\.md$
^CONTRIBUTING\.md$
^Rplots\.pdf$
^vignettes/articles$
3 changes: 2 additions & 1 deletion DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ Suggests:
tidygeocoder
VignetteBuilder:
knitr
Config/Needs/website: dieghernan/gitdevr, remotes, devtools, tidyverse
Config/Needs/website: dieghernan/gitdevr, remotes, devtools, tidyverse,
leaflet, reactable, crosswalk, tidyr
Config/testthat/edition: 3
Config/testthat/parallel: true
Copyright: Data © OpenStreetMap contributors, ODbL 1.0.
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
`geo_lite_struct()` / `geo_lite_struct_sf(),` so now are more robust and
compatible with **sf** objects.

- Improve unnesting of fields when requiring `extratags`, i.e.
`custom_query = list(extratags = TRUE)`.

- It is possible to use **nominatimlite** with local server thanks to the new
argument `nominatim_server` (#42 \@alexwhitedatamine).

Expand Down
2 changes: 1 addition & 1 deletion R/geo_amenity.R
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ geo_amenity <- function(

# Clean columns and names
nm <- names(all_res)
nm[nm == "q_amenity"] <- "amenity"
nm[nm == "q_amenity"] <- "query"
names(all_res) <- nm
all_res <- all_res[, !grepl("^q_", nm)]

Expand Down
4 changes: 3 additions & 1 deletion R/geo_amenity_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,17 @@ geo_amenity_sf <- function(

# Clean columns and names
nm <- names(all_res)
nm[nm == "q_amenity"] <- "amenity"
nm[nm == "q_amenity"] <- "query"
names(all_res) <- nm
all_res <- all_res[, !grepl("^q_", nm)]
all_res <- sf_to_tbl(all_res)

if (strict) {
bbox_sf <- bbox_to_poly(bbox)
int <- as.vector(sf::st_intersects(all_res, bbox_sf, sparse = FALSE))
all_res <- all_res[int, ]
}


return(all_res)
}
70 changes: 65 additions & 5 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,17 @@ is_named <- function(x) {

keep_names <- function(x, return_addresses, full_results,
colstokeep = "query") {
names(x) <- gsub("address.", "", names(x), fixed = TRUE)
names(x) <- gsub("namedetails.", "", names(x), fixed = TRUE)
names(x) <- gsub("display_name", "address", names(x), fixed = TRUE)
x$address <- x$display_name
if ("boundingbox" %in% names(x)) {
bbun <- lapply(x$boundingbox, function(y) {
unl <- unlist(y)
bb <- dplyr::tibble(boundingbox = list(as.double(unl)))
bb
})
bbun <- dplyr::bind_rows(bbun)
cln <- x[, names(x) != "boundingbox"]
x <- dplyr::bind_cols(cln, bbun)
}

out_cols <- colstokeep
if (return_addresses) out_cols <- c(out_cols, "address")
Expand All @@ -55,8 +63,10 @@ keep_names <- function(x, return_addresses, full_results,
keep_names_rev <- function(x, address = "address", return_coords = FALSE,
full_results = FALSE,
colstokeep = address) {
names(x) <- gsub("display_name", address, names(x))

x$xxxyyyzzz <- x$display_name
nm <- names(x)
nm <- gsub("xxxyyyzzz", address, nm, fixed = TRUE)
names(x) <- nm
out_cols <- colstokeep
if (return_coords) out_cols <- c(out_cols, "lat", "lon")
if (full_results) out_cols <- c(out_cols, "lat", "lon", names(x))
Expand Down Expand Up @@ -110,11 +120,14 @@ unnest_reverse <- function(x) {
# OSM address
if ("address" %in% names(lngths)) {
ad <- dplyr::as_tibble(x$address)[1, ]
names(ad) <- paste0("address.", names(ad))

endobj <- dplyr::bind_cols(endobj, ad)
}

if ("extratags" %in% names(lngths)) {
xtra <- dplyr::as_tibble(x$extratags)[1, ]
names(xtra) <- paste0("extratags.", names(xtra))
endobj <- dplyr::bind_cols(endobj, xtra)
}

Expand Down Expand Up @@ -153,6 +166,51 @@ sf_to_tbl <- function(x) {

unnest_sf <- function(x) {
# Unnest
if ("address" %in% names(x)) {
# Need to unnest
add <- as.character(x$address)
newadd <- lapply(add, function(x) {
df <- jsonlite::fromJSON(x, simplifyVector = TRUE)
dplyr::as_tibble(df)
})

newadd <- dplyr::bind_rows(newadd)
names(newadd) <- paste0("address.", names(newadd))

newsfobj <- x
newsfobj <- x[, setdiff(names(x), "address")]
x <- dplyr::bind_cols(newsfobj, newadd)
}

if ("extratags" %in% names(x)) {
# Need to unnest
xtra <- as.character(x$extratags)

newxtra <- lapply(xtra, function(x) {
if (any(is.na(x), is.null(x))) {
return(dplyr::tibble(xxx_empty_remove = NA))
}
df <- jsonlite::fromJSON(x, simplifyVector = TRUE)
dplyr::as_tibble(df)
})

newxtra <- dplyr::bind_rows(newxtra)
names(newxtra) <- paste0("extratags.", names(newxtra))

newsfobj <- x
newsfobj <- x[, setdiff(names(x), "extratags")]
x <- dplyr::bind_cols(newsfobj, newxtra)
x <- x[, setdiff(names(x), "extratags.xxx_empty_remove")]
}


x <- sf_to_tbl(x)

x

# Unnest fields
need_un <- any(c("address", "extratags") %in% names(x))

Check warning on line 212 in R/utils.R

View workflow job for this annotation

GitHub Actions / Run lintr scanning

file=R/utils.R,line=212,col=3,[object_usage_linter] local variable 'need_un' assigned but may not be used

Check warning

Code scanning / lintr

local variable 'need_un' assigned but may not be used Warning

local variable 'need_un' assigned but may not be used

if (!("address" %in% names(x))) {
return(x)
}
Expand Down Expand Up @@ -186,6 +244,7 @@ unnest_sf_reverse <- function(x) {
})

newadd <- dplyr::bind_rows(newadd)[1, ]
names(newadd) <- paste0("address.", names(newadd))

newsfobj <- x
newsfobj <- x[, setdiff(names(x), "address")]
Expand All @@ -202,6 +261,7 @@ unnest_sf_reverse <- function(x) {
})

newxtra <- dplyr::bind_rows(newxtra)[1, ]
names(newxtra) <- paste0("extratags.", names(newxtra))

newsfobj <- x
newsfobj <- x[, setdiff(names(x), "extratags")]
Expand Down
2 changes: 1 addition & 1 deletion codemeta.json
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@
},
"applicationCategory": "cartography",
"keywords": ["r", "geocoding", "openstreetmap", "address", "nominatim", "reverse-geocoding", "rstats", "shapefile", "r-package", "spatial", "cran", "api-wrapper", "api", "gis"],
"fileSize": "242.977KB",
"fileSize": "244.777KB",
"citation": [
{
"@type": "SoftwareSourceCode",
Expand Down
5 changes: 5 additions & 0 deletions inst/WORDLIST
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ CMD
Cambon
CodeFactor
DOI
Flaticon
Freepik
Geocode
Geocodes
Geocoding
Expand All @@ -21,7 +23,9 @@ Transamerica
al
api
arcgeocoder
browsable
codecov
crosstalk
de
et
geo
Expand All @@ -35,6 +39,7 @@ json
lon
osm
osmdata
reactable
rlang
testthat
tibble
Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-geo_amenity.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ test_that("Progress bar", {
skip_if_api_server()
skip_if_offline()

bbox <- c(2.113482, 41.328553, 2.206866, 41.420785)
bbox <- c(-73.9894467311, 40.75573629, -73.9830630737, 40.75789245)

# No pbar
expect_silent(geo_amenity(bbox, "school"))
expect_silent(geo_amenity(bbox, "school", progressbar = TRUE))
expect_silent(geo_amenity(bbox, "restaurant"))
expect_silent(geo_amenity(bbox, "restaurant", progressbar = TRUE))

# Get a pbar
expect_output(aa <- geo_amenity(bbox, c("pub", "school")))
expect_output(aa <- geo_amenity(bbox, c("pub", "restaurant")))

# Not
expect_silent(aa <- geo_amenity(
bbox, c("pub", "school"),
bbox, c("pub", "restaurant"),
progressbar = FALSE
))
})
Expand All @@ -31,7 +31,7 @@ test_that("Checking query", {
), "50 results")


expect_identical(names(obj), c("amenity", "lat", "lon", "address"))
expect_identical(names(obj), c("query", "lat", "lon", "address"))

obj <- geo_amenity(
bbox = c(-1.1446, 41.5022, -0.4854, 41.8795),
Expand All @@ -40,7 +40,7 @@ test_that("Checking query", {
full_results = FALSE,
return_addresses = FALSE
)
expect_identical(names(obj), c("amenity", "at", "ong"))
expect_identical(names(obj), c("query", "at", "ong"))

obj <- geo_amenity(
bbox = c(-1.1446, 41.5022, -0.4854, 41.8795),
Expand All @@ -50,7 +50,7 @@ test_that("Checking query", {
return_addresses = TRUE
)

expect_identical(names(obj), c("amenity", "at", "ong", "address"))
expect_identical(names(obj), c("query", "at", "ong", "address"))

obj <- geo_amenity(
bbox = c(-1.1446, 41.5022, -0.4854, 41.8795),
Expand All @@ -60,7 +60,7 @@ test_that("Checking query", {
return_addresses = FALSE
)

expect_identical(names(obj)[1:4], c("amenity", "at", "ong", "address"))
expect_identical(names(obj)[1:4], c("query", "at", "ong", "address"))
expect_gt(ncol(obj), 4)


Expand Down
18 changes: 9 additions & 9 deletions tests/testthat/test-geo_amenity_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,18 @@ test_that("Progress bar", {
skip_if_api_server()
skip_if_offline()

bbox <- c(2.113482, 41.328553, 2.206866, 41.420785)
bbox <- c(-73.9894467311, 40.75573629, -73.9830630737, 40.75789245)

# No pbar
expect_silent(geo_amenity_sf(bbox, "school"))
expect_silent(geo_amenity_sf(bbox, "school", progressbar = TRUE))
expect_silent(geo_amenity_sf(bbox, "restaurant"))
expect_silent(geo_amenity_sf(bbox, "restaurant", progressbar = TRUE))

# Get a pbar
expect_output(aa <- geo_amenity_sf(bbox, c("pub", "school")))
expect_output(aa <- geo_amenity_sf(bbox, c("pub", "restaurant")))

# Not
expect_silent(aa <- geo_amenity_sf(
bbox, c("pub", "school"),
bbox, c("pub", "restaurant"),
progressbar = FALSE
))
})
Expand All @@ -31,15 +31,15 @@ test_that("Checking query", {
), "50 results")


expect_identical(names(obj), c("amenity", "address", "geometry"))
expect_identical(names(obj), c("query", "address", "geometry"))

obj <- geo_amenity_sf(
bbox = c(-1.1446, 41.5022, -0.4854, 41.8795),
"pub",
full_results = FALSE,
return_addresses = FALSE
)
expect_identical(names(obj), c("amenity", "geometry"))
expect_identical(names(obj), c("query", "geometry"))

obj <- geo_amenity_sf(
bbox = c(-1.1446, 41.5022, -0.4854, 41.8795),
Expand All @@ -48,7 +48,7 @@ test_that("Checking query", {
return_addresses = TRUE
)

expect_identical(names(obj), c("amenity", "address", "geometry"))
expect_identical(names(obj), c("query", "address", "geometry"))

obj <- geo_amenity_sf(
bbox = c(-1.1446, 41.5022, -0.4854, 41.8795),
Expand All @@ -57,7 +57,7 @@ test_that("Checking query", {
return_addresses = FALSE
)

expect_identical(names(obj)[1:2], c("amenity", "address"))
expect_identical(names(obj)[1:2], c("query", "address"))
expect_gt(ncol(obj), 3)


Expand Down
Loading

0 comments on commit 1658018

Please sign in to comment.