Skip to contents

This is to prepare the network for the search of shortest paths between node pairs. The computed weights can account for edge lenghts, distance from a target geometry, and whether or not an edge falls within a specified region, which we aim to exclude from search of the shortest paths.

Usage

add_weights(
  network,
  target = NULL,
  exclude_area = NULL,
  penalty = 1000,
  weight_name = "weight"
)

Arguments

network

A network object

target

Target geometry to calculate distances from, as a simple feature geometry

exclude_area

Area that we aim to exclude from the shortest-path search, as a simple feature geometry

penalty

Penalty (in the network CRS' units) that is added to the edges that falls within the excluded area

weight_name

Name of the column in the edge table where to add the weights

Value

A network object with weights added as a column in the edge table

Details

For the i-th edge of the network, its weight \(w_i\) is defined in the following way: $$ w_i = |e_i| + d_{geom}(e_i) + p_{buf}(e_i) $$ where the first term is the edge length, the second one is the distance from a target geometry (target, optional) and the last one is a penalty that is added if the centroid of the edge falls within a specified region (exclude_area, optional).

Shortest paths calculated on the resulting network will thus tend to prefer edges close to target and to avoid edges within exclude_area.

Examples

if (!requireNamespace("CRiSpData", quietly = TRUE)) {
  message("Install CRiSpData from GitHub to run this example.")
} else {
  edges <- rbind(CRiSpData::bucharest_osm$streets,
                CRiSpData::bucharest_osm$railways)
  network <- sfnetworks::as_sfnetwork(edges, directed = FALSE)
  target <- sf::st_centroid(CRiSpData::bucharest_osm$river_centerline)
  exclude_area <- sf::st_buffer(CRiSpData::bucharest_osm$river_centerline,
                                1000)
  add_weights(network, target, exclude_area)
}
#> # A sfnetwork with 9318 nodes and 9640 edges
#> #
#> # CRS:  EPSG:32635 
#> #
#> # An undirected multigraph with 942 components with spatially explicit edges
#> #
#> # Edge data: 9,640 × 5 (active)
#>    from    to type                                               geometry weight
#>   <int> <int> <chr>                                      <LINESTRING [m]>    [m]
#> 1     1     2 tertiary (427899.3 4924401, 427901.6 4924397, 427911.8 492… 59839.
#> 2     3     4 tertiary (427853.8 4924451, 427842.3 4924442, 427797.7 492… 59559.
#> 3     5     6 primary  (427348.7 4924016, 427360 4924008, 427382.4 49239… 59388.
#> 4     7     8 primary  (432042.4 4918609, 432051.4 4918605, 432065 49185… 67098.
#> 5     9    10 tertiary (432099.2 4919990, 432100.5 4919987, 432101.6 491… 66002.
#> 6    11    12 primary  (435628.7 4921177, 435651.9 4921179, 435682.8 492… 67143.
#> # ℹ 9,634 more rows
#> #
#> # Node data: 9,318 × 1
#>             geometry
#>          <POINT [m]>
#> 1 (427899.3 4924401)
#> 2 (428081.7 4924019)
#> 3 (427853.8 4924451)
#> # ℹ 9,315 more rows