Skip to contents

A spatial network specific join function which makes a spatial full join on the geometries of the nodes data. Edge data are combined using a bind_rows semantic, meaning that data are matched by column name and values are filled with NA if missing in either of the networks. The from and to columns in the edge data are updated such that they match the new node indices of the resulting network.

Usage

st_network_join(x, y, ...)

Arguments

x

An object of class sfnetwork.

y

An object of class sfnetwork, or directly convertible to it using as_sfnetwork.

...

Arguments passed on to graph_join.

Value

The joined networks as an object of class sfnetwork.

Note

By default sfnetworks rounds coordinates to 12 decimal places to determine spatial equality. You can influence this behavior by explicitly setting the precision of the networks using st_set_precision.

Examples

library(sf, quietly = TRUE)

oldpar = par(no.readonly = TRUE)
par(mar = c(1,1,1,1), mfrow = c(1,2))

# Create two networks.
n1 = st_point(c(0, 0))
n2 = st_point(c(1, 0))
n3 = st_point(c(1,1))
n4 = st_point(c(0,1))

e1 = st_sfc(st_linestring(c(n1, n2)))
e2 = st_sfc(st_linestring(c(n2, n3)))
e3 = st_sfc(st_linestring(c(n3, n4)))

neta = as_sfnetwork(c(e1, e2))
netb = as_sfnetwork(c(e2, e3))

# Join the networks based on spatial equality of nodes.
net = st_network_join(neta, netb)
net
#> # A sfnetwork: 4 nodes and 4 edges
#> #
#> # A directed acyclic multigraph with 1 component and spatially explicit edges
#> #
#> # Dimension: XY
#> # Bounding box: xmin: 0 ymin: 0 xmax: 1 ymax: 1
#> # CRS: NA
#> #
#> # Node data: 4 × 1 (active)
#>         x
#>   <POINT>
#> 1   (0 0)
#> 2   (1 0)
#> 3   (1 1)
#> 4   (0 1)
#> #
#> # Edge data: 4 × 3
#>    from    to            x
#>   <int> <int> <LINESTRING>
#> 1     1     2   (0 0, 1 0)
#> 2     2     3   (1 0, 1 1)
#> 3     2     3   (1 0, 1 1)
#> # ℹ 1 more row

# Plot.
plot(neta, pch = 15, cex = 2, lwd = 4)
plot(netb, col = "orange", pch = 18, cex = 2, lty = 3, lwd = 4, add = TRUE)
plot(net, cex = 2, lwd = 4)


par(oldpar)