Created
September 19, 2012 20:04
-
-
Save abelsonlive/3751902 to your computer and use it in GitHub Desktop.
google geocoding API, R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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