Skip to content

Commit

Permalink
removed dependency on arcgislayers
Browse files Browse the repository at this point in the history
  • Loading branch information
bocinsky committed Dec 10, 2024
1 parent 920d0de commit 4b08a75
Show file tree
Hide file tree
Showing 13 changed files with 343 additions and 162 deletions.
4 changes: 2 additions & 2 deletions CRAN-SUBMISSION
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
Version: 4.1.0
Date: 2024-11-05 14:41:13 UTC
SHA: 2a7f2d8290a3e91ac9ffbf8953d904dcc080e50a
Date: 2024-11-07 23:20:16 UTC
SHA: 920d0de8687f8918c9c2e9566973ae8f1b649a4e
9 changes: 5 additions & 4 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ Type: Package
Package: FedData
Title: Download Geospatial Data Available from
Several Federated Data Sources
Version: 4.1.0
Date: 2024-11-06
Version: 4.2.0
Date: 2024-12-10
Authors@R:
c(person(given = c("R.", "Kyle"),
family = "Bocinsky",
Expand Down Expand Up @@ -65,17 +65,18 @@ Imports:
readr,
terra (>= 1.0),
sf (>= 1.0),
arcgislayers (>= 0.2.0),
ggplot2,
glue,
magrittr
magrittr,
jsonlite
Encoding: UTF-8
LazyData: true
NeedsCompilation: no
Repository: CRAN
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.3.2
Suggests:
arcgislayers (>= 0.2.0),
knitr,
leaflet,
mapview,
Expand Down
1 change: 1 addition & 0 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# Generated by roxygen2: do not edit by hand

export("%>%")
export(agol_filter)
export(cdl_colors)
export(check_service)
export(download_data)
Expand Down
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
# FedData (development version)

# FedData (4.2.0)
- Removed `arcgislayers` dependency, as we weren't using it much anyway. Re-Closes [Issue #109](https://github.com/ropensci/FedData/issues/109).

# FedData (4.1.0)
- Removed {styler} and several other packages from the Suggests field in the DESCRIPTION file.
- Moved {ggplot2} and {ncdf4} to the Suggests field.
Expand Down
65 changes: 18 additions & 47 deletions R/NHD_FUNCTIONS.R
Original file line number Diff line number Diff line change
Expand Up @@ -67,24 +67,11 @@ get_nhd <-
)

nhd_out <-
"https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer" %>%
arcgislayers::arc_open() %>%
arcgislayers::get_layers(
name = layers
) %>%
purrr::map(
~ tryCatch(
arcgislayers::arc_select(
.x,
filter_geom =
template
),
error = function(e) {
NULL
}
)
) %>%
magrittr::set_names(layers) %$%
agol_filter(
url = "https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer",
layer_name = layers,
geom = template
) %$%
list(
Point = NHDPoint,
Flowline = list(
Expand All @@ -107,24 +94,11 @@ get_nhd <-
)

nhd_out <-
"https://hydro.nationalmap.gov/arcgis/rest/services/nhd/MapServer" %>%
arcgislayers::arc_open() %>%
arcgislayers::get_layers(
name = layers
) %>%
purrr::map(
~ tryCatch(
arcgislayers::arc_select(
.x,
filter_geom =
template
),
error = function(e) {
NULL
}
)
) %>%
magrittr::set_names(layers) %$%
agol_filter(
url = "https://hydro.nationalmap.gov/arcgis/rest/services/nhd/MapServer",
layer_name = layers,
geom = template
) %$%
list(
Point = Point,
Flowline = `Flowline - Large Scale`,
Expand Down Expand Up @@ -255,17 +229,14 @@ get_wbd <- function(template,
return(read_sf_all(out_dsn))
}

"https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer/" %>%
arcgislayers::arc_open() %>%
arcgislayers::get_layer(
name = "WBDHU12"
) %>%
arcgislayers::arc_select(
filter_geom =
template %>%
template_to_sf() %>%
sf::st_as_sfc()
) %>%
agol_filter(
url = "https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer/",
layer_name = "WBDHU12",
geom =
template |>
template_to_sf() |>
sf::st_as_sfc()
) |>
sf::write_sf(dsn = out_dsn)

return(
Expand Down
61 changes: 41 additions & 20 deletions R/PADUS_FUNCTIONS.R
Original file line number Diff line number Diff line change
Expand Up @@ -108,38 +108,59 @@ get_padus <-
padus_services[layer] %>%
purrr::map(
function(x) {
file.path(padus_base_url, x, "FeatureServer/") %>%
arcgislayers::arc_open() %>%
arcgislayers::get_layer(id = 0) %>%
arcgislayers::arc_select(
where =
paste0(
"Unit_Nm IN (",
paste(paste0("'", template, "'"), collapse = ","),
")"
)
)
file.path(padus_base_url, x, "FeatureServer") %>%
httr::parse_url() %>%
httr::modify_url(.,
path = c(.$path, 0, "query"),
query = list(
f = "pgeojson",
where =
paste0(
"Unit_Nm IN (",
paste(paste0("'", template, "'"), collapse = ","),
")"
)
)
) %>%
sf::read_sf()
}
)
} else {
template %<>%
geom <-
template %>%
template_to_sf() %>%
sf::st_transform(4326) %>%
sf::st_as_sfc() %>%
sf::st_union() %>%
sf::st_cast("POLYGON")
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/") %>%
arcgislayers::arc_open() %>%
arcgislayers::get_layer(id = 0) %>%
arcgislayers::arc_select(
filter_geom =
template
)
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()
}
)
}
Expand Down
143 changes: 142 additions & 1 deletion R/UTILITY_FUNCTIONS.R
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ if (getRversion() >= "2.15.1") {
"STATION",
"DAY",
"DATE",
"outfile"
"outfile",
"coordinates"
))
}

Expand Down Expand Up @@ -396,3 +397,143 @@ compare_rast_dims <-

all(x_dims == y_dims)
}

#' Scaffolds the common pattern of selecting a layer and filter a geometry from
#' an ArcGIS feature service.
#'
#' This function uses the arcgislayers package, which has has had compatibility
#' issues for several commonly used platforms. It is mainly here for historical
#' reasons.
#'
#' @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(
#' 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(
#' 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(
#' 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(
#' 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 <-
function(url, layer_name = NULL, geom, simplify = TRUE) {
if (!requireNamespace("arcgislayers", quietly = TRUE)) {
stop("Package \"arcgislayers\" needed for this function to work. Please install it.",
call. = FALSE
)
}

geom <-
geom |>
template_to_sf() |>
sf::st_as_sfc()

service <-
url |>
arcgislayers::arc_open()

if (is.numeric(layer_name)) {
all_layers <-
arcgislayers::list_items(service)

layer_name <-
all_layers$name[which(all_layers$id == layer_name)]
}

if (is.null(layer_name)) {
layers <-
service |>
arcgislayers::get_all_layers() |>
magrittr::extract2("layers")

layer_name <-
arcgislayers::list_items(service)$name
} else if (length(layer_name) > 1) {
if (is.numeric(layer_name)) {
layers <-
arcgislayers::get_layers(
service,
id = layer_name
)
}
layers <-
service |>
arcgislayers::get_layers(
name = layer_name
)
} else {
layers <-
service |>
arcgislayers::get_layer(
name = layer_name
) |>
list(`0` = _)
}

out <-
layers |>
purrr::map(
~ tryCatch(
arcgislayers::arc_select(
.x,
filter_geom =
geom
),
error = function(e) {
NULL
}
)
) |>
magrittr::set_names(layer_name)

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

return(out)
}
Loading

0 comments on commit 4b08a75

Please sign in to comment.