Monday, November 24, 2014

Geocoding in R with Google Maps API

There are some occasions where you might need to translate an address into geospatial coordinates for analysis. Here's a quick post on how to do this using R and Google Maps API.

I will be using two libraries common with reaching out and getting information from various APIs on the web.
  • RJSONIO: Useful for parsing JSON objects
  • RCurl: Used in sending a web request to an API
With these two packages, we'll build out the url for the request, send the request, and parse the results. There are several parameters returned in the results, but I really care about four pieces of information: 
  • A formatted version of the address
  • The latitude and longitude coordinates for the address
  • The accuracy for the given coordinates
The formatted address can be helpful in making sure Google correctly interpreted your address. Unfortunately, Google can't pinpoint where every address is exactly. They classify their accuracy into four different categories. The descriptions below have been taken from the API website:
  • "ROOFTOP" indicates that the returned result is a precise geocode for which we have location information accurate down to street address precision.
  • "RANGE_INTERPOLATED" indicates that the returned result reflects an approximation (usually on a road) interpolated between two precise points (such as intersections). Interpolated results are generally returned when rooftop geocodes are unavailable for a street address.
  • "GEOMETRIC_CENTER" indicates that the returned result is the geometric center of a result such as a polyline (for example, a street) or polygon (region).
  • "APPROXIMATE" indicates that the returned result is approximate.
From my testing, it appears that normal home addresses are almost always "RANGE_INTERPOLATED" whereas landmarks are marked as "ROOFTOP". 


For more information, see the Google Maps API documentation. This post was inspired by a post done by Jose Gonzalez. 

No comments:

Post a Comment