-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute_fun.R
84 lines (59 loc) · 1.99 KB
/
route_fun.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
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
79
80
81
82
83
84
#Functions and libraries called by route.R
library(httr)
library(rjson)
#Send pairs of co-ordinates to local openrouteservice server
get_route_dist <- function(lat1, lon1, lat2, lon2){
url <- paste0('http://localhost:8080/ors/v2/directions/driving-car?start=',
lon1,
',',
lat1,
'&end=',
lon2,
',',
lat2)
res <- GET(url,
add_headers(accept='application/json')
)
r <- fromJSON(content(res, 'text', encoding = 'UTF-8'))
if(is.null(r$features[[1]]$properties$segments[[1]]$distance)){
distance <- 1000000
}else{
distance <- r$features[[1]]$properties$segments[[1]]$distance
}
print(distance)
return(distance)
}
#Compares start co-ordrinates to all points in data frame of co-ordinates
#Calls get_route_dist
one_step <- function(lat1, lon1, destination){
one_step_mat <- matrix(data=0, nrow=nrow(destination), ncol=2)
one_step_df <- as.data.frame(one_step_mat)
for(i in 1:nrow(destination)){
one_end_lon <- destination[i,2]
one_end_lat <- destination[i,3]
distance <- get_route_dist(lat1 = lat1,
lon1 = lon1,
lat2 = one_end_lat,
lon2 = one_end_lon
)
one_step_df[i,1] <- as.character(destination[i, 1])
one_step_df[i,2] <- distance
}
colnames(one_step_df) <- c("address", "distance")
return(one_step_df)
}
#Old function: uses here.com API for route finding
next_loc <- function(start, end){
begin <- geocode(start,
autocomplete = FALSE, sf = TRUE, url_only = FALSE)
finish <- geocode(end,
autocomplete = FALSE, sf = TRUE, url_only = FALSE)
res <- route(origin = begin,
destination = finish,
mode = "car",
type="fastest")
addr_dist <- data.frame("address"=end,
"distance"=res$distance
)
return(addr_dist)
}