Created
October 25, 2021 15:14
-
-
Save jhollist/da679c3f83261aa373edbb7ad3e9dbeb to your computer and use it in GitHub Desktop.
Example code for using a dataframe to access USEPA WATERS API. Shows two iteration examples: purrr and for loop.
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
library(purrr) | |
library(jsonlite) | |
library(dplyr) | |
library(usethis) | |
# The Data.gov API requires a key and has a 1000 request/hour limit | |
# Get Key from https://api.data.gov/signup/ | |
# Save key in r environ | |
usethis::edit_r_environ() | |
# Add DATAGOV_KEY=YOURKEYGOESHERE to the file. Save and restart R | |
dg_key <- Sys.getenv("DATAGOV_KEY") | |
# Example dataframe with coords as x, y columns | |
xdf <- data.frame(id = c("A","B"), x = c(-87.941093, -87.93053627014162), y = c(42.544354, 42.529982254508425), numeric_id = 1:2) | |
# Function that accepts a one row data frame, builds WATERS API URL, gets result | |
# and returns COMID. It assumes 4 columns named id, x, y, and numeric_id. | |
my_fun <- function(df, key){ | |
#API limits to 1000 requests/hour | |
Sys.sleep(4) | |
base_url <- "https://api.epa.gov/waters/v1/pointindexing?pGeometry=SRID%3D4269%3BPOINT%28" | |
end_url <- "%29&pPointIndexingMethod=RAINDROP&pOutputPathFlag=TRUE&pReturnFlowlineGeomFlag=TRUE&f=json&api_key=" | |
url <- paste0(base_url, df$x, "%20", df$y, end_url, key) | |
response <- jsonlite::read_json(url, flatten = TRUE) | |
# Simple way to see progress, better solutions but this is easy | |
cat("\r", paste(df$numeric_id)) | |
flush.console() | |
response$output$ary_flowlines[[1]][[4]] | |
} | |
# purrr solution | |
comids_purrr <- purrr::map2_chr(split(xdf, xdf$id), dg_key, my_fun) | |
xdf_purrr <- mutate(xdf, comids = comids_purrr) | |
# for loop solution | |
# preallocate object/memory | |
comids_loop <- vector("character", nrow(xdf)) | |
for(i in seq_along(comids_loop)){ | |
comids_loop[i] <- my_fun(xdf[i,], key = dg_key) | |
} | |
xdf_loop <- mutate(xdf, comids = comids_loop) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment