## ----include = FALSE----------------------------------------------------------
knitr::opts_chunk$set(
  collapse = TRUE,
  comment = "#>"
)

## ----setup--------------------------------------------------------------------
library(RGDrivers)
library(terra)

## -----------------------------------------------------------------------------
network <- vect(system.file("extdata", "rede_exemplo.gpkg", package = "RGDrivers"))
network

## -----------------------------------------------------------------------------
g <- create_network_graph(network, col_id = "id", col_downstream = "jus")
g

## -----------------------------------------------------------------------------
lm <- calculate_link_magnitude(g)
lm

## -----------------------------------------------------------------------------
df <- as.data.frame(network)
df$link_mag <- lm[as.character(df$id)]
df <- calculate_dlink(df, col_id = "id", col_downstream = "jus", col_order = "link_mag")
df

## -----------------------------------------------------------------------------
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)

