15

I need to find a package in R or source code in R that performs batch geocoding using Bing, Yahoo, OpenStreetmap. Do you have any suggestions?

18

API conditions constantly change, but this should work right now.

OSM:

devtools::install_github("hrbrmstr/nominatim")
library(nominatim)
b1 <- osm_geocode("Berlin, Germany")
b1[c("lat", "lon")]

Yahoo:

devtools::install_github("trestletech/rydn")
library(rydn)
options(RYDN_KEY="yourAPIkey", RYDN_SECRET="yourSecret")
b2 <- find_place("Berlin, Germany")  
b2[c("latitude", "longitude")]

Bing: taRifx.geo (works with Google) and supposedly works with Bing, but I could never get it to work, so I wrote my own function.

bGeoCode <- function(str, BingMapsKey){
    require(RCurl)
    require(RJSONIO)
    u <- URLencode(paste0("http://dev.virtualearth.net/REST/v1/Locations?q=", str, "&maxResults=1&key=", BingMapsKey))
    d <- getURL(u)
    j <- fromJSON(d,simplify = FALSE) 
    if (j$resourceSets[[1]]$estimatedTotal > 0) {
      lat <- j$resourceSets[[1]]$resources[[1]]$point$coordinates[[1]]
      lng <- j$resourceSets[[1]]$resources[[1]]$point$coordinates[[2]]
    }
    else {    
      lat <- lng <- NA
    }
    c(lat,lng)
}  

bGeoCode("Berlin, Germany", "yourAPIKeyHere")

Google:

library(ggmap)  
geocode("Berlin, Germany", source="google")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.