-
Notifications
You must be signed in to change notification settings - Fork 0
/
README.qmd
78 lines (58 loc) · 1.95 KB
/
README.qmd
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
---
title: "Sample of cppRouting with SfNetworks"
format: gfm
---
```{R, echo = FALSE}
#spsUtil::quiet(pacman::p_install("mapview", force = FALSE))
spsUtil::quiet(pacman::p_install("sf", force = FALSE))
spsUtil::quiet(pacman::p_install("sfnetworks", force = FALSE))
spsUtil::quiet(pacman::p_install("dplyr", force = FALSE))
```
Sample code to use sfnetworks with cppRouting
First load the functions.
```{R}
library(wrapr)
#Transform the sfnetworks network, in cppRouting format
sfnet2cppRouting <- modules::use("sfnet2cppRouting.R")
shortest_path <- modules::use("shortest_path.R")
min_distance <- modules::use("min_distance.R")
```
Then load the network and get the cppRouting one too.
```{R}
network <- "oldenburg_walking_network.geojson" %.>%
#Read the file
sf::st_read(., quiet = TRUE) %.>%
#Only linestrings
sf::st_cast(., "LINESTRING") %.>%
#Set a column for the weight of the line as the distance
dplyr::mutate(., weight = sf::st_length(.)) %.>%
#Get the sfnetworks object
sfnetworks::as_sfnetwork(., directed = FALSE)
weight <- "weight"
cppRouting_graph <- sfnet2cppRouting$sfnet2cppRouting(network, weight)
```
Set some random nodes to use:
```{R}
from <- c(1, 2, 3)
to <- c(5, 6, 7)
```
## Shortest Path Matrix:
```{R}
shortest_path$networks.shortest_path.node2node.cppRouting.matrix(network, cppRouting_graph, from, to)
```
## Shortest Path Pairs
```{R}
shortest_path$networks.shortest_path.node2node.cppRouting.pairs(network, cppRouting_graph, from, to)
```
## Min Distance Matrix
```{R}
min_distance$networks.min_distance.node2node.cppRouting.matrix(network, cppRouting_graph, from, to)
```
## Min Distance Paris
```{R}
min_distance$networks.min_distance.node2node.cppRouting.pairs(network, cppRouting_graph, from, to)
```
## Notes
All returns are a dataframe with "from", "to" columns with the nodes, the last column depends if is distance or shortestpath.
- distance: Use the column distance
- shortestpath: Use the "path" column, is a geom column