Skip to content

Instantly share code, notes, and snippets.

@abelsonlive
Created September 19, 2012 20:04
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save abelsonlive/3751902 to your computer and use it in GitHub Desktop.
Save abelsonlive/3751902 to your computer and use it in GitHub Desktop.
google geocoding API, R
geocode.addr <- function(uid_query) {
require("rjson")
require("RCurl")
require("plyr")
uid <- uid_query$uid
query <- uid_query$query
geo.url <- "http://maps.googleapis.com/maps/api/geocode/json?address="
geo.text <- try(getURL(paste(geo.url, URLencode(query), "&sensor=false", sep="")))
if(class(geo.text)=="try-error"){
geo.text = try(readLines(paste(geo.url, URLencode(query), "&sensor=false", sep="")))
}
if (class(geo.text)=="try-error"){
print(paste("having trouble reading this query:", uid))
}
geo.json <- fromJSON(geo.text)
if(geo.json$status == "OK"){
lat = geo.json$results[[1]]$geometry$location$lat
lng = geo.json$results[[1]]$geometry$location$lng
type = geo.json$results[[1]]$geometry$location_type
info <- data.frame(uid, lat, lng, type, stringsAsFactors=F)
return(info)
}
else{
if(geo.json$status == "OVER_QUERY_LIMIT") {
stop(paste("Hit rate limit at:", uid))
}
}
out = ddply(uid_address, .(uid), geocode.addr, .progress="text")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment