library(sf, quietly = TRUE)
## Create sfnetwork from sf objects
p1 = st_point(c(7, 51))
p2 = st_point(c(7, 52))
p3 = st_point(c(8, 52))
nodes = st_as_sf(st_sfc(p1, p2, p3, crs = 4326))
e1 = st_cast(st_union(p1, p2), "LINESTRING")
e2 = st_cast(st_union(p1, p3), "LINESTRING")
e3 = st_cast(st_union(p3, p2), "LINESTRING")
edges = st_as_sf(st_sfc(e1, e2, e3, crs = 4326))
edges$from = c(1, 1, 3)
edges$to = c(2, 3, 2)
# Default.
sfnetwork(nodes, edges)
# Undirected network.
sfnetwork(nodes, edges, directed = FALSE)
# Using character encoded from and to columns.
nodes$name = c("city", "village", "farm")
edges$from = c("city", "city", "farm")
edges$to = c("village", "farm", "village")
sfnetwork(nodes, edges, node_key = "name")
# Spatially implicit edges.
sfnetwork(nodes, edges, edges_as_lines = FALSE)
# Store edge lenghts in a weight column.
sfnetwork(nodes, edges, length_as_weight = TRUE)
# Adjust the number of features printed by active and inactive components
oldoptions = options(sfn_max_print_active = 1, sfn_max_print_inactive = 2)
sfnetwork(nodes, edges)
options(oldoptions)
Run the code above in your browser using DataLab