1
+
2
+
3
+
1
4
# if running manually, please run the following line first:
2
5
# source("tests/testthat/setup.R")
3
6
@@ -7,10 +10,11 @@ testthat::skip_on_cran()
7
10
# test if setting max_speed to 0 closes the road
8
11
9
12
# data.frame with new speed info
10
- new_carspeeds <- read.csv(file.path(data_path , " poa_osm_congestion.csv" ))
13
+ edge_speeds <- read.csv(file.path(data_path , " poa_osm_congestion.csv" ))
11
14
12
15
# sf with congestion polygons
13
16
congestion_poly <- readRDS(file.path(data_path , " poa_poly_congestion.rds" ))
17
+ congestion_poly $ scale <- c(0.2 , 0.5 )
14
18
15
19
# get origin and destination points in a single road
16
20
network <- r5r :: street_network_to_sf(r5r_network )
@@ -31,12 +35,14 @@ point_dest$id <- as.character(point_dest$id)
31
35
meta_fun <- function (
32
36
fun = r5r :: travel_time_matrix ,
33
37
new_carspeeds = NULL ,
34
- carspeed_scale = 1 ){
38
+ carspeed_scale = 1 ,
39
+ point_origg = point_orig ,
40
+ point_destt = point_dest ){
35
41
36
42
fun(
37
43
r5r_network = r5r_network ,
38
- origins = point_orig ,
39
- destinations = point_dest ,
44
+ origins = point_origg ,
45
+ destinations = point_destt ,
40
46
mode = ' car' ,
41
47
departure_datetime = Sys.time(),
42
48
max_trip_duration = 60 ,
@@ -48,8 +54,7 @@ meta_fun <- function(
48
54
49
55
50
56
# car speeds with osm ids -------------------------------------------------------------------
51
- test_that(" success in increasing travel times" , {
52
-
57
+ test_that(" success in increasing travel times with osm ids" , {
53
58
54
59
# calculate travel times / access *before* changing road speeds
55
60
ttm_pre <- meta_fun(r5r :: travel_time_matrix )
@@ -64,25 +69,21 @@ test_that("success in increasing travel times", {
64
69
max_trip_duration = 60
65
70
)
66
71
# to do: r5r::accessibility
67
-
68
72
# plot(det_pre['total_duration'])
69
- # mapview(network$edges) + network$vertices + det
70
-
71
- # put all roads at 10% of their speed
72
- mock_data <- data.frame (osm_id = 9999 , max_speed = 9999 , speed_type = " scale" )
73
+ # mapview::mapview(network$edges) + network$vertices + det_pre
74
+ # mapview::mapview(network$edges) + det_pre
73
75
74
- # calculate travel times / access *before* changing road speeds
75
- ttm_pos <- meta_fun(r5r :: travel_time_matrix , new_carspeeds = mock_data , carspeed_scale = 0.1 )
76
- expanded_ttm_pos <- meta_fun(r5r :: expanded_travel_time_matrix , new_carspeeds = mock_data , carspeed_scale = 0.1 )
77
- det_pos <- meta_fun(r5r :: detailed_itineraries , new_carspeeds = mock_data , carspeed_scale = 0.5 )
76
+ # changing CARSPEED_SCALE without changing new_carspeeds
77
+ ttm_pos <- meta_fun(r5r :: travel_time_matrix , carspeed_scale = 0.1 )
78
+ expanded_ttm_pos <- meta_fun(r5r :: expanded_travel_time_matrix , carspeed_scale = 0.1 )
79
+ det_pos <- meta_fun(r5r :: detailed_itineraries , carspeed_scale = 0.5 )
78
80
arrival_ttm_pos <- r5r :: arrival_travel_time_matrix(
79
81
r5r_network = r5r_network ,
80
82
origins = point_orig ,
81
83
destinations = point_dest ,
82
84
mode = ' car' ,
83
85
arrival_datetime = Sys.time(),
84
86
max_trip_duration = 60 ,
85
- new_carspeeds = mock_data ,
86
87
carspeed_scale = 0.1
87
88
)
88
89
@@ -96,11 +97,76 @@ test_that("success in increasing travel times", {
96
97
testthat :: expect_true(det_pos $ total_duration > det_pre $ total_duration )
97
98
# testthat::expect_true(det_pos$total_distance == det_pre$total_distance)
98
99
100
+ # setting NEW_CARSPEEDS without changing carspeed_scale
101
+ fast_carspeeds <- data.frame (osm_id = c(450002312 , 390862071 ), max_speed = 1.5 , speed_type = " scale" )
102
+ ttm_3 <- meta_fun(r5r :: travel_time_matrix , new_carspeeds = fast_carspeeds )
103
+ testthat :: expect_true(ttm_3 $ travel_time_p50 < ttm_pre $ travel_time_p50 )
104
+
105
+ # test closing road with speed = 0"
106
+ closed_road <- data.frame (osm_id = 450002312 , max_speed = 0 , speed_type = " scale" )
107
+ det_closed <- meta_fun(r5r :: detailed_itineraries , new_carspeeds = closed_road )
108
+ testthat :: expect_true(det_closed $ total_duration > det_pre $ total_duration )
109
+ # mapview::mapview(det_closed) + det_pre
110
+
99
111
})
100
112
101
113
114
+
102
115
# car speeds with polygons -------------------------------------------------------------------
116
+ test_that(" success in increasing travel times with polygons" , {
117
+
118
+ # calculate travel times / access *before* changing road speeds
119
+ ttm_pre <- meta_fun(r5r :: travel_time_matrix ,
120
+ point_origg = pois [1 ], point_destt = pois [12 ])
121
+ expanded_ttm_pre <- meta_fun(r5r :: expanded_travel_time_matrix ,
122
+ point_origg = pois [1 ], point_destt = pois [12 ])
123
+ det_pre <- meta_fun(r5r :: detailed_itineraries ,
124
+ point_origg = pois [1 ], point_destt = pois [12 ])
125
+ arrival_ttm_pre <- r5r :: arrival_travel_time_matrix(
126
+ r5r_network = r5r_network ,
127
+ origins = pois [1 ],
128
+ destinations = pois [12 ],
129
+ mode = ' car' ,
130
+ arrival_datetime = Sys.time(),
131
+ max_trip_duration = 60
132
+ )
133
+ # to do: r5r::accessibility
134
+ # plot(det_pre['total_duration'])
135
+ # mapview::mapview(network$edges) + network$vertices + det
136
+
137
+ # changing CARSPEED_SCALE without changing new_carspeeds
138
+ ttm_pos <- meta_fun(r5r :: travel_time_matrix , new_carspeeds = congestion_poly ,
139
+ point_origg = pois [1 ], point_destt = pois [12 ])
140
+ expanded_ttm_pos <- meta_fun(r5r :: expanded_travel_time_matrix , new_carspeeds = congestion_poly ,
141
+ point_origg = pois [1 ], point_destt = pois [12 ])
142
+ det_pos <- meta_fun(r5r :: detailed_itineraries , new_carspeeds = congestion_poly ,
143
+ point_origg = pois [1 ], point_destt = pois [12 ])
144
+ arrival_ttm_pos <- r5r :: arrival_travel_time_matrix(
145
+ r5r_network = r5r_network ,
146
+ origins = pois [1 ],
147
+ destinations = pois [12 ],
148
+ mode = ' car' ,
149
+ arrival_datetime = Sys.time(),
150
+ max_trip_duration = 60 ,
151
+ new_carspeeds = congestion_poly
152
+ )
153
+
154
+
155
+ # mapview::mapview(det_pre) + det_pos
156
+
157
+ # checking for longer travel times
158
+ testthat :: expect_true(ttm_pos $ travel_time_p50 > ttm_pre $ travel_time_p50 )
159
+ testthat :: expect_true(all(expanded_ttm_pos $ total_time > expanded_ttm_pre $ total_time ))
160
+ testthat :: expect_true(arrival_ttm_pos $ total_time > arrival_ttm_pre $ total_time )
161
+ testthat :: expect_true(det_pos $ total_duration > det_pre $ total_duration )
162
+ # testthat::expect_true(det_pos$total_distance == det_pre$total_distance)
103
163
164
+ # setting carspeed_scale without changing NEW_CARSPEEDS
165
+ ttm_3 <- meta_fun(r5r :: travel_time_matrix , carspeed_scale = 1.3 )
166
+ testthat :: expect_true(ttm_3 $ travel_time_p50 < ttm_pre $ travel_time_p50 )
167
+ })
168
+
169
+ # errors in congestion polygon -------------------------------------------------------------------
104
170
test_that(" errors in congestion polygon" , {
105
171
106
172
# wrong col names
@@ -133,29 +199,30 @@ test_that("errors due to incorrect input types", {
133
199
})
134
200
135
201
136
-
137
-
138
202
test_that(" errors error in the new_carspeeds column names" , {
139
203
140
- mock_data <- data.frame (osm_id = ' 9999 ' , max_speed = 9999 , speed_type = " banana" )
204
+ mock_data <- data.frame (osm_id = ' 27184648 ' , max_speed = 10 , speed_type = " banana" )
141
205
testthat :: expect_error(meta_fun(new_carspeeds = mock_data ))
142
206
143
207
mock_data <- data.frame (my_osm_id = ' 9999' , max_speed = 9999 , speed_type = " km/h" )
144
208
testthat :: expect_error(meta_fun(new_carspeeds = mock_data ))
145
209
146
- mock_data <- data.frame (osm_id = ' 9999' , max_speed = 9999 , speed_type = " km/h" )
147
- testthat :: expect_error(meta_fun(new_carspeeds = mock_data , carspeed_scale = Inf ))
148
- testthat :: expect_error(meta_fun(new_carspeeds = mock_data , carspeed_scale = 0 ))
210
+ testthat :: expect_error(meta_fun(carspeed_scale = Inf ))
211
+ testthat :: expect_error(meta_fun(carspeed_scale = - 1 ))
149
212
150
213
})
151
214
152
215
153
- # test_that("message for missing OSM ids", {
154
- #
155
- # mock_data <- data.frame(osm_id = 9999, max_speed = 100, speed_type="km/h")
156
- #
157
- # testthat::expect_message(
158
- # meta_fun(new_carspeeds = mock_data, carspeed_scale = 1),
159
- # regexp = "Cannot find the following OSM IDs in network"
160
- # )
161
- # })
216
+ test_that(" message for missing OSM ids" , {
217
+
218
+ mock_data <- data.frame (osm_id = 45698769 , max_speed = 100 , speed_type = " km/h" )
219
+ log_file <- file.path(r5r_network @ jcore $ getLogPath())
220
+ expect_true(file.exists(log_file ), info = paste(" Log file not found at" , log_file ))
221
+
222
+ meta_fun(new_carspeeds = mock_data )
223
+ log <- readLines(log_file , warn = FALSE )
224
+ expect_true(
225
+ any(grepl(" 45698769" , log , fixed = TRUE )),
226
+ info = " Did not find warning for a bad OSM Id in log"
227
+ )
228
+ })
0 commit comments