Kjetil Haukås
Calculate and visualize shortest paths using sf object with minimal coding. Package is based on the following source:
https://www.r-spatial.org/r/2019/09/26/spatial-networks.html
devtools::install_github("kjetilhaukas/shortestpath", auth_token = github_token)
library(shortestpath)
library(ggplot2)
library(sf)
muenster <- get_sample_data()
ggplot(muenster) +
geom_sf()
graph <- sf_to_tidygraph(muenster, directed = FALSE)
path <- shortest_path(graph, from_node = 3, to_node = c(100, 106))
visualize_path(path[[1]], graph)
route_info <- subset_edges_as_sf(graph, path[[1]])
route_info %>%
summarise(lenght = sum(st_length(geometry)))
## Simple feature collection with 1 feature and 1 field
## geometry type: MULTILINESTRING
## dimension: XY
## bbox: xmin: 7.625097 ymin: 51.95617 xmax: 7.628404 ymax: 51.96413
## geographic CRS: WGS 84
## # A tibble: 1 x 2
## lenght geometry
## [m] <MULTILINESTRING [°]>
## 1 1083.412 ((7.627493 51.96123, 7.627585 51.96129, 7.627779 51.9616, 7.627867 5~
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.