Skip to content

Commit

Permalink
Merge pull request #43 from dieghernan/localsup
Browse files Browse the repository at this point in the history
Include local support and add tests
  • Loading branch information
dieghernan committed Mar 8, 2024
2 parents 9122576 + cfa8eed commit 9bc2d79
Show file tree
Hide file tree
Showing 28 changed files with 195 additions and 84 deletions.
7 changes: 2 additions & 5 deletions CITATION.cff
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ message: 'To cite package "nominatimlite" in publications use:'
type: software
license: MIT
title: 'nominatimlite: Interface with ''Nominatim'' API Service'
version: 0.2.1.9000
version: 0.3.0.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 and return
Expand All @@ -27,7 +27,7 @@ preferred-citation:
email: [email protected]
orcid: https://orcid.org/0000-0001-8457-4658
year: '2024'
version: 0.2.1.9000
version: 0.3.0.9000
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/>.
Expand Down Expand Up @@ -106,9 +106,6 @@ references:
email: [email protected]
orcid: https://orcid.org/0000-0002-4035-0289
year: '2024'
identifiers:
- type: url
value: https://arxiv.org/abs/1403.2805
version: '>= 1.7.0'
- type: software
title: lifecycle
Expand Down
5 changes: 3 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
Type: Package
Package: nominatimlite
Title: Interface with 'Nominatim' API Service
Version: 0.3.0
Version: 0.3.0.9000
Authors@R: c(
person("Diego", "Hernangómez", , "[email protected]", role = c("aut", "cre", "cph"),
comment = c(ORCID = "0000-0001-8457-4658")),
person("Jindra", "Lacko", role = c("ctb", "rev"),
comment = c(ORCID = "0000-0002-0375-5156"))
comment = c(ORCID = "0000-0002-0375-5156")),
person("Alex", "White", role = "ctb")
)
Description: Lite interface for getting data from 'OSM' service
'Nominatim' <https://nominatim.org/release-docs/latest/>. Extract
Expand Down
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# nominatimlite (development version)

- It is possible to use **nominatimlite** with local serves (#42
\@alexwhitedatamine).

# nominatimlite 0.3.0

