From b517cfd96c730411c2eadbb3478d11736e0c9b8b Mon Sep 17 00:00:00 2001 From: David Blodgett Date: Tue, 17 Dec 2024 10:35:17 -0600 Subject: [PATCH] improve performance of discover nhdplus by calling geoserver directly for #417 --- R/discover_nhdplus.R | 15 +++++++++++++-- R/get_nldi.R | 2 +- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/R/discover_nhdplus.R b/R/discover_nhdplus.R index 52d53c51..0918e9f9 100644 --- a/R/discover_nhdplus.R +++ b/R/discover_nhdplus.R @@ -35,8 +35,19 @@ discover_nhdplus_id <- function(point = NULL, nldi_feature = NULL, raindrop = FA coords = sf::st_coordinates(point) comid <- tryCatch({ - as.integer(dataRetrieval::findNLDI(location = c(X = coords[1], - Y = coords[2]))$origin$identifier) + + URL <- paste0(get("geoserver_root", envir = nhdplusTools_env), + "wmadata", + "/ows", + "?service=WFS&request=GetFeature&version=1.0.0", # axis order switched in 1.1.0 + "&typeName=catchmentsp&outputFormat=application/json", + "&CQL_FILTER=INTERSECTS(the_geom, POINT (", + coords[1], " ", coords[2], "))", + "&propertyName=featureid") + + d <- jsonlite::fromJSON(rawToChar(RETRY("GET", URLencode(URL))$content)) + + as.integer(d$features$properties$featureid) }, error = function(e) NULL) diff --git a/R/get_nldi.R b/R/get_nldi.R index 19221486..b358290e 100644 --- a/R/get_nldi.R +++ b/R/get_nldi.R @@ -291,7 +291,7 @@ query_nldi <- function(query, base_path = "/linked-data", parse_json = TRUE, err message(url) } - req_data <- rawToChar(httr::RETRY("GET", url)$content) + req_data <- rawToChar(httr::RETRY("GET", utils::URLencode(url))$content) if (nchar(req_data) == 0) { NULL