Skip to content

Instantly share code, notes, and snippets.

@rhansson
Last active February 11, 2017 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rhansson/1efb9a1b9bec053aa19d71c67f0e27a3 to your computer and use it in GitHub Desktop.
Save rhansson/1efb9a1b9bec053aa19d71c67f0e27a3 to your computer and use it in GitHub Desktop.
GeoTuple API access from R
# Download JSON via GeoTuple API
# http://geotuple.org//pages/api.html
library(jsonlite)
library(httr)
# Thanks to http://stackoverflow.com/users/5729528/daeyoung-kim
# for this function
getFunctionEndPoint <- function(url, format) {
return(paste(url, format, sep = '/'))
}
# set up http request:
# CalEnviroScreen + Income per Capita
# for Sacramento downtown area
api_url <- "http://geotuple.com/ocpu/user/rolandhhansson/library/geotuple/R/api_getpoints"
API_KEY <- "YOUR API KEY"
args <- list(sw_lon=-121.552, sw_lat=38.543, ne_lon=-121.44, ne_lat=38.61, zoom=11, themes=list("calenviro", "inc_percap"), key=API_KEY)
# make request
resp <- POST(
url = getFunctionEndPoint(
url = api_url,
format = "json"),
body = args,
encode = 'json')
# into data frame
df <- fromJSON(rawToChar(resp$content))
# fit linear model and plot regression line
if (length(df) > 0) {
frm <- formula(paste("calenviro","~","inc_percap"))
LM.model = lm(frm, data=df)
plot(frm, data=df, pch=20, xlab="Income per Capita", ylab="CalEnviroScreen")
abline(LM.model, col="red") # Draws the regression line on the plot
} else {
print("Invalid request or quota exceeded!") # max 1000 points per request and/or 5 < requests/minute
}
@rhansson
Copy link
Author

rhansson commented Feb 7, 2017

api_plot_calenviro-incpcap

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment