# Two crossing roads
a <- vect("LINESTRING(0 0, 10 10)", crs = "local")
b <- vect("LINESTRING(0 10, 10 0)", crs = "local")
roads <- rbind(a, b)
roads$speed <- c(50, 100) # km/h, used as a "cost" column
net <- netw(roads)
sp <- shortestPath(net, from = 1, to = 4)
sp$distance
# Multiple destinations from one source
shortestPath(net, from = 1, to = c(2, 3, 4, 5))$distance
# Custom weights from a column of the source SpatVector
nslow <- netw(roads, weights = "speed")
shortestPath(nslow, from = 1, to = 4)$distance
Run the code above in your browser using DataLab