stplanr (version 0.9.0)

sum_network_routes: Summarise shortest path between nodes on network

Description

Summarise shortest path between nodes on network

Usage

sum_network_routes(
  sln,
  start,
  end,
  sumvars = weightfield(sln),
  combinations = FALSE
)

Arguments

sln

The SpatialLinesNetwork or sfNetwork to use.

start

Integer of node indices where route starts.

end

Integer of node indices where route ends.

sumvars

Character vector of variables for which to calculate summary statistics. The default value is weightfield(sln).

combinations

Boolean value indicating if all combinations of start and ends should be calculated. If TRUE then every start Node ID will be routed to every end Node ID. This is faster than passing every combination to start and end. Default is FALSE.

Details

Find the shortest path on the network between specified nodes and returns a SpatialLinesDataFrame (or an sf object with LINESTRING geometry) containing the path(s) and summary statistics of each one.

The start and end arguments must be integers representing the node index. To find which node is closest to a geographic point, use find_nearest_node().

If the start and end node are identical, the function will return a degenerate line with just two (identical) points. See #444.

See Also

Other rnet: SpatialLinesNetwork, calc_catchment_sum(), calc_catchment(), calc_moving_catchment(), calc_network_catchment(), find_network_nodes(), gsection(), islines(), lineLabels(), overline_spatial(), overline(), plot,SpatialLinesNetwork,ANY-method, plot,sfNetwork,ANY-method, rnet_breakup_vertices(), rnet_group(), sln2points(), sum_network_links()

Examples

Run this code
# NOT RUN {
sln <- SpatialLinesNetwork(route_network)
weightfield(sln) # field used to determine shortest path
shortpath <- sum_network_routes(sln, start = 1, end = 50, sumvars = "length")
plot(shortpath, col = "red", lwd = 4)
plot(sln, add = TRUE)

# with sf objects
sln <- SpatialLinesNetwork(route_network_sf)
weightfield(sln) # field used to determine shortest path
shortpath <- sum_network_routes(sln, start = 1, end = 50, sumvars = "length")
plot(sf::st_geometry(shortpath), col = "red", lwd = 4)
plot(sln, add = TRUE)

# find shortest path between two coordinates
sf::st_bbox(sln@sl)
start_coords <- c(-1.546, 53.826)
end_coords <- c(-1.519, 53.816)
plot(sln)
plot(sf::st_point(start_coords), cex = 3, add = TRUE, col = "red")
plot(sf::st_point(end_coords), cex = 3, add = TRUE, col = "blue")
nodes <- find_network_nodes(sln, rbind(start_coords, end_coords))
shortpath <- sum_network_routes(sln, nodes[1], nodes[2])
plot(sf::st_geometry(shortpath), col = "darkred", lwd = 3, add = TRUE)

# degenerate path
sum_network_routes(sln, start = 1, end = 1)
# }

Run the code above in your browser using DataLab