Skip to content

Commit f111478

Browse files
Merge pull request #843 from ldecicco-USGS/commas
Commas
2 parents 619785e + 2b3bd4b commit f111478

19 files changed

+138
-95
lines changed

R/construct_api_requests.R

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,31 +45,41 @@ construct_api_requests <- function(service,
4545

4646
POST <- FALSE
4747

48-
single_params <- c("datetime", "last_modified", "begin", "end", "time", "limit")
48+
single_params <- c("datetime", "last_modified",
49+
"begin", "end", "time", "limit",
50+
"begin_utc", "end_utc")
51+
comma_params <- c("monitoring_location_id", "parameter_code",
52+
"statistic_id", "time_series_id",
53+
"computation_period_identifier",
54+
"computation_identifier")
55+
56+
if(service %in% c("monitoring-locations", #"parameter-codes",
57+
"time-series-metadata")){
58+
comma_params <- c(comma_params, "id")
59+
}
4960

5061
full_list <- list(...)
5162

5263
if(all(is.na(full_list)) & all(is.na(bbox))){
5364
warning("No filtering arguments specified.")
5465
}
55-
56-
# GET list refers to arguments that will go in the URL no matter what (not POST)
57-
get_list <- full_list[names(full_list) %in% single_params]
66+
# Figure out if the GET request will be > 2048 characters
67+
comma_params_filtered <- Filter(Negate(anyNA), full_list[comma_params])
5868

59-
get_list[["skipGeometry"]] <- skipGeometry
60-
61-
#POST list are the arguments that need to be in the POST body
62-
post_list <- full_list[!names(full_list) %in% single_params]
63-
64-
post_params <- explode_post(post_list)
69+
force_post <- nchar(paste0(unlist(comma_params_filtered), collapse = ",")) > 2048
6570

66-
if(length(post_params) > 0){
67-
POST = TRUE
71+
if(force_post){
72+
get_list <- full_list[names(full_list) %in% c(single_params)]
73+
} else {
74+
# GET list refers to arguments that will go in the URL no matter what (not POST)
75+
get_list <- full_list[names(full_list) %in% c(single_params, comma_params)]
6876
}
77+
78+
get_list[["skipGeometry"]] <- skipGeometry
6979

7080
get_list <- get_list[!is.na(get_list)]
7181

72-
time_periods <- c("last_modified", "datetime", "time", "begin", "end")
82+
time_periods <- c("last_modified", "datetime", "time", "begin", "end", "begin_utc", "end_utc")
7383
if(any(time_periods %in% names(get_list))){
7484

7585
for(i in time_periods[time_periods %in% names(get_list)]){
@@ -85,7 +95,7 @@ construct_api_requests <- function(service,
8595
format_type <- ifelse(isTRUE(no_paging), "csv", "json")
8696

8797
baseURL <- setup_api(service, format = format_type)
88-
baseURL <- explode_query(baseURL, POST = FALSE, get_list)
98+
baseURL <- explode_query(baseURL, POST = FALSE, get_list, multi = "comma")
8999

90100
if(all(!is.na(bbox))){
91101
baseURL <- httr2::req_url_query(baseURL,
@@ -111,7 +121,15 @@ construct_api_requests <- function(service,
111121
.multi = "comma")
112122
}
113123

114-
if(POST){
124+
#POST list are the arguments that need to be in the POST body
125+
post_list <- full_list[!names(full_list) %in% names(get_list)]
126+
127+
post_params <- explode_post(post_list)
128+
129+
# Should we do a POST?
130+
POST = length(post_params) > 0
131+
132+
if(POST){
115133
baseURL <- baseURL |>
116134
httr2::req_headers(`Content-Type` = "application/query-cql-json")
117135

@@ -126,7 +144,7 @@ construct_api_requests <- function(service,
126144
baseURL <- httr2::req_body_raw(baseURL, x)
127145

128146
} else {
129-
baseURL <- explode_query(baseURL, POST = FALSE, full_list)
147+
baseURL <- explode_query(baseURL, POST = FALSE, full_list, multi = "comma")
130148
}
131149

132150
return(baseURL)

R/get_ogc_data.R

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,7 @@ get_ogc_data <- function(args,
2525

2626
req <- do.call(construct_api_requests, args)
2727

28-
if("no_paging" %in% names(args)){
29-
no_paging <- args[["no_paging"]]
30-
args[["no_paging"]] <- NULL
31-
} else {
32-
no_paging <- FALSE
33-
}
28+
no_paging <- grepl("f=csv", req$url)
3429

3530
message("Requesting:\n", req$url)
3631

@@ -68,7 +63,7 @@ get_ogc_data <- function(args,
6863

6964
attr(return_list, "request") <- req
7065
attr(return_list, "queryTime") <- Sys.time()
71-
return_list
66+
return(return_list)
7267
}
7368

7469
order_results <- function(df){

R/read_waterdata_continuous.R

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
#' for new direct download functions that are expected to be available sometime
99
#' in 2026.
1010
#'
11-
#' Geometry output is not supported in the continuous data API
11+
#' Geometry output is not supported in the continuous data API endpoint.
1212
#'
1313
#' @export
1414
#' @param monitoring_location_id `r get_params("continuous")$monitoring_location_id`
15+
#' Multiple monitoring_location_ids can be requested as a character vector.
1516
#' @param parameter_code `r get_params("continuous")$parameter_code`
17+
#' Multiple parameter_codes can be requested as a character vector.
1618
#' @param time `r get_params("continuous")$time`.
1719
#' You can also use a vector of length 2: the first value being the starting date,
1820
#' the second value being the ending date. NA's within the vector indicate a
@@ -23,10 +25,8 @@
2325
#' @param approval_status `r get_params("continuous")$approval_status`
2426
#' @param last_modified `r get_params("continuous")$last_modified`
2527
#' @param time_series_id `r get_params("continuous")$time_series_id`
28+
#' Multiple time_series_ids can be requested as a character vector.
2629
#' @param qualifier `r get_params("continuous")$qualifier`
27-
#' @param statistic_id `r get_params("continuous")$statistic_id`. Note that
28-
#' for continuous data, the statistic_id is almost universally 00011.
29-
#' Requesting anything else will most-likely cause a timeout.
3030
#' @param properties A vector of requested columns to be returned from the query.
3131
#' Available options are:
3232
#' `r dataRetrieval:::get_properties_for_docs("continuous", "continuous_id")`.
@@ -73,7 +73,6 @@ read_waterdata_continuous <- function(monitoring_location_id = NA_character_,
7373
approval_status = NA_character_,
7474
unit_of_measure = NA_character_,
7575
qualifier = NA_character_,
76-
statistic_id = NA_character_,
7776
value = NA,
7877
last_modified = NA_character_,
7978
time = NA_character_,
@@ -87,10 +86,6 @@ read_waterdata_continuous <- function(monitoring_location_id = NA_character_,
8786
args <- mget(names(formals()))
8887
args[["skipGeometry"]] <- TRUE
8988

90-
if(!is.na(statistic_id) & !all(statistic_id == "00011")){
91-
warning("With few if any exceptions, statistic_id is always 00011 for continuous data, and requesting other statistic ids will likely return no data.")
92-
}
93-
9489
return_list <- get_ogc_data(args,
9590
output_id,
9691
service)

R/read_waterdata_daily.R

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
#'
55
#' @export
66
#' @param monitoring_location_id `r get_params("daily")$monitoring_location_id`
7+
#' Multiple monitoring_location_ids can be requested as a character vector.
78
#' @param parameter_code `r get_params("daily")$parameter_code`
9+
#' Multiple parameter_codes can be requested as a character vector.
810
#' @param statistic_id `r get_params("daily")$statistic_id`
11+
#' Multiple statistic_ids can be requested as a character vector.
912
#' @param time `r get_params("daily")$time`
1013
#' You can also use a vector of length 2: the first value being the starting date,
1114
#' the second value being the ending date. NA's within the vector indicate a
@@ -16,6 +19,7 @@
1619
#' @param approval_status `r get_params("daily")$approval_status`
1720
#' @param last_modified `r get_params("daily")$last_modified`
1821
#' @param time_series_id `r get_params("daily")$time_series_id`
22+
#' Multiple time_series_ids can be requested as a character vector.
1923
#' @param qualifier `r get_params("daily")$qualifier`
2024
#' @param properties A vector of requested columns to be returned from the query.
2125
#' Available options are:
@@ -75,6 +79,9 @@
7579
#' dv_data_quick <- read_waterdata_daily(monitoring_location_id = site,
7680
#' parameter_code = "00060",
7781
#' no_paging = TRUE)
82+
#'
83+
#' dv_post <- read_waterdata_daily(monitoring_location_id = site,
84+
#' approval_status = c("Approved", "Provisional"))
7885
#'
7986
#' }
8087
read_waterdata_daily <- function(monitoring_location_id = NA_character_,

R/read_waterdata_field_measurements.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#'
55
#' @export
66
#' @param monitoring_location_id `r get_params("field-measurements")$monitoring_location_id`
7+
#' Multiple monitoring_location_ids can be requested as a character vector.
78
#' @param parameter_code `r get_params("field-measurements")$parameter_code`
9+
#' Multiple parameter_codes can be requested as a character vector.
810
#' @param observing_procedure_code `r get_params("field-measurements")$observing_procedure_code`
911
#' @param time `r get_params("field-measurements")$time`
1012
#' You can also use a vector of length 2: the first value being the starting date,

R/read_waterdata_latest_continuous.R

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,9 @@
44
#'
55
#' @export
66
#' @param monitoring_location_id `r get_params("latest-continuous")$monitoring_location_id`
7+
#' Multiple monitoring_location_ids can be requested as a character vector.
78
#' @param parameter_code `r get_params("latest-continuous")$parameter_code`
9+
#' Multiple parameter_codes can be requested as a character vector.
810
#' @param time `r get_params("latest-continuous")$time`
911
#' You can also use a vector of length 2: the first value being the starting date,
1012
#' the second value being the ending date. NA's within the vector indicate a
@@ -15,10 +17,8 @@
1517
#' @param approval_status `r get_params("latest-continuous")$approval_status`
1618
#' @param last_modified `r get_params("latest-continuous")$last_modified`
1719
#' @param time_series_id `r get_params("latest-continuous")$time_series_id`
20+
#' Multiple time_series_ids can be requested as a character vector.
1821
#' @param qualifier `r get_params("latest-continuous")$qualifier`
19-
#' @param statistic_id `r get_params("latest-continuous")$statistic_id`. Note that
20-
#' for continuous data, the statistic_id is almost universally 00011.
21-
#' Requesting anything else will most-likely cause a timeout.
2222
#' @param properties A vector of requested columns to be returned from the query.
2323
#' Available options are:
2424
#' `r dataRetrieval:::get_properties_for_docs("latest-continuous", "latest_continuous_id")`.
@@ -79,7 +79,6 @@
7979
#' }
8080
read_waterdata_latest_continuous <- function(monitoring_location_id = NA_character_,
8181
parameter_code = NA_character_,
82-
statistic_id = NA_character_,
8382
properties = NA_character_,
8483
time_series_id = NA_character_,
8584
approval_status = NA_character_,

R/read_waterdata_latest_daily.R

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@
44
#'
55
#' @export
66
#' @param monitoring_location_id `r get_params("latest-daily")$monitoring_location_id`
7+
#' Multiple monitoring_location_ids can be requested as a character vector.
78
#' @param parameter_code `r get_params("latest-daily")$parameter_code`
9+
#' Multiple parameter_codes can be requested as a character vector.
810
#' @param statistic_id `r get_params("latest-daily")$statistic_id`
11+
#' Multiple statistic_ids can be requested as a character vector.
912
#' @param time `r get_params("latest-daily")$time`
1013
#' You can also use a vector of length 2: the first value being the starting date,
1114
#' the second value being the ending date. NA's within the vector indicate a
@@ -16,6 +19,7 @@
1619
#' @param approval_status `r get_params("latest-daily")$approval_status`
1720
#' @param last_modified `r get_params("latest-daily")$last_modified`
1821
#' @param time_series_id `r get_params("latest-daily")$time_series_id`
22+
#' Multiple time_series_ids can be requested as a character vector.
1923
#' @param qualifier `r get_params("latest-daily")$qualifier`
2024
#' @param properties A vector of requested columns to be returned from the query.
2125
#' Available options are:

R/read_waterdata_monitoring_location.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#'
55
#' @export
66
#' @param monitoring_location_id `r get_params("monitoring-locations")$id`
7+
#' Multiple monitoring_location_ids can be requested as a character vector.
78
#' @param agency_code `r get_params("monitoring-locations")$agency_code`
89
#' @param agency_name `r get_params("monitoring-locations")$agency_name`
910
#' @param monitoring_location_number `r get_params("monitoring-locations")$monitoring_location_number`

R/read_waterdata_parameter_codes.R

Lines changed: 4 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -65,26 +65,10 @@ read_waterdata_parameter_codes <- function(parameter_code = NA_character_,
6565
args[["skipGeometry"]] <- TRUE
6666
args[["bbox"]] <- NA
6767
args[["no_paging"]] <- FALSE # drops id if TRUE
68-
69-
if(all(lengths(args) == 1)){
70-
return_list <- suppressWarnings(get_ogc_data(args = args,
71-
output_id = output_id,
72-
service = service))
73-
} else {
74-
75-
message("Current API functionality requires pulling the full parameter-codes list.
76-
It is expected that updates to the API will eliminate this need.")
77-
78-
return_list <- read_waterdata_metadata(collection = service,
79-
limit = limit)
80-
args[["convertType"]] <- NULL
81-
args[["skipGeometry"]] <- NULL
82-
args[["no_paging"]] <- NULL
83-
args_to_filter <- args[!is.na(args)]
84-
for(param in names(args_to_filter)){
85-
return_list <- return_list[return_list[[param]] %in% args_to_filter[[param]],]
86-
}
87-
}
68+
69+
return_list <- get_ogc_data(args = args,
70+
output_id = output_id,
71+
service = service)
8872

8973
return(return_list)
9074
}

R/read_waterdata_samples.R

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -293,7 +293,7 @@ check_profile <- function(dataProfile, profile_convert){
293293
return(dataProfile)
294294
}
295295

296-
explode_query <- function(baseURL, POST = FALSE, x){
296+
explode_query <- function(baseURL, POST = FALSE, x, multi = "explode"){
297297

298298
if(!is.list(x)){
299299
return(baseURL)
@@ -307,7 +307,7 @@ explode_query <- function(baseURL, POST = FALSE, x){
307307
} else {
308308
baseURL <- httr2::req_url_query(baseURL,
309309
!!!x,
310-
.multi = "explode")
310+
.multi = multi)
311311
}
312312

313313
}

0 commit comments

Comments
 (0)