Skip to content

Commit 9965e38

Browse files
authored
Merge pull request #498 from ipeaGIT/add-closing-roads
Added closing roads to new_carspeeds
2 parents 7aa9084 + dab597f commit 9965e38

File tree

3 files changed

+109
-35
lines changed

3 files changed

+109
-35
lines changed

java-r5rcore/src/org/ipea/r5r/Scenario/RoadCongestionOSM.java

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,20 @@
11
package org.ipea.r5r.Scenario;
22

3+
34
import com.conveyal.r5.analyst.scenario.Modification;
45
import com.conveyal.r5.streets.EdgeStore;
56
import com.conveyal.r5.transit.TransportNetwork;
67
import gnu.trove.list.TShortList;
8+
import gnu.trove.list.array.TIntArrayList;
79
import gnu.trove.list.array.TLongArrayList;
810
import gnu.trove.list.array.TShortArrayList;
911
import gnu.trove.set.hash.TLongHashSet;
1012
import org.slf4j.LoggerFactory;
1113

1214
import java.util.HashMap;
1315

16+
import static com.conveyal.r5.streets.EdgeStore.EdgeFlag;
17+
1418
public class RoadCongestionOSM extends Modification {
1519
private static final org.slf4j.Logger LOG = LoggerFactory.getLogger(RoadCongestionOSM.class);
1620

@@ -56,16 +60,19 @@ public boolean apply(TransportNetwork network) {
5660

5761
EdgeStore edgeStore = network.streetLayer.edgeStore;
5862
EdgeStore.Edge edge = edgeStore.getCursor();
63+
network.streetLayer.edgeStore.flags = new TIntArrayList(network.streetLayer.edgeStore.flags);
5964

6065
while (edge.advance()) {
6166
Float value = speedMap.get(edge.getOSMID());
6267

63-
if (value == null) {
64-
edge.setSpeed((short) (edge.getSpeed() * defaultScaling));
65-
} else if (absoluteMode) {
68+
float scaling = (value == null) ? defaultScaling : value;
69+
70+
if (scaling == 0) {
71+
edge.clearFlag(EdgeFlag.ALLOWS_CAR);
72+
} else if (value != null && absoluteMode) {
6673
edge.setSpeedKph(value);
6774
} else {
68-
edge.setSpeed((short) (edge.getSpeed() * value)); // saving cm/sec
75+
edge.setSpeed((short) (edge.getSpeed() * scaling));
6976
}
7077
}
7178

r-package/inst/jar/r5r.jar

194 Bytes
Binary file not shown.

r-package/tests/testthat/test-scenarios_car_speeds.R

Lines changed: 98 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
2+
3+
14
# if running manually, please run the following line first:
25
# source("tests/testthat/setup.R")
36

@@ -7,10 +10,11 @@ testthat::skip_on_cran()
710
# test if setting max_speed to 0 closes the road
811

912
# 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"))
1114

1215
# sf with congestion polygons
1316
congestion_poly <- readRDS(file.path(data_path, "poa_poly_congestion.rds"))
17+
congestion_poly$scale <- c(0.2, 0.5)
1418

1519
# get origin and destination points in a single road
1620
network <- r5r::street_network_to_sf(r5r_network)
@@ -31,12 +35,14 @@ point_dest$id <- as.character(point_dest$id)
3135
meta_fun <- function(
3236
fun = r5r::travel_time_matrix,
3337
new_carspeeds= NULL,
34-
carspeed_scale = 1){
38+
carspeed_scale = 1,
39+
point_origg = point_orig,
40+
point_destt = point_dest){
3541

3642
fun(
3743
r5r_network = r5r_network,
38-
origins = point_orig,
39-
destinations = point_dest,
44+
origins = point_origg,
45+
destinations = point_destt,
4046
mode = 'car',
4147
departure_datetime = Sys.time(),
4248
max_trip_duration = 60,
@@ -48,8 +54,7 @@ meta_fun <- function(
4854

4955

5056
# 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", {
5358

5459
# calculate travel times / access *before* changing road speeds
5560
ttm_pre <- meta_fun(r5r::travel_time_matrix)
@@ -64,25 +69,21 @@ test_that("success in increasing travel times", {
6469
max_trip_duration = 60
6570
)
6671
# to do: r5r::accessibility
67-
6872
# 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
7375

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)
7880
arrival_ttm_pos <- r5r::arrival_travel_time_matrix(
7981
r5r_network = r5r_network,
8082
origins = point_orig,
8183
destinations = point_dest,
8284
mode = 'car',
8385
arrival_datetime = Sys.time(),
8486
max_trip_duration = 60,
85-
new_carspeeds = mock_data,
8687
carspeed_scale = 0.1
8788
)
8889

@@ -96,11 +97,76 @@ test_that("success in increasing travel times", {
9697
testthat::expect_true(det_pos$total_duration > det_pre$total_duration)
9798
# testthat::expect_true(det_pos$total_distance == det_pre$total_distance)
9899

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+
99111
})
100112

101113

114+
102115
# 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)
103163

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 -------------------------------------------------------------------
104170
test_that("errors in congestion polygon", {
105171

106172
# wrong col names
@@ -133,29 +199,30 @@ test_that("errors due to incorrect input types", {
133199
})
134200

135201

136-
137-
138202
test_that("errors error in the new_carspeeds column names", {
139203

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")
141205
testthat::expect_error(meta_fun(new_carspeeds = mock_data))
142206

143207
mock_data <- data.frame(my_osm_id = '9999', max_speed = 9999, speed_type="km/h")
144208
testthat::expect_error(meta_fun(new_carspeeds = mock_data))
145209

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))
149212

150213
})
151214

152215

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

Comments
 (0)