Skip to content

Commit

Permalink
Deprecate geo amenities
Browse files Browse the repository at this point in the history
  • Loading branch information
dieghernan committed Jan 8, 2024
1 parent 6f85ef5 commit 5b5759c
Show file tree
Hide file tree
Showing 37 changed files with 334 additions and 1,067 deletions.
23 changes: 19 additions & 4 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ title: 'nominatimlite: Interface with ''Nominatim'' API Service'
version: 0.2.1.9000
doi: 10.5281/zenodo.5113195
abstract: Lite interface for getting data from 'OSM' service 'Nominatim' <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.
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
Expand All @@ -31,8 +31,8 @@ preferred-citation:
doi: 10.5281/zenodo.5113195
url: https://dieghernan.github.io/nominatimlite/
abstract: Lite interface for getting data from OSM service Nominatim <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.
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/
Expand Down Expand Up @@ -110,6 +110,21 @@ references:
- 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: [email protected]
- family-names: Wickham
given-names: Hadley
email: [email protected]
orcid: https://orcid.org/0000-0003-4757-117X
year: '2024'
- type: software
title: sf
abstract: 'sf: Simple Features for R'
Expand Down
6 changes: 3 additions & 3 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Authors@R: c(
)
Description: Lite interface for getting data from 'OSM' service
'Nominatim' <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.
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
Expand All @@ -21,6 +21,7 @@ Depends:
Imports:
dplyr (>= 1.0.0),
jsonlite (>= 1.7.0),
lifecycle,
sf (>= 0.9.0),
utils
Suggests:
Expand All @@ -37,7 +38,6 @@ Config/testthat/parallel: true
Copyright: Data © OpenStreetMap contributors, ODbL 1.0.
<https://www.openstreetmap.org/copyright>
Encoding: UTF-8
LazyData: true
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
X-schema.org-applicationCategory: cartography
Expand Down
6 changes: 6 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
`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.
Expand Down
1 change: 0 additions & 1 deletion R/bbox_to_poly.R
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
#' @seealso [sf::st_as_sfc()]
#'
#' @family spatial
#' @family amenity
#'
#' @details
#'
Expand Down
43 changes: 0 additions & 43 deletions R/data.R

This file was deleted.

162 changes: 12 additions & 150 deletions R/geo_amenity.R
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
#' Geocode amenities
#'
#' @description
#' This function search amenities as defined by OpenStreetMap on a restricted
#' area defined by a bounding box in the form of
#' `(<min_latitude>, <min_longitude>, <max_latitude>, <max_longitude>)`. 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
#' `(<min_latitude>, <min_longitude>, <max_latitude>, <max_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.
Expand All @@ -26,8 +22,6 @@
#' @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
Expand All @@ -37,22 +31,7 @@
#' <https://wiki.openstreetmap.org/wiki/Key:amenity>.
#'
#'
#' @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,
Expand All @@ -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
}
Loading

0 comments on commit 5b5759c

Please sign in to comment.