Skip to content

Commit

Permalink
moved to arcgislayers
Browse files Browse the repository at this point in the history
  • Loading branch information
bocinsky committed Feb 11, 2024
1 parent a47c524 commit 4ce3381
Show file tree
Hide file tree
Showing 10 changed files with 116 additions and 255 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/pkgdown.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
env:
GITHUB_PAT: ${{ secrets.GITHUB_TOKEN }}
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4

- uses: r-lib/actions/setup-pandoc@v2

Expand Down
7 changes: 5 additions & 2 deletions DESCRIPTION
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ Imports:
purrr,
readr,
terra (>= 1.0),
sf (>= 1.0)
sf (>= 1.0),
arcgislayers (>= 0.1.0)
Encoding: UTF-8
LazyData: true
NeedsCompilation: no
Repository: CRAN
Roxygen: list(markdown = TRUE)
RoxygenNote: 7.2.3
RoxygenNote: 7.3.1
Suggests:
covr,
ggplot2,
Expand All @@ -80,3 +81,5 @@ Suggests:
rmarkdown,
leaflet,
rmapshaper
Remotes:
R-ArcGIS/arcgislayers
1 change: 1 addition & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
- Updated `get_nlcd()` to include the 2021 NLCD as the default, in response to [Issue #105](https://github.com/ropensci/FedData/issues/105).
- Updated outdated package description
- Bumped GDAL version req to >= 3.1.0 to accommodate storing spatial vectors as FlatGeoBufs
- Added `arcgislayers` dependency and retired self-written esri functions. Closes [Issue #109](https://github.com/ropensci/FedData/issues/109).

# FedData 4.0.0
- Updated the [README](README.md) and moved examples to an article
Expand Down
116 changes: 75 additions & 41 deletions R/NHD_FUNCTIONS.R
Original file line number Diff line number Diff line change
Expand Up @@ -50,51 +50,85 @@ get_nhd <-
}

template %<>%
template_to_sf()
template_to_sf() %>%
sf::st_as_sfc() %>%
sf::st_union() %>%
sf::st_cast("POLYGON")

if (nhdplus) {
layers <-
c(
"NHDPoint",
"NetworkNHDFlowline",
"NonNetworkNHDFlowline",
"NHDLine",
"NHDArea",
"NHDWaterbody"
)

nhd_out <-
esri_query(
url = "https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer",
geom = template,
layers = c(
"NHDPoint",
"NetworkNHDFlowline",
"NonNetworkNHDFlowline",
"NHDLine",
"NHDArea",
"NHDWaterbody"
"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) %$%
list(
Point = NHDPoint,
Flowline = list(
nhd_out$NetworkNHDFlowline,
nhd_out$NonNetworkNHDFlowline
NetworkNHDFlowline,
NonNetworkNHDFlowline
) %>%
purrr::map_dfr(tibble::as_tibble) %>%
sf::st_as_sf(),
dplyr::bind_rows(),
Line = NHDLine,
Area = NHDArea,
Waterbody = NHDWaterbody
)
} else {
layers <-
c(
"Point",
"Flowline - Large Scale",
"Line - Large Scale ",
"Area - Large Scale",
"Waterbody - Large Scale"
)

nhd_out <-
esri_query(
url = "https://hydro.nationalmap.gov/arcgis/rest/services/nhd/MapServer",
geom = template,
layers = c(
"Point",
"Flowline - Large Scale",
"Line - Large Scale",
"Area - Large Scale",
"Waterbody - Large Scale"
"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) %$%
list(
Point = Point,
Flowline = `Flowline - Large Scale`,
Line = `Line - Large Scale`,
Line = `Line - Large Scale `,
Area = `Area - Large Scale`,
Waterbody = `Waterbody - Large Scale`
)
Expand Down Expand Up @@ -194,7 +228,7 @@ plot_nhd <-
#' @param label A character string naming the study area.
#' @param extraction.dir A character string indicating where the extracted and cropped NHD data should be put.
#' @param force.redo If an extraction for this template and label already exists, should a new one be created?
#' @return A `sf` collection of the HUC 12 regions within
#' @return An `sf` collection of the HUC 12 regions within
#' the specified \code{template}.
#' @export
get_wbd <- function(template,
Expand All @@ -221,20 +255,20 @@ get_wbd <- function(template,
return(read_sf_all(out_dsn))
}

template %<>%
template_to_sf()

wbd_out <-
"https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer/" %>%
esri_query(
geom = template,
layers = "WBDHU12"
"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()
) %>%
magrittr::extract2(1)


wbd_out %>%
sf::write_sf(dsn = out_dsn)

return(sf::read_sf(out_dsn))
return(
sf::read_sf(out_dsn)
)
}
29 changes: 20 additions & 9 deletions R/PADUS_FUNCTIONS.R
Original file line number Diff line number Diff line change
Expand Up @@ -109,26 +109,37 @@ get_padus <-
purrr::map(
function(x) {
file.path(padus_base_url, x, "FeatureServer/") %>%
esri_feature_query(
where = paste0("Unit_Nm IN (", paste(paste0("'", template, "'"), collapse = ","), ")")
) %>%
magrittr::extract2(1)
arcgislayers::arc_open() %>%
arcgislayers::get_layer(id = 0) %>%
arcgislayers::arc_select(
where =
paste0(
"Unit_Nm IN (",
paste(paste0("'", template, "'"), collapse = ","),
")"
)
)
}
)
} else {
template %<>%
template_to_sf() %>%
sf::st_transform(4326)
sf::st_transform(4326) %>%
sf::st_as_sfc() %>%
sf::st_union() %>%
sf::st_cast("POLYGON")

padus_out <-
padus_services[layer] %>%
purrr::map(
function(x) {
file.path(padus_base_url, x, "FeatureServer/") %>%
esri_feature_query(
geom = template
) %>%
magrittr::extract2(1)
arcgislayers::arc_open() %>%
arcgislayers::get_layer(id = 0) %>%
arcgislayers::arc_select(
filter_geom =
template
)
}
)
}
Expand Down
3 changes: 3 additions & 0 deletions R/UTILITY_FUNCTIONS.R
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ if (getRversion() >= "2.15.1") {
"Point",
"SPATIALVER",
"Waterbody - Large Scale",
"Line - Large Scale ",
"NetworkNHDFlowline",
"NonNetworkNHDFlowline",
"mukey",
"musym",
"spatial",
Expand Down
Loading

0 comments on commit 4ce3381

Please sign in to comment.