Skip to content

Commit

Permalink
removed arcgislayers from nhd
Browse files Browse the repository at this point in the history
  • Loading branch information
bocinsky committed Dec 10, 2024
1 parent 25b3b2e commit 63c28c8
Show file tree
Hide file tree
Showing 5 changed files with 228 additions and 33 deletions.
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

export("%>%")
export(agol_filter)
export(agol_filter_httr)
export(cdl_colors)
export(check_service)
export(download_data)
Expand Down
18 changes: 16 additions & 2 deletions R/NHD_FUNCTIONS.R
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,20 @@ get_nhd <-
sf::st_union() %>%
sf::st_cast("POLYGON")

geom <-
template %>%
jsonlite::toJSON() %>%
jsonlite::fromJSON(flatten = TRUE) %$%
coordinates %>%
purrr::map(\(x){
x |>
purrr::array_tree() |>
unlist(recursive = FALSE) |>
purrr::map(unlist)
}) %>%
list(rings = .) %>%
jsonlite::toJSON()

if (nhdplus) {
layers <-
c(
Expand All @@ -67,7 +81,7 @@ get_nhd <-
)

nhd_out <-
agol_filter(
agol_filter_httr(
url = "https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer",
layer_name = layers,
geom = template
Expand All @@ -94,7 +108,7 @@ get_nhd <-
)

nhd_out <-
agol_filter(
agol_filter_httr(
url = "https://hydro.nationalmap.gov/arcgis/rest/services/nhd/MapServer",
layer_name = layers,
geom = template
Expand Down
37 changes: 6 additions & 31 deletions R/PADUS_FUNCTIONS.R
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ get_padus <-
httr::modify_url(.,
path = c(.$path, 0, "query"),
query = list(
outFields = "*",
f = "pgeojson",
where =
paste0(
Expand All @@ -126,41 +127,15 @@ get_padus <-
}
)
} else {
geom <-
template %>%
template_to_sf() %>%
sf::st_transform(4326) %>%
sf::st_as_sfc() %>%
sf::st_union() %>%
sf::st_cast("POLYGON") %>%
jsonlite::toJSON() %>%
jsonlite::fromJSON(flatten = TRUE) %$%
coordinates %>%
purrr::map(\(x){
x |>
purrr::array_tree() |>
unlist(recursive = FALSE) |>
purrr::map(unlist)
}) %>%
list(rings = .) %>%
jsonlite::toJSON()

padus_out <-
padus_services[layer] %>%
purrr::map(
function(x) {
file.path(padus_base_url, x, "FeatureServer", 0, "query") %>%
httr::POST(
body =
list(
where = "1=1",
f = "pgeojson",
geometry = geom,
geometryType = "esriGeometryPolygon",
spatialRel = "esriSpatialRelIntersects"
)
) %>%
sf::read_sf()
agol_filter_httr(
url = file.path(padus_base_url, x, "FeatureServer"),
layer_name = 0,
geom = template
)
}
)
}
Expand Down
132 changes: 132 additions & 0 deletions R/UTILITY_FUNCTIONS.R
Original file line number Diff line number Diff line change
Expand Up @@ -537,3 +537,135 @@ agol_filter <-

return(out)
}

#' Scaffolds the common pattern of selecting a layer and filter a geometry from
#' an ArcGIS feature service.
#'
#' This function does **not** use the `arcgislayers` package, which has has had compatibility
#' issues for several commonly used platforms.
#'
#' @param url the url of the remote resource. Must be of length one.
#' @param layer_name the name(s) associated with the layer you want
#' to retrieve. Can be a character vector. If `NULL` (the default),
#' iterates through all layers.
#' @param geom an object of class `bbox`, `sfc` or `sfg` used to filter query
#' results based on a predicate function.
#' @param simplify when only one layer exists, just return the `sf` object or
#' `data.frame`, otherwise return a list of these objects.
#'
#' @return An `sf` object, or a `data.frame`, or a list of these objects if
#' `layer_name == NULL` or if `length(layer_name) > 1`. Missing layers return
#' "NULL".
#' @export
#' @keywords internal
#' @examples
#' \dontrun{
#'
#' # Get a single layer
#' agol_filter_httr(
#' url = "https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer/",
#' layer_name = "WBDHU12",
#' geom = FedData::meve
#' )
#'
#' # Can be returned as a list
#' agol_filter_httr(
#' url = "https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer/",
#' layer_name = "WBDHU12",
#' geom = FedData::meve,
#' simplify = FALSE
#' )
#'
#' # Get a list with all layers
#' agol_filter_httr(
#' url = "https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer/",
#' geom = FedData::meve
#' )
#'
#' # Or include a vector of layer names
#' # Note that missing layers are returned as `NULL` values
#' agol_filter_httr(
#' url = "https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer/",
#' layer_name = c(
#' "NHDPoint",
#' "NetworkNHDFlowline",
#' "NonNetworkNHDFlowline",
#' "NHDLine",
#' "NHDArea",
#' "NHDWaterbody"
#' ),
#' geom = FedData::meve
#' )
#' }
#'
agol_filter_httr <-
function(url, layer_name = NULL, geom, simplify = TRUE) {
geom <-
geom |>
template_to_sf() |>
sf::st_transform(4326) |>
sf::st_as_sfc() |>
sf::st_union() |>
sf::st_cast("POLYGON") |>
jsonlite::toJSON() |>
jsonlite::fromJSON(flatten = TRUE) |>
magrittr::extract2("coordinates") |>
purrr::map(\(x){
x |>
purrr::array_tree() |>
unlist(recursive = FALSE) |>
purrr::map(unlist)
}) |>
list(rings = _) |>
jsonlite::toJSON()

all_layers <-
httr::GET(url,
query = list(f = "pjson")
) |>
httr::content(as = "text") |>
jsonlite::fromJSON() |>
magrittr::extract2("layers") %$%
magrittr::set_names(id, name)

if (is.numeric(layer_name)) {
layers <-
all_layers[which(all_layers %in% layer_name)]
} else if (!is.null(layer_name)) {
layers <- all_layers[layer_name]
} else {
layers <- all_layers
}

out <-
layers |>
purrr::map(
\(x){
tryCatch(
file.path(url, x, "query") %>%
httr::POST(
body =
list(
where = "1=1",
outFields = "*",
f = "geojson",
geometry = geom,
inSR = 4326,
geometryType = "esriGeometryPolygon",
spatialRel = "esriSpatialRelIntersects"
)
) %>%
sf::read_sf(),
error = function(e) {
NULL
}
)
}
)

if (simplify && length(out) == 1) {
return(out[[1]])
}

return(out)
}
73 changes: 73 additions & 0 deletions man/agol_filter_httr.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 63c28c8

Please sign in to comment.