lawinslow/hydrolinks

nhdplus flowline traversal blocked by virtual stream segments

Closed this issue · 5 comments

jsta commented

It looks to me like traverse_flowlines using nhdplusv2 does not travel on virtual stream segments while nhdh does travel on them. Specifically, nhdh traverses through wb 155442272 while nhdplusv2 does not travel through (the equivalent) wb 6863055.

Is this intended?

Compare the two chunks below:

setup

library(hydrolinks)
library(purrr)
library(magrittr)

wb_coords <- c(44.00467, -88.43445)

traverse nhd hi-res

dset <- "nhdh"

wb_id <- link_to_waterbodies(wb_coords[1], wb_coords[2], 
                             1, dataset = dset) %>%
         purrr::when(any(names(.) %in% "COMID") 
              ~ .$COMID,
              ~.$PERMANENT_)

nhd_wb <- get_shape_by_id(wb_id, feature_type = "waterbody", dataset = dset)

f_lines <- traverse_flowlines(max_distance = 50, direction = "in", 
                   start = wb_id, 
                   dataset = dset, md5check = FALSE) %>%
  purrr::when(any(names(.) %in% "COMID") 
              ~ .$COMID,
              ~.$PERMANENT_)

upstream_shp <- get_shape_by_id(f_lines, dataset = dset, 
                                feature_type = "flowline")

plot(upstream_shp$geometry)
plot(nhd_wb$geometry, add = TRUE)

nhd

traverse nhdplus

dset <- "nhdplusv2"

wb_id <- link_to_waterbodies(wb_coords[1], wb_coords[2], 
                             1, dataset = dset) %>%
  purrr::when(any(names(.) %in% "COMID") 
              ~ .$COMID,
              ~.$PERMANENT_)

nhd_wb <- get_shape_by_id(wb_id, feature_type = "waterbody", dataset = dset)

f_lines <- traverse_flowlines(max_distance = 50, direction = "in", 
                              start = wb_id, 
                              dataset = dset, md5check = FALSE) %>%
  purrr::when(any(names(.) %in% "COMID") 
              ~ .$COMID,
              ~.$PERMANENT_)

upstream_shp <- get_shape_by_id(f_lines, dataset = dset, 
                                feature_type = "flowline")

plot(upstream_shp$geometry)
plot(nhd_wb$geometry, add = TRUE)

nhdplus

@rainshapes can you diagnose? This is not intended operation, but may be an artifact in dealing with the different dataset structures.

@jsta could you give #45 a try with your above issue? This should have fixed your issue (among others). If it does, we'll work on a CRAN release.

Should be solved in #45

jsta commented

Hmm, the example I linked above gives me an error now for both nhdplus and nhdh:

Error in traverse_flowlines(max_distance = 50, direction = "in", start = wb_id, :
Cannot traverse from node 0!

I already rolled my own traversal code for my use case so I'm afraid I will not do much more to diagnose.

Got it.

For future reference. This is fixed. Reproducible with this code.

library(hydrolinks)
library(purrr)
library(magrittr)

wb_coords <- c(44.00467, -88.43445)
dset <- "nhdh"

wb_id <- link_to_waterbodies(wb_coords[1], wb_coords[2], 
                             1, dataset = dset) %>%
  purrr::when(any(names(.) %in% "comid") 
              ~ .$comid,
              ~.$permanent_)

nhd_wb <- get_shape_by_id(wb_id, feature_type = "waterbody", dataset = dset)


f_lines <- traverse_flowlines(max_distance = 50, direction = "in", 
                              start = wb_id, 
                              dataset = dset, md5check = FALSE) %>%
  purrr::when(any(names(.) %in% "comid") 
              ~ .$comid,
              ~.$permanent_)

upstream_shp <- get_shape_by_id(f_lines, dataset = dset, 
                                feature_type = "flowline")
upstream_wb <- get_shape_by_id(f_lines, dataset = dset, 
                                feature_type = "waterbody")

plot(upstream_shp$geometry)
plot(upstream_wb$geometry, add=TRUE, col='dodgerblue')
plot(nhd_wb$geometry, add = TRUE)

image

dset <- "nhdplusv2"

wb_id <- link_to_waterbodies(wb_coords[1], wb_coords[2], 
                             1, dataset = dset) %>%
  purrr::when(any(names(.) %in% "comid") 
              ~ .$comid,
              ~.$permanent_)

nhd_wb <- get_shape_by_id(wb_id, feature_type = "waterbody", dataset = dset)

f_lines <- traverse_flowlines(max_distance = 50, direction = "in", 
                              start = wb_id, 
                              dataset = dset, md5check = FALSE) %>%
  purrr::when(any(names(.) %in% "comid") 
              ~ .$comid,
              ~.$permanent_)

upstream_shp <- get_shape_by_id(f_lines, dataset = dset, 
                                feature_type = "flowline")
upstream_wb <- get_shape_by_id(f_lines, dataset = dset, 
                               feature_type = "waterbody")

plot(upstream_shp$geometry)
plot(upstream_wb$geometry, add=TRUE, col='dodgerblue')
plot(nhd_wb$geometry, add = TRUE, col='dodgerblue')

image