README.md

R package shortestpath - Calculate shortest paths from sf objects

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)

Load some data

muenster <- get_sample_data()

ggplot(muenster) +
  geom_sf()

Create graph

graph <- sf_to_tidygraph(muenster, directed = FALSE)

Calculate shortest paths

path <- shortest_path(graph, from_node = 3, to_node = c(100, 106))

Visualize path

visualize_path(path[[1]], graph)

Create summary statistics

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~


kjetilhaukas/shortestpath documentation built on April 28, 2022, 12:06 a.m.