-
Notifications
You must be signed in to change notification settings - Fork 32
/
get_nldi.R
243 lines (216 loc) · 7.76 KB
/
get_nldi.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
#' @title Discover NLDI Sources
#' @description Function to retrieve available feature
#' and data sources from the Network Linked Data Index.
#' @param tier character optional "prod" or "test"
#' @return data.frame with three columns "source", "sourceName"
#' and "features"
#' @export
#' @examples
#' \donttest{
#' discover_nldi_sources()
#' }
discover_nldi_sources <- function(tier = "prod") {
return(query_nldi(query = "", tier))
}
#' @title Discover NLDI Navigation Options
#' @description Discover available navigation options for a
#' given feature source and id.
#' @param nldi_feature length 2 list list with optionsal names `featureSource`
#' and `featureID` where `featureSource` is derived from the "source" column of
#' the response of discover_nldi_sources() and the `featureSource` is a known identifier
#' from the specified `featureSource`. e.g. list("nwissite", "USGS-08279500")
#' @param tier character optional "prod" or "test"
#' @return data.frame with three columns "source", "sourceName" and "features"
#' @export
#' @examples
#' \donttest{
#' discover_nldi_sources()
#'
#' nldi_nwis <- list(featureSource = "nwissite", featureID = "USGS-08279500")
#'
#' discover_nldi_navigation(nldi_nwis)
#'
#' discover_nldi_navigation("nwissite", "USGS-08279500")
#' }
discover_nldi_navigation <- function(nldi_feature, tier = "prod") {
nldi_feature <- check_nldi_feature(nldi_feature)
query <- paste(nldi_feature[["featureSource"]],
nldi_feature[["featureID"]],
"navigate", sep = "/")
query_nldi(query, tier)
}
#' @title Navigate NLDI
#' @description Navigate the Network Linked Data Index network.
#' @param nldi_feature list with names `featureSource` and `featureID` where
#' `featureSource` is derived from the "source" column of the response of
#' discover_nldi_sources() and the `featureSource` is a known identifier
#' from the specified `featureSource`.
#' @param mode character chosen from names, URLs, or url parameters
#' returned by discover_nldi_navigation(nldi_feature). See examples.
#' @param data_source character chosen from "source" column of the response
#' of discover_nldi_sources() or empty string for flowline geometry.
#' @param distance_km numeric distance in km to stop navigating.
#' @param tier character optional "prod" or "test"
#' @return sf data.frame with result
#' @export
#' @importFrom utils tail
#' @examples
#' \donttest{
#' library(sf)
#' library(dplyr)
#'
#' nldi_nwis <- list(featureSource = "nwissite", featureID = "USGS-05428500")
#'
#' navigate_nldi(nldi_feature = nldi_nwis,
#' mode = "upstreamTributaries",
#' data_source = "") %>%
#' st_geometry() %>%
#' plot()
#'
#' navigate_nldi(nldi_feature = nldi_nwis,
#' mode = "UM",
#' data_source = "") %>%
#' st_geometry() %>%
#' plot(col = "blue", add = TRUE)
#'
#'
#'
#' nwissite <- navigate_nldi(nldi_feature = nldi_nwis,
#' mode = "UT",
#' data_source = "nwissite")
#'
#' st_geometry(nwissite) %>%
#' plot(col = "green", add = TRUE)
#'
#' nwissite
#' }
#'
navigate_nldi <- function(nldi_feature, mode = "upstreamMain",
data_source = "flowline", distance_km = NULL,
tier = "prod") {
nldi_feature <- check_nldi_feature(nldi_feature)
nav_lookup <- list(upstreamMain = "UM",
upstreamTributaries = "UT",
downstreamMain = "DM",
downstreamDiversions = "DD")
if (nchar(mode) > 2) {
if (nchar(mode) < 30) {
mode <- nav_lookup[[mode]]
} else {
mode <- tail(unlist(strsplit(mode, "/")), n = 1)
}
}
if(data_source == "flowline") data_source <- ""
query <- paste(nldi_feature[["featureSource"]],
nldi_feature[["featureID"]],
"navigate", mode, data_source,
sep = "/")
if (!is.null(distance_km)) {
query <- paste0(query, "?distance=", distance_km)
}
out <- query_nldi(query, tier = tier, parse_json = FALSE)
if(!is.null(out)) {
return(sf::read_sf(out))
}
return(dplyr::tibble())
}
#' @title Get NLDI Basin Boundary
#' @description Get a basin boundary for a given NLDI feature.
#' @details Only resolves to the nearest NHDPlus catchment divide. See:
#' https://owi.usgs.gov/blog/nldi-intro/ for more info on the nldi.
#' @param nldi_feature list with names `featureSource` and `featureID` where
#' `featureSource` is derived from the "source" column of the response of
#' discover_nldi_sources() and the `featureSource` is a known identifier
#' from the specified `featureSource`.
#' @param tier character optional "prod" or "test"
#' @return sf data.frame with result basin boundary
#' @export
#' @examples
#' \donttest{
#' library(sf)
#' library(dplyr)
#'
#' nldi_nwis <- list(featureSource = "nwissite", featureID = "USGS-05428500")
#'
#' basin <- get_nldi_basin(nldi_feature = nldi_nwis)
#'
#' basin %>%
#' st_geometry() %>%
#' plot()
#'
#' basin
#' }
get_nldi_basin <- function(nldi_feature,
tier = "prod") {
nldi_feature <- check_nldi_feature(nldi_feature)
query <- paste(nldi_feature[["featureSource"]],
nldi_feature[["featureID"]],
"basin",
sep = "/")
return(sf::read_sf(query_nldi(query, tier = tier, parse_json = FALSE)))
}
#' @title Get NLDI Feature
#' @description Get a single feature from the NLDI
#' @param nldi_feature list with names `featureSource` and `featureID` where
#' `featureSource` is derived from the "source" column of the response of
#' discover_nldi_sources() and the `featureSource` is a known identifier
#' from the specified `featureSource`.
#' @param tier character optional "prod" or "test"
#' @return sf feature collection with one feature
#' @examples
#' get_nldi_feature(list("featureSource" = "nwissite", featureID = "USGS-05428500"))
#' @export
get_nldi_feature <- function(nldi_feature, tier = "prod") {
nldi_feature <- check_nldi_feature(nldi_feature)
return(sf::read_sf(query_nldi(paste(nldi_feature[["featureSource"]],
nldi_feature[["featureID"]],
sep = "/"),
tier, parse_json = FALSE)))
}
#' @importFrom httr GET
#' @importFrom jsonlite fromJSON
#' @noRd
query_nldi <- function(query, tier = "prod", parse_json = TRUE) {
nldi_base_url <- get_nldi_url(tier)
url <- paste(nldi_base_url, query,
sep = "/")
req_data <- rawToChar(httr::RETRY("GET", url, times = 10, pause_cap = 240)$content)
if (nchar(req_data) == 0) {
NULL
} else {
if (parse_json) {
tryCatch(jsonlite::fromJSON(req_data, simplifyVector = TRUE),
error = function(e) {
message("Something went wrong accessing the NLDI.\n", e)
}, warning = function(w) {
message("Something went wrong accessing the NLDI.\n", w)
})
} else {
req_data
}
}
}
#' @noRd
get_nldi_url <- function(tier = "prod") {
if (tier == "prod") {
"https://cida.usgs.gov/nldi"
} else if (tier == "test") {
"https://cida-test.er.usgs.gov/nldi"
} else {
stop("only prod or test allowed.")
}
}
#' @noRd
check_nldi_feature <- function(nldi_feature) {
expect_names <- c("featureSource", "featureID")
if (!all(expect_names %in% names(nldi_feature))) {
if(length(nldi_feature) != 2 | !all(sapply(nldi_feature, is.character)))
stop(paste0("Missing some required input for NLDI. ",
"Expected length 2 character fector with optional names: ",
paste(expect_names[which(!(expect_names %in%
names(nldi_feature)))],
collapse = ", ")))
}
names(nldi_feature) <- expect_names
return(nldi_feature[expect_names])
}