12

I am considering rChart/LeafLet to create a shiny app for housing sales in my county. There are several hundred houses for sale at any given time. Want to map street address-to-geolocation (lat/long) for all and display them on a map. So, I am looking for a r package, service or database that can map street address to geolocation.

Antex
  • 1,364
  • 4
  • 18
  • 35
  • 6
    `nominatim` : https://github.com/hrbrmstr/nominatim ; `ggmap::geocode` ; `geocodeHERE::geocodeHERE_simple` ; `geonames` package; also `google r street address geolocation` – hrbrmstr Sep 10 '15 at 14:52
  • Excelent article with code example (using package ggmap): http://www.shanelynn.ie/massive-geocoding-with-r-and-google-maps/ – Paulo S. Abreu Apr 26 '17 at 18:39

3 Answers3

24

Here is a function based on Harvey's suggestion. It will look for the address and give the coordinates of the first result. Have a look at the structure of x in the function to see other information you can get.

geocodeAdddress <- function(address) {
  require(RJSONIO)
  url <- "http://maps.google.com/maps/api/geocode/json?address="
  url <- URLencode(paste(url, address, "&sensor=false", sep = ""))
  x <- fromJSON(url, simplify = FALSE)
  if (x$status == "OK") {
    out <- c(x$results[[1]]$geometry$location$lng,
             x$results[[1]]$geometry$location$lat)
  } else {
    out <- NA
  }
  Sys.sleep(0.2)  # API only allows 5 requests per second
  out
}

For example:

R> geocodeAdddress("Time Square, New York City")
[1] -73.98722  40.7575
maccruiskeen
  • 2,748
  • 2
  • 13
  • 23
5

I have used Google Geolocation, This is simple to set-up and easy to implement on almost any project:

https://developers.google.com/maps/documentation/geocoding/intro

Harvey
  • 1,320
  • 3
  • 13
  • 33
  • 3
    For future reference, this belongs in a comment, but I guess you don't have enough rep to do that so OK. – jlhoward Sep 10 '15 at 14:47
  • 1
    Thanks Harvey. The goole maps limit the number of request to 10/sec or 2500 a day. Which is fine. is there a way to make a bulk request. Say I send 100 street addresses in and get 100 lon/lat back? – Antex Sep 10 '15 at 15:16
  • Sorry I should have mentioned there was a limit. but for small applications its not much to worry about. I managed to perform multiple in an iOS app by simply looping requests. Although you may have to limit the speed of this due to multiple connections too fast may lock you out. Sorry I don't know the R specifics! – Harvey Sep 10 '15 at 15:37
1

Modified answer to account for the API requirement:

api_key <- c("YOUR_API_KEY")

geocodeAdddress <- function(address) {
  require(RJSONIO)
  url <- "https://maps.googleapis.com/maps/api/geocode/json?address="
  url <- URLencode(paste(url, address, sep = ""))
  url <- URLencode(paste(url, "&key=", api_key, sep = ""))
  x <- fromJSON(url, simplify = FALSE)
  print(x$status)
  if (x$status == "OK") {
    out <- c(x$results[[1]]$geometry$location$lng,
             x$results[[1]]$geometry$location$lat)
  } else {
    out <- NA
  }
  Sys.sleep(0.2)  # API only allows 5 requests per second
  out
}