- Add a `progressbar` parameter to `geo_lite()`, `geo_lite_sf()`,
Expand Down
6 changes: 5 additions & 1 deletion R/geo_address_lookup.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,13 @@ geo_address_lookup <- function(osm_ids,
full_results = FALSE,
return_addresses = TRUE,
verbose = FALSE,
nominatim_server =
"https://nominatim.openstreetmap.org/",
custom_query = list()) {
# Step 1: Download ----
api <- "https://nominatim.openstreetmap.org/lookup?"
# First build the api address. If the passed nominatim_server does not end
# with a trailing forward-slash, add one
api <- prepare_api_url(nominatim_server, "lookup?")

# Prepare nodes
osm_ids <- as.integer(osm_ids)
Expand Down
7 changes: 5 additions & 2 deletions R/geo_address_lookup_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,13 @@ geo_address_lookup_sf <- function(osm_ids,
full_results = FALSE,
return_addresses = TRUE,
verbose = FALSE,
nominatim_server =
"https://nominatim.openstreetmap.org/",
custom_query = list(),
points_only = TRUE) {
# Step 1: Download ----
api <- "https://nominatim.openstreetmap.org/lookup?"
# First build the api address. If the passed nominatim_server does not end
# with a trailing forward-slash, add one
api <- prepare_api_url(nominatim_server, "lookup?")

# Prepare nodes
osm_ids <- as.integer(osm_ids)
Expand Down
11 changes: 9 additions & 2 deletions R/geo_lite.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,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 nominatim_server The URL of the Nominatim server to use.
#' Defaults to `"https://nominatim.openstreetmap.org/"`.
#' @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
Expand Down Expand Up @@ -53,6 +55,7 @@ geo_lite <- function(address,
full_results = FALSE,
return_addresses = TRUE,
verbose = FALSE,
nominatim_server = "https://nominatim.openstreetmap.org/",
progressbar = TRUE,
custom_query = list()) {
if (limit > 50) {
Expand Down Expand Up @@ -90,6 +93,7 @@ geo_lite <- function(address,
full_results,
return_addresses,
verbose,
nominatim_server = nominatim_server,
custom_query
)
})
Expand All @@ -111,9 +115,12 @@ geo_lite_single <- function(address,
full_results = TRUE,
return_addresses = TRUE,
verbose = FALSE,
nominatim_server =
"https://nominatim.openstreetmap.org/",
custom_query = list()) {
# Step 1: Download ----
api <- "https://nominatim.openstreetmap.org/search?q="
# First build the api address. If the passed nominatim_server does not end
# with a trailing forward-slash, add one
api <- prepare_api_url(nominatim_server, "search.php?q=")

# Replace spaces with +
address2 <- gsub(" ", "+", address)
Expand Down
12 changes: 9 additions & 3 deletions R/geo_lite_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@ geo_lite_sf <- function(address,
full_results = FALSE,
verbose = FALSE,
progressbar = TRUE,
nominatim_server =
"https://nominatim.openstreetmap.org/",
custom_query = list(),
points_only = TRUE) {
if (limit > 50) {
Expand Down Expand Up @@ -121,7 +123,8 @@ geo_lite_sf <- function(address,
full_results,
verbose,
custom_query,
points_only
points_only,
nominatim_server = nominatim_server
)
})

Expand Down Expand Up @@ -154,10 +157,13 @@ geo_lite_sf_single <- function(address,
return_addresses = TRUE,
full_results = FALSE,
verbose = FALSE,
nominatim_server =
"https://nominatim.openstreetmap.org/",
custom_query = list(),
points_only = TRUE) {
# Step 1: Download ----
api <- "https://nominatim.openstreetmap.org/search?q="
# First build the api address. If the passed nominatim_server does not end
# with a trailing forward-slash, add one
api <- prepare_api_url(nominatim_server, "search.php?q=")

# Replace spaces with +
address2 <- gsub(" ", "+", address)
Expand Down
59 changes: 23 additions & 36 deletions R/nominatim_check_access.R
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,19 @@
#' }
#' @keywords internal
#' @export
nominatim_check_access <- function() {
url <- "https://nominatim.openstreetmap.org/status.php?format=json"
nominatim_check_access <- function(
nominatim_server = "https://nominatim.openstreetmap.org/") {
# First build the api address. If the passed nominatim_server does not end
# with a trailing forward-slash, add one
url <- prepare_api_url(nominatim_server, "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
Expand Down Expand Up @@ -69,43 +71,28 @@ skip_if_api_server <- function() {
#'
#' @keywords internal
#'
api_call <- function(url, destfile, quiet) {
# nocov start
dwn_res <-
tryCatch(
api_call <- function(url, destfile = tempfile(fileext = ".json"), quiet) {
dwn_res <- suppressWarnings(
try(
download.file(url, destfile = destfile, quiet = quiet, mode = "wb"),
warning = function(e) {
return(FALSE)
},
error = function(e) {
return(FALSE)
}
silent = TRUE
)
# 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 {
if (!inherits(dwn_res, "try-error")) {
return(TRUE)
}
# nocov end
if (isFALSE(quiet)) message("Retrying query")
Sys.sleep(1)

dwn_res <- suppressWarnings(
try(
download.file(url, destfile = destfile, quiet = quiet, mode = "wb"),
silent = TRUE
)
)

!inherits(dwn_res, "try-error")
}
24 changes: 15 additions & 9 deletions R/reverse_geo_lite.R
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ reverse_geo_lite <- function(lat,
full_results = FALSE,
return_coords = TRUE,
verbose = FALSE,
nominatim_server =
"https://nominatim.openstreetmap.org/",
progressbar = TRUE,
custom_query = list()) {
# Check inputs
Expand Down Expand Up @@ -131,13 +133,14 @@ reverse_geo_lite <- function(lat,
}
rw <- key[x, ]
res_single <- reverse_geo_lite_single(
as.double(rw$lat_cap_int),
as.double(rw$long_cap_int),
address,
full_results,
return_coords,
verbose,
custom_query
lat_cap = as.double(rw$lat_cap_int),
long_cap = as.double(rw$long_cap_int),
address = address,
full_results = full_results,
return_coords = return_coords,
verbose = verbose,
custom_query = custom_query,
nominatim_server = nominatim_server
)

res_single <- dplyr::bind_cols(res_single, rw[, c(1, 2)])
Expand All @@ -164,9 +167,12 @@ reverse_geo_lite_single <- function(lat_cap,
full_results = FALSE,
return_coords = TRUE,
verbose = TRUE,
nominatim_server =
"https://nominatim.openstreetmap.org/",
custom_query = list()) {
# Step 1: Download ----
api <- "https://nominatim.openstreetmap.org/reverse?"
# First build the api address. If the passed nominatim_server does not end
# with a trailing forward-slash, add one
api <- prepare_api_url(nominatim_server, "reverse?")

# Compose url
url <- paste0(api, "lat=", lat_cap, "&lon=", long_cap, "&format=json")
Expand Down
27 changes: 17 additions & 10 deletions R/reverse_geo_lite_sf.R
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,8 @@ reverse_geo_lite_sf <- function(lat,
full_results = FALSE,
return_coords = TRUE,
verbose = FALSE,
nominatim_server =
"https://nominatim.openstreetmap.org/",
progressbar = TRUE,
custom_query = list(),
points_only = TRUE) {
Expand Down Expand Up @@ -119,15 +121,17 @@ reverse_geo_lite_sf <- function(lat,
setTxtProgressBar(pb, x)
}
rw <- key[x, ]

res_single <- reverse_geo_lite_sf_single(
as.double(rw$lat_cap_int),
as.double(rw$long_cap_int),
address,
full_results,
return_coords,
verbose,
custom_query,
points_only
lat_cap = as.double(rw$lat_cap_int),
long_cap = as.double(rw$long_cap_int),
address = address,
full_results = full_results,
return_coords = return_coords,
verbose = verbose,
custom_query = custom_query,
points_only = points_only,
nominatim_server = nominatim_server
)

res_single <- dplyr::bind_cols(res_single, rw[, c(1, 2)])
Expand Down Expand Up @@ -172,10 +176,13 @@ reverse_geo_lite_sf_single <- function(lat_cap,
full_results = TRUE,
return_coords = TRUE,
verbose = TRUE,
nominatim_server =
"https://nominatim.openstreetmap.org/",
custom_query = list(),
points_only = FALSE) {
# Step 1: Download ----
api <- "https://nominatim.openstreetmap.org/reverse?"
# First build the api address. If the passed nominatim_server does not end
# with a trailing forward-slash, add one
api <- prepare_api_url(nominatim_server, "reverse?")

# Compose url
url <- paste0(api, "lat=", lat_cap, "&lon=", long_cap, "&format=geojson")
Expand Down
7 changes: 7 additions & 0 deletions R/utils.R
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,13 @@ keep_names_rev <- function(x, address = "address", return_coords = FALSE,
return(out)
}

prepare_api_url <- function(
nominatim_server = "https://nominatim.openstreetmap.org/",
entry) {
api <- paste0(gsub("/$", "", nominatim_server), "/", entry)
api
}

# tibble helpers ----

empty_tbl <- function(x, lat, lon) {
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ A BibTeX entry for LaTeX users is
title = {{nominatimlite}: Interface with {Nominatim} {API} Service},
author = {Diego Hernangómez},
year = {2024},
version = {0.3.0},
version = {0.3.0.9000},
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 and return spatial objects on sf format.},
Expand Down
Loading

0 comments on commit 9bc2d79

Please sign in to comment.