Calling Google Maps API from R

October 5, 2011
By

(This article was first published on Statisfaction » R, and kindly contributed to R-bloggers)

Hi,

Related to Julyan’s previous post, I want to share an easy way to access Google Maps API through R. And then we’ll stop about Google, otherwise it’ll look like we’re just looking for jobs.

My problem was the following: I have a database (from priceofweed.com), with locations written as “city, region, country”. What I wanted was the precise location (latitude, longitude) for each city. After some browsing it’s possible to grab a list of cities for each country from some local geographical institute and merge that with the database. The problem is that for each country the database is often in a different format, and full of unnecessary information for the problem at hand (and hence unnecessarily large). For example the information for the US is there somewhere (and it’s amazingly detailed by the way), whereas for other countries it’s there.

So instead a “lazy” method consists in calling Google Maps to find the location for each city, since google maps has a pretty good world-wide coverage of geographic names, it should work! The R function is described there, and I copy paste it here:

getDocNodeVal=function(doc, path)
{
   sapply(getNodeSet(doc, path), function(el) xmlValue(el))
}

gGeoCode=function(str)
{
  library(XML)
  u=paste('http://maps.google.com/maps/api/geocode/xml?sensor=false&address=',str)
  doc = xmlTreeParse(u, useInternal=TRUE)
  str=gsub(' ','%20',str)
  lat=getDocNodeVal(doc, "/GeocodeResponse/result/geometry/location/lat")
  lng=getDocNodeVal(doc, "/GeocodeResponse/result/geometry/location/lng")
  list(lat = lat, lng = lng)
}

gGeoCode("Malakoff, France")

Created by Pretty R at inside-R.org

There are limitations though: it’s free up to 2,500 requests per day and then you’re kicked out for 24 hours. Otherwise… you have to pay! See the terms here. Pretty convenient though!

EDIT: a more detailed post about Google GeoCoding, and the use of it on Missouri Sex Offender Registry:
http://www.franklincenterhq.org/2541/geocoding-addresses-from-missouri-sex-offender-registry/


To leave a comment for the author, please follow the link and comment on his blog: Statisfaction » R.

R-bloggers.com offers daily e-mail updates about R news and tutorials on topics such as: visualization (ggplot2, Boxplots, maps, animation), programming (RStudio, Sweave, LaTeX, SQL, Eclipse, git, hadoop, Web Scraping) statistics (regression, PCA, time series, trading) and more...



If you got this far, why not subscribe for updates from the site? Choose your flavor: e-mail, twitter, RSS, or facebook...

Tags: ,

Comments are closed.