GeoTuple API access from 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
# 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 | |
} |
Author
rhansson
commented
Feb 7, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment