@@ -138,7 +138,8 @@ gSSURGO.Query <- function(mukeys,
138138
139139}
140140
141- SSURGO_API_MAX_AREA_M2 <- 10100000000
141+ # ' Maximum area for SSURGO API requests
142+ SSURGO_API_MAX_AREA_M2 <- 10100000000 # nolint: object_name_linter
142143
143144# ' Get map unit keys (mukeys) from gSSURGO
144145# '
@@ -192,9 +193,16 @@ ssurgo_mukeys_bbox <- function(bbox) {
192193
193194 wgs84_crs <- sf :: st_crs(4326 )
194195
195- bbox_poly <- sf :: st_polygon(list (
196- matrix (c(xmin , ymin , xmax , ymin , xmax , ymax , xmin , ymax , xmin , ymin ), ncol = 2 , byrow = TRUE )
197- ))
196+ # Calculate the area of the bbox to make sure that it's smaller than the
197+ # SSURGO limit (`SSURGO_API_MAX_AREA_M2`).
198+ bbox_matrix <- rbind(
199+ c(xmin , ymin ),
200+ c(xmax , ymin ),
201+ c(xmax , ymax ),
202+ c(xmin , ymax ),
203+ c(xmin , ymin )
204+ )
205+ bbox_poly <- sf :: st_polygon(list (bbox_matrix ))
198206 bbox_sf <- sf :: st_sfc(bbox_poly , crs = wgs84_crs )
199207 area <- as.numeric(sf :: st_area(bbox_sf ))
200208
@@ -225,23 +233,7 @@ ssurgo_mukeys_bbox <- function(bbox) {
225233
226234 httr2 :: resp_check_status(resp )
227235
228- resp_text <- httr2 :: resp_body_string(resp )
229-
230- resp_xml <- XML :: xmlParse(resp_text )
231-
232- mukey_nodes <- XML :: getNodeSet(resp_xml , " //MapUnitKeyList" )
233-
234- if (length(mukey_nodes ) == 0 ) {
235- return (character (0 ))
236- }
237-
238- mukey_str <- XML :: xmlValue(mukey_nodes [[1 ]])
239-
240- if (is.null(mukey_str ) || nchar(trimws(mukey_str )) == 0 ) {
241- return (character (0 ))
242- }
243-
244- mukeys <- unique(strsplit(trimws(mukey_str ), " ," )[[1 ]])
236+ mukeys <- unique(parse_mukey_response(resp ))
245237
246238 mukeys
247239}
@@ -300,23 +292,7 @@ ssurgo_mukeys_point <- function(point, distance) {
300292
301293 httr2 :: resp_check_status(resp )
302294
303- resp_text <- httr2 :: resp_body_string(resp )
304-
305- resp_xml <- XML :: xmlParse(resp_text )
306-
307- mukey_nodes <- XML :: getNodeSet(resp_xml , " //MapUnitKeyList" )
308-
309- if (length(mukey_nodes ) == 0 ) {
310- return (character (0 ))
311- }
312-
313- mukey_str <- XML :: xmlValue(mukey_nodes [[1 ]])
314-
315- if (is.null(mukey_str ) || nchar(trimws(mukey_str )) == 0 ) {
316- return (character (0 ))
317- }
318-
319- mukeys <- unique(strsplit(trimws(mukey_str ), " ," )[[1 ]])
295+ mukeys <- unique(parse_mukey_response(resp ))
320296
321297 mukeys
322298}
@@ -339,16 +315,21 @@ ssurgo_mukeys_bigbbox <- function(bbox) {
339315
340316 wgs84_crs <- sf :: st_crs(4326 )
341317
342- bbox_poly <- sf :: st_polygon(list (
343- matrix (c(xmin , ymin , xmax , ymin , xmax , ymax , xmin , ymax , xmin , ymin ), ncol = 2 , byrow = TRUE )
344- ))
318+ # Get the total bbox area.
319+ bbox_matrix <- rbind(
320+ c(xmin , ymin ),
321+ c(xmax , ymin ),
322+ c(xmax , ymax ),
323+ c(xmin , ymax ),
324+ c(xmin , ymin )
325+ )
326+ bbox_poly <- sf :: st_polygon(list (bbox_matrix ))
345327 bbox_sf <- sf :: st_sfc(bbox_poly , crs = wgs84_crs )
346328
347329 bbox_area <- as.numeric(sf :: st_area(bbox_sf ))
348330
349- bbox_wgs84_box <- sf :: st_bbox(bbox_sf )
350- width_deg <- bbox_wgs84_box [" xmax" ] - bbox_wgs84_box [" xmin" ]
351- height_deg <- bbox_wgs84_box [" ymax" ] - bbox_wgs84_box [" ymin" ]
331+ width_deg <- xmax - xmin
332+ height_deg <- ymax - ymin
352333
353334 aspect_ratio <- width_deg / height_deg
354335
@@ -388,7 +369,11 @@ ssurgo_mukeys_bigbbox <- function(bbox) {
388369 httr2 :: req_url_query(!!! query )
389370 })
390371
391- reqs_throttled <- purrr :: map(reqs , ~ .x | > httr2 :: req_throttle(10 / 60 ))
372+ reqs_throttled <- reqs | >
373+ # max 10 tries per minute
374+ purrr :: map(httr2 :: req_throttle , capacity = 10 ) | >
375+ # keep trying for 2 minutes before giving up
376+ purrr :: map(httr2 :: req_retry , max_seconds = 120 )
392377
393378 resps <- httr2 :: req_perform_parallel(
394379 reqs_throttled ,
@@ -400,17 +385,7 @@ ssurgo_mukeys_bigbbox <- function(bbox) {
400385 parse_mukeys <- function (resp ) {
401386 if (inherits(resp , " httr2_response" )) {
402387 tryCatch({
403- resp_text <- httr2 :: resp_body_string(resp )
404- resp_xml <- XML :: xmlParse(resp_text )
405- mukey_nodes <- XML :: getNodeSet(resp_xml , " //MapUnitKeyList" )
406- if (length(mukey_nodes ) == 0 ) {
407- return (character (0 ))
408- }
409- mukey_str <- XML :: xmlValue(mukey_nodes [[1 ]])
410- if (is.null(mukey_str ) || nchar(trimws(mukey_str )) == 0 ) {
411- return (character (0 ))
412- }
413- strsplit(trimws(mukey_str ), " ," )[[1 ]]
388+ parse_mukey_response(resp )
414389 }, error = function (e ) {
415390 character (0 )
416391 })
@@ -423,3 +398,21 @@ ssurgo_mukeys_bigbbox <- function(bbox) {
423398
424399 unique(unlist(mukeys_list , use.names = FALSE ))
425400}
401+
402+ # ' Parse responses from the mukey WFS service
403+ # '
404+ # ' @params resp `httr2` response object from SSURGO mukey WFS API
405+ # ' @return character vector of mukeys
406+ parse_mukey_response <- function (resp ) {
407+ resp_text <- httr2 :: resp_body_string(resp )
408+ resp_xml <- XML :: xmlParse(resp_text )
409+ mukey_nodes <- XML :: getNodeSet(resp_xml , " //MapUnitKeyList" )
410+ if (length(mukey_nodes ) == 0 ) {
411+ return (character (0 ))
412+ }
413+ mukey_str <- XML :: xmlValue(mukey_nodes [[1 ]])
414+ if (is.null(mukey_str ) || nchar(trimws(mukey_str )) == 0 ) {
415+ return (character (0 ))
416+ }
417+ strsplit(trimws(mukey_str ), " ," )[[1 ]]
418+ }
0 commit comments