Skip to content

Commit c2f216b

Browse files
authored
Large requests (#69)
* added functionality to accept gt 700 COMIDs in POST header * incremented package version (minor update) * updates to accomodate changes for large number of COMIDs in POST header
1 parent f4bf4ea commit c2f216b

9 files changed

Lines changed: 91 additions & 36 deletions

File tree

DESCRIPTION

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
Package: StreamCatTools
22
Type: Package
33
Title: Tools to Work with the StreamCat API Within R and Access the Full Suite of StreamCat and LakeCat Metrics
4-
Version: 0.5.0
4+
Version: 0.6.0
55
Authors@R: c(person(given = "Marc",
66
family = "Weber",
77
role = c("aut", "cre"),
@@ -47,7 +47,8 @@ Suggests:
4747
magrittr,
4848
readr,
4949
tidyr,
50-
stringr
50+
stringr,
51+
purrr
5152
Encoding: UTF-8
5253
URL: https://usepa.github.io/StreamCatTools/, https://github.com/USEPA/StreamCatTools
5354
VignetteBuilder: knitr,rmarkdown

NEWS.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# StreamCatTools 0.6.0
2+
13
# StreamCatTools 0.5.0
24
- Overhauled `sc_get_data` and `lc_get_data` functions to be able to pull all metrics from StreamCat or LakeCat for an area of interest
35
- Updated `sc_get_data` and `lc_get_data` to be able to pass hundreds of COMIDs at a time as a function parameter and not error out in call to server

R/lc_get_data.R

Lines changed: 52 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,6 @@
6969
#' aoi='cat,ws', comid='23783629,23794487,23812618',
7070
#' countOnly=TRUE)
7171
#'
72-
#' df <- lc_get_data(comid='23783629', aoi='ws', metric='all')
7372
#'
7473
#' }
7574
#' @export
@@ -87,8 +86,12 @@ lc_get_data <- function(comid = NULL,
8786
# Base API URL.
8887
req <- httr2::request('https://api.epa.gov/StreamCat/lakes/metrics')
8988
# Collapse comids into a single string separated by a comma.
90-
if (!is.null(comid))
89+
if (!is.null(comid)){
9190
comid <- paste(comid, collapse = ",")
91+
if (length(strsplit(comid, ",")[[1]]) > 700){
92+
comids_split <- split(comid, ceiling(seq_along(comid)/700))
93+
}
94+
}
9295
# Force old and odd naming convention to behave correctly
9396
if (!is.null(aoi)){
9497
if (aoi == 'catchment') aoi <- 'cat'
@@ -100,39 +103,68 @@ lc_get_data <- function(comid = NULL,
100103
if (!is.null(conus) & metric=='all'){
101104
stop('If you are requesting all metrics please request for regions, states or counties rather than all of conus')
102105
}
106+
if (metric=='all'){
107+
message("Using metric='all' with a large aoi may take a considerable amount of time to return results - request may timeout if multiple AOIs are requested")
108+
}
103109
metric = tolower(metric)
104110
items = unlist(strsplit(metric,','))
105111
items = gsub(" ","",items)
106112
items = gsub("\n","",items)
107113
params <- sc_get_params(param='metric_names')
108114
if (metric != 'all' & !all(items %in% params)){
109-
stop("One or more of the provided metric names do not match the expected metric names in StreamCat. Use sc_get_params(param='name') to list valid metric names for StreamCat")
115+
message("One or more of the provided metric names do not match the expected metric names in StreamCat. Use sc_get_params(param='name') to list valid metric names for StreamCat")
110116
}
111117
metric = tolower(metric)
112118
items = unlist(strsplit(metric,','))
113119
items = gsub(" ","",items)
114120
items = gsub("\n","",items)
115121
params <- sc_get_params(param='metric_names')
116122
if (metric != 'all' & !all(items %in% params)){
117-
stop("One or more of the provided metric names do not match the expected metric names in StreamCat. Use sc_get_params(param='name') to list valid metric names for StreamCat")
123+
message("One or more of the provided metric names do not match the expected metric names in StreamCat. Use sc_get_params(param='name') to list valid metric names for StreamCat")
118124
}
119-
df <- req |>
120-
httr2::req_method("POST") |>
121-
httr2::req_headers(comid=comid,aoi=aoi,name=metric,showareasqkm=showAreaSqKm,
122-
showpctfull=showPctFull,state=state,county=county,region=region,
123-
conus=conus,countOnly=countOnly) |>
124-
httr2::req_method("POST") |>
125-
httr2::req_throttle(rate = 30 / 60) |>
126-
httr2::req_retry(backoff = ~ 5, max_tries = 3) |>
127-
httr2::req_perform() |>
128-
httr2::resp_body_string() |>
129-
jsonlite::fromJSON()
130-
# Return a data frame
131-
if (is.null(countOnly)){
132-
df <- df$items |>
133-
dplyr::select(comid, dplyr::everything())
125+
if (exists('comid_split')){
126+
create_post_request <- function(comids) {
127+
df <- req |>
128+
httr2::req_method("POST") |>
129+
httr2::req_headers(comid=comids,aoi=aoi,name=metric,showareasqkm=showAreaSqKm,
130+
showpctfull=showPctFull,state=state,county=county,region=region,
131+
conus=conus,countOnly=countOnly) |>
132+
httr2::req_method("POST") |>
133+
httr2::req_throttle(rate = 30 / 60) |>
134+
httr2::req_retry(backoff = ~ 5, max_tries = 3) |>
135+
httr2::req_perform() |>
136+
httr2::resp_body_string() |>
137+
jsonlite::fromJSON()
138+
# Return a data frame
139+
if (is.null(countOnly)){
140+
df <- df$items |>
141+
dplyr::select(comid, dplyr::everything())
142+
return(df)
143+
} else return(df$items)
144+
}
145+
# Create a list of requests using purrr::map()
146+
df <- purrr::map_dfr(comids_split, create_post_request)
134147
return(df)
135-
} else return(df$items)
148+
149+
} else {
150+
df <- req |>
151+
httr2::req_method("POST") |>
152+
httr2::req_headers(comid=comid,aoi=aoi,name=metric,showareasqkm=showAreaSqKm,
153+
showpctfull=showPctFull,state=state,county=county,region=region,
154+
conus=conus,countOnly=countOnly) |>
155+
httr2::req_method("POST") |>
156+
httr2::req_throttle(rate = 30 / 60) |>
157+
httr2::req_retry(backoff = ~ 5, max_tries = 3) |>
158+
httr2::req_perform() |>
159+
httr2::resp_body_string() |>
160+
jsonlite::fromJSON()
161+
# Return a data frame
162+
if (is.null(countOnly)){
163+
df <- df$items |>
164+
dplyr::select(comid, dplyr::everything())
165+
return(df)
166+
} else return(df$items)
167+
}
136168
}
137169

138170
#' @title Get NLCD Data

R/lc_get_params.R

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ lc_get_params <- function(param = NULL) {
8888

8989
lc_fullname <- function(metric = NULL) {
9090
resp <- jsonlite::fromJSON("https://api.epa.gov/StreamCat/lakes/datadictionary")$items
91-
resp <- as.data.frame(resp$dictionary)
92-
result <- unique(resp[resp$metric_prefix %in% unlist(strsplit(metric, split = ',')), 1])
91+
result <- unique(resp$short_display_name[resp$metric_prefix==metric])
9392
return(result)
9493
}

R/sc_get_data.R

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,9 @@ sc_get_data <- function(comid = NULL,
9696
# Collapse comids into a single string separated by a comma.
9797
if (!is.null(comid)){
9898
comid <- paste(comid, collapse = ",")
99+
if (length(strsplit(comid, ",")[[1]]) > 700){
100+
comids_split <- split(comid, ceiling(seq_along(comid)/700))
101+
}
99102
}
100103
# Force old and odd naming convention to behave correctly
101104
if (!is.null(aoi)){
@@ -119,16 +122,41 @@ sc_get_data <- function(comid = NULL,
119122
stop('If you are requesting all metrics please request for regions, states or counties rather than all of conus')
120123
}
121124
if (metric=='all'){
122-
warning("Using metric='all' with a large aoi may take a considerable amount of time to return results - request may timeout if multiple AOIs are requested")
125+
message("Using metric='all' with a large aoi may take a considerable amount of time to return results - request may timeout if multiple AOIs are requested")
123126
}
124127
metric = tolower(metric)
125128
items = unlist(strsplit(metric,','))
126129
items = gsub(" ","",items)
127130
items = gsub("\n","",items)
128131
params <- sc_get_params(param='metric_names')
129132
if (metric != 'all' & !all(items %in% params)){
130-
warning("One or more of the provided metric names do not match the expected metric names in StreamCat. Use sc_get_params(param='name') to list valid metric names for StreamCat")
133+
message("One or more of the provided metric names do not match the expected metric names in StreamCat. Use sc_get_params(param='name') to list valid metric names for StreamCat")
131134
}
135+
if (exists('comid_split')){
136+
create_post_request <- function(comids) {
137+
df <- req |>
138+
httr2::req_method("POST") |>
139+
httr2::req_headers(comid=comids,aoi=aoi,name=metric,showareasqkm=showAreaSqKm,
140+
showpctfull=showPctFull,state=state,county=county,region=region,
141+
conus=conus,countOnly=countOnly) |>
142+
httr2::req_method("POST") |>
143+
httr2::req_throttle(rate = 30 / 60) |>
144+
httr2::req_retry(backoff = ~ 5, max_tries = 3) |>
145+
httr2::req_perform() |>
146+
httr2::resp_body_string() |>
147+
jsonlite::fromJSON()
148+
# Return a data frame
149+
if (is.null(countOnly)){
150+
df <- df$items |>
151+
dplyr::select(comid, dplyr::everything())
152+
return(df)
153+
} else return(df$items)
154+
}
155+
# Create a list of requests using purrr::map()
156+
df <- purrr::map_dfr(comids_split, create_post_request)
157+
return(df)
158+
159+
} else {
132160
df <- req |>
133161
httr2::req_method("POST") |>
134162
httr2::req_headers(comid=comid,aoi=aoi,name=metric,showareasqkm=showAreaSqKm,
@@ -146,6 +174,7 @@ sc_get_data <- function(comid = NULL,
146174
dplyr::select(comid, dplyr::everything())
147175
return(df)
148176
} else return(df$items)
177+
}
149178
}
150179

151180

R/sc_get_params.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,6 @@ sc_get_params <- function(param = NULL) {
8787

8888
sc_fullname <- function(metric = NULL) {
8989
resp <- jsonlite::fromJSON("https://api.epa.gov/StreamCat/streams/datadictionary")$items
90-
result <- unique(resp[resp$metric_prefix %in% unlist(strsplit(metric, split = ',')), 1])
90+
result <- unique(resp$short_display_name[resp$metric_prefix==metric])
9191
return(result)
9292
}

man/lc_get_data.Rd

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

tests/testthat/test-lc_get_params.R

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ test_that("lc_get_params for region parameters", {
1919
test_that("lc_get_params for name parameters", {
2020
params <- lc_get_params(param='metric_names')
2121
expect_true(exists("params"))
22-
expect_equal(length(params),497)
22+
expect_equal(length(params),517)
2323
})
2424

2525
test_that("lc_get_params for state parameters", {

vignettes/LakeCat.Rmd

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -99,13 +99,6 @@ df <- lc_get_data(metric='pctwdwet2006', aoi='ws', county='41003')
9999
knitr::kable(head(df))
100100
```
101101

102-
## Get all metrics for COMIDs or an Area of Interest
103-
We can also get all LakeCat metrics for a set of COMIDs or an area of interest. **Please do not request all metrics for conus in order not to overload requests to the server.**
104-
```{r all}
105-
df <- lc_get_data(comid='23783629', aoi='ws', metric='all')
106-
knitr::kable(head(df))
107-
```
108-
109102
## Get NLCD data
110103

111104
In this example we access National Land Cover Dataset (NLCD) data for 2019, just at the catchment level for several COMIDs using the `lc_nlcd` function. Loads data into a tibble we can view.

0 commit comments

Comments
 (0)