source(system.file("extdata/sample_data.R", package = "nhdplusTools"))
fline <- sf::read_sf(sample_data, "NHDFlowline_Network")
# We add a tocomid with prepare_nhdplus
fline <- sf::st_sf(prepare_nhdplus(fline, 0, 0, 0, FALSE),
geom = sf::st_zm(sf::st_geometry(fline)))
# Look at the end node of the 10th line.
(n1 <- get_node(fline[10, ], position = "end"))
# Break the geometry by reversing it.
sf::st_geometry(fline)[10] <- sf::st_reverse(sf::st_geometry(fline)[10])
# Note that the end node is different now.
(n2 <- get_node(fline[10, ], position = "end"))
# Pass the broken geometry to fix_flowdir with the network for toCOMID
sf::st_geometry(fline)[10] <- fix_flowdir(fline$COMID[10], fline)
# Note that the geometry is now in the right order.
(n3 <- get_node(fline[10, ], position = "end"))
plot(sf::st_geometry(fline)[10])
plot(n1, add = TRUE)
plot(n2, add = TRUE, col = "blue")
plot(n3, add = TRUE, cex = 2, col = "red")
Run the code above in your browser using DataLab