RGDrivers

library(RGDrivers)
library(terra)
#> terra 1.9.34

Introduction

RGDrivers provides tools for analyzing stream networks, including the construction of directed graphs from stream reaches, the calculation of Link Magnitude (Shreve stream order) and D-LINK (downstream reach order), and the export of results back to a spatial file.

This vignette uses the small sample set of stream reaches included in the package (inst/extdata/rede_exemplo.gpkg).

Loading the sample data

network <- vect(system.file("extdata", "rede_exemplo.gpkg", package = "RGDrivers"))
network
#> class       : SpatVector
#> geometry    : lines
#> dimensions  : 3, 2  (geometries, attributes)
#> extent      : 0, 3, 0, 3  (xmin, xmax, ymin, ymax)
#> source      : rede_exemplo.gpkg
#> coord. ref. : lon/lat WGS 84 (EPSG:4326)
#> names       :    id   jus
#> type        : <num> <num>
#> values      :     1     2
#>                   2     3
#>                   3    NA

Creating the directed graph

g <- create_network_graph(network, col_id = "id", col_downstream = "jus")
g
#> IGRAPH 6590aea DN-- 3 2 -- 
#> + attr: name (v/c)
#> + edges from 6590aea (vertex names):
#> [1] 1->2 2->3

Writing the result to a new spatial vector

network$link_mag <- df$link_mag
network$D_LINK <- df$D_LINK

output <- tempfile(fileext = ".gpkg")
write_network_with_orders(
  original_vector = network,
  updated_df = df,
  col_id = "id",
  additional_cols = c("link_mag", "D_LINK"),
  output_path = output
)
file.exists(output)
#> [1] TRUE