# NOT RUN {
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)
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab