Statisfaction

Calling Google Maps API from R

Posted in Geek, R by Pierre Jacob on 5 October 2011

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/

About these ads

10 Responses

Subscribe to comments with RSS.

  1. kay said, on 6 October 2011 at 20:21

    ..nice!
    keep up the good work!

    one question: would i have to call the function for each place, or would this thing also work for a list of places?

    best,
    kay

    • Pierre Jacob said, on 6 October 2011 at 23:03

      Kay: it would not work, since the list of strings would result in a long string in the “paste” inside gGeoCode (as if you asked for several places at once when using Google Maps).

      However you can easily make the call inside a loop, for instance.

  2. Pawel said, on 6 October 2011 at 22:02

    Great job – simple and sweet.

  3. Earl F Glynn said, on 9 October 2011 at 20:11

    Here’s an article I wrote about using R to geocode a list of >10,000 names. With the 2500/day Google geocode limit, the output file from one day after some minor editing became the input file for the next day:

    Geocoding addresses from Missouri Sex Offender Registry
    http://www.franklincenterhq.org/2541/geocoding-addresses-from-missouri-sex-offender-registry/

    • Pierre Jacob said, on 10 October 2011 at 14:23

      Cheers, that’s very interesting. I’m adding it to the post.

  4. The Best Colleges said, on 10 October 2011 at 18:56

    We wanted to let you know that your blog was included in our list of the top 50 statistics blogs of 2011. Our goal was to highlight blogs that students and prospective students will find useful and interesting in their exploration of the field.

    You can view the entire list at http://www.thebestcolleges.org/best-statistics-blogs/

    Congratulations!

    • Pierre Jacob said, on 10 October 2011 at 22:42

      Thanks a lot, we’re honored!

  5. Google Fusion Tables | Statisfaction said, on 8 December 2011 at 19:03

    [...] quick post about another Google service that I discovered recently called Fusion Tables. There you can store, share and visualize data up [...]

  6. [...] to points of interest longitude / latitude, here churches stored in a vector eglises (use e.g. this to geolocalise places of interest). Then run this code from Baptiste’s [...]


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 51 other followers

%d bloggers like this: