subset_nhdplus: Subset NHDPlus

Description Usage Arguments Details Value Examples

View source: R/subset_nhdplus.R

Description

Saves a subset of the National Seamless database or other nhdplusTools compatible data based on a specified collection of COMIDs.

Usage

1
2
subset_nhdplus(comids, output_file, nhdplus_data = NULL,
  simplified = TRUE, overwrite = FALSE, status = TRUE)

Arguments

comids

integer vector of COMIDs to include.

output_file

character path to save the output to defaults to the directory of the nhdplus_data.

nhdplus_data

character path to the .gpkg or .gdb containing the national seamless database, a subset of NHDPlusHR, or "download" to use a web service to download NHDPlusV2.1 data. Not required if nhdplus_path has been set or the default has been adopted. See details for more.

simplified

boolean if TRUE (the default) the CatchmentSP layer will be included. Not relevant to the "download" option or NHDPlusHR data.

overwrite

boolean should the output file be overwritten

status

boolean should the function print status messages

Details

If stage_national_data has been run in the current session, this function will use the staged national data automatically.

This function relies on the National Seamless Geodatabase or Geopackage. It can be downloaded here.

The "download" option of this function should be considered preliminary and subject to revision. It does not include as many layers and may not be available permenantly.

Value

path to the saved subset geopackage

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
sample_data <- system.file("extdata/sample_natseamless.gpkg",
                           package = "nhdplusTools")

nhdplus_path(sample_data)

staged_nhdplus <- stage_national_data(output_path = tempdir())

sample_flines <- readRDS(staged_nhdplus$flowline)

geom_col <- attr(sample_flines, "sf_column")

plot(sample_flines[[geom_col]],
     lwd = 3)

start_point <- sf::st_sfc(sf::st_point(c(-89.362239, 43.090266)),
                          crs = 4326)

plot(start_point, cex = 1.5, lwd = 2, col = "red", add = TRUE)

start_comid <- discover_nhdplus_id(start_point)

comids <- get_UT(sample_flines, start_comid)

plot(dplyr::filter(sample_flines, COMID %in% comids)[[geom_col]],
     add=TRUE, col = "red", lwd = 2)

output_file <- tempfile(fileext = ".gpkg")

subset_nhdplus(comids = comids,
               output_file = output_file,
               nhdplus_data = sample_data,
               overwrite = TRUE,
               status = TRUE)

sf::st_layers(output_file)

catchment <- sf::read_sf(output_file, "CatchmentSP")

plot(catchment[[attr(catchment, "sf_column")]], add = TRUE)

waterbody <- sf::read_sf(output_file, "NHDWaterbody")

plot(waterbody[[attr(waterbody, "sf_column")]],
     col = rgb(0, 0, 1, alpha = 0.5), add = TRUE)

# Download Option:
subset_nhdplus(comids = comids,
               output_file = output_file,
               nhdplus_data = "download",
               overwrite = TRUE,
               status = TRUE)

sf::st_layers(output_file)

# NHDPlusHR
temp_dir <- tempdir()
temp_file <- tempfile(fileext = ".zip", tmpdir = temp_dir)
download.file("https://usgs-r.github.io/nhdplusTools/data/03_sub.zip",
              temp_file)
unzip(temp_file, exdir = temp_dir)

hr_data <- get_nhdplushr(temp_dir,
                         out_gpkg = file.path(temp_dir, "nhd_hr.gpkg"),
                         layers = NULL)
flowlines <- sf::read_sf(hr_data, "NHDFlowline")

up_ids <- get_UT(flowlines, 15000500028335)

sub_nhdhr <- subset_nhdplus(up_ids, file.path(temp_dir, "sub.gpkg"),
                      hr_data, overwrite = TRUE)

sf::st_layers(sub_nhdhr)

sub_flowline <- sf::read_sf(sub_nhdhr, "NHDFlowline")
plot(sf::st_geometry(flowlines), lwd = 0.5)
plot(sf::st_geometry(sub_flowline), lwd = 0.6, col = "red", add = TRUE)

nhdplusTools documentation built on Nov. 7, 2019, 5:08 p.m.