Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link
Owner Author

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
You can’t perform that action at this time.