Skip to content

Commit

Permalink
fix up mainstem uris
Browse files Browse the repository at this point in the history
  • Loading branch information
dblodgett-usgs committed Mar 21, 2024
1 parent a6f1387 commit 87d6683
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
37 changes: 16 additions & 21 deletions R/gage_locations.R
Original file line number Diff line number Diff line change
Expand Up @@ -178,26 +178,21 @@ get_hydrologic_locations <- function(all_gages, hydrologic_locations, nhdpv2_fli
}

add_mainstems <- function(gage_hydrologic_locations, mainstems, vaa) {
mainstems <- mainstems[,c("id", "uri"), drop = TRUE]
mainstems$id <- as.integer(mainstems$id)

vaa <- data.table::as.data.table(vaa)

vaa <- data.table::merge.data.table(unique(vaa), unique(mainstems),
by.x = "levelpathi", by.y = "id",
all.x = FALSE, all.y = TRUE)
mainstems <- mainstems[,c("head_nhdpv2_COMID", "uri"), drop = TRUE]
mainstems$head_nhdpv2_COMID <- as.integer(gsub("https://geoconnex.us/nhdplusv2/comid/", "",
mainstems$head_nhdpv2_COMID))

vaa <- vaa[,c("comid", "uri")]

names(vaa) <- c("comid", "mainstem_uri")

vaa <- vaa[!is.na(comid)]

gage_hydrologic_locations <- data.table::as.data.table(gage_hydrologic_locations)

out <- data.table::merge.data.table(gage_hydrologic_locations, vaa,
by.x = "nhdpv2_COMID", by.y = "comid",
all.x = TRUE, all.y = FALSE)

sf::st_sf(as.data.frame(out))
mainstem_lookup <- group_by(vaa, levelpathi) |>
filter(hydroseq == max(hydroseq)) |>
ungroup() |>
select(head_nhdpv2_COMID = comid, levelpathi) |>
distinct() |>
left_join(mainstems, by = "head_nhdpv2_COMID") |>
filter(!is.na(uri)) |>
select(-head_nhdpv2_COMID) |>
right_join(select(vaa, comid, levelpathi),
by = "levelpathi") |>
select(-levelpathi, comid, mainstem_uri = uri)
dplyr::left_join(gage_hydrologic_locations, mainstem_lookup, by = c("nhdpv2_COMID" = "comid"))
}
2 changes: 1 addition & 1 deletion _targets.R
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ list(
tar_target("nhdpv2_fline_proc", select(st_transform(nhdpv2_fline, 5070),
COMID, REACHCODE, ToMeas, FromMeas)),
tar_target("mainstems", get_all_mainstems("data/mainstems/")),
tar_target("vaa", get_vaa(atts = c("comid", "levelpathi"),
tar_target("vaa", get_vaa(atts = c("comid", "levelpathi", "hydroseq"),
updated_network = TRUE)),
# This function downloads all NWIS sites from the site file
tar_target("nwis_gage", get_nwis_sites()),
Expand Down

0 comments on commit 87d6683

Please sign in to comment.