Calling Google Maps API from R
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/
..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
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.
Great job – simple and sweet.
All the credit goes to the guys there:
http://stackoverflow.com/questions/3257441/geocoding-in-r-with-google-maps
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/
Cheers, that’s very interesting. I’m adding it to the post.
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!
Thanks a lot, we’re honored!
[...] quick post about another Google service that I discovered recently called Fusion Tables. There you can store, share and visualize data up [...]
[...] 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 [...]