diff --git a/.github/workflows/wipe-cache.yaml b/.github/workflows/wipe-cache.yaml index 45fbff09..1a64277a 100644 --- a/.github/workflows/wipe-cache.yaml +++ b/.github/workflows/wipe-cache.yaml @@ -1,6 +1,8 @@ name: Clear all Github actions caches manually on: workflow_dispatch: + schedule: + - cron: '0 16 15 * *' jobs: cache-clear: diff --git a/CITATION.cff b/CITATION.cff index 72223d98..d728000f 100644 --- a/CITATION.cff +++ b/CITATION.cff @@ -8,11 +8,11 @@ message: 'To cite package "nominatimlite" in publications use:' type: software license: MIT title: 'nominatimlite: Interface with ''Nominatim'' API Service' -version: 0.2.1 +version: 0.2.1.9000 doi: 10.5281/zenodo.5113195 abstract: Lite interface for getting data from 'OSM' service 'Nominatim' . - Extract coordinates from addresses, find places near a set of coordinates, search - for amenities and return spatial objects on 'sf' format. + Extract coordinates from addresses, find places near a set of coordinates and return + spatial objects on 'sf' format. authors: - family-names: Hernangómez given-names: Diego @@ -26,13 +26,13 @@ preferred-citation: given-names: Diego email: diego.hernangomezherrero@gmail.com orcid: https://orcid.org/0000-0001-8457-4658 - year: '2023' - version: 0.2.1 + year: '2024' + version: 0.2.1.9000 doi: 10.5281/zenodo.5113195 url: https://dieghernan.github.io/nominatimlite/ abstract: Lite interface for getting data from OSM service Nominatim . - Extract coordinates from addresses, find places near a set of coordinates, search - for amenities and return spatial objects on sf format. + Extract coordinates from addresses, find places near a set of coordinates and + return spatial objects on sf format. repository: https://CRAN.R-project.org/package=nominatimlite repository-code: https://github.com/dieghernan/nominatimlite url: https://dieghernan.github.io/nominatimlite/ @@ -65,7 +65,7 @@ references: - name: R Core Team location: name: Vienna, Austria - year: '2023' + year: '2024' institution: name: R Foundation for Statistical Computing version: '>= 3.6.0' @@ -92,7 +92,7 @@ references: given-names: Davis email: davis@posit.co orcid: https://orcid.org/0000-0003-4777-038X - year: '2023' + year: '2024' version: '>= 1.0.0' - type: software title: jsonlite @@ -105,11 +105,26 @@ references: given-names: Jeroen email: jeroen@berkeley.edu orcid: https://orcid.org/0000-0002-4035-0289 - year: '2023' + year: '2024' identifiers: - type: url value: https://arxiv.org/abs/1403.2805 version: '>= 1.7.0' +- type: software + title: lifecycle + abstract: 'lifecycle: Manage the Life Cycle of your Package Functions' + notes: Imports + url: https://lifecycle.r-lib.org/ + repository: https://CRAN.R-project.org/package=lifecycle + authors: + - family-names: Henry + given-names: Lionel + email: lionel@posit.co + - family-names: Wickham + given-names: Hadley + email: hadley@posit.co + orcid: https://orcid.org/0000-0003-4757-117X + year: '2024' - type: software title: sf abstract: 'sf: Simple Features for R' @@ -121,7 +136,7 @@ references: given-names: Edzer email: edzer.pebesma@uni-muenster.de orcid: https://orcid.org/0000-0001-8049-7069 - year: '2023' + year: '2024' version: '>= 0.9.0' - type: software title: utils @@ -131,7 +146,7 @@ references: - name: R Core Team location: name: Vienna, Austria - year: '2023' + year: '2024' institution: name: R Foundation for Statistical Computing - type: software @@ -168,7 +183,7 @@ references: - family-names: Dunnington given-names: Dewey orcid: https://orcid.org/0000-0002-9415-4582 - year: '2023' + year: '2024' version: '>= 3.0.0' - type: software title: knitr @@ -181,7 +196,7 @@ references: given-names: Yihui email: xie@yihui.name orcid: https://orcid.org/0000-0003-0645-5666 - year: '2023' + year: '2024' - type: software title: rmarkdown abstract: 'rmarkdown: Dynamic Documents for R' @@ -224,7 +239,7 @@ references: given-names: Richard email: rich@posit.co orcid: https://orcid.org/0000-0003-3925-190X - year: '2023' + year: '2024' - type: software title: testthat abstract: 'testthat: Unit Testing for R' @@ -235,7 +250,7 @@ references: - family-names: Wickham given-names: Hadley email: hadley@posit.co - year: '2023' + year: '2024' version: '>= 3.0.0' - type: software title: tidygeocoder @@ -260,4 +275,4 @@ references: given-names: Daniel email: possenriede+r@gmail.com orcid: https://orcid.org/0000-0002-6738-9845 - year: '2023' + year: '2024' diff --git a/DESCRIPTION b/DESCRIPTION index 16379758..20e20aa6 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Type: Package Package: nominatimlite Title: Interface with 'Nominatim' API Service -Version: 0.2.1 +Version: 0.2.1.9000 Authors@R: c( person("Diego", "Hernangómez", , "diego.hernangomezherrero@gmail.com", role = c("aut", "cre", "cph"), comment = c(ORCID = "0000-0001-8457-4658")), @@ -10,8 +10,8 @@ Authors@R: c( ) Description: Lite interface for getting data from 'OSM' service 'Nominatim' . Extract - coordinates from addresses, find places near a set of coordinates, - search for amenities and return spatial objects on 'sf' format. + coordinates from addresses, find places near a set of coordinates and + return spatial objects on 'sf' format. License: MIT + file LICENSE URL: https://dieghernan.github.io/nominatimlite/, https://github.com/dieghernan/nominatimlite @@ -21,6 +21,7 @@ Depends: Imports: dplyr (>= 1.0.0), jsonlite (>= 1.7.0), + lifecycle, sf (>= 0.9.0), utils Suggests: @@ -37,7 +38,6 @@ Config/testthat/parallel: true Copyright: Data © OpenStreetMap contributors, ODbL 1.0. Encoding: UTF-8 -LazyData: true Roxygen: list(markdown = TRUE) RoxygenNote: 7.2.3 X-schema.org-applicationCategory: cartography diff --git a/NAMESPACE b/NAMESPACE index ce795640..6c64dc25 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -11,3 +11,5 @@ export(nominatim_check_access) export(reverse_geo_lite) export(reverse_geo_lite_sf) importFrom(utils,download.file) +importFrom(utils,setTxtProgressBar) +importFrom(utils,txtProgressBar) diff --git a/NEWS.md b/NEWS.md index ada39426..906fa4be 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,15 @@ +# nominatimlite (development version) + +- Add a `progressbar` parameter to `geo_lite()`, `geo_lite_sf()`, + `reverse_geo_lite()` and `reverse_geo_lite_sf()` to display progress in the + console. + +## Deprecated + +- `geo_amenity()` and `geo_amenity_sf()`, see + [Nominatim/issues/1311](https://github.com/osm-search/Nominatim/issues/1311). +- `nominatimlite::osm_amenities` data set deleted. + # nominatimlite 0.2.1 - Remove **osmdata** from Suggests. diff --git a/R/bbox_to_poly.R b/R/bbox_to_poly.R index cf7f1e11..96e50031 100644 --- a/R/bbox_to_poly.R +++ b/R/bbox_to_poly.R @@ -1,4 +1,4 @@ -#' Create a bounding box `sf` object +#' Create a bounding box \CRANpkg{sf} object #' #' @description #' @@ -16,7 +16,6 @@ #' @seealso [sf::st_as_sfc()] #' #' @family spatial -#' @family amenity #' #' @details #' diff --git a/R/data.R b/R/data.R deleted file mode 100644 index 523dd9ad..00000000 --- a/R/data.R +++ /dev/null @@ -1,43 +0,0 @@ -#' OpenStreetMap amenity database -#' -#' @description -#' Database with the list of amenities available on OpenStreetMap. -#' -#' @family datasets -#' @family amenity -#' -#' @encoding UTF-8 -#' -#' @name osm_amenities -#' -#' @docType data -#' -#' @format A `tibble` with with -#' `r prettyNum(nrow(nominatimlite::osm_amenities), big.mark=",")` rows and -#' fields: -#' \describe{ -#' \item{category}{The category of the amenity} -#' \item{amenity}{The name of the amenity} -#' } -#' -#' @details -#' -#' ```{r, echo=FALSE} -#' -#' t <- nominatimlite::osm_amenities -#' -#' knitr::kable(t, col.names = c("**category**", "**amenity**")) -#' -#' -#' ``` -#' -#' @source -#' -#' @note Data extracted on **14 June 2021**. -#' -#' @examples -#' -#' amenities <- nominatimlite::osm_amenities -#' -#' amenities -NULL diff --git a/R/geo_address_lookup.R b/R/geo_address_lookup.R index 82af913f..43dadec9 100644 --- a/R/geo_address_lookup.R +++ b/R/geo_address_lookup.R @@ -21,7 +21,7 @@ #' @family lookup #' @family geocoding #' -#' @return A `tibble` with the results found by the query. +#' @return A \CRANpkg{tibble} with the results found by the query. #' #' @examplesIf nominatim_check_access() #' \donttest{ diff --git a/R/geo_address_lookup_sf.R b/R/geo_address_lookup_sf.R index 642bea71..6af9829c 100644 --- a/R/geo_address_lookup_sf.R +++ b/R/geo_address_lookup_sf.R @@ -6,7 +6,7 @@ #' \CRANpkg{sf} spatial object associated with the query, see #' [geo_address_lookup()] for retrieving the data in \CRANpkg{tibble} format. #' -#' @return A `sf` object with the results. +#' @return A \CRANpkg{sf} object with the results. #' #' @inheritParams geo_lite_sf #' @inheritParams geo_address_lookup diff --git a/R/geo_amenity.R b/R/geo_amenity.R index e5643b8d..48a157bc 100644 --- a/R/geo_amenity.R +++ b/R/geo_amenity.R @@ -1,33 +1,27 @@ #' Geocode amenities #' #' @description -#' This function search amenities as defined by OpenStreetMap on a restricted -#' area defined by a bounding box in the form of -#' `(, , , )`. This -#' function returns the \CRANpkg{tibble} associated with the query, see -#' [geo_amenity_sf()] for retrieving the data as a spatial object -#' (\CRANpkg{sf} format). +#' `r lifecycle::badge("deprecated")` +#' +#' This operation is not supported any more. Use +#' [**osmdata**](https://github.com/ropensci/osmdata) instead. #' #' #' @param bbox A numeric vector of latitude and longitude #' `(, , , )` that #' restrict the search area. See **Details**. #' @param amenity A character of a vector of character with the amenities to be -#' geolocated (i.e. `c("pub", "restaurant")`). See **Details** and -#' [nominatimlite::osm_amenities]. +#' geolocated (i.e. `c("pub", "restaurant")`). #' @param custom_query API-specific parameters to be used. -#' See [nominatimlite::geo_lite()]. #' @param strict Logical `TRUE/FALSE`. Force the results to be included inside #' the `bbox`. Note that Nominatim default behavior may return results located #' outside the provided bounding box. #' #' @inheritParams geo_lite #' -#' @return A `tibble` with the results. +#' @return A \CRANpkg{tibble} with the results. #' #' @seealso [geo_amenity_sf()] -#' @family amenity -#' @family geocoding #' @details #' #' Bounding boxes can be located using different online tools, as @@ -37,22 +31,7 @@ #' . #' #' -#' @examplesIf nominatim_check_access() -#' \donttest{ -#' # Times Square, NY, USA -#' bbox <- c(-73.9894467311, 40.75573629, -73.9830630737, 40.75789245) -#' -#' geo_amenity(bbox = bbox, amenity = "restaurant") -#' -#' # Several amenities -#' geo_amenity(bbox = bbox, amenity = c("restaurant", "pub")) -#' -#' # Increase limit and use with strict -#' geo_amenity( -#' bbox = bbox, amenity = c("restaurant", "pub"), limit = 10, -#' strict = TRUE -#' ) -#' } +#' @keywords internal #' #' @export geo_amenity <- function(bbox, @@ -65,129 +44,12 @@ geo_amenity <- function(bbox, verbose = FALSE, custom_query = list(), strict = FALSE) { - if (limit > 50) { - message(paste( - "Nominatim provides 50 results as a maximum. ", - "Your query may be incomplete" - )) - limit <- min(50, limit) - } - - - # Dedupe for query - amenity <- as.character(amenity) - init_key <- dplyr::tibble(query = amenity) - key <- unique(amenity) - - all_res <- lapply(key, function(x) { - geo_amenity_single( - bbox = bbox, - amenity = x, - lat, - long, - limit, - full_results, - return_addresses, - verbose, - custom_query + if (requireNamespace("lifecycle", quietly = TRUE)) { + lifecycle::deprecate_stop("0.3.0", "geo_amenity()", + details = paste( + "Operation not supported any", + "more by the Nominatim API." + ) ) - }) - - all_res <- dplyr::bind_rows(all_res) - all_res <- dplyr::left_join(init_key, all_res, by = "query") - - if (strict) { - strict <- all_res[lat] >= bbox[2] & - all_res[lat] <= bbox[4] & - all_res[long] >= bbox[1] & - all_res[long] <= bbox[3] - - strict <- as.logical(strict) - - all_res <- all_res[strict, ] - } - - - - return(all_res) -} - - - -#' @noRd -#' @inheritParams geo_amenity -geo_amenity_single <- function(bbox, - amenity, - lat = "lat", - long = "lon", - limit = 1, - full_results = TRUE, - return_addresses = TRUE, - verbose = FALSE, - custom_query = list()) { - # Step 1: Download ---- - bbox_txt <- paste0(bbox, collapse = ",") - - api <- "https://nominatim.openstreetmap.org/search?" - - url <- paste0( - api, "viewbox=", bbox_txt, "&q=[", amenity, - "]&format=json&limit=", limit - ) - - if (full_results) url <- paste0(url, "&addressdetails=1") - if (!"bounded" %in% names(custom_query)) url <- paste0(url, "&bounded=1") - - # Add options - url <- add_custom_query(custom_query, url) - - # Download to temp file - json <- tempfile(fileext = ".json") - res <- api_call(url, json, isFALSE(verbose)) - - # Step 2: Read and parse results ---- - - # Keep a tbl with the query - tbl_query <- dplyr::tibble(query = amenity) - - # If no response... - if (isFALSE(res)) { - message(url, " not reachable.") - out <- empty_tbl(tbl_query, lat, long) - return(invisible(out)) - } - - result <- dplyr::as_tibble(jsonlite::fromJSON(json, flatten = TRUE)) - - # Rename lat and lon - nmes <- names(result) - nmes[nmes == "lat"] <- lat - nmes[nmes == "lon"] <- long - - names(result) <- nmes - - # Empty query - if (nrow(result) == 0) { - message("No results for query ", amenity) - out <- empty_tbl(tbl_query, lat, long) - return(invisible(out)) } - - # Coords as double - result[lat] <- as.double(result[[lat]]) - result[long] <- as.double(result[[long]]) - - # Add query - result_clean <- result - result_clean$query <- amenity - - # Keep names - result_out <- keep_names(result_clean, return_addresses, full_results, - colstokeep = c("query", lat, long) - ) - - # As tibble - result_out <- dplyr::as_tibble(result_out) - - result_out } diff --git a/R/geo_amenity_sf.R b/R/geo_amenity_sf.R index fc26df09..80d2b0f1 100644 --- a/R/geo_amenity_sf.R +++ b/R/geo_amenity_sf.R @@ -1,16 +1,16 @@ #' Geocode amenities in Spatial format #' #' @description -#' This function search amenities as defined by OpenStreetMap on a restricted -#' area defined by a bounding box in the form of -#' `(, , , )`. This -#' function returns the \CRANpkg{sf} spatial object associated with the query, -#' see [geo_amenity()] for retrieving the data in \CRANpkg{tibble} format. +#' `r lifecycle::badge("deprecated")` +#' +#' This operation is not supported any more. Use +#' [**osmdata**](https://github.com/ropensci/osmdata) instead. +#' #' #' @inheritParams geo_lite_sf #' @inheritParams geo_amenity #' -#' @return A `sf` object with the results. +#' @return A \CRANpkg{sf} object with the results. #' #' @details #' @@ -22,37 +22,8 @@ #' #' @inheritSection geo_lite_sf About Geometry Types #' -#' @seealso [geo_amenity()] -#' @family amenity -#' @family geocoding -#' @family spatial -#' -#' @examplesIf nominatim_check_access() -#' \donttest{ -#' # Madrid, Spain -#' -#' library(ggplot2) -#' -#' bbox <- c(-3.888954, 40.311977, -3.517916, 40.643729) -#' -#' # Restaurants and pubs -#' -#' rest_pub <- geo_amenity_sf(bbox, c("restaurant", "pub"), limit = 50) -#' -#' if (any(!sf::st_is_empty(rest_pub))) { -#' ggplot(rest_pub) + -#' geom_sf() -#' } +#' @keywords internal #' -#' # Hospital as polygon -#' -#' hosp <- geo_amenity_sf(bbox, "hospital", points_only = FALSE) -#' -#' if (any(!sf::st_is_empty(hosp))) { -#' ggplot(hosp) + -#' geom_sf() -#' } -#' } #' @export geo_amenity_sf <- function(bbox, amenity, @@ -63,133 +34,12 @@ geo_amenity_sf <- function(bbox, custom_query = list(), points_only = TRUE, strict = FALSE) { - if (limit > 50) { - message(paste( - "Nominatim provides 50 results as a maximum. ", - "Your query may be incomplete" - )) - limit <- min(50, limit) - } - - - # Dedupe for query - amenity <- as.character(amenity) - init_key <- dplyr::tibble(query = amenity) - key <- unique(amenity) - - all_res <- lapply(key, function(x) { - geo_amenity_sf_single( - bbox = bbox, - amenity = x, - limit, - full_results, - return_addresses, - verbose, - custom_query, - points_only + if (requireNamespace("lifecycle", quietly = TRUE)) { + lifecycle::deprecate_stop("0.3.0", "geo_amenity_sf()", + details = paste( + "Operation not supported any", + "more by the Nominatim API." + ) ) - }) - - all_res <- dplyr::bind_rows(all_res) - - all_res <- sf_to_tbl(all_res) - - # Handle dupes in sf - if (!identical(as.character(init_key$query), key)) { - # Join with indexes - template <- sf::st_drop_geometry(all_res)[, "query"] - template$rindex <- seq_len(nrow(template)) - getrows <- dplyr::left_join(init_key, template, by = "query") - - # Select rows - all_res <- all_res[as.integer(getrows$rindex), ] - all_res <- sf_to_tbl(all_res) - } - - if (strict) { - bbox_sf <- bbox_to_poly(bbox) - strict <- sf::st_covered_by(all_res, bbox_sf, sparse = FALSE) - all_res <- all_res[strict, ] - all_res <- sf_to_tbl(all_res) - } - - return(all_res) -} - - - -#' @noRd -#' @inheritParams geo_amenity_sf -geo_amenity_sf_single <- function(bbox, - amenity, - limit = 1, - full_results = TRUE, - return_addresses = TRUE, - verbose = FALSE, - custom_query = list(), - points_only = TRUE) { - # Step 1: Download ---- - bbox_txt <- paste0(bbox, collapse = ",") - api <- "https://nominatim.openstreetmap.org/search?" - - url <- paste0( - api, "viewbox=", bbox_txt, "&q=[", amenity, - "]&format=geojson&limit=", limit - ) - - - if (full_results) url <- paste0(url, "&addressdetails=1") - if (!isTRUE(points_only)) url <- paste0(url, "&polygon_geojson=1") - if (!"bounded" %in% names(custom_query)) url <- paste0(url, "&bounded=1") - - # Add options - url <- add_custom_query(custom_query, url) - - # Download to temp file - json <- tempfile(fileext = ".geojson") - res <- api_call(url, json, quiet = isFALSE(verbose)) - - # Step 2: Read and parse results ---- - - # Keep a tbl with the query - tbl_query <- dplyr::tibble(query = amenity) - - # If no response... - if (isFALSE(res)) { - message(url, " not reachable.") - out <- empty_sf(tbl_query) - return(invisible(out)) - } - - # Read - sfobj <- sf::read_sf(json, stringsAsFactors = FALSE) - - # Empty query - if (length(names(sfobj)) == 1) { - message("No results for query ", amenity) - out <- empty_sf(tbl_query) - return(invisible(out)) } - - - # Prepare output - - # Unnest address - sfobj <- unnest_sf(sfobj) - - - - # Prepare output - sf_clean <- sfobj - sf_clean$query <- amenity - - # Keep names - result_out <- keep_names(sf_clean, return_addresses, full_results, - colstokeep = "query" - ) - - # Attach as tibble - result_out <- sf_to_tbl(result_out) - - result_out } diff --git a/R/geo_lite.R b/R/geo_lite.R index 557520ff..9f1c2329 100644 --- a/R/geo_lite.R +++ b/R/geo_lite.R @@ -18,6 +18,8 @@ #' returned. See also `return_addresses`. #' @param return_addresses return input addresses with results if `TRUE`. #' @param verbose if `TRUE` then detailed logs are output to the console. +#' @param progressbar Logical. If `TRUE` displays a progress bar to indicate +#' the progress of the function. #' @param custom_query A named list with API-specific parameters to be used #' (i.e. `list(countrycodes = "US")`). See **Details**. #' @@ -26,7 +28,7 @@ #' See for additional #' parameters to be passed to `custom_query`. #' -#' @return A `tibble` with the results. +#' @return A \CRANpkg{tibble} with the results. #' #' @examplesIf nominatim_check_access() #' \donttest{ @@ -52,6 +54,7 @@ geo_lite <- function(address, full_results = FALSE, return_addresses = TRUE, verbose = FALSE, + progressbar = TRUE, custom_query = list()) { if (limit > 50) { message(paste( @@ -66,9 +69,23 @@ geo_lite <- function(address, init_key <- dplyr::tibble(query = address) key <- unique(address) - all_res <- lapply(key, function(x) { + # Set progress bar + ntot <- length(key) + # Set progress bar if n > 1 + progressbar <- all(progressbar, ntot > 1) + if (progressbar) { + pb <- txtProgressBar(min = 0, max = ntot, width = 50, style = 3) + } + seql <- seq(1, ntot, 1) + + all_res <- lapply(seql, function(x) { + ad <- key[x] + if (progressbar) { + setTxtProgressBar(pb, x) + cat(paste0(" (", x, "/", ntot, ") ")) + } geo_lite_single( - address = x, + address = ad, lat, long, limit, @@ -78,6 +95,7 @@ geo_lite <- function(address, custom_query ) }) + if (progressbar) close(pb) all_res <- dplyr::bind_rows(all_res) all_res <- dplyr::left_join(init_key, all_res, by = "query") diff --git a/R/geo_lite_sf.R b/R/geo_lite_sf.R index 1cc6ae44..f51911a8 100644 --- a/R/geo_lite_sf.R +++ b/R/geo_lite_sf.R @@ -38,7 +38,7 @@ #' The function is vectorized, allowing for multiple addresses to be geocoded; #' in case of `points_only = FALSE` multiple geometry types may be returned. #' -#' @return A `sf` object with the results. +#' @return A \CRANpkg{sf} object with the results. #' #' @examplesIf nominatim_check_access() #' \donttest{ @@ -82,6 +82,7 @@ geo_lite_sf <- function(address, return_addresses = TRUE, full_results = FALSE, verbose = FALSE, + progressbar = TRUE, custom_query = list(), points_only = TRUE) { if (limit > 50) { @@ -97,10 +98,25 @@ geo_lite_sf <- function(address, init_key <- dplyr::tibble(query = address) key <- unique(address) + # Set progress bar + ntot <- length(key) + # Set progress bar if n > 1 + progressbar <- all(progressbar, ntot > 1) + if (progressbar) { + pb <- txtProgressBar(min = 0, max = ntot, width = 50, style = 3) + } + + seql <- seq(1, ntot, 1) + # Loop - all_res <- lapply(key, function(x) { + all_res <- lapply(seql, function(x) { + ad <- key[x] + if (progressbar) { + setTxtProgressBar(pb, x) + cat(paste0(" (", x, "/", ntot, ") ")) + } geo_lite_sf_single( - address = x, + address = ad, limit, return_addresses, full_results, @@ -110,6 +126,8 @@ geo_lite_sf <- function(address, ) }) + if (progressbar) close(pb) + all_res <- dplyr::bind_rows(all_res) all_res <- sf_to_tbl(all_res) diff --git a/R/nominatim_check_access.R b/R/nominatim_check_access.R index 16395a6b..74d7ab51 100644 --- a/R/nominatim_check_access.R +++ b/R/nominatim_check_access.R @@ -1,111 +1,111 @@ -#' Check access to Nominatim API -#' -#' @family api_management -#' -#' @description -#' Check if R has access to resources at -#' . -#' -#' @return a logical. -#' -#' @seealso -#' -#' -#' @examples -#' \donttest{ -#' nominatim_check_access() -#' } -#' @keywords internal -#' @export -nominatim_check_access <- function() { - url <- "https://nominatim.openstreetmap.org/status.php?format=json" - destfile <- tempfile(fileext = ".json") - - api_res <- api_call(url, destfile, TRUE) - - # nocov start - if (isFALSE(api_res)) { - return(FALSE) - } - # nocov end - result <- dplyr::as_tibble(jsonlite::fromJSON(destfile, flatten = TRUE)) - - # nocov start - if (result$status == 0 || result$message == "OK") { - return(TRUE) - } else { - return(FALSE) - } - # nocov end -} - -skip_if_api_server <- function() { - # nocov start - if (nominatim_check_access()) { - return(invisible(TRUE)) - } - - if (requireNamespace("testthat", quietly = TRUE)) { - testthat::skip("Nominatim API not reachable") - } - return(invisible()) - # nocov end -} - - -#' Helper function for centralize API queries -#' -#' @description -#' A wrapper of [utils::download.file()]. On warning on error it will -#' retry the call. Requests are adjusted to the rate of 1 query per second. -#' -#' See [Nominatim Usage -#' Policy](https://operations.osmfoundation.org/policies/nominatim/). -#' -#' @family api_management -#' -#' @inheritParams utils::download.file -#' @return A logical `TRUE/FALSE` -#' -#' @keywords internal -#' -api_call <- function(url, destfile, quiet) { - # nocov start - dwn_res <- - tryCatch( - download.file(url, destfile = destfile, quiet = quiet, mode = "wb"), - warning = function(e) { - return(FALSE) - }, - error = function(e) { - return(FALSE) - } - ) - # nocov end - # Always sleep to make 1 call per sec - Sys.sleep(1) - - # nocov start - if (isFALSE(dwn_res)) { - if (isFALSE(quiet)) message("Retrying query") - Sys.sleep(1) - - dwn_res <- - tryCatch( - download.file(url, destfile = destfile, quiet = quiet, mode = "wb"), - warning = function(e) { - return(FALSE) - }, - error = function(e) { - return(FALSE) - } - ) - } - - if (isFALSE(dwn_res)) { - return(FALSE) - } else { - return(TRUE) - } - # nocov end -} +#' Check access to Nominatim API +#' +#' @family api_management +#' +#' @description +#' Check if **R** has access to resources at +#' . +#' +#' @return a logical. +#' +#' @seealso +#' +#' +#' @examples +#' \donttest{ +#' nominatim_check_access() +#' } +#' @keywords internal +#' @export +nominatim_check_access <- function() { + url <- "https://nominatim.openstreetmap.org/status.php?format=json" + destfile <- tempfile(fileext = ".json") + + api_res <- api_call(url, destfile, TRUE) + + # nocov start + if (isFALSE(api_res)) { + return(FALSE) + } + # nocov end + result <- dplyr::as_tibble(jsonlite::fromJSON(destfile, flatten = TRUE)) + + # nocov start + if (result$status == 0 || result$message == "OK") { + return(TRUE) + } else { + return(FALSE) + } + # nocov end +} + +skip_if_api_server <- function() { + # nocov start + if (nominatim_check_access()) { + return(invisible(TRUE)) + } + + if (requireNamespace("testthat", quietly = TRUE)) { + testthat::skip("Nominatim API not reachable") + } + return(invisible()) + # nocov end +} + + +#' Helper function for centralize API queries +#' +#' @description +#' A wrapper of [utils::download.file()]. On warning on error it will +#' retry the call. Requests are adjusted to the rate of 1 query per second. +#' +#' See [Nominatim Usage +#' Policy](https://operations.osmfoundation.org/policies/nominatim/). +#' +#' @family api_management +#' +#' @inheritParams utils::download.file +#' @return A logical `TRUE/FALSE` +#' +#' @keywords internal +#' +api_call <- function(url, destfile, quiet) { + # nocov start + dwn_res <- + tryCatch( + download.file(url, destfile = destfile, quiet = quiet, mode = "wb"), + warning = function(e) { + return(FALSE) + }, + error = function(e) { + return(FALSE) + } + ) + # nocov end + # Always sleep to make 1 call per sec + Sys.sleep(1) + + # nocov start + if (isFALSE(dwn_res)) { + if (isFALSE(quiet)) message("Retrying query") + Sys.sleep(1) + + dwn_res <- + tryCatch( + download.file(url, destfile = destfile, quiet = quiet, mode = "wb"), + warning = function(e) { + return(FALSE) + }, + error = function(e) { + return(FALSE) + } + ) + } + + if (isFALSE(dwn_res)) { + return(FALSE) + } else { + return(TRUE) + } + # nocov end +} diff --git a/R/nominatimlite-package.R b/R/nominatimlite-package.R index 91289122..aaba2ada 100644 --- a/R/nominatimlite-package.R +++ b/R/nominatimlite-package.R @@ -8,5 +8,5 @@ NULL # import stuffs -#' @importFrom utils download.file +#' @importFrom utils download.file txtProgressBar setTxtProgressBar NULL diff --git a/R/reverse_geo_lite.R b/R/reverse_geo_lite.R index 444e360c..c1384fdb 100644 --- a/R/reverse_geo_lite.R +++ b/R/reverse_geo_lite.R @@ -47,7 +47,7 @@ #' #' ``` #' -#' @return A `tibble` with the results. +#' @return A \CRANpkg{tibble} with the results. #' #' @examplesIf nominatim_check_access() #' \donttest{ @@ -78,6 +78,7 @@ reverse_geo_lite <- function(lat, full_results = FALSE, return_coords = TRUE, verbose = FALSE, + progressbar = TRUE, custom_query = list()) { # Check inputs if (!is.numeric(lat) || !is.numeric(long)) { @@ -111,8 +112,22 @@ reverse_geo_lite <- function(lat, ) key <- dplyr::distinct(init_key) + # Set progress bar + ntot <- nrow(key) + # Set progress bar if n > 1 + progressbar <- all(progressbar, ntot > 1) + if (progressbar) { + pb <- txtProgressBar(min = 0, max = ntot, width = 50, style = 3) + } + + seql <- seq(1, ntot, 1) - all_res <- lapply(seq_len(nrow(key)), function(x) { + + all_res <- lapply(seql, function(x) { + if (progressbar) { + setTxtProgressBar(pb, x) + cat(paste0(" (", x, "/", ntot, ") ")) + } rw <- key[x, ] res_single <- reverse_geo_lite_single( as.double(rw$lat_cap_int), @@ -128,7 +143,7 @@ reverse_geo_lite <- function(lat, res_single }) - + if (progressbar) close(pb) all_res <- dplyr::bind_rows(all_res) all_res <- dplyr::left_join(init_key[, c(1, 2)], all_res, diff --git a/R/reverse_geo_lite_sf.R b/R/reverse_geo_lite_sf.R index f4365a99..c15e4a27 100644 --- a/R/reverse_geo_lite_sf.R +++ b/R/reverse_geo_lite_sf.R @@ -16,7 +16,7 @@ #' @inheritSection reverse_geo_lite About Zooming #' @inheritSection geo_lite_sf About Geometry Types #' -#' @return A `sf` object with the results. +#' @return A \CRANpkg{sf} object with the results. #' #' @examplesIf nominatim_check_access() #' \donttest{ @@ -67,6 +67,7 @@ reverse_geo_lite_sf <- function(lat, full_results = FALSE, return_coords = TRUE, verbose = FALSE, + progressbar = TRUE, custom_query = list(), points_only = TRUE) { # Check inputs @@ -101,7 +102,22 @@ reverse_geo_lite_sf <- function(lat, ) key <- dplyr::distinct(init_key) - all_res <- lapply(seq_len(nrow(key)), function(x) { + # Set progress bar + ntot <- nrow(key) + # Set progress bar if n > 1 + progressbar <- all(progressbar, ntot > 1) + if (progressbar) { + pb <- txtProgressBar(min = 0, max = ntot, width = 50, style = 3) + } + + seql <- seq(1, ntot, 1) + + + all_res <- lapply(seql, function(x) { + if (progressbar) { + setTxtProgressBar(pb, x) + cat(paste0(" (", x, "/", ntot, ") ")) + } rw <- key[x, ] res_single <- reverse_geo_lite_sf_single( as.double(rw$lat_cap_int), @@ -118,6 +134,7 @@ reverse_geo_lite_sf <- function(lat, res_single }) + if (progressbar) close(pb) all_res <- dplyr::bind_rows(all_res) diff --git a/README.md b/README.md index 15981cc9..19d35c2a 100644 --- a/README.md +++ b/README.md @@ -156,6 +156,7 @@ some_addresses <- tribble( # geocode the addresses lat_longs <- geo_lite(some_addresses$addr, lat = "latitude", long = "longitude") +#> | | | 0% | |================= | 33% (1/3) | |================================= | 67% (2/3) | |==================================================| 100% (3/3) ``` Only latitude and longitude are returned from the geocoder service in @@ -180,6 +181,7 @@ reverse <- reverse_geo_lite( lat = lat_longs$latitude, long = lat_longs$longitude, address = "address_found" ) +#> | | | 0% | |================= | 33% (1/3) | |================================= | 67% (2/3) | |==================================================| 100% (3/3) ``` | address_found | lat | lon | @@ -195,7 +197,7 @@ the parameters available. ## Citation

