-
Notifications
You must be signed in to change notification settings - Fork 0
/
sfnet2cppRouting.R
40 lines (36 loc) · 1.51 KB
/
sfnet2cppRouting.R
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
#Author: Felipe Matas, with University Adolfo Ibáñez
spsUtil::quiet(pacman::p_install("wrapr", force = FALSE))
spsUtil::quiet(pacman::p_install("igraph", force = FALSE))
spsUtil::quiet(pacman::p_install("dplyr", force = FALSE))
spsUtil::quiet(pacman::p_install("sf", force = FALSE))
spsUtil::quiet(pacman::p_install("cppRouting", force = FALSE))
modules::import("wrapr")
sfnet2cppRouting <- function(network, weight){
#if (is.null(network$libs)){
# network$libs <- list()
#}
#The idea is insert the graph in the ver, but future lib have
#problems, and the var is not completely moved inside the new
#child, causing to fail
#if (is.null(network$libs$cppRouting)){
cppRouting_graph <- cppRouting::makegraph(
network %.>%
sfnetworks::activate(., "edges") %.>%
sf::st_as_sf(.) %.>%
sf::st_drop_geometry(.) %.>%
as.data.frame(.) %.>%
dplyr::transmute(., from, to, cost=.data[[weight]])
,
directed = igraph::is_directed(network)
)
#}
original_nodes <- network %.>%
sfnetworks::activate(., "nodes") %.>%
sf::st_as_sf(.)
#This has happened some time ago, no idea why some nodes was deleted on the transformation
#I tested it again with recent versions of cppRouging, all worked, still I like have this check
if (nrow(original_nodes) != cppRouting_graph$nbnode){
stop("The net was simplified by cppRouting, the nodes assignation can be broken.")
}
cppRouting_graph
}