diff --git a/DESCRIPTION b/DESCRIPTION index a0c93f19..14956775 100644 --- a/DESCRIPTION +++ b/DESCRIPTION @@ -1,7 +1,7 @@ Package: nhdplusTools Type: Package Title: NHDPlus Tools -Version: 1.3.0 +Version: 1.3.1 Authors@R: c(person(given = "David", family = "Blodgett", role = c("aut", "cre"), diff --git a/NAMESPACE b/NAMESPACE index 44924ca8..537594de 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -32,6 +32,7 @@ export(get_huc) export(get_hydro_location) export(get_levelpaths) export(get_nhdarea) +export(get_nhdphr) export(get_nhdplus) export(get_nhdplushr) export(get_nldi_basin) diff --git a/NEWS.md b/NEWS.md index 073b528c..0d6b9e2b 100644 --- a/NEWS.md +++ b/NEWS.md @@ -1,3 +1,8 @@ +nhdplusTools 1.3.1 +========== + +- added a web service client for the NHDPlusHR argis server `get_nhdphr()`. #415 + nhdplusTools 1.3.0 ========== diff --git a/R/arcrest_tools.R b/R/arcrest_tools.R index 47a6f6bd..407a031a 100644 --- a/R/arcrest_tools.R +++ b/R/arcrest_tools.R @@ -1,15 +1,17 @@ -get_3dhp_service_info <- memoise::memoise(function() { +get_arcrest_service_info <- memoise::memoise(function(service = "3DHP_all") { - # TODO: support more services? - server <- "3DHP_all" + stopifnot(service %in% c("3DHP_all", "NHDPlus_HR")) url_base <- paste0(get("arcrest_root", envir = nhdplusTools_env), - server, + service, "/MapServer/") all_layers <- jsonlite::read_json(paste0(url_base, "?f=json")) - list(url_base = url_base, all_layers = all_layers) + id_name <- "id3dhp" + if(service == "NHDPlus_HR") id_name <- "nhdplusid" + + list(url_base = url_base, all_layers = all_layers, id = id_name) }) @@ -31,9 +33,9 @@ get_3dhp_service_info <- memoise::memoise(function() { #' @param ids character. A set of identifier(s) from the data #' type requested, for 3dhp, this is id3dhp. #' @param type character. Type of feature to return -#' ("hydrolocation", "flowline", "waterbody", "drainage area", "catchment"). -#' If NULL (default) a data.frame of available resources is returned -#' @param where character. An where clause to pass to the server. +#' If NULL (default) a data.frame of available types is returned +#' @param service character chosen from "3DHP_all", "NHDPlus_HR" +#' @param where character An where clause to pass to the server. #' @param t_srs character (PROJ string or EPSG code) or numeric (EPSG code). #' A user specified - target -Spatial Reference System (SRS/CRS) for returned objects. #' Will default to the CRS of the input AOI if provided, and to 4326 for ID requests. @@ -48,12 +50,13 @@ get_3dhp_service_info <- memoise::memoise(function() { #' @importFrom dplyr filter #' @importFrom methods as query_usgs_arcrest <- function(AOI = NULL, ids = NULL, - type = NULL, where = NULL, + type = NULL, service = NULL, + where = NULL, t_srs = NULL, buffer = 0.5, - page_size = 2000){ + page_size = 2000) { - si <- get_3dhp_service_info() + si <- get_arcrest_service_info(service) source <- data.frame(user_call = sapply(si$all_layers$layers, \(x) tolower(x$name)), layer = sapply(si$all_layers$layers, \(x) x$id)) @@ -73,7 +76,8 @@ query_usgs_arcrest <- function(AOI = NULL, ids = NULL, return(NULL) } - if(grepl(paste(sapply(group_layers, \(x) x$name), + if(length(group_layers) > 0 && + grepl(paste(sapply(group_layers, \(x) x$name), collapse = "|"), type, ignore.case = TRUE)) { layer_id <- filter(source, .data$user_call == !!type)$layer @@ -107,8 +111,14 @@ query_usgs_arcrest <- function(AOI = NULL, ids = NULL, if(!is.null(where)) stop("can't specify ids and where") - where <- paste0("id3dhp IN ('", - paste(ids, collapse = "', '"), "')") + if(si$id == "nhdplusid") { + where <- paste0(si$id, " IN (", + paste(ids, collapse = ", "), ")") + } else { + where <- paste0(si$id, " IN ('", + paste(ids, collapse = "', '"), "')") + } + } post_body <- list(where = where, @@ -186,8 +196,12 @@ query_usgs_arcrest <- function(AOI = NULL, ids = NULL, if(inherits(out[[1]], "data.frame")) { out <- bind_rows(unify_types(out)) - out <- check_valid(out[!duplicated(out[["id3dhp"]]), ], - out_prj = t_srs) + if("id3dhp" %in% names(out)) { + out <- check_valid(out[!duplicated(out[["id3dhp"]]), ], + out_prj = t_srs) + } else { + out <- check_valid(out, out_prj = t_srs) + } } else { diff --git a/R/get_hydro.R b/R/get_hydro.R index 5e63a71f..4d9a65d4 100644 --- a/R/get_hydro.R +++ b/R/get_hydro.R @@ -185,6 +185,9 @@ get_nwis <- function(AOI = NULL, t_srs = NULL, buffer = 20000){ #' for source data documentation. #' #' @inherit query_usgs_arcrest details return params +#' @param type character. Type of feature to return. e.g. +#' ("hydrolocation", "flowline", "waterbody", "drainage area", "catchment"). +#' If NULL (default) a data.frame of available types is returned #' @param ids character vector of id3dhp ids or mainstem uris #' @param universalreferenceid character vector of hydrolocation universal #' reference ids such as reachcodes @@ -248,7 +251,67 @@ get_3dhp <- function(AOI = NULL, ids = NULL, type = NULL, ids <- NULL } - query_usgs_arcrest(AOI, ids, type, where, t_srs, buffer, page_size) + query_usgs_arcrest(AOI, ids, type, "3DHP_all", where, t_srs, buffer, page_size) } +#' Get NHDPlusHR Data +#' @description +#' Calls the NHDPlus_HR web service and returns sf data.frames for the selected +#' layers. See https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer +#' for source data documentation. +#' +#' @inherit query_usgs_arcrest details return params +#' +#' @param type character. Type of feature to return e.g. +#' c("networknhdflowline", nonnetworknhdflowline", nhdwaterbody", "nhdpluscatchment"). +#' If NULL (default) a data.frame of available types is returned +#' +#' @param ids character vector of nhdplusid ids +#' +#' @param reachcode character vector of reachcodes +#' NOTE: performance of this query is currently very poor, +#' spatial queries are the primary use of this function. +#' +#' @export +#' @examples +#' \donttest{ +#' AOI <- sf::st_as_sfc(sf::st_bbox(c(xmin = -89.56684, ymin = 42.99816, +#' xmax = -89.24681, ymax = 43.17192), +#' crs = "+proj=longlat +datum=WGS84 +no_defs")) +#' +#' # get flowlines and hydrolocations +#' flowlines <- get_nhdphr(AOI = AOI, type = "networknhdflowline") +#' point <- get_nhdphr(AOI = AOI, type = "nhdpoint") +#' waterbody <- get_nhdphr(AOI = AOI, type = "nhdwaterbody") +#' +#' if(!is.null(waterbody) & !is.null(flowlines) & !is.null(point)) { +#' plot(sf::st_geometry(waterbody), col = "lightblue", border = "lightgrey") +#' plot(sf::st_geometry(flowlines), col = "blue", add = TRUE) +#' plot(sf::st_geometry(point), col = "grey", pch = "+", add = TRUE) } +#' +#' # given universalreferenceid (reachcodes), can query for them but only +#' # for hydrolocations. This is useful for looking up mainstem ids. +#' +#' get_nhdphr(reachcode = "13020101021927", type = "networknhdflowline") +#'} +get_nhdphr <- function(AOI = NULL, ids = NULL, type = NULL, + reachcode = NULL, + t_srs = NULL, buffer = 0.5, + page_size = 2000) { + + if(!is.null(reachcode) && !isTRUE(grepl("nhdplusgage|nhdpoint|networknhdflowline|nonnetworknhdflowline|flowdirection|nhdwaterbody", + type))) { + stop("reachcode not defined for ", type) + } + + where <- NULL + if(!is.null(reachcode)) { + where <- paste(paste0("reachcode IN ('", + paste(reachcode, collapse = "', '"), "')")) + if(!is.null(ids)) stop("can not specify both reachcode and other ids") + } + + query_usgs_arcrest(AOI, ids, type, "NHDPlus_HR", where, t_srs, buffer, page_size) + +} diff --git a/_pkgdown.yml b/_pkgdown.yml index e2f57821..bc0b5164 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -6,6 +6,7 @@ reference: Functions that help get access data. (all U.S. context) - contents: - '`get_3dhp`' + - '`get_nhdphr`' - '`discover_geoconnex_reference`' - '`get_geoconnex_reference`' - '`download_nhdplushr`' diff --git a/docs/404.html b/docs/404.html index 9efb8e6e..a76f130b 100644 --- a/docs/404.html +++ b/docs/404.html @@ -39,7 +39,7 @@ nhdplusTools - 1.3.0 + 1.3.1 diff --git a/docs/DISCLAIMER.html b/docs/DISCLAIMER.html index 1c3ddcad..7d122bf2 100644 --- a/docs/DISCLAIMER.html +++ b/docs/DISCLAIMER.html @@ -17,7 +17,7 @@ nhdplusTools - 1.3.0 + 1.3.1 diff --git a/docs/LICENSE.html b/docs/LICENSE.html index 5bd3c7db..64639c67 100644 --- a/docs/LICENSE.html +++ b/docs/LICENSE.html @@ -17,7 +17,7 @@ nhdplusTools - 1.3.0 + 1.3.1 diff --git a/docs/articles/index.html b/docs/articles/index.html index c6652b5e..377c4c72 100644 --- a/docs/articles/index.html +++ b/docs/articles/index.html @@ -17,7 +17,7 @@ nhdplusTools - 1.3.0 + 1.3.1 diff --git a/docs/authors.html b/docs/authors.html index 30ddf960..26013f81 100644 --- a/docs/authors.html +++ b/docs/authors.html @@ -17,7 +17,7 @@ nhdplusTools - 1.3.0 + 1.3.1 diff --git a/docs/index.html b/docs/index.html index 88c2d696..09cc271a 100644 --- a/docs/index.html +++ b/docs/index.html @@ -40,7 +40,7 @@ nhdplusTools - 1.3.0 + 1.3.1 diff --git a/docs/news/index.html b/docs/news/index.html index 63926d52..6a073e15 100644 --- a/docs/news/index.html +++ b/docs/news/index.html @@ -17,7 +17,7 @@ nhdplusTools - 1.3.0 + 1.3.1 @@ -73,7 +73,11 @@

Changelog

- + +
+
+
@@ -108,9 +108,9 @@

Arguments

type
-

character. Type of feature to return +

character. Type of feature to return. e.g. ("hydrolocation", "flowline", "waterbody", "drainage area", "catchment"). -If NULL (default) a data.frame of available resources is returned

+If NULL (default) a data.frame of available types is returned

universalreferenceid
diff --git a/docs/reference/get_nhdphr-1.png b/docs/reference/get_nhdphr-1.png new file mode 100644 index 00000000..ca2ba34b Binary files /dev/null and b/docs/reference/get_nhdphr-1.png differ diff --git a/docs/reference/get_nhdphr.html b/docs/reference/get_nhdphr.html new file mode 100644 index 00000000..4c92d1f3 --- /dev/null +++ b/docs/reference/get_nhdphr.html @@ -0,0 +1,349 @@ + +Get NHDPlusHR Data — get_nhdphr • nhdplusTools + + +
+
+ + + +
+
+ + +
+

Calls the NHDPlus_HR web service and returns sf data.frames for the selected +layers. See https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer +for source data documentation.

+
+ +
+
get_nhdphr(
+  AOI = NULL,
+  ids = NULL,
+  type = NULL,
+  reachcode = NULL,
+  t_srs = NULL,
+  buffer = 0.5,
+  page_size = 2000
+)
+
+ +
+

Arguments

+ + +
AOI
+

sf (MULTI)POINT or (MULTI)POLYGON. An 'area of interest' can +be provided as either a location (sf POINT) or area (sf POLYGON) +in any Spatial Reference System.

+ + +
ids
+

character vector of nhdplusid ids

+ + +
type
+

character. Type of feature to return e.g. +c("networknhdflowline", nonnetworknhdflowline", nhdwaterbody", "nhdpluscatchment"). +If NULL (default) a data.frame of available types is returned

+ + +
reachcode
+

character vector of reachcodes +NOTE: performance of this query is currently very poor, +spatial queries are the primary use of this function.

+ + +
t_srs
+

character (PROJ string or EPSG code) or numeric (EPSG code). +A user specified - target -Spatial Reference System (SRS/CRS) for returned objects. +Will default to the CRS of the input AOI if provided, and to 4326 for ID requests.

+ + +
buffer
+

numeric. The amount (in meters) to buffer a POINT AOI by for an +extended search. Default = 0.5

+ + +
page_size
+

numeric default number of features to request at a time. Reducing +may help if 500 errors are experienced.

+ +
+
+

Value

+

a simple features (sf) object or valid types if no type supplied

+
+
+

Details

+

The returned object(s) will have the same +Spatial Reference System (SRS) as the input AOI. If a individual or set of +IDs are used to query, then the default CRS of EPSG:4269 is +preserved. In all cases, a user-defined SRS can be passed to t_srs +which will override all previous SRS (either input or default). +All buffer and distance operations are handled internally using in +EPSG:5070 Albers Equal Area projection

+
+ +
+

Examples

+
# \donttest{
+AOI <- sf::st_as_sfc(sf::st_bbox(c(xmin = -89.56684, ymin = 42.99816,
+                                   xmax = -89.24681, ymax = 43.17192),
+                                 crs = "+proj=longlat +datum=WGS84 +no_defs"))
+
+# get flowlines and hydrolocations
+flowlines <- get_nhdphr(AOI = AOI, type = "networknhdflowline")
+point <- get_nhdphr(AOI = AOI, type = "nhdpoint")
+waterbody <- get_nhdphr(AOI = AOI, type = "nhdwaterbody")
+
+if(!is.null(waterbody) & !is.null(flowlines) & !is.null(point)) {
+plot(sf::st_geometry(waterbody), col = "lightblue", border = "lightgrey")
+plot(sf::st_geometry(flowlines), col = "blue", add = TRUE)
+plot(sf::st_geometry(point), col = "grey", pch = "+", add = TRUE) }
+
+
+# given universalreferenceid (reachcodes), can query for them but only
+# for hydrolocations. This is useful for looking up mainstem ids.
+
+get_nhdphr(reachcode = "13020101021927", type = "networknhdflowline")
+#> Simple feature collection with 10 features and 82 fields
+#> Geometry type: LINESTRING
+#> Dimension:     XY
+#> Bounding box:  xmin: -105.9805 ymin: 36.17496 xmax: -105.9253 ymax: 36.21542
+#> Geodetic CRS:  WGS 84
+#>    permanent_identifier        fdate resolution  gnis_id  gnis_name   lengthkm
+#> 1             120928749 1.502237e+12          2 01385432 Rio Grande 0.12637349
+#> 2             120928746 1.502237e+12          2 01385432 Rio Grande 1.77445619
+#> 3             120928753 1.502237e+12          2 01385432 Rio Grande 0.10023801
+#> 4             120928745 1.502237e+12          2 01385432 Rio Grande 0.64380256
+#> 5             120928744 1.502237e+12          2 01385432 Rio Grande 1.96787731
+#> 6             120928752 1.502237e+12          2 01385432 Rio Grande 0.09364129
+#> 7             120928748 1.502237e+12          2 01385432 Rio Grande 0.35127735
+#> 8             120928754 1.502237e+12          2 01385432 Rio Grande 0.86485354
+#> 9             120928751 1.502237e+12          2 01385432 Rio Grande 0.07149613
+#> 10            120928747 1.502237e+12          2 01385432 Rio Grande 2.12124057
+#>         reachcode flowdir wbarea_permanent_identifier ftype fcode mainpath
+#> 1  13020101021927       1                   120932465   558 55800        0
+#> 2  13020101021927       1                   120932465   558 55800        0
+#> 3  13020101021927       1                   120932465   558 55800        0
+#> 4  13020101021927       1                   120932465   558 55800        0
+#> 5  13020101021927       1                   120932465   558 55800        0
+#> 6  13020101021927       1                   120932465   558 55800        0
+#> 7  13020101021927       1                   120932465   558 55800        0
+#> 8  13020101021927       1                   120932465   558 55800        0
+#> 9  13020101021927       1                   120932465   558 55800        0
+#> 10 13020101021927       1                   120932465   558 55800        0
+#>    innetwork visibilityfilter   nhdplusid vpuid streamleve streamorde
+#> 1          1          5000000 3.50006e+13  1302          4          8
+#> 2          1          5000000 3.50006e+13  1302          4          8
+#> 3          1          5000000 3.50006e+13  1302          4          8
+#> 4          1          5000000 3.50006e+13  1302          4          8
+#> 5          1          5000000 3.50006e+13  1302          4          8
+#> 6          1          5000000 3.50006e+13  1302          4          8
+#> 7          1          5000000 3.50006e+13  1302          4          8
+#> 8          1          5000000 3.50006e+13  1302          4          8
+#> 9          1          5000000 3.50006e+13  1302          4          8
+#> 10         1          5000000 3.50006e+13  1302          4          8
+#>    streamcalc    fromnode      tonode    hydroseq  levelpathi pathlength
+#> 1           8 3.50006e+13 3.50006e+13 3.50006e+13 3.50001e+13   2706.329
+#> 2           8 3.50006e+13 3.50006e+13 3.50006e+13 3.50001e+13   2704.203
+#> 3           8 3.50006e+13 3.50006e+13 3.50006e+13 3.50001e+13   2699.370
+#> 4           8 3.50006e+13 3.50006e+13 3.50006e+13 3.50001e+13   2703.560
+#> 5           8 3.50006e+13 3.50006e+13 3.50006e+13 3.50001e+13   2701.592
+#> 6           8 3.50006e+13 3.50006e+13 3.50006e+13 3.50001e+13   2706.527
+#> 7           8 3.50006e+13 3.50006e+13 3.50006e+13 3.50001e+13   2705.978
+#> 8           8 3.50006e+13 3.50006e+13 3.50006e+13 3.50001e+13   2698.505
+#> 9           8 3.50006e+13 3.50006e+13 3.50006e+13 3.50001e+13   2706.455
+#> 10          8 3.50006e+13 3.50006e+13 3.50006e+13 3.50001e+13   2699.470
+#>     terminalpa arbolatesu divergence startflag terminalfl  uplevelpat
+#> 1  3.50001e+13   33149.18          1         0          0 3.50001e+13
+#> 2  3.50001e+13   33152.93          0         0          0 3.50001e+13
+#> 3  3.50001e+13   33165.89          1         0          0 3.50001e+13
+#> 4  3.50001e+13   33158.34          0         0          0 3.50001e+13
+#> 5  3.50001e+13   33161.50          0         0          0 3.50001e+13
+#> 6  3.50001e+13   33145.15          0         0          0 3.50001e+13
+#> 7  3.50001e+13   33150.69          0         0          0 3.50001e+13
+#> 8  3.50001e+13   33166.75          1         0          0 3.50001e+13
+#> 9  3.50001e+13   33149.05          0         0          0 3.50001e+13
+#> 10 3.50001e+13   33165.79          0         0          0 3.50001e+13
+#>     uphydroseq dnlevel  dnlevelpat  dnhydroseq  dnminorhyd dndraincou frommeas
+#> 1  3.50006e+13       4 3.50001e+13 3.50006e+13 0.00000e+00          1 96.38273
+#> 2  3.50006e+13       4 3.50001e+13 3.50006e+13 0.00000e+00          1 70.06044
+#> 3  3.50006e+13       4 3.50001e+13 3.50006e+13 3.50006e+13          2 10.61793
+#> 4  3.50006e+13       4 3.50001e+13 3.50006e+13 0.00000e+00          1 62.06700
+#> 5  3.50006e+13       4 3.50001e+13 3.50006e+13 0.00000e+00          1 37.84511
+#> 6  3.50006e+13       4 3.50001e+13 3.50006e+13 0.00000e+00          1 98.83942
+#> 7  3.50006e+13       4 3.50001e+13 3.50006e+13 0.00000e+00          1 92.04084
+#> 8  3.50006e+13       4 3.50001e+13 3.50006e+13 0.00000e+00          1  0.00000
+#> 9  3.50006e+13       4 3.50001e+13 3.50006e+13 3.50006e+13          2 97.95167
+#> 10 3.50006e+13       4 3.50001e+13 3.50006e+13 3.50006e+13          2 11.85308
+#>       tomeas rtndiv thinner vpuin vpuout   areasqkm totdasqkm divdasqkm
+#> 1   97.95167      0    <NA>     0      0 0.01670001  15674.28  14789.65
+#> 2   92.04084      1    <NA>     0      0 2.64680001  15678.19  14793.55
+#> 3   11.85308      0    <NA>     0      0 0.08019999  15694.47  14809.84
+#> 4   70.06044      0    <NA>     0      0 1.28390001  15682.44  14797.80
+#> 5   62.06700      0    <NA>     0      0 3.88990000  15687.72  14803.08
+#> 6  100.00000      0    <NA>     0      0 0.03410001  15671.63  14787.00
+#> 7   96.38273      0    <NA>     0      0 0.08129999  15675.28  14790.65
+#> 8   10.61793      0    <NA>     0      0 0.06200002  15694.54  14809.90
+#> 9   98.83942      0    <NA>     0      0 0.01359998  15674.26  14789.63
+#> 10  37.84511      0    <NA>     0      0 4.35159998  15694.39  14809.76
+#>    maxelevraw minelevraw maxelevsmo minelevsmo      slope slopelenkm elevfixed
+#> 1       -9998     177474     177652     177474 0.01408523 0.12637349      <NA>
+#> 2       -9998     177019     177269     177051 0.00122855 1.77445619      <NA>
+#> 3       -9998     175774     175780     175780 0.00001000 0.10023801      <NA>
+#> 4       -9998     176572     177051     176672 0.00588690 0.64380256      <NA>
+#> 5       -9998     176567     176672     176590 0.00041669 1.96787731      <NA>
+#> 6       -9998     177678     177678     177678 0.00001000 0.09364129      <NA>
+#> 7       -9998     177262     177474     177269 0.00583584 0.35127735      <NA>
+#> 8       -9998     175728     175780     175780 0.00001000 0.86485354      <NA>
+#> 9       -9998     177652     177678     177652 0.00363656 0.07149613      <NA>
+#> 10      -9998     175714     176590     175780 0.00381852 2.12124057      <NA>
+#>    hwtype hwnodesqkm statusflag     qama     vama qincrama     qbma     vbma
+#> 1    <NA>       <NA>          A 3714.387 3.536884 0.000243 3714.387 3.536884
+#> 2    <NA>       <NA>          A 3714.445 2.499065 0.039421 3714.445 2.499065
+#> 3    <NA>       <NA>          A 3714.690 1.328012 0.001217 3714.690 1.328012
+#> 4    <NA>       <NA>          A 3714.507 3.118966 0.018690 3714.507 3.118966
+#> 5    <NA>       <NA>          A 3714.584 2.153226 0.056736 3714.584 2.153226
+#> 6    <NA>       <NA>          A 3714.347 1.327770 0.000496 3714.347 1.327770
+#> 7    <NA>       <NA>          A 3714.402 3.114867 0.001184 3714.402 3.114867
+#> 8    <NA>       <NA>          A 3714.691 1.328012 0.000931 3714.691 1.328012
+#> 9    <NA>       <NA>          A 3714.387 2.911523 0.000198 3714.387 2.911523
+#> 10   <NA>       <NA>          A 3714.689 2.932359 0.068250 3714.689 2.932359
+#>    qincrbma     qcma     vcma   qincrcma     qdma     vdma   qincrdma     qema
+#> 1  0.000243 3714.387 3.536884 0.00024300 3714.387 3.536884 0.00024300 1711.386
+#> 2  0.039421 3714.445 2.499065 0.04207118 3714.445 2.499065 0.04207118 1711.444
+#> 3  0.001217 3714.690 1.328012 0.00121700 3714.690 1.328012 0.00121700 1711.690
+#> 4  0.018690 3714.507 3.118966 0.04763696 3714.507 3.118966 0.04763696 1711.506
+#> 5  0.056736 3714.584 2.153226 0.07050796 3714.584 2.153226 0.07050796 1711.583
+#> 6  0.000496 3714.347 1.327770 0.42352419 3714.347 1.327770 0.42352419 1711.347
+#> 7  0.001184 3714.402 3.114867 0.01036309 3714.402 3.114867 0.01036309 1711.401
+#> 8  0.000931 3714.691 1.328012 0.00093100 3714.691 1.328012 0.00093100 1711.691
+#> 9  0.000198 3714.387 2.911523 0.02664062 3714.387 2.911523 0.02664062 1711.386
+#> 10 0.068250 3714.689 2.932359 0.09319502 3714.689 2.932359 0.09319502 1711.688
+#>        vema   qincrema     qfma   qincrfma arqnavma petma qlossma qgadjma
+#> 1  3.340468 0.00024300 1711.386 0.00024300        0     0       0       0
+#> 2  2.365791 0.04207118 1711.444 0.04207118        0     0       0       0
+#> 3  1.265987 0.00121700 1711.690 0.00121700        0     0       0       0
+#> 4  2.947984 0.04763696 1711.506 0.04763696        0     0       0       0
+#> 5  2.040998 0.07050796 1711.583 0.07050796        0     0       0       0
+#> 6  1.265752 0.42352419 1711.347 0.42352419        0     0       0       0
+#> 7  2.944127 0.01036309 1711.401 0.01036309        0     0       0       0
+#> 8  1.265988 0.00093100 1711.691 0.00093100        0     0       0       0
+#> 9  2.753153 0.02664062 1711.386 0.02664062        0     0       0       0
+#> 10 2.772740 0.09319502 1711.688 0.09319502        0     0       0       0
+#>    qgnavma gageadjma avgqadjma gageidma gageqma Shape_Length
+#> 1        0         1         0        0       0    157.84395
+#> 2        0         1         0        0       0   2212.36438
+#> 3        0         1         0        0       0    124.42048
+#> 4        0         1         0        0       0    804.11669
+#> 5        0         1         0        0       0   2441.27200
+#> 6        0         1         0        0       0    116.80600
+#> 7        0         1         0        0       0    437.25416
+#> 8        0         1         0        0       0   1070.42970
+#> 9        0         1         0        0       0     89.30904
+#> 10       0         1         0        0       0   2621.91428
+#>                          geometry
+#> 1  LINESTRING (-105.9271 36.21...
+#> 2  LINESTRING (-105.9317 36.21...
+#> 3  LINESTRING (-105.9782 36.18...
+#> 4  LINESTRING (-105.9499 36.20...
+#> 5  LINESTRING (-105.957 36.209...
+#> 6  LINESTRING (-105.9253 36.21...
+#> 7  LINESTRING (-105.9285 36.21...
+#> 8  LINESTRING (-105.9774 36.18...
+#> 9  LINESTRING (-105.9263 36.21...
+#> 10 LINESTRING (-105.972 36.197...
+# }
+
+
+
+ +
+ + +
+ + + + + + + + diff --git a/docs/reference/index.html b/docs/reference/index.html index fcfd464c..2c19ac74 100644 --- a/docs/reference/index.html +++ b/docs/reference/index.html @@ -17,7 +17,7 @@ nhdplusTools - 1.3.0 + 1.3.1 @@ -79,6 +79,10 @@

Data Access get_3dhp()

Get 3DHP Data

+ +

get_nhdphr()

+ +

Get NHDPlusHR Data

discover_geoconnex_reference()

diff --git a/docs/reference/query_usgs_arcrest.html b/docs/reference/query_usgs_arcrest.html index 866fbba8..dcd5a3cb 100644 --- a/docs/reference/query_usgs_arcrest.html +++ b/docs/reference/query_usgs_arcrest.html @@ -18,7 +18,7 @@ nhdplusTools - 1.3.0 + 1.3.1 @@ -84,6 +84,7 @@

Query USGS Hydro ESRI Rest Server

AOI = NULL, ids = NULL, type = NULL, + service = NULL, where = NULL, t_srs = NULL, buffer = 0.5, @@ -108,12 +109,15 @@

Arguments

type

character. Type of feature to return -("hydrolocation", "flowline", "waterbody", "drainage area", "catchment"). -If NULL (default) a data.frame of available resources is returned

+If NULL (default) a data.frame of available types is returned

+ + +
service
+

character chosen from "3DHP_all", "NHDPlus_HR"

where
-

character. An where clause to pass to the server.

+

character An where clause to pass to the server.

t_srs
diff --git a/docs/sitemap.xml b/docs/sitemap.xml index 59499b6a..4a15c400 100644 --- a/docs/sitemap.xml +++ b/docs/sitemap.xml @@ -48,6 +48,7 @@ https://doi-usgs.github.io/nhdplusTools/reference/get_huc8.html https://doi-usgs.github.io/nhdplusTools/reference/get_levelpaths.html https://doi-usgs.github.io/nhdplusTools/reference/get_nhdarea.html +https://doi-usgs.github.io/nhdplusTools/reference/get_nhdphr.html https://doi-usgs.github.io/nhdplusTools/reference/get_nhdplus.html https://doi-usgs.github.io/nhdplusTools/reference/get_nhdplushr.html https://doi-usgs.github.io/nhdplusTools/reference/get_nldi_basin.html diff --git a/man/get_3dhp.Rd b/man/get_3dhp.Rd index 2653e0fd..95035b63 100644 --- a/man/get_3dhp.Rd +++ b/man/get_3dhp.Rd @@ -21,9 +21,9 @@ in any Spatial Reference System.} \item{ids}{character vector of id3dhp ids or mainstem uris} -\item{type}{character. Type of feature to return +\item{type}{character. Type of feature to return. e.g. ("hydrolocation", "flowline", "waterbody", "drainage area", "catchment"). -If NULL (default) a data.frame of available resources is returned} +If NULL (default) a data.frame of available types is returned} \item{universalreferenceid}{character vector of hydrolocation universal reference ids such as reachcodes} diff --git a/man/get_nhdphr.Rd b/man/get_nhdphr.Rd new file mode 100644 index 00000000..7b450f93 --- /dev/null +++ b/man/get_nhdphr.Rd @@ -0,0 +1,80 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/get_hydro.R +\name{get_nhdphr} +\alias{get_nhdphr} +\title{Get NHDPlusHR Data} +\usage{ +get_nhdphr( + AOI = NULL, + ids = NULL, + type = NULL, + reachcode = NULL, + t_srs = NULL, + buffer = 0.5, + page_size = 2000 +) +} +\arguments{ +\item{AOI}{sf (MULTI)POINT or (MULTI)POLYGON. An 'area of interest' can +be provided as either a location (sf POINT) or area (sf POLYGON) +in any Spatial Reference System.} + +\item{ids}{character vector of nhdplusid ids} + +\item{type}{character. Type of feature to return e.g. +c("networknhdflowline", nonnetworknhdflowline", nhdwaterbody", "nhdpluscatchment"). +If NULL (default) a data.frame of available types is returned} + +\item{reachcode}{character vector of reachcodes +NOTE: performance of this query is currently very poor, +spatial queries are the primary use of this function.} + +\item{t_srs}{character (PROJ string or EPSG code) or numeric (EPSG code). +A user specified - target -Spatial Reference System (SRS/CRS) for returned objects. +Will default to the CRS of the input AOI if provided, and to 4326 for ID requests.} + +\item{buffer}{numeric. The amount (in meters) to buffer a POINT AOI by for an +extended search. Default = 0.5} + +\item{page_size}{numeric default number of features to request at a time. Reducing +may help if 500 errors are experienced.} +} +\value{ +a simple features (sf) object or valid types if no type supplied +} +\description{ +Calls the NHDPlus_HR web service and returns sf data.frames for the selected +layers. See https://hydro.nationalmap.gov/arcgis/rest/services/NHDPlus_HR/MapServer +for source data documentation. +} +\details{ +The returned object(s) will have the same +Spatial Reference System (SRS) as the input AOI. If a individual or set of +IDs are used to query, then the default CRS of EPSG:4269 is +preserved. In all cases, a user-defined SRS can be passed to \code{t_srs} +which will override all previous SRS (either input or default). +All buffer and distance operations are handled internally using in +EPSG:5070 Albers Equal Area projection +} +\examples{ +\donttest{ +AOI <- sf::st_as_sfc(sf::st_bbox(c(xmin = -89.56684, ymin = 42.99816, + xmax = -89.24681, ymax = 43.17192), + crs = "+proj=longlat +datum=WGS84 +no_defs")) + +# get flowlines and hydrolocations +flowlines <- get_nhdphr(AOI = AOI, type = "networknhdflowline") +point <- get_nhdphr(AOI = AOI, type = "nhdpoint") +waterbody <- get_nhdphr(AOI = AOI, type = "nhdwaterbody") + +if(!is.null(waterbody) & !is.null(flowlines) & !is.null(point)) { +plot(sf::st_geometry(waterbody), col = "lightblue", border = "lightgrey") +plot(sf::st_geometry(flowlines), col = "blue", add = TRUE) +plot(sf::st_geometry(point), col = "grey", pch = "+", add = TRUE) } + +# given universalreferenceid (reachcodes), can query for them but only +# for hydrolocations. This is useful for looking up mainstem ids. + +get_nhdphr(reachcode = "13020101021927", type = "networknhdflowline") +} +} diff --git a/man/query_usgs_arcrest.Rd b/man/query_usgs_arcrest.Rd index 98d23004..a12046a7 100644 --- a/man/query_usgs_arcrest.Rd +++ b/man/query_usgs_arcrest.Rd @@ -8,6 +8,7 @@ query_usgs_arcrest( AOI = NULL, ids = NULL, type = NULL, + service = NULL, where = NULL, t_srs = NULL, buffer = 0.5, @@ -23,10 +24,11 @@ in any Spatial Reference System.} type requested, for 3dhp, this is id3dhp.} \item{type}{character. Type of feature to return -("hydrolocation", "flowline", "waterbody", "drainage area", "catchment"). -If NULL (default) a data.frame of available resources is returned} +If NULL (default) a data.frame of available types is returned} -\item{where}{character. An where clause to pass to the server.} +\item{service}{character chosen from "3DHP_all", "NHDPlus_HR"} + +\item{where}{character An where clause to pass to the server.} \item{t_srs}{character (PROJ string or EPSG code) or numeric (EPSG code). A user specified - target -Spatial Reference System (SRS/CRS) for returned objects. diff --git a/nhdplusTools.Rproj b/nhdplusTools.Rproj index 29736fb3..35418cc2 100644 --- a/nhdplusTools.Rproj +++ b/nhdplusTools.Rproj @@ -1,4 +1,5 @@ Version: 1.0 +ProjectId: 903deedb-5779-4eeb-a8bc-71f5168cf6f9 RestoreWorkspace: Default SaveWorkspace: Default diff --git a/tests/testthat/test_arcrest.R b/tests/testthat/test_arcrest.R index a078950f..0fcb6b63 100644 --- a/tests/testthat/test_arcrest.R +++ b/tests/testthat/test_arcrest.R @@ -61,12 +61,14 @@ test_that("basic 3dhp service requests", { xmax = -89.4, ymax = 43.1), crs = "+proj=longlat +datum=WGS84 +no_defs")) - expect_message(expect_s3_class(nhdplusTools:::query_usgs_arcrest(AOI), + expect_message(expect_s3_class(nhdplusTools:::query_usgs_arcrest(AOI, service = "3DHP_all"), "data.frame")) - expect_warning(expect_warning(nhdplusTools:::query_usgs_arcrest(AOI, type = "hydrolocation"))) + expect_warning(expect_warning(nhdplusTools:::query_usgs_arcrest(AOI, service = "3DHP_all", + type = "hydrolocation"))) - test_data <- nhdplusTools:::query_usgs_arcrest(AOI, type = "reach code, external connection") + test_data <- nhdplusTools:::query_usgs_arcrest(AOI, , service = "3DHP_all", + type = "reach code, external connection") expect_s3_class(test_data, "sf") }) diff --git a/tests/testthat/test_get_nhdphr.R b/tests/testthat/test_get_nhdphr.R new file mode 100644 index 00000000..33e31950 --- /dev/null +++ b/tests/testthat/test_get_nhdphr.R @@ -0,0 +1,23 @@ +test_that("get_nhdphr", { + + skip_on_cran() + + expect_error(get_nhdphr(reachcode = "01234", type = "test"), + "not defined") + + expect_error(get_nhdphr(reachcode = "01234", type = "networknhdflowline", + ids = "1"), + "can not specify both") + + f <- get_nhdphr(ids = "50001000103671", + type = "networknhdflowline") + + expect_equal(f$nhdplusid, 50001000103671) + + expect_s3_class(f, "sf") + + skip("performance") + f2 <- get_nhdphr(reachcode = f$reachcode, + type = "networknhdflowline") + +})