I would like to use the OpenStreetMap API to search GPS coordinates of cities. I would like to be able to retrieve these coordinates with zip codes, but I would also like to be able to do so with city names.
The issue is that the API's URL is no longer valid !
My function is :
mygeocode <- function(adresses){
nominatim_osm <- function(address = NULL){
## details: http://wiki.openstreetmap.org/wiki/Nominatim
## fonction nominatim_osm proposée par D.Kisler
if(suppressWarnings(is.null(address))) return(data.frame())
tryCatch(
d <- jsonlite::fromJSON(
gsub('\\@addr\\@', gsub('\\s+', '\\%20', address),
'http://nominatim.openstreetmap.org/search/@addr@?format=json&addressdetails=0&limit=1')
), error = function(c) return(data.frame())
)
if(length(d) == 0) return(data.frame())
return(c(as.numeric(d$lon), as.numeric(d$lat)))
}
tableau <- t(sapply(adresses,nominatim_osm))
colnames(tableau) <- c("lon","lat")
return(tableau)
}````
If you trigger that query in your browser (e.g. http://nominatim.openstreetmap.org/search/Helsinki,%20Finland?format=json&addressdetails=0&limit=1 ) you'll be advised on changes you should make:
Though you can also pick some client library,
{tidygeocoder}for example:Created on 2023-10-30 with reprex v2.0.2