0

I'm handling object data in R with additional geo-information like street-name, street-number, postal-code and city from Germany. I want to use a function to fetch the lon and lat info for the specific adress and update my dataframe. With the lon and lat for each opbject from that dataframe I'm generating a geom_point in a qmap.

Any help or information for a possible solution in R or where to look up for more information is welcome. Sebastian

SebastianS
  • 477
  • 7
  • 14

1 Answers1

0

You can use the Google Maps API for reverse geocoding, but you will get rate limited rather quickly. There is already an R package that uses this Google API, but I wrote up this function for reverse geo-coding using the openstreetmap API. Make sure to put your email in the call, however, so that they can contact you if you begin to stress their server.

FindGPS <- function(cityname)  {
  library("RJSONIO") 
  cityname <- gsub(" ","\\+",cityname) #add '+' character to URL 
  #Open Connection
  SITE <- paste('http://nominatim.openstreetmap.org/
          search?q=',cityname,'&format=json&email=
          youremailhere@gmail.com', sep="") 
  con <- url(SITE)
  data.json <- fromJSON(paste(readLines(con,warn=FALSE),
                              collapse=""))
  close(con)
  t<-length(data.json)

  if(t>0)   {
    lat <- data.json[[1]]$lat
    long <- data.json[[1]]$lon
    latlong<-c(lat, long)
    return (latlong)
  }
}
Chris Bail
  • 307
  • 1
  • 8
  • Hi Chris, thank you for your function. I need lon and lat for a given street and streetnumber in a city. So my dataframe looks like street; street_number;postal_code;citiy. Can I expand the city name snippet in FindGPS with street and streetnumber and city? At the end of the function "return(latlon)" I need to update my dataframe with the two new fields lon and lat. – SebastianS Mar 26 '15 at 09:43
  • I *think* so but I haven't used the code in a few years- maybe check the api documentation? – Chris Bail Mar 26 '15 at 12:39