Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Geocode function for R using Infochimps
geocode = function(location) {
library(RJSONIO)
api.uri = "http://api.infochimps.com/"
geocode.uri = "geo/utils/geolocate?"
api.key = "apikey=xxxxxxxxxxxx"
print(location)
uri = paste(api.uri, geocode.uri, api.key, "&f.address_text=", location, sep="")
raw.data = readLines(uri, warn="F")
results = fromJSON(raw.data)
if (!is.null(results[[2]][[1]]["county_id"])) {
county = results[[2]][[1]][["county_id"]]
df = data.frame(county=county)
}
if (!is.null(results[[2]][[1]][["coordinates"]])) {
long = results[[2]][[1]][["coordinates"]][[1]]
lat = results[[2]][[1]][["coordinates"]][[2]]
if (is.null(df)) {
df = data.frame(lat=as.numeric(lat), long=as.numeric(long))
}
else {
df = cbind(df,data.frame(lat=as.numeric(lat), long=as.numeric(long)))
}
}
df
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.