-Hernangómez D (2023). nominatimlite: Interface with Nominatim API +Hernangómez D (2024). nominatimlite: Interface with Nominatim API Service. doi:10.5281/zenodo.5113195, https://dieghernan.github.io/nominatimlite/. @@ -206,11 +208,11 @@ A BibTeX entry for LaTeX users is @Manual{R-nominatimlite, title = {{nominatimlite}: Interface with {Nominatim} {API} Service}, author = {Diego Hernangómez}, - year = {2023}, - version = {0.2.1}, + year = {2024}, + version = {0.2.1.9000}, doi = {10.5281/zenodo.5113195}, url = {https://dieghernan.github.io/nominatimlite/}, - abstract = {Lite interface for getting data from OSM service Nominatim . Extract coordinates from addresses, find places near a set of coordinates, search for amenities and return spatial objects on sf format.}, + abstract = {Lite interface for getting data from OSM service Nominatim . Extract coordinates from addresses, find places near a set of coordinates and return spatial objects on sf format.}, } ## References diff --git a/codemeta.json b/codemeta.json index b9230e7e..cbcab856 100644 --- a/codemeta.json +++ b/codemeta.json @@ -2,13 +2,13 @@ "@context": "https://doi.org/10.5063/schema/codemeta-2.0", "@type": "SoftwareSourceCode", "identifier": "nominatimlite", - "description": "Lite interface for getting data from 'OSM' service 'Nominatim' . Extract coordinates from addresses, find places near a set of coordinates, search for amenities and return spatial objects on 'sf' format.", + "description": "Lite interface for getting data from 'OSM' service 'Nominatim' . Extract coordinates from addresses, find places near a set of coordinates and return spatial objects on 'sf' format.", "name": "nominatimlite: Interface with 'Nominatim' API Service", "relatedLink": ["https://dieghernan.github.io/nominatimlite/", "https://CRAN.R-project.org/package=nominatimlite"], "codeRepository": "https://github.com/dieghernan/nominatimlite", "issueTracker": "https://github.com/dieghernan/nominatimlite/issues", "license": "https://spdx.org/licenses/MIT", - "version": "0.2.1", + "version": "0.2.1.9000", "programmingLanguage": { "@type": "ComputerLanguage", "name": "R", @@ -154,6 +154,18 @@ "sameAs": "https://CRAN.R-project.org/package=jsonlite" }, "4": { + "@type": "SoftwareApplication", + "identifier": "lifecycle", + "name": "lifecycle", + "provider": { + "@id": "https://cran.r-project.org", + "@type": "Organization", + "name": "Comprehensive R Archive Network (CRAN)", + "url": "https://cran.r-project.org" + }, + "sameAs": "https://CRAN.R-project.org/package=lifecycle" + }, + "5": { "@type": "SoftwareApplication", "identifier": "sf", "name": "sf", @@ -166,7 +178,7 @@ }, "sameAs": "https://CRAN.R-project.org/package=sf" }, - "5": { + "6": { "@type": "SoftwareApplication", "identifier": "utils", "name": "utils" @@ -175,11 +187,11 @@ }, "applicationCategory": "cartography", "keywords": ["r", "geocoding", "openstreetmap", "address", "nominatim", "reverse-geocoding", "rstats", "shapefile", "r-package", "spatial", "cran", "api-wrapper", "api", "gis"], - "fileSize": "258.67KB", + "fileSize": "251.765KB", "citation": [ { "@type": "SoftwareSourceCode", - "datePublished": "2023", + "datePublished": "2024", "author": [ { "@type": "Person", diff --git a/data/osm_amenities.rda b/data/osm_amenities.rda deleted file mode 100644 index 34df3242..00000000 Binary files a/data/osm_amenities.rda and /dev/null differ diff --git a/inst/WORDLIST b/inst/WORDLIST index 71a12e32..9ef50f02 100644 --- a/inst/WORDLIST +++ b/inst/WORDLIST @@ -1,3 +1,4 @@ +Belanger CMD Cambon CodeFactor @@ -6,20 +7,21 @@ Geocode Geocodes Geocoding Mapbox +Maëlle Nominatim ORCID OSM OpenStreetMap Padgham +Possenriede +Rudis TomTom Transamerica +al api -atm -bbq -biergarten -centre codecov de +et geocode geocoded geocoder @@ -28,13 +30,9 @@ geolocated ie json lon -osm osmdata rlang -stripclub -swingerclub -theatre tibble -townhall +tidygeocoder unnesting vectorized diff --git a/inst/schemaorg.json b/inst/schemaorg.json index 2716cfcb..c637c64e 100644 --- a/inst/schemaorg.json +++ b/inst/schemaorg.json @@ -20,7 +20,7 @@ "familyName": "Hernangómez", "givenName": "Diego" }, - "description": "Lite interface for getting data from 'OSM' service 'Nominatim' . Extract coordinates from addresses, find places near a set of coordinates, search for amenities and return spatial objects on 'sf' format.", + "description": "Lite interface for getting data from 'OSM' service 'Nominatim' . Extract coordinates from addresses, find places near a set of coordinates and return spatial objects on 'sf' format.", "license": "https://spdx.org/licenses/MIT", "name": "nominatimlite: Interface with 'Nominatim' API Service", "programmingLanguage": { @@ -35,7 +35,7 @@ "url": "https://cran.r-project.org" }, "runtimePlatform": "R version 4.3.2 (2023-10-31 ucrt)", - "version": "0.2.1" + "version": "0.2.1.9000" }, { "id": "https://doi.org/10.5281/zenodo.5113195", diff --git a/man/bbox_to_poly.Rd b/man/bbox_to_poly.Rd index a44e85e0..5644db51 100644 --- a/man/bbox_to_poly.Rd +++ b/man/bbox_to_poly.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/bbox_to_poly.R \name{bbox_to_poly} \alias{bbox_to_poly} -\title{Create a bounding box \code{sf} object} +\title{Create a bounding box \CRANpkg{sf} object} \usage{ bbox_to_poly(bbox = NA, xmin = NA, ymin = NA, xmax = NA, ymax = NA, crs = 4326) } @@ -64,14 +64,7 @@ if (any(!sf::st_is_empty(sfobj))) { Get spatial (\code{sf}) objects: \code{\link{geo_address_lookup_sf}()}, -\code{\link{geo_amenity_sf}()}, \code{\link{geo_lite_sf}()}, \code{\link{reverse_geo_lite_sf}()} - -Search amenities: -\code{\link{geo_amenity_sf}()}, -\code{\link{geo_amenity}()}, -\code{\link{osm_amenities}} } -\concept{amenity} \concept{spatial} diff --git a/man/figures/README-pizzahut-1.png b/man/figures/README-pizzahut-1.png index 75c8303a..3fbf34d4 100644 Binary files a/man/figures/README-pizzahut-1.png and b/man/figures/README-pizzahut-1.png differ diff --git a/man/figures/README-statue_liberty-1.png b/man/figures/README-statue_liberty-1.png index 57b48770..a9755839 100644 Binary files a/man/figures/README-statue_liberty-1.png and b/man/figures/README-statue_liberty-1.png differ diff --git a/man/figures/lifecycle-archived.svg b/man/figures/lifecycle-archived.svg new file mode 100644 index 00000000..745ab0c7 --- /dev/null +++ b/man/figures/lifecycle-archived.svg @@ -0,0 +1,21 @@ + + lifecycle: archived + + + + + + + + + + + + + + + lifecycle + + archived + + diff --git a/man/figures/lifecycle-defunct.svg b/man/figures/lifecycle-defunct.svg new file mode 100644 index 00000000..d5c9559e --- /dev/null +++ b/man/figures/lifecycle-defunct.svg @@ -0,0 +1,21 @@ + + lifecycle: defunct + + + + + + + + + + + + + + + lifecycle + + defunct + + diff --git a/man/figures/lifecycle-deprecated.svg b/man/figures/lifecycle-deprecated.svg new file mode 100644 index 00000000..b61c57c3 --- /dev/null +++ b/man/figures/lifecycle-deprecated.svg @@ -0,0 +1,21 @@ + + lifecycle: deprecated + + + + + + + + + + + + + + + lifecycle + + deprecated + + diff --git a/man/figures/lifecycle-experimental.svg b/man/figures/lifecycle-experimental.svg new file mode 100644 index 00000000..5d88fc2c --- /dev/null +++ b/man/figures/lifecycle-experimental.svg @@ -0,0 +1,21 @@ + + lifecycle: experimental + + + + + + + + + + + + + + + lifecycle + + experimental + + diff --git a/man/figures/lifecycle-maturing.svg b/man/figures/lifecycle-maturing.svg new file mode 100644 index 00000000..897370ec --- /dev/null +++ b/man/figures/lifecycle-maturing.svg @@ -0,0 +1,21 @@ + + lifecycle: maturing + + + + + + + + + + + + + + + lifecycle + + maturing + + diff --git a/man/figures/lifecycle-questioning.svg b/man/figures/lifecycle-questioning.svg new file mode 100644 index 00000000..7c1721d0 --- /dev/null +++ b/man/figures/lifecycle-questioning.svg @@ -0,0 +1,21 @@ + + lifecycle: questioning + + + + + + + + + + + + + + + lifecycle + + questioning + + diff --git a/man/figures/lifecycle-soft-deprecated.svg b/man/figures/lifecycle-soft-deprecated.svg new file mode 100644 index 00000000..9c166ff3 --- /dev/null +++ b/man/figures/lifecycle-soft-deprecated.svg @@ -0,0 +1,21 @@ + + lifecycle: soft-deprecated + + + + + + + + + + + + + + + lifecycle + + soft-deprecated + + diff --git a/man/figures/lifecycle-stable.svg b/man/figures/lifecycle-stable.svg new file mode 100644 index 00000000..9bf21e76 --- /dev/null +++ b/man/figures/lifecycle-stable.svg @@ -0,0 +1,29 @@ + + lifecycle: stable + + + + + + + + + + + + + + + + lifecycle + + + + stable + + + diff --git a/man/figures/lifecycle-superseded.svg b/man/figures/lifecycle-superseded.svg new file mode 100644 index 00000000..db8d757f --- /dev/null +++ b/man/figures/lifecycle-superseded.svg @@ -0,0 +1,21 @@ + + lifecycle: superseded + + + + + + + + + + + + + + + lifecycle + + superseded + + diff --git a/man/geo_address_lookup.Rd b/man/geo_address_lookup.Rd index 4025644e..f5c95974 100644 --- a/man/geo_address_lookup.Rd +++ b/man/geo_address_lookup.Rd @@ -39,7 +39,7 @@ returned. See also \code{return_addresses}.} (i.e. \code{list(countrycodes = "US")}). See \strong{Details}.} } \value{ -A \code{tibble} with the results found by the query. +A \CRANpkg{tibble} with the results found by the query. } \description{ The lookup API allows to query the address and other details of one or @@ -71,8 +71,6 @@ Address Lookup API: Geocoding strings: \code{\link{geo_address_lookup_sf}()}, -\code{\link{geo_amenity_sf}()}, -\code{\link{geo_amenity}()}, \code{\link{geo_lite_sf}()}, \code{\link{geo_lite}()} } diff --git a/man/geo_address_lookup_sf.Rd b/man/geo_address_lookup_sf.Rd index fe8c9dc3..51b14ce5 100644 --- a/man/geo_address_lookup_sf.Rd +++ b/man/geo_address_lookup_sf.Rd @@ -38,7 +38,7 @@ points (\code{TRUE}, which is the default) or potentially other shapes as provided by the Nominatim API (\code{FALSE}). See \strong{About Geometry Types}.} } \value{ -A \code{sf} object with the results. +A \CRANpkg{sf} object with the results. } \description{ The lookup API allows to query the address and other details of one or @@ -111,14 +111,11 @@ Address Lookup API: Geocoding strings: \code{\link{geo_address_lookup}()}, -\code{\link{geo_amenity_sf}()}, -\code{\link{geo_amenity}()}, \code{\link{geo_lite_sf}()}, \code{\link{geo_lite}()} Get spatial (\code{sf}) objects: \code{\link{bbox_to_poly}()}, -\code{\link{geo_amenity_sf}()}, \code{\link{geo_lite_sf}()}, \code{\link{reverse_geo_lite_sf}()} } diff --git a/man/geo_amenity.Rd b/man/geo_amenity.Rd index 2487e822..c0aeef23 100644 --- a/man/geo_amenity.Rd +++ b/man/geo_amenity.Rd @@ -23,8 +23,7 @@ geo_amenity( restrict the search area. See \strong{Details}.} \item{amenity}{A character of a vector of character with the amenities to be -geolocated (i.e. \code{c("pub", "restaurant")}). See \strong{Details} and -\link{osm_amenities}.} +geolocated (i.e. \code{c("pub", "restaurant")}).} \item{lat}{latitude column name in the output data (default \code{"lat"}).} @@ -41,23 +40,20 @@ returned. See also \code{return_addresses}.} \item{verbose}{if \code{TRUE} then detailed logs are output to the console.} -\item{custom_query}{API-specific parameters to be used. -See \code{\link[=geo_lite]{geo_lite()}}.} +\item{custom_query}{API-specific parameters to be used.} \item{strict}{Logical \code{TRUE/FALSE}. Force the results to be included inside the \code{bbox}. Note that Nominatim default behavior may return results located outside the provided bounding box.} } \value{ -A \code{tibble} with the results. +A \CRANpkg{tibble} with the results. } \description{ -This function search amenities as defined by OpenStreetMap on a restricted -area defined by a bounding box in the form of -\verb{(, , , )}. This -function returns the \CRANpkg{tibble} associated with the query, see -\code{\link[=geo_amenity_sf]{geo_amenity_sf()}} for retrieving the data as a spatial object -(\CRANpkg{sf} format). +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} + +This operation is not supported any more. Use +\href{https://github.com/ropensci/osmdata}{\strong{osmdata}} instead. } \details{ Bounding boxes can be located using different online tools, as @@ -66,39 +62,7 @@ Bounding boxes can be located using different online tools, as For a full list of valid amenities see \url{https://wiki.openstreetmap.org/wiki/Key:amenity}. } -\examples{ -\dontshow{if (nominatim_check_access()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -\donttest{ -# Times Square, NY, USA -bbox <- c(-73.9894467311, 40.75573629, -73.9830630737, 40.75789245) - -geo_amenity(bbox = bbox, amenity = "restaurant") - -# Several amenities -geo_amenity(bbox = bbox, amenity = c("restaurant", "pub")) - -# Increase limit and use with strict -geo_amenity( - bbox = bbox, amenity = c("restaurant", "pub"), limit = 10, - strict = TRUE -) -} -\dontshow{\}) # examplesIf} -} \seealso{ \code{\link[=geo_amenity_sf]{geo_amenity_sf()}} - -Search amenities: -\code{\link{bbox_to_poly}()}, -\code{\link{geo_amenity_sf}()}, -\code{\link{osm_amenities}} - -Geocoding strings: -\code{\link{geo_address_lookup_sf}()}, -\code{\link{geo_address_lookup}()}, -\code{\link{geo_amenity_sf}()}, -\code{\link{geo_lite_sf}()}, -\code{\link{geo_lite}()} } -\concept{amenity} -\concept{geocoding} +\keyword{internal} diff --git a/man/geo_amenity_sf.Rd b/man/geo_amenity_sf.Rd index 45163deb..f492c596 100644 --- a/man/geo_amenity_sf.Rd +++ b/man/geo_amenity_sf.Rd @@ -22,8 +22,7 @@ geo_amenity_sf( restrict the search area. See \strong{Details}.} \item{amenity}{A character of a vector of character with the amenities to be -geolocated (i.e. \code{c("pub", "restaurant")}). See \strong{Details} and -\link{osm_amenities}.} +geolocated (i.e. \code{c("pub", "restaurant")}).} \item{limit}{maximum number of results to return per input address. Note that each query returns a maximum of 50 results.} @@ -48,14 +47,13 @@ the \code{bbox}. Note that Nominatim default behavior may return results located outside the provided bounding box.} } \value{ -A \code{sf} object with the results. +A \CRANpkg{sf} object with the results. } \description{ -This function search amenities as defined by OpenStreetMap on a restricted -area defined by a bounding box in the form of -\verb{(, , , )}. This -function returns the \CRANpkg{sf} spatial object associated with the query, -see \code{\link[=geo_amenity]{geo_amenity()}} for retrieving the data in \CRANpkg{tibble} format. +\ifelse{html}{\href{https://lifecycle.r-lib.org/articles/stages.html#deprecated}{\figure{lifecycle-deprecated.svg}{options: alt='[Deprecated]'}}}{\strong{[Deprecated]}} + +This operation is not supported any more. Use +\href{https://github.com/ropensci/osmdata}{\strong{osmdata}} instead. } \details{ Bounding boxes can be located using different online tools, as @@ -84,56 +82,4 @@ The function is vectorized, allowing for multiple addresses to be geocoded; in case of \code{points_only = FALSE} multiple geometry types may be returned. } -\examples{ -\dontshow{if (nominatim_check_access()) (if (getRversion() >= "3.4") withAutoprint else force)(\{ # examplesIf} -\donttest{ -# Madrid, Spain - -library(ggplot2) - -bbox <- c(-3.888954, 40.311977, -3.517916, 40.643729) - -# Restaurants and pubs - -rest_pub <- geo_amenity_sf(bbox, c("restaurant", "pub"), limit = 50) - -if (any(!sf::st_is_empty(rest_pub))) { - ggplot(rest_pub) + - geom_sf() -} - -# Hospital as polygon - -hosp <- geo_amenity_sf(bbox, "hospital", points_only = FALSE) - -if (any(!sf::st_is_empty(hosp))) { - ggplot(hosp) + - geom_sf() -} -} -\dontshow{\}) # examplesIf} -} -\seealso{ -\code{\link[=geo_amenity]{geo_amenity()}} - -Search amenities: -\code{\link{bbox_to_poly}()}, -\code{\link{geo_amenity}()}, -\code{\link{osm_amenities}} - -Geocoding strings: -\code{\link{geo_address_lookup_sf}()}, -\code{\link{geo_address_lookup}()}, -\code{\link{geo_amenity}()}, -\code{\link{geo_lite_sf}()}, -\code{\link{geo_lite}()} - -Get spatial (\code{sf}) objects: -\code{\link{bbox_to_poly}()}, -\code{\link{geo_address_lookup_sf}()}, -\code{\link{geo_lite_sf}()}, -\code{\link{reverse_geo_lite_sf}()} -} -\concept{amenity} -\concept{geocoding} -\concept{spatial} +\keyword{internal} diff --git a/man/geo_lite.Rd b/man/geo_lite.Rd index 8b1ae60a..1f76e33e 100644 --- a/man/geo_lite.Rd +++ b/man/geo_lite.Rd @@ -12,6 +12,7 @@ geo_lite( full_results = FALSE, return_addresses = TRUE, verbose = FALSE, + progressbar = TRUE, custom_query = list() ) } @@ -35,11 +36,14 @@ returned. See also \code{return_addresses}.} \item{verbose}{if \code{TRUE} then detailed logs are output to the console.} +\item{progressbar}{Logical. If \code{TRUE} displays a progress bar to indicate +the progress of the function.} + \item{custom_query}{A named list with API-specific parameters to be used (i.e. \code{list(countrycodes = "US")}). See \strong{Details}.} } \value{ -A \code{tibble} with the results. +A \CRANpkg{tibble} with the results. } \description{ Geocodes addresses given as character values. This @@ -73,8 +77,6 @@ geo_lite(c("Madrid", "Barcelona"), Geocoding strings: \code{\link{geo_address_lookup_sf}()}, \code{\link{geo_address_lookup}()}, -\code{\link{geo_amenity_sf}()}, -\code{\link{geo_amenity}()}, \code{\link{geo_lite_sf}()} } \concept{geocoding} diff --git a/man/geo_lite_sf.Rd b/man/geo_lite_sf.Rd index 578ee6d0..72058fb9 100644 --- a/man/geo_lite_sf.Rd +++ b/man/geo_lite_sf.Rd @@ -10,6 +10,7 @@ geo_lite_sf( return_addresses = TRUE, full_results = FALSE, verbose = FALSE, + progressbar = TRUE, custom_query = list(), points_only = TRUE ) @@ -30,6 +31,9 @@ If \code{FALSE} (default) only address columns are returned. See also \item{verbose}{if \code{TRUE} then detailed logs are output to the console.} +\item{progressbar}{Logical. If \code{TRUE} displays a progress bar to indicate +the progress of the function.} + \item{custom_query}{A named list with API-specific parameters to be used (i.e. \code{list(countrycodes = "US")}). See \strong{Details}.} @@ -38,7 +42,7 @@ points (\code{TRUE}, which is the default) or potentially other shapes as provided by the Nominatim API (\code{FALSE}). See \strong{About Geometry Types}.} } \value{ -A \code{sf} object with the results. +A \CRANpkg{sf} object with the results. } \description{ This function allows you to geocode addresses and return the corresponding @@ -109,14 +113,11 @@ if (any(!sf::st_is_empty(Madrid))) { Geocoding strings: \code{\link{geo_address_lookup_sf}()}, \code{\link{geo_address_lookup}()}, -\code{\link{geo_amenity_sf}()}, -\code{\link{geo_amenity}()}, \code{\link{geo_lite}()} Get spatial (\code{sf}) objects: \code{\link{bbox_to_poly}()}, \code{\link{geo_address_lookup_sf}()}, -\code{\link{geo_amenity_sf}()}, \code{\link{reverse_geo_lite_sf}()} } \concept{geocoding} diff --git a/man/nominatim_check_access.Rd b/man/nominatim_check_access.Rd index 9c62510a..65398060 100644 --- a/man/nominatim_check_access.Rd +++ b/man/nominatim_check_access.Rd @@ -10,7 +10,7 @@ nominatim_check_access() a logical. } \description{ -Check if R has access to resources at +Check if \strong{R} has access to resources at \url{https://nominatim.openstreetmap.org}. } \examples{ diff --git a/man/nominatimlite-package.Rd b/man/nominatimlite-package.Rd index 1ba31c09..3a627e0d 100644 --- a/man/nominatimlite-package.Rd +++ b/man/nominatimlite-package.Rd @@ -8,7 +8,7 @@ \description{ \if{html}{\figure{logo.png}{options: style='float: right' alt='logo' width='120'}} -Lite interface for getting data from 'OSM' service 'Nominatim' \url{https://nominatim.org/release-docs/latest/}. Extract coordinates from addresses, find places near a set of coordinates, search for amenities and return spatial objects on 'sf' format. +Lite interface for getting data from 'OSM' service 'Nominatim' \url{https://nominatim.org/release-docs/latest/}. Extract coordinates from addresses, find places near a set of coordinates and return spatial objects on 'sf' format. } \seealso{ Useful links: diff --git a/man/osm_amenities.Rd b/man/osm_amenities.Rd deleted file mode 100644 index bcf78a4c..00000000 --- a/man/osm_amenities.Rd +++ /dev/null @@ -1,144 +0,0 @@ -% Generated by roxygen2: do not edit by hand -% Please edit documentation in R/data.R -\docType{data} -\encoding{UTF-8} -\name{osm_amenities} -\alias{osm_amenities} -\title{OpenStreetMap amenity database} -\format{ -A \code{tibble} with with -100 rows and -fields: -\describe{ -\item{category}{The category of the amenity} -\item{amenity}{The name of the amenity} -} -} -\source{ -\url{https://wiki.openstreetmap.org/wiki/Key:amenity} -} -\description{ -Database with the list of amenities available on OpenStreetMap. -} -\details{ -\tabular{ll}{ - \strong{category} \tab \strong{amenity} \cr - Sustenance \tab bar \cr - Sustenance \tab biergarten \cr - Sustenance \tab cafe \cr - Sustenance \tab fast_food \cr - Sustenance \tab food_court \cr - Sustenance \tab ice_cream \cr - Sustenance \tab pub \cr - Sustenance \tab restaurant \cr - Education \tab college \cr - Education \tab driving_school \cr - Education \tab kindergarten \cr - Education \tab language_school \cr - Education \tab library \cr - Education \tab toy_library \cr - Education \tab music_school \cr - Education \tab school \cr - Education \tab university \cr - Transportation \tab bicycle_parking \cr - Transportation \tab bicycle_repair_station \cr - Transportation \tab bicycle_rental \cr - Transportation \tab boat_rental \cr - Transportation \tab boat_sharing \cr - Transportation \tab bus_station \cr - Transportation \tab car_rental \cr - Transportation \tab car_sharing \cr - Transportation \tab car_wash \cr - Transportation \tab vehicle_inspection \cr - Transportation \tab charging_station \cr - Transportation \tab ferry_terminal \cr - Transportation \tab fuel \cr - Transportation \tab grit_bin \cr - Transportation \tab motorcycle_parking \cr - Transportation \tab parking \cr - Transportation \tab parking_entrance \cr - Transportation \tab parking_space \cr - Transportation \tab taxi \cr - Financial \tab atm \cr - Financial \tab bank \cr - Financial \tab bureau_de_change \cr - Healthcare \tab baby_hatch \cr - Healthcare \tab clinic \cr - Healthcare \tab dentist \cr - Healthcare \tab doctors \cr - Healthcare \tab hospital \cr - Healthcare \tab nursing_home \cr - Healthcare \tab pharmacy \cr - Healthcare \tab social_facility \cr - Healthcare \tab veterinary \cr - Entertainment-Arts-Culture \tab arts_centre \cr - Entertainment-Arts-Culture \tab brothel \cr - Entertainment-Arts-Culture \tab casino \cr - Entertainment-Arts-Culture \tab cinema \cr - Entertainment-Arts-Culture \tab community_centre \cr - Entertainment-Arts-Culture \tab conference_centre \cr - Entertainment-Arts-Culture \tab events_venue \cr - Entertainment-Arts-Culture \tab fountain \cr - Entertainment-Arts-Culture \tab gambling \cr - Entertainment-Arts-Culture \tab love_hotel \cr - Entertainment-Arts-Culture \tab nightclub \cr - Entertainment-Arts-Culture \tab planetarium \cr - Entertainment-Arts-Culture \tab public_bookcase \cr - Entertainment-Arts-Culture \tab social_centre \cr - Entertainment-Arts-Culture \tab stripclub \cr - Entertainment-Arts-Culture \tab studio \cr - Entertainment-Arts-Culture \tab swingerclub \cr - Entertainment-Arts-Culture \tab theatre \cr - Public Service \tab courthouse \cr - Public Service \tab embassy \cr - Public Service \tab fire_station \cr - Public Service \tab police \cr - Public Service \tab post_box \cr - Public Service \tab post_depot \cr - Public Service \tab post_office \cr - Public Service \tab prison \cr - Public Service \tab ranger_station \cr - Public Service \tab townhall \cr - Facilities \tab bbq \cr - Facilities \tab bench \cr - Facilities \tab dog_toilet \cr - Facilities \tab drinking_water \cr - Facilities \tab give_box \cr - Facilities \tab shelter \cr - Facilities \tab shower \cr - Facilities \tab telephone \cr - Facilities \tab toilets \cr - Facilities \tab water_point \cr - Facilities \tab watering_place \cr - Waste Management \tab sanitary_dump_station \cr - Waste Management \tab recycling \cr - Waste Management \tab waste_basket \cr - Waste Management \tab waste_disposal \cr - Waste Management \tab waste_transfer_station \cr - Others \tab animal_boarding \cr - Others \tab animal_breeding \cr - Others \tab animal_shelter \cr - Others \tab baking_oven \cr - Others \tab childcare \cr - Others \tab clock \cr - Others \tab crematorium \cr - Others \tab dive_centre \cr -} -} -\note{ -Data extracted on \strong{14 June 2021}. -} -\examples{ - -amenities <- nominatimlite::osm_amenities - -amenities -} -\seealso{ -Search amenities: -\code{\link{bbox_to_poly}()}, -\code{\link{geo_amenity_sf}()}, -\code{\link{geo_amenity}()} -} -\concept{amenity} -\concept{datasets} diff --git a/man/reverse_geo_lite.Rd b/man/reverse_geo_lite.Rd index f152d9ff..2545e841 100644 --- a/man/reverse_geo_lite.Rd +++ b/man/reverse_geo_lite.Rd @@ -11,6 +11,7 @@ reverse_geo_lite( full_results = FALSE, return_coords = TRUE, verbose = FALSE, + progressbar = TRUE, custom_query = list() ) } @@ -31,11 +32,14 @@ returned. See also \code{return_addresses}.} \item{verbose}{if \code{TRUE} then detailed logs are output to the console.} +\item{progressbar}{Logical. If \code{TRUE} displays a progress bar to indicate +the progress of the function.} + \item{custom_query}{API-specific parameters to be used, passed as a named list (ie. \code{list(zoom = 3)}). See \strong{Details}.} } \value{ -A \code{tibble} with the results. +A \CRANpkg{tibble} with the results. } \description{ Generates an address from a latitude and longitude. Latitudes must be diff --git a/man/reverse_geo_lite_sf.Rd b/man/reverse_geo_lite_sf.Rd index 18e3b0cf..c53d1a6d 100644 --- a/man/reverse_geo_lite_sf.Rd +++ b/man/reverse_geo_lite_sf.Rd @@ -11,6 +11,7 @@ reverse_geo_lite_sf( full_results = FALSE, return_coords = TRUE, verbose = FALSE, + progressbar = TRUE, custom_query = list(), points_only = TRUE ) @@ -32,6 +33,9 @@ returned. See also \code{return_addresses}.} \item{verbose}{if \code{TRUE} then detailed logs are output to the console.} +\item{progressbar}{Logical. If \code{TRUE} displays a progress bar to indicate +the progress of the function.} + \item{custom_query}{API-specific parameters to be used, passed as a named list (ie. \code{list(zoom = 3)}). See \strong{Details}.} @@ -40,7 +44,7 @@ points (\code{TRUE}, which is the default) or potentially other shapes as provided by the Nominatim API (\code{FALSE}). See \strong{About Geometry Types}.} } \value{ -A \code{sf} object with the results. +A \CRANpkg{sf} object with the results. } \description{ Generates an address from a latitude and longitude. Latitudes must be @@ -139,7 +143,6 @@ Reverse geocoding coordinates: Get spatial (\code{sf}) objects: \code{\link{bbox_to_poly}()}, \code{\link{geo_address_lookup_sf}()}, -\code{\link{geo_amenity_sf}()}, \code{\link{geo_lite_sf}()} } \concept{reverse} diff --git a/man/roxygen/meta.R b/man/roxygen/meta.R index 3f731d8c..ee4c8a6e 100644 --- a/man/roxygen/meta.R +++ b/man/roxygen/meta.R @@ -1,7 +1,6 @@ list( rd_family_title = list( lookup = "Address Lookup API: ", - amenity = "Search amenities: ", geocoding = "Geocoding strings: ", reverse = "Reverse geocoding coordinates: ", spatial = "Get spatial (\\code{sf}) objects: ", diff --git a/pkgdown/_pkgdown.yml b/pkgdown/_pkgdown.yml index a44c42d4..1c0810aa 100644 --- a/pkgdown/_pkgdown.yml +++ b/pkgdown/_pkgdown.yml @@ -10,9 +10,6 @@ template: creator: "@dhernangomez" card: summary_large_image -development: - mode: auto - repo: branch: main @@ -43,11 +40,6 @@ reference: contents: - has_concept("spatial") -- title: Datasets - desc: Helper datasets. - contents: - - has_concept("datasets") - - title: About the package contents: - nominatimlite-package diff --git a/tests/testthat/_snaps/geo_amenity.md b/tests/testthat/_snaps/geo_amenity.md new file mode 100644 index 00000000..5264cbf6 --- /dev/null +++ b/tests/testthat/_snaps/geo_amenity.md @@ -0,0 +1,9 @@ +# Deprecated + + Code + geo_amenity() + Condition + Error: + ! `geo_amenity()` was deprecated in nominatimlite 0.3.0 and is now defunct. + i Operation not supported any more by the Nominatim API. + diff --git a/tests/testthat/_snaps/geo_amenity_sf.md b/tests/testthat/_snaps/geo_amenity_sf.md new file mode 100644 index 00000000..5139302d --- /dev/null +++ b/tests/testthat/_snaps/geo_amenity_sf.md @@ -0,0 +1,9 @@ +# Deprecated + + Code + geo_amenity_sf() + Condition + Error: + ! `geo_amenity_sf()` was deprecated in nominatimlite 0.3.0 and is now defunct. + i Operation not supported any more by the Nominatim API. + diff --git a/tests/testthat/test-geo_amenity.R b/tests/testthat/test-geo_amenity.R index 5648b43f..b8964b37 100644 --- a/tests/testthat/test-geo_amenity.R +++ b/tests/testthat/test-geo_amenity.R @@ -1,190 +1,5 @@ -test_that("Returning not reachable", { - expect_message(geo_amenity( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - amenity = "xbzbzbzoa aiaia" - ), "not reachable") +test_that("Deprecated", { + skip_if_not_installed("lifecycle") - skip_on_cran() - skip_if_api_server() - - expect_message(obj <- geo_amenity( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - amenity = "xbzbzbzoa aiaia" - )) - - expect_true(nrow(obj) == 1) - expect_true(obj$query == "xbzbzbzoa aiaia") - expect_s3_class(obj, "tbl") - expect_identical(names(obj), c("query", "lat", "lon")) - expect_true(all( - vapply(obj, class, FUN.VALUE = character(1)) - == c("character", rep("numeric", 2)) - )) - expect_true(is.na(obj$lat)) - expect_true(is.na(obj$lon)) - - expect_message( - obj_renamed <- geo_amenity( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - amenity = "xbzbzbzoa aiaia", - lat = "lata", - long = "longa" - ), - "not reachable" - ) - - expect_identical(names(obj_renamed), c("query", "lata", "longa")) - - names(obj_renamed) <- names(obj) - - expect_identical(obj, obj_renamed) -}) - -test_that("Returning empty query", { - skip_on_cran() - skip_if_api_server() - - - expect_message( - obj <- geo_amenity( - bbox = c(-88.1446, 41.5022, -87.4854, 41.8795), - amenity = "grit_bin" - ), - "No results" - ) - - - expect_true(nrow(obj) == 1) - expect_true(obj$query == "grit_bin") - expect_s3_class(obj, "tbl") - expect_identical(names(obj), c("query", "lat", "lon")) - expect_true(all( - vapply(obj, class, FUN.VALUE = character(1)) - == c("character", rep("numeric", 2)) - )) - expect_true(is.na(obj$lat)) - expect_true(is.na(obj$lon)) - - expect_message( - obj_renamed <- geo_amenity( - bbox = c(-88.1446, 41.5022, -87.4854, 41.8795), - amenity = "grit_bin", - lat = "lata", - long = "longa" - ), - "No results" - ) - - expect_identical(names(obj_renamed), c("query", "lata", "longa")) - - names(obj_renamed) <- names(obj) - - expect_identical(obj, obj_renamed) -}) - - -test_that("Data format", { - skip_on_cran() - skip_if_api_server() - skip_if_offline() - - obj <- geo_amenity( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - c("pub", "restaurant"), - ) - - expect_s3_class(obj, "tbl") - expect_false(inherits(obj, "sf")) -}) - -test_that("Checking query", { - skip_on_cran() - skip_if_api_server() - skip_if_offline() - - expect_message(obj <- geo_amenity( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - c("pub", "restaurant"), - limit = 51 - ), "50 results") - - - expect_identical(names(obj), c("query", "lat", "lon", "address")) - - obj <- geo_amenity( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - "pub", - long = "ong", lat = "at", - full_results = FALSE, - return_addresses = FALSE - ) - expect_identical(names(obj), c("query", "at", "ong")) - - obj <- geo_amenity( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - "pub", - long = "ong", lat = "at", - full_results = FALSE, - return_addresses = TRUE - ) - - expect_identical(names(obj), c("query", "at", "ong", "address")) - - obj <- geo_amenity( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - "pub", - long = "ong", lat = "at", - full_results = TRUE, - return_addresses = FALSE - ) - - expect_identical(names(obj)[1:4], c("query", "at", "ong", "address")) - expect_gt(ncol(obj), 4) - - - expect_gt(nrow(geo_amenity( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - "pub", - limit = 10, - custom_query = list(countrycode = "es") - )), 4) - expect_equal(nrow(geo_amenity( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - "pub", - custom_query = list(countrycode = "es") - )), 1) - expect_equal(nrow(geo_amenity( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - "pub", - custom_query = list(extratags = 1) - )), 1) - - expect_lt(nrow(geo_amenity( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - "pub", - limit = 1, - strict = TRUE - )), 2) -}) - -test_that("Dedupe", { - skip_on_cran() - skip_if_api_server() - skip_if_offline() - - # Dupes - dup <- geo_amenity( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - rep(c("pub", "restaurant"), 50), - limit = 1 - ) - - expect_equal(nrow(dup), 100) - expect_equal(as.character(dup$query), rep(c("pub", "restaurant"), 50)) - - # Check deduping - dedup <- dplyr::distinct(dup) - - expect_equal(nrow(dedup), 2) - expect_equal(as.character(dedup$query), rep(c("pub", "restaurant"), 1)) + expect_snapshot(geo_amenity(), error = TRUE) }) diff --git a/tests/testthat/test-geo_amenity_sf.R b/tests/testthat/test-geo_amenity_sf.R index bf87617a..02f8cbf6 100644 --- a/tests/testthat/test-geo_amenity_sf.R +++ b/tests/testthat/test-geo_amenity_sf.R @@ -1,209 +1,5 @@ -test_that("Returning not reachable", { - expect_message(geo_amenity_sf( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - amenity = "xbzbzbzoa aiaia" - ), "not reachable") +test_that("Deprecated", { + skip_if_not_installed("lifecycle") - skip_on_cran() - skip_if_api_server() - - expect_message( - obj <- geo_amenity_sf( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - amenity = "xbzbzbzoa aiaia" - ) - ) - - expect_true(nrow(obj) == 1) - expect_true(obj$query == "xbzbzbzoa aiaia") - expect_s3_class(obj, "sf") - expect_s3_class(obj, "tbl") - expect_true(sf::st_is_empty(obj)) - expect_identical(sf::st_crs(obj), sf::st_crs(4326)) -}) - - -test_that("Returning empty query", { - skip_on_cran() - skip_if_api_server() - - - expect_message( - obj <- geo_amenity_sf( - bbox = c(-88.1446, 41.5022, -87.4854, 41.8795), - amenity = "grit_bin" - ), - "No results" - ) - - - expect_true(nrow(obj) == 1) - expect_true(obj$query == "grit_bin") - expect_s3_class(obj, "sf") - expect_s3_class(obj, "tbl") - expect_true(sf::st_is_empty(obj)) - expect_identical(sf::st_crs(obj), sf::st_crs(4326)) -}) - - -test_that("Data format", { - skip_on_cran() - skip_if_api_server() - skip_if_offline() - - obj <- geo_amenity_sf( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - c("pub", "restaurant"), - ) - - expect_s3_class(obj, "sf") - expect_s3_class(obj, "tbl") - expect_equal(nrow(obj), 2) - expect_identical(as.character(obj$query), c("pub", "restaurant")) - - - # Polygon - - expect_message( - hosp <- geo_amenity_sf( - c( - -3.888954, 40.311977, - -3.517916, 40.643729 - ), - c("hospital", "dump", "pub"), - points_only = FALSE - ), "No results for query dump" - ) - - expect_s3_class(hosp, "sf") - expect_s3_class(hosp, "tbl") - expect_equal(nrow(hosp), 3) - expect_identical(as.character(hosp$query), c("hospital", "dump", "pub")) - expect_identical(sf::st_is_empty(hosp), c(FALSE, TRUE, FALSE)) -}) - - -test_that("Checking query", { - skip_on_cran() - skip_if_api_server() - skip_if_offline() - - - expect_message(obj <- geo_amenity_sf( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - c("pub", "restaurant"), - limit = 51 - ), "50 results") - - - expect_identical(names(obj), c("query", "address", "geometry")) - expect_s3_class(obj, "sf") - expect_s3_class(obj, "tbl") - - 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("query", "geometry")) - expect_s3_class(obj, "sf") - expect_s3_class(obj, "tbl") - - obj <- geo_amenity_sf( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - "pub", - full_results = FALSE, - return_addresses = TRUE - ) - - expect_identical(names(obj), c("query", "address", "geometry")) - expect_s3_class(obj, "sf") - expect_s3_class(obj, "tbl") - - obj <- geo_amenity_sf( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - "pub", - full_results = TRUE, - return_addresses = FALSE - ) - - expect_identical(names(obj)[1:2], c("query", "address")) - expect_gt(ncol(obj), 3) - expect_s3_class(obj, "sf") - expect_s3_class(obj, "tbl") - expect_identical(attributes(obj)$sf_column, "geometry") - - expect_gt(nrow(geo_amenity_sf( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - "pub", - limit = 10, - custom_query = list(countrycode = "es") - )), 4) - - - expect_equal(nrow(geo_amenity_sf( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - "pub", - custom_query = list(countrycode = "es") - )), 1) - - expect_equal(nrow(geo_amenity_sf( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - "pub", - custom_query = list(extratags = 1) - )), 1) - - expect_lt(nrow(geo_amenity_sf( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - "pub", - limit = 1, - strict = TRUE - )), 2) -}) - - -test_that("Dedupe", { - skip_on_cran() - skip_if_api_server() - skip_if_offline() - - # Dupes - dup <- geo_amenity_sf( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - rep(c("pub", "restaurant"), 50), - limit = 1 - ) - - expect_s3_class(dup, "sf") - expect_s3_class(dup, "tbl") - - expect_equal(nrow(dup), 100) - expect_equal(as.character(dup$query), rep(c("pub", "restaurant"), 50)) - - # Check deduping - dedup <- dplyr::distinct(dup) - - expect_equal(nrow(dedup), 2) - expect_equal(as.character(dedup$query), rep(c("pub", "restaurant"), 1)) -}) - - -test_that("Verify names", { - skip_on_cran() - skip_if_api_server() - skip_if_offline() - - # Ok - several <- geo_amenity_sf( - bbox = c(-1.1446, 41.5022, -0.4854, 41.8795), - c("pub", "restaurant"), - limit = 20, - full_results = TRUE - ) - - expect_identical(names(several), unique(names(several))) - - # Do I have dups by any chance? - expect_false(any(grepl("\\.[0-9]$", names(several)))) + expect_snapshot(geo_amenity_sf(), error = TRUE) }) diff --git a/tests/testthat/test-geo_lite.R b/tests/testthat/test-geo_lite.R index de3f7de9..473f360f 100644 --- a/tests/testthat/test-geo_lite.R +++ b/tests/testthat/test-geo_lite.R @@ -116,6 +116,7 @@ test_that("Dedupe", { expect_silent( dup <- geo_lite(rep(c("Pentagon", "Barcelona"), 50), limit = 1, + progressbar = FALSE, verbose = FALSE ) ) @@ -129,3 +130,19 @@ test_that("Dedupe", { expect_equal(nrow(dedup), 2) expect_equal(as.character(dedup$query), rep(c("Pentagon", "Barcelona"), 1)) }) + + +test_that("Progress bar", { + skip_on_cran() + skip_if_api_server() + skip_if_offline() + # No pbar + expect_silent(geo_lite("Madrid")) + expect_silent(geo_lite("Madrid", progressbar = TRUE)) + + # Get a pbar + expect_output(aa <- geo_lite(c("Madrid", "Barcelona")), "1/2") + + # Not + expect_silent(aa <- geo_lite(c("Madrid", "Barcelona"), progressbar = FALSE)) +}) diff --git a/tests/testthat/test-geo_lite_sf.R b/tests/testthat/test-geo_lite_sf.R index 0ee7b23b..872a7e68 100644 --- a/tests/testthat/test-geo_lite_sf.R +++ b/tests/testthat/test-geo_lite_sf.R @@ -155,3 +155,20 @@ test_that("Verify names", { # Do I have dups by any chance? expect_false(any(grepl("\\.[0-9]$", names(several)))) }) + +test_that("Progress bar", { + skip_on_cran() + skip_if_api_server() + skip_if_offline() + # No pbar + expect_silent(geo_lite_sf("Madrid")) + expect_silent(geo_lite_sf("Madrid", progressbar = TRUE)) + + # Get a pbar + expect_output(aa <- geo_lite_sf(c("Madrid", "Barcelona")), "1/2") + + # Not + expect_silent( + aa <- geo_lite_sf(c("Madrid", "Barcelona"), progressbar = FALSE) + ) +}) diff --git a/tests/testthat/test-reverse_geo_lite.R b/tests/testthat/test-reverse_geo_lite.R index ca091a1e..d728d6ae 100644 --- a/tests/testthat/test-reverse_geo_lite.R +++ b/tests/testthat/test-reverse_geo_lite.R @@ -195,7 +195,7 @@ test_that("Dedupe", { lats <- rep(c(40.75728, 55.95335), 50) longs <- rep(c(-73.98586, -3.188375), 50) - expect_silent(dup <- reverse_geo_lite(lats, longs)) + expect_silent(dup <- reverse_geo_lite(lats, longs, progressbar = FALSE)) expect_equal(nrow(dup), 100) @@ -209,3 +209,22 @@ test_that("Dedupe", { expect_equal(nrow(dedup), 2) expect_equal(as.character(dedup$address), nms) }) + +test_that("Progress bar", { + skip_on_cran() + skip_if_api_server() + skip_if_offline() + + lat <- c(40.75728, 55.95335) + long <- c(-73.98586, -3.188375) + + # No pbar + expect_silent(reverse_geo_lite(lat[1], long[1])) + expect_silent(reverse_geo_lite(lat[1], long[1], progressbar = TRUE)) + + # Get a pbar + expect_output(aa <- reverse_geo_lite(lat, long), "1/2") + + # Not + expect_silent(aa <- reverse_geo_lite(lat, long, progressbar = FALSE)) +}) diff --git a/tests/testthat/test-reverse_geo_lite_sf.R b/tests/testthat/test-reverse_geo_lite_sf.R index afee89a1..843bcb54 100644 --- a/tests/testthat/test-reverse_geo_lite_sf.R +++ b/tests/testthat/test-reverse_geo_lite_sf.R @@ -214,7 +214,7 @@ test_that("Dedupe", { lats <- rep(c(40.75728, 55.95335), 50) longs <- rep(c(-73.98586, -3.188375), 50) - expect_silent(dup <- reverse_geo_lite_sf(lats, longs)) + expect_silent(dup <- reverse_geo_lite_sf(lats, longs, progressbar = FALSE)) expect_s3_class(dup, "sf") expect_s3_class(dup, "tbl") @@ -230,3 +230,22 @@ test_that("Dedupe", { expect_equal(nrow(dedup), 2) expect_equal(as.character(dedup$address), nms) }) + +test_that("Progress bar", { + skip_on_cran() + skip_if_api_server() + skip_if_offline() + + lat <- c(40.75728, 55.95335) + long <- c(-73.98586, -3.188375) + + # No pbar + expect_silent(reverse_geo_lite_sf(lat[1], long[1])) + expect_silent(reverse_geo_lite_sf(lat[1], long[1], progressbar = TRUE)) + + # Get a pbar + expect_output(aa <- reverse_geo_lite_sf(lat, long), "1/2") + + # Not + expect_silent(aa <- reverse_geo_lite_sf(lat, long, progressbar = FALSE)) +}) diff --git a/vignettes/nominatimlite.Rmd b/vignettes/nominatimlite.Rmd index 338712ef..9255e4cc 100644 --- a/vignettes/nominatimlite.Rmd +++ b/vignettes/nominatimlite.Rmd @@ -1,155 +1,157 @@ ---- -title: "Get started with nominatimlite" -output: rmarkdown::html_vignette -desc: > - Quick examples showing what `nominatimlite` can do for you. -vignette: > - %\VignetteIndexEntry{Get started with nominatimlite} - %\VignetteEngine{knitr::rmarkdown} - %\VignetteEncoding{UTF-8} -bibliography: references.bib -link-citations: true ---- - - - - - -The goal of `nominatimlite` is to provide a light interface for geocoding -addresses, based on the [Nominatim -API](https://nominatim.org/release-docs/latest/). **Nominatim** is a tool to -search [OpenStreetMap](https://www.openstreetmap.org/) data by name and address -([geocoding](https://wiki.openstreetmap.org/wiki/Geocoding "Geocoding")) and to -generate synthetic addresses of OSM points (reverse geocoding). - -It also allows to load spatial objects using the `sf` package. - -Full site with examples and vignettes on - - -## Why `nominatimlite`? - -The main goal of `nominatimlite` is to access the Nominatim API avoiding the -dependency on `curl`. In some situations, `curl` may not be available or -accessible, so `nominatimlite` uses base functions to overcome this limitation. - -## Recommended packages - -There are other packages much more complete and mature than `nominatimlite`, -that presents similar features: - -- [`tidygeocoder`](https://jessecambon.github.io/tidygeocoder/) - [@R-tidygeocoder]. Allows to interface with Nominatim, Google, TomTom, - Mapbox, etc. for geocoding and reverse geocoding. -- [`osmdata`](https://docs.ropensci.org/osmdata/) [@R-osmdata]. Great for - downloading spatial data from OpenStreetMap, via the [Overpass - API](https://wiki.openstreetmap.org/wiki/Overpass_API). - -## Usage - -### `sf` objects - -With `nominatimlite` you can extract spatial objects easily: - - -```r -library(nominatimlite) - -# Extract some points - Pizza Hut in California - -CA <- geo_lite_sf("California", points_only = FALSE) - -pizzahut <- geo_lite_sf("Pizza Hut, California", - limit = 50, - custom_query = list(countrycodes = "us") -) - -library(ggplot2) - -ggplot(CA) + - geom_sf() + - geom_sf(data = pizzahut, col = "red") -``` - -![Pizza Hut in California](../man/figures/README-pizzahut-1.png){width="100%"} - -You can also extract polygon and line objects (if available) using the option -`points_only = FALSE`: - - -```r -sol_poly <- geo_lite_sf("Statue of Liberty, NY, USA", points_only = FALSE) - -ggplot(sol_poly) + - geom_sf() -``` - -![The Statue of -Liberty](../man/figures/README-statue_liberty-1.png){width="100%"} - -### Geocoding and reverse geocoding - -*Note: examples adapted from `tidygeocoder` package* - -In this first example we will geocode a few addresses using the `geo_lite()` -function: - - -```r -library(tibble) - -# create a dataframe with addresses -some_addresses <- tribble( - ~name, ~addr, - "White House", "1600 Pennsylvania Ave NW, Washington, DC", - "Transamerica Pyramid", "600 Montgomery St, San Francisco, CA 94111", - "Willis Tower", "233 S Wacker Dr, Chicago, IL 60606" -) - -# geocode the addresses -lat_longs <- geo_lite(some_addresses$addr, lat = "latitude", long = "longitude") -``` - -Only latitude and longitude are returned from the geocoder service in this -example, but `full_results = TRUE` can be used to return all of the data from -the geocoder service. - - - -|query | latitude| longitude|address | -|:------------------------------------------|--------:|----------:|:-----------------------------------------------------------------------------------------------------------------| -|1600 Pennsylvania Ave NW, Washington, DC | 38.89770| -77.03655|White House, 1600, Pennsylvania Avenue Northwest, Ward 2, Washington, District of Columbia, 20500, United States | -|600 Montgomery St, San Francisco, CA 94111 | 37.79520| -122.40279|Transamerica Pyramid, 600, Montgomery Street, Financial District, San Francisco, California, 94111, United States | -|233 S Wacker Dr, Chicago, IL 60606 | 41.87874| -87.63596|Willis Tower, 233, South Wacker Drive, Printer's Row, Loop, Chicago, Cook County, Illinois, 60606, United States | - - - -To perform reverse geocoding (obtaining addresses from geographic coordinates), -we can use the `reverse_geo_lite()` function. The arguments are similar to the -`geo_lite()` function, but now we specify the input data columns with the `lat` -and `long` arguments. The dataset used here is from the geocoder query above. -The single line address is returned in a column named by the `address`. - - -```r -reverse <- reverse_geo_lite( - lat = lat_longs$latitude, long = lat_longs$longitude, - address = "address_found" -) -``` - - - -|address_found | lat| lon| -|:-----------------------------------------------------------------------------------------------------------------|--------:|----------:| -|White House, 1600, Pennsylvania Avenue Northwest, Ward 2, Washington, District of Columbia, 20500, United States | 38.89770| -77.03655| -|Transamerica Pyramid, 600, Montgomery Street, Financial District, San Francisco, California, 94111, United States | 37.79520| -122.40279| -|Willis Tower, 233, South Wacker Drive, Printer's Row, Loop, Chicago, Cook County, Illinois, 60606, United States | 41.87874| -87.63596| - - - -For more advance users, see [Nominatim -docs](https://nominatim.org/release-docs/latest/api/Search/) to check the -parameters available. - -## References +--- +title: "Get started with nominatimlite" +output: rmarkdown::html_vignette +desc: > + Quick examples showing what `nominatimlite` can do for you. +vignette: > + %\VignetteIndexEntry{Get started with nominatimlite} + %\VignetteEngine{knitr::rmarkdown} + %\VignetteEncoding{UTF-8} +bibliography: references.bib +link-citations: true +--- + + + + + +The goal of `nominatimlite` is to provide a light interface for geocoding +addresses, based on the [Nominatim +API](https://nominatim.org/release-docs/latest/). **Nominatim** is a tool to +search [OpenStreetMap](https://www.openstreetmap.org/) data by name and address +([geocoding](https://wiki.openstreetmap.org/wiki/Geocoding "Geocoding")) and to +generate synthetic addresses of OSM points (reverse geocoding). + +It also allows to load spatial objects using the `sf` package. + +Full site with examples and vignettes on + + +## Why `nominatimlite`? + +The main goal of `nominatimlite` is to access the Nominatim API avoiding the +dependency on `curl`. In some situations, `curl` may not be available or +accessible, so `nominatimlite` uses base functions to overcome this limitation. + +## Recommended packages + +There are other packages much more complete and mature than `nominatimlite`, +that presents similar features: + +- [`tidygeocoder`](https://jessecambon.github.io/tidygeocoder/) + [@R-tidygeocoder]. Allows to interface with Nominatim, Google, TomTom, + Mapbox, etc. for geocoding and reverse geocoding. +- [`osmdata`](https://docs.ropensci.org/osmdata/) [@R-osmdata]. Great for + downloading spatial data from OpenStreetMap, via the [Overpass + API](https://wiki.openstreetmap.org/wiki/Overpass_API). + +## Usage + +### `sf` objects + +With `nominatimlite` you can extract spatial objects easily: + + +```r +library(nominatimlite) + +# Extract some points - Pizza Hut in California + +CA <- geo_lite_sf("California", points_only = FALSE) + +pizzahut <- geo_lite_sf("Pizza Hut, California", + limit = 50, + custom_query = list(countrycodes = "us") +) + +library(ggplot2) + +ggplot(CA) + + geom_sf() + + geom_sf(data = pizzahut, col = "red") +``` + +![Pizza Hut in California](../man/figures/README-pizzahut-1.png){width="100%"} + +You can also extract polygon and line objects (if available) using the option +`points_only = FALSE`: + + +```r +sol_poly <- geo_lite_sf("Statue of Liberty, NY, USA", points_only = FALSE) + +ggplot(sol_poly) + + geom_sf() +``` + +![The Statue of +Liberty](../man/figures/README-statue_liberty-1.png){width="100%"} + +### Geocoding and reverse geocoding + +*Note: examples adapted from `tidygeocoder` package* + +In this first example we will geocode a few addresses using the `geo_lite()` +function: + + +```r +library(tibble) + +# create a dataframe with addresses +some_addresses <- tribble( + ~name, ~addr, + "White House", "1600 Pennsylvania Ave NW, Washington, DC", + "Transamerica Pyramid", "600 Montgomery St, San Francisco, CA 94111", + "Willis Tower", "233 S Wacker Dr, Chicago, IL 60606" +) + +# geocode the addresses +lat_longs <- geo_lite(some_addresses$addr, lat = "latitude", long = "longitude") +#> | | | 0% | |================= | 33% (1/3) | |================================= | 67% (2/3) | |==================================================| 100% (3/3) +``` + +Only latitude and longitude are returned from the geocoder service in this +example, but `full_results = TRUE` can be used to return all of the data from +the geocoder service. + + + +|query | latitude| longitude|address | +|:------------------------------------------|--------:|----------:|:-----------------------------------------------------------------------------------------------------------------| +|1600 Pennsylvania Ave NW, Washington, DC | 38.89770| -77.03655|White House, 1600, Pennsylvania Avenue Northwest, Ward 2, Washington, District of Columbia, 20500, United States | +|600 Montgomery St, San Francisco, CA 94111 | 37.79520| -122.40279|Transamerica Pyramid, 600, Montgomery Street, Financial District, San Francisco, California, 94111, United States | +|233 S Wacker Dr, Chicago, IL 60606 | 41.87874| -87.63596|Willis Tower, 233, South Wacker Drive, Printer's Row, Loop, Chicago, Cook County, Illinois, 60606, United States | + + + +To perform reverse geocoding (obtaining addresses from geographic coordinates), +we can use the `reverse_geo_lite()` function. The arguments are similar to the +`geo_lite()` function, but now we specify the input data columns with the `lat` +and `long` arguments. The dataset used here is from the geocoder query above. +The single line address is returned in a column named by the `address`. + + +```r +reverse <- reverse_geo_lite( + lat = lat_longs$latitude, long = lat_longs$longitude, + address = "address_found" +) +#> | | | 0% | |================= | 33% (1/3) | |================================= | 67% (2/3) | |==================================================| 100% (3/3) +``` + + + +|address_found | lat| lon| +|:-----------------------------------------------------------------------------------------------------------------|--------:|----------:| +|White House, 1600, Pennsylvania Avenue Northwest, Ward 2, Washington, District of Columbia, 20500, United States | 38.89770| -77.03655| +|Transamerica Pyramid, 600, Montgomery Street, Financial District, San Francisco, California, 94111, United States | 37.79520| -122.40279| +|Willis Tower, 233, South Wacker Drive, Printer's Row, Loop, Chicago, Cook County, Illinois, 60606, United States | 41.87874| -87.63596| + + + +For more advance users, see [Nominatim +docs](https://nominatim.org/release-docs/latest/api/Search/) to check the +parameters available. + +## References