Skip to content

Commit 443964f

Browse files
authored
Add files via upload
1 parent f136555 commit 443964f

File tree

6 files changed

+109
-2
lines changed

6 files changed

+109
-2
lines changed

R/RcppExports.R

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ Dijkstra_multi_path <- function(gfrom, gto, gw, NbNodes, dep, arr, dict) {
4141
.Call(`_cppRouting_Dijkstra_multi_path`, gfrom, gto, gw, NbNodes, dep, arr, dict)
4242
}
4343

44+
Remove_duplicate <- function(gfrom, gto, gw, NbNodes) {
45+
.Call(`_cppRouting_Remove_duplicate`, gfrom, gto, gw, NbNodes)
46+
}
47+
4448
Isochrone <- function(gfrom, gto, gw, NbNodes, dep, max_limit, dict) {
4549
.Call(`_cppRouting_Isochrone`, gfrom, gto, gw, NbNodes, dep, max_limit, dict)
4650
}
@@ -49,3 +53,7 @@ Isochrone_multi <- function(gfrom, gto, gw, NbNodes, dep, limit_vec, max_limit,
4953
.Call(`_cppRouting_Isochrone_multi`, gfrom, gto, gw, NbNodes, dep, limit_vec, max_limit, setdif, dict)
5054
}
5155

56+
Simplify2 <- function(gfrom, gto, gw, NbNodes, loop, keep, dict) {
57+
.Call(`_cppRouting_Simplify2`, gfrom, gto, gw, NbNodes, loop, keep, dict)
58+
}
59+

R/get_distance_pair.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,12 @@ get_distance_pair<-function(Graph,from,to,algorithm="Dijkstra",constant=1,allcor
4343
if (any(is.na(cbind(from,to)))) stop("NAs are not allowed in origin/destination nodes")
4444

4545
from<-as.character(from)
46-
from_id<-Graph$dict$id[match(from,Graph$dict$ref)]
46+
4747
to<-as.character(to)
48+
allnodes<-c(from,to)
49+
if (sum(allnodes %in% Graph$dict$ref)<length(allnodes)) stop("Some nodes are not in the graph")
50+
51+
from_id<-Graph$dict$id[match(from,Graph$dict$ref)]
4852
to_id<-Graph$dict$id[match(to,Graph$dict$ref)]
4953

5054

R/get_isochrone.R

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ get_isochrone<-function(Graph,from,lim,setdif=FALSE){
2929

3030
if (any(is.na(from))) stop("NAs are not allowed in origin nodes")
3131
from<-as.character(from)
32+
if (sum(from %in% Graph$dict$ref)<length(from)) stop("Some nodes are not in the graph")
33+
3234
from_id<-Graph$dict$id[match(from,Graph$dict$ref)]
3335
lim<-as.numeric(lim)
3436
if (any(is.na(lim))) stop("NAs are not allowed in cost value(s)")

R/get_path_pair.R

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,12 @@ get_path_pair<-function(Graph,from,to,algorithm="Dijkstra",constant=1){
4141

4242
if (any(is.na(cbind(from,to)))) stop("NAs are not allowed in origin/destination nodes")
4343
from<-as.character(from)
44-
from_id<-Graph$dict$id[match(from,Graph$dict$ref)]
44+
4545
to<-as.character(to)
46+
allnodes<-c(from,to)
47+
if (sum(allnodes %in% Graph$dict$ref)<length(allnodes)) stop("Some nodes are not in the graph")
48+
49+
from_id<-Graph$dict$id[match(from,Graph$dict$ref)]
4650
to_id<-Graph$dict$id[match(to,Graph$dict$ref)]
4751

4852

R/simplify.R

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
cpp_simplify<-function(Graph,keep=NULL,new_edges=FALSE,rm_loop=TRUE,iterate=FALSE,silent=TRUE){
2+
3+
#Nodes to keep
4+
to_keep<-rep(0,Graph$nbnode)
5+
if (!is.null(keep)) {
6+
to_keep[Graph$dict$ref %in% keep]<-1
7+
}
8+
9+
10+
simp<-Simplify2(Graph$data$from,Graph$data$to,Graph$data$dist,Graph$nbnode,loop=rm_loop,keep = to_keep,dict = Graph$dict$ref)
11+
12+
13+
14+
if (new_edges==TRUE)edges<-list(simp[[3]])
15+
else edges<-NULL
16+
17+
18+
19+
20+
#Because removing nodes can create other nodes to remove
21+
counter<-1
22+
while(iterate==TRUE){
23+
if (counter==1 & silent==FALSE) message(paste(" iteration :",counter,"-",Graph$nbnode-simp[[2]],"nodes removed"))
24+
25+
if (simp[[2]]==Graph$nbnode) break
26+
count<-simp[[2]]
27+
28+
29+
30+
rd<-Remove_duplicate(simp[[1]][,1],simp[[1]][,2],simp[[1]][,3],Graph$nbnode)
31+
simp<-Simplify2(rd[,1],rd[,2],rd[,3],Graph$nbnode,loop=rm_loop,keep = to_keep,dict = Graph$dict$ref)
32+
33+
counter<-counter+1
34+
35+
36+
37+
38+
if (count == simp[[2]]) break
39+
40+
if(silent==FALSE) message(paste(" iteration :",counter,"-",count-simp[[2]],"nodes removed"))
41+
42+
43+
44+
if (new_edges==TRUE) edges[[length(edges)+1]]<-simp[[3]]
45+
46+
}
47+
48+
rd<-Remove_duplicate(simp[[1]][,1],simp[[1]][,2],simp[[1]][,3],Graph$nbnode)
49+
50+
51+
simp<-rd
52+
if (nrow(simp)==0) stop("All nodes have been removed")
53+
54+
Nodes=unique(c(simp[,1],simp[,2]))
55+
56+
57+
dict<-Graph$dict[Graph$dict$id %in% Nodes,]
58+
dict$idnew<-0:(nrow(dict)-1)
59+
simp[,1]<-dict$idnew[match(simp[,1],dict$id)]
60+
simp[,2]<-dict$idnew[match(simp[,2],dict$id)]
61+
simp<-as.data.frame(simp)
62+
simp[,1]<-as.integer(simp[,1])
63+
simp[,2]<-as.integer(simp[,2])
64+
colnames(simp)<-c("from","to","dist")
65+
if (!is.null(Graph$coords)){
66+
coords<-Graph$coords
67+
coords<-coords[match(dict$id,Graph$dict$id),]
68+
}
69+
else coords=NULL
70+
71+
dict<-dict[,-2]
72+
colnames(dict)<-c("ref","id")
73+
74+
75+
76+
77+
return(list(graph=list(data=simp,
78+
coords=coords,
79+
nbnode=length(Nodes),
80+
dict=dict),
81+
new_edges=edges))
82+
83+
}

R/to_df.R

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
to_df<-function(Graph){
2+
dat<-Graph$data
3+
dat$from<-Graph$dict$ref[match(dat$from,Graph$dict$id)]
4+
dat$to<-Graph$dict$ref[match(dat$to,Graph$dict$id)]
5+
return(dat)
6+
}

0 commit comments

Comments
 (0)