forked from valhalla/valhalla
/
test_traffic.cc
1021 lines (903 loc) · 41.9 KB
/
test_traffic.cc
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "gurka.h"
#include "test.h"
#include "baldr/graphreader.h"
#include "baldr/traffictile.h"
#include <boost/property_tree/ptree.hpp>
#include <cmath>
#include <sstream>
#include <sys/mman.h>
#include <sys/stat.h>
using namespace valhalla;
using LiveTrafficCustomize = test::LiveTrafficCustomize;
TEST(Traffic, BasicUpdates) {
const std::string ascii_map = R"(
A----B----C
| |
D----E)";
const gurka::ways ways = {{"AB", {{"highway", "primary"}, {"maxspeed", "10"}}},
{"BC", {{"highway", "primary"}, {"maxspeed", "10"}}},
{"BD", {{"highway", "primary"}, {"maxspeed", "10"}}},
{"CE", {{"highway", "primary"}, {"maxspeed", "10"}}},
{"DE", {{"highway", "primary"}, {"maxspeed", "10"}}}};
const auto layout = gurka::detail::map_to_coordinates(ascii_map, 100);
std::string tile_dir = "test/data/traffic_basicupdates";
auto map = gurka::buildtiles(layout, ways, {}, {}, tile_dir);
map.config.put("mjolnir.traffic_extract", tile_dir + "/traffic.tar");
test::build_live_traffic_data(map.config);
auto clean_reader = test::make_clean_graphreader(map.config.get_child("mjolnir"));
std::cout << "[ ] Do a route with initial traffic" << std::endl;
{
auto result = gurka::do_action(valhalla::Options::route, map, {"A", "C"}, "auto",
{{"/date_time/type", "0"}}, clean_reader);
gurka::assert::osrm::expect_steps(result, {"AB"});
gurka::assert::raw::expect_path(result, {"AB", "BC"});
gurka::assert::raw::expect_eta(result, 360.0177);
}
std::cout << "[ ] Make some updates to the traffic .tar file. "
"Mostly just updates every edge in the file to 24km/h, except for one "
"specific edge (B->D) where we simulate a closure (speed=0, congestion high)"
<< std::endl;
auto cb_setter_24kmh = [&map](baldr::GraphReader& reader, baldr::TrafficTile& tile, uint32_t index,
valhalla::baldr::TrafficSpeed* current) -> void {
baldr::GraphId tile_id(tile.header->tile_id);
auto BD = gurka::findEdge(reader, map.nodes, "BD", "D", tile_id);
current->breakpoint1 = 255;
if (std::get<1>(BD) != nullptr && std::get<0>(BD).id() == index) {
current->overall_encoded_speed = 0;
current->encoded_speed1 = 0;
} else {
current->overall_encoded_speed = 24 >> 1;
current->encoded_speed1 = 24 >> 1;
}
};
test::customize_live_traffic_data(map.config, cb_setter_24kmh);
std::cout << "[ ] Now do another route with the same (not restarted) actor to see if"
" it's noticed the changes in the live traffic file"
<< std::endl;
{
auto result = gurka::do_action(valhalla::Options::route, map, {"A", "C"}, "auto",
{{"/date_time/type", "0"}}, clean_reader);
gurka::assert::osrm::expect_steps(result, {"AB"});
gurka::assert::raw::expect_path(result, {"AB", "BC"});
// live-traffic is mixed with default traffic
gurka::assert::raw::expect_eta(result, 153.27858, 0.5);
}
std::cout << "[ ] Next, set the speed to the highest possible to ensure nothing breaks"
<< std::endl;
auto cb_setter_max = [&map](baldr::GraphReader& reader, baldr::TrafficTile& tile, uint32_t index,
baldr::TrafficSpeed* current) -> void {
baldr::GraphId tile_id(tile.header->tile_id);
auto BD = gurka::findEdge(reader, map.nodes, "BD", "D", tile_id);
current->breakpoint1 = 255;
if (std::get<1>(BD) != nullptr && std::get<0>(BD).id() == index) {
current->overall_encoded_speed = 0;
} else {
current->overall_encoded_speed = UNKNOWN_TRAFFIC_SPEED_RAW - 1;
}
};
test::customize_live_traffic_data(map.config, cb_setter_max);
{
auto result = gurka::do_action(valhalla::Options::route, map, {"A", "C"}, "auto",
{{"/date_time/type", "0"}}, clean_reader);
gurka::assert::osrm::expect_steps(result, {"AB"});
gurka::assert::raw::expect_path(result, {"AB", "BC"});
gurka::assert::raw::expect_eta(result, 25.731991);
}
std::cout << "[ ] Back to previous speed" << std::endl;
test::customize_live_traffic_data(map.config, cb_setter_24kmh);
// And verify that without the "current" timestamp, the live traffic
// results aren't used
{
auto result =
gurka::do_action(valhalla::Options::route, map, {"A", "C"}, "auto", {}, clean_reader);
gurka::assert::osrm::expect_steps(result, {"AB"});
gurka::assert::raw::expect_path(result, {"AB", "BC"});
gurka::assert::raw::expect_eta(result, 360.0177);
}
std::cout << "[ ] Now do another route with the same (not restarted) actor to see if "
"it's noticed the changes in the live traffic file"
<< std::endl;
{
auto result = gurka::do_action(valhalla::Options::route, map, {"B", "D"}, "auto",
{{"/date_time/type", "0"}}, clean_reader);
gurka::assert::osrm::expect_steps(result, {"BC", "CE", "DE"});
// BD is excluded
gurka::assert::raw::expect_path(result, {"BC", "CE", "DE"});
// live-traffic is mixed with default traffic.
gurka::assert::raw::expect_eta(result, 184.5, 0.5);
}
{
auto result = gurka::do_action(valhalla::Options::route, map, {"D", "B"}, "auto",
{{"/date_time/type", "0"}}, clean_reader);
gurka::assert::osrm::expect_steps(result, {"BD"});
gurka::assert::raw::expect_path(result, {"BD"});
gurka::assert::raw::expect_eta(result, 30., 0.01);
}
std::cout
<< "[ ] Repeat the B->D route, but this time with no timestamp - this should disable "
"using live traffc and the road should be open again"
<< std::endl;
{
auto result =
gurka::do_action(valhalla::Options::route, map, {"B", "D"}, "auto", {}, clean_reader);
gurka::assert::osrm::expect_steps(result, {"BD"});
gurka::assert::raw::expect_path(result, {"BD"});
gurka::assert::raw::expect_eta(result, 72);
}
}
TEST(Traffic, CutGeoms) {
const std::string ascii_map = R"(
A----B----C-F
| 1 2
| | G
| | 3
| | H
D----E-I )";
const gurka::ways ways = {{"AB", {{"highway", "primary"}}}, {"BC", {{"highway", "primary"}}},
{"BD", {{"highway", "primary"}}}, {"CE", {{"highway", "primary"}}},
{"CF", {{"highway", "primary"}}}, {"FGHI", {{"highway", "primary"}}},
{"EI", {{"highway", "primary"}}}, {"DE", {{"highway", "primary"}}}};
// build the tiles
const auto layout = gurka::detail::map_to_coordinates(ascii_map, 100);
std::string tile_dir = "test/data/traffic_cutgeoms";
auto map = gurka::buildtiles(layout, ways, {}, {}, tile_dir,
{{"mjolnir.traffic_extract", tile_dir + "/traffic.tar"}});
// empty traffic for now
test::build_live_traffic_data(map.config);
// first we get the edge without traffic on it
{
auto clean_reader = test::make_clean_graphreader(map.config.get_child("mjolnir"));
tyr::actor_t actor(map.config, *clean_reader);
valhalla::Api api;
actor.route(
R"({"locations":[
{"lat":)" +
std::to_string(map.nodes["C"].second) + R"(,"lon":)" +
std::to_string(map.nodes["C"].first) +
R"(},
{"lat":)" +
std::to_string(map.nodes["E"].second) + R"(,"lon":)" +
std::to_string(map.nodes["E"].first) +
R"(}
],"costing":"auto","date_time":{"type":0},
"filters":{"attributes":["edge.length","edge.speed","edge.begin_shape_index",
"edge.end_shape_index","shape","shape_attributes.length","shape_attributes.time","shape_attributes.speed"],
"action":"include"}})",
nullptr, &api);
const auto& leg = api.trip().routes(0).legs(0);
auto shapes = midgard::decode<std::vector<valhalla::midgard::PointLL>>(leg.shape());
EXPECT_EQ(leg.node_size(), 2); // 1 edge
EXPECT_EQ(shapes.size(), 2); // 2 shape points
// An attribute for each pair formed by the shape-points
EXPECT_EQ(leg.shape_attributes().time_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().length_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().speed_size(), shapes.size() - 1);
}
// then we add one portion of the edge having traffic
{
valhalla::baldr::TrafficSpeed ts{
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
0u,
0u,
0u,
0u,
0u,
0u,
};
ts.overall_encoded_speed = 42 >> 1;
ts.encoded_speed1 = 42 >> 1;
ts.congestion1 = baldr::MAX_CONGESTION_VAL - 1;
ts.breakpoint1 = 127;
auto cb_setter_speed = [&map, &ts](baldr::GraphReader& reader, baldr::TrafficTile& tile,
uint32_t index, baldr::TrafficSpeed* current) -> void {
baldr::GraphId tile_id(tile.header->tile_id);
auto BD = gurka::findEdge(reader, map.nodes, "BD", "D", tile_id);
current->breakpoint1 = 255;
if (std::get<1>(BD) != nullptr && std::get<0>(BD).id() == index) {
current->overall_encoded_speed = 0;
current->encoded_speed1 = 0;
current->breakpoint1 = 255;
} else {
*current = ts;
}
};
test::customize_live_traffic_data(map.config, cb_setter_speed);
auto clean_reader = test::make_clean_graphreader(map.config.get_child("mjolnir"));
tyr::actor_t actor(map.config, *clean_reader);
valhalla::Api api;
actor.route(
R"({"locations":[
{"lat":)" +
std::to_string(map.nodes["C"].second) + R"(,"lon":)" +
std::to_string(map.nodes["C"].first) +
R"(},
{"lat":)" +
std::to_string(map.nodes["E"].second) + R"(,"lon":)" +
std::to_string(map.nodes["E"].first) +
R"(}
],"costing":"auto","date_time":{"type":0},
"filters":{"attributes":["edge.length","edge.speed","edge.begin_shape_index",
"edge.end_shape_index","shape","shape_attributes.length","shape_attributes.time","shape_attributes.speed"],
"action":"include"}})",
nullptr, &api);
const auto& leg = api.trip().routes(0).legs(0);
auto shapes = midgard::decode<std::vector<valhalla::midgard::PointLL>>(leg.shape());
EXPECT_EQ(leg.node_size(), 2); // 1 edge
EXPECT_EQ(shapes.size(), 3); // 3 shape points
// An attribute for each pair formed by the shape-points
EXPECT_EQ(leg.shape_attributes().time_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().length_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().speed_size(), shapes.size() - 1);
EXPECT_TRUE(map.nodes["C"].ApproximatelyEqual(shapes[0]));
auto b1 = map.nodes["C"].PointAlongSegment(map.nodes["E"], 127 / 255.0);
EXPECT_TRUE(b1.ApproximatelyEqual(shapes[1]));
}
// Another permutation of subsegments
{
{
valhalla::baldr::TrafficSpeed ts{
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
0u,
0u,
0u,
0u,
0u,
0u,
};
ts.overall_encoded_speed = 30 >> 1;
ts.encoded_speed1 = 20 >> 1;
ts.congestion1 = baldr::MAX_CONGESTION_VAL - 1;
ts.breakpoint1 = 100;
ts.encoded_speed2 = 40 >> 1;
ts.congestion2 = 1; // low but not unknown - 0 = unknown
ts.breakpoint2 = 200;
auto cb_setter_speed = [&map, &ts](baldr::GraphReader& reader, baldr::TrafficTile& tile,
uint32_t index, baldr::TrafficSpeed* current) -> void {
baldr::GraphId tile_id(tile.header->tile_id);
auto BD = gurka::findEdge(reader, map.nodes, "BD", "D", tile_id);
current->breakpoint1 = 255;
if (std::get<1>(BD) != nullptr && std::get<0>(BD).id() == index) {
current->overall_encoded_speed = 0;
current->encoded_speed1 = 0;
} else {
*current = ts;
}
};
test::customize_live_traffic_data(map.config, cb_setter_speed);
}
auto clean_reader = test::make_clean_graphreader(map.config.get_child("mjolnir"));
tyr::actor_t actor(map.config, *clean_reader);
valhalla::Api api;
actor.route(
R"({"locations":[
{"lat":)" +
std::to_string(map.nodes["C"].second) + R"(,"lon":)" +
std::to_string(map.nodes["C"].first) +
R"(},
{"lat":)" +
std::to_string(map.nodes["E"].second) + R"(,"lon":)" +
std::to_string(map.nodes["E"].first) +
R"(}
],"costing":"auto","date_time":{"type":0},
"filters":{"attributes":["edge.length","edge.speed","edge.begin_shape_index",
"edge.end_shape_index","shape","shape_attributes.length","shape_attributes.time","shape_attributes.speed"],
"action":"include"}})",
nullptr, &api);
const auto& leg = api.trip().routes(0).legs(0);
auto shapes = midgard::decode<std::vector<valhalla::midgard::PointLL>>(leg.shape());
EXPECT_EQ(leg.node_size(), 2); // 1 edge
EXPECT_EQ(shapes.size(), 4);
// An attribute for each pair formed by the shape-points
EXPECT_EQ(leg.shape_attributes().time_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().length_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().speed_size(), shapes.size() - 1);
{
auto b1 = map.nodes["C"].PointAlongSegment(map.nodes["E"], 100 / 255.0);
EXPECT_TRUE(b1.ApproximatelyEqual(shapes[1]));
}
{
auto b2 = map.nodes["C"].PointAlongSegment(map.nodes["E"], 200 / 255.0);
EXPECT_TRUE(b2.ApproximatelyEqual(shapes[2]));
}
}
// Another permutation of subsegments
{
{
valhalla::baldr::TrafficSpeed ts{
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
0u,
0u,
0u,
0u,
0u,
0u,
};
ts.overall_encoded_speed = 36 >> 1;
ts.encoded_speed1 = 20 >> 1;
ts.congestion1 = baldr::MAX_CONGESTION_VAL - 1;
ts.breakpoint1 = 100;
ts.encoded_speed2 = 40 >> 1;
ts.congestion2 = 1;
ts.breakpoint2 = 200;
ts.encoded_speed3 = 60 >> 1;
ts.congestion3 = 1;
auto cb_setter_speed = [&map, &ts](baldr::GraphReader& reader, baldr::TrafficTile& tile,
uint32_t index, baldr::TrafficSpeed* current) -> void {
baldr::GraphId tile_id(tile.header->tile_id);
auto BD = gurka::findEdge(reader, map.nodes, "BD", "D", tile_id);
current->breakpoint1 = 255;
if (std::get<1>(BD) != nullptr && std::get<0>(BD).id() == index) {
current->overall_encoded_speed = 0;
current->encoded_speed1 = 0;
} else {
*current = ts;
}
};
test::customize_live_traffic_data(map.config, cb_setter_speed);
}
auto clean_reader = test::make_clean_graphreader(map.config.get_child("mjolnir"));
tyr::actor_t actor(map.config, *clean_reader);
valhalla::Api api;
{
// Test the full edge CE
actor.route(
R"({"locations":[
{"lat":)" +
std::to_string(map.nodes["C"].second) + R"(,"lon":)" +
std::to_string(map.nodes["C"].first) +
R"(},
{"lat":)" +
std::to_string(map.nodes["E"].second) + R"(,"lon":)" +
std::to_string(map.nodes["E"].first) +
R"(}
],"costing":"auto","date_time":{"type":0},
"filters":{"attributes":["edge.length","edge.speed","edge.begin_shape_index",
"edge.end_shape_index","shape","shape_attributes.length","shape_attributes.time","shape_attributes.speed"],
"action":"include"}})",
nullptr, &api);
const auto& leg = api.trip().routes(0).legs(0);
auto shapes = midgard::decode<std::vector<valhalla::midgard::PointLL>>(leg.shape());
EXPECT_EQ(leg.node_size(), 2); // 1 edge
EXPECT_EQ(shapes.size(), 4);
// An attribute for each pair formed by the shape-points
EXPECT_EQ(leg.shape_attributes().time_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().length_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().speed_size(), shapes.size() - 1);
{ EXPECT_TRUE(map.nodes["C"].ApproximatelyEqual(shapes[0])); }
{
auto b1 = map.nodes["C"].PointAlongSegment(map.nodes["E"], 100 / 255.0);
EXPECT_TRUE(b1.ApproximatelyEqual(shapes[1]));
}
{
auto b2 = map.nodes["C"].PointAlongSegment(map.nodes["E"], 200 / 255.0);
EXPECT_TRUE(b2.ApproximatelyEqual(shapes[2]));
}
}
{
// Test partial CE
actor.route(
R"({"locations":[
{"lat":)" +
std::to_string(map.nodes["1"].second) + R"(,"lon":)" +
std::to_string(map.nodes["1"].first) +
R"(},
{"lat":)" +
std::to_string(map.nodes["E"].second) + R"(,"lon":)" +
std::to_string(map.nodes["E"].first) +
R"(}
],"costing":"auto","date_time":{"type":0},
"filters":{"attributes":["edge.length","edge.speed","edge.begin_shape_index",
"edge.end_shape_index","shape","shape_attributes.length","shape_attributes.time","shape_attributes.speed"],
"action":"include"}})",
nullptr, &api);
const auto& leg = api.trip().routes(0).legs(0);
auto shapes = midgard::decode<std::vector<valhalla::midgard::PointLL>>(leg.shape());
EXPECT_EQ(leg.node_size(), 2); // 1 edge
EXPECT_EQ(shapes.size(), 4);
// An attribute for each pair formed by the shape-points
EXPECT_EQ(leg.shape_attributes().time_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().length_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().speed_size(), shapes.size() - 1);
EXPECT_TRUE(map.nodes["1"].ApproximatelyEqual(shapes[0]));
{
auto b1 = map.nodes["C"].PointAlongSegment(map.nodes["E"], 100 / 255.0);
EXPECT_TRUE(b1.ApproximatelyEqual(shapes[1]));
}
{
auto b2 = map.nodes["C"].PointAlongSegment(map.nodes["E"], 200 / 255.0);
EXPECT_TRUE(b2.ApproximatelyEqual(shapes[2]));
}
}
{
{
valhalla::baldr::TrafficSpeed ts{
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
0u,
0u,
0u,
0u,
0u,
0u,
};
ts.overall_encoded_speed = 36 >> 1;
ts.encoded_speed1 = 20 >> 1;
ts.congestion1 = baldr::MAX_CONGESTION_VAL - 1;
ts.breakpoint1 = 100;
ts.encoded_speed2 = 40 >> 1;
ts.congestion2 = 1;
ts.breakpoint2 = 200;
ts.encoded_speed3 = 60 >> 1;
ts.congestion3 = 50;
auto cb_setter_speed = [&map, &ts](baldr::GraphReader& reader, baldr::TrafficTile& tile,
uint32_t index, baldr::TrafficSpeed* current) -> void {
baldr::GraphId tile_id(tile.header->tile_id);
auto BD = gurka::findEdge(reader, map.nodes, "BD", "D", tile_id);
current->breakpoint1 = 255;
if (std::get<1>(BD) != nullptr && std::get<0>(BD).id() == index) {
current->overall_encoded_speed = 0;
current->encoded_speed1 = 0;
} else {
*current = ts;
}
};
test::customize_live_traffic_data(map.config, cb_setter_speed);
}
auto clean_reader = test::make_clean_graphreader(map.config.get_child("mjolnir"));
tyr::actor_t actor(map.config, *clean_reader);
valhalla::Api api;
{
// Test the full edge CE
actor.route(
R"({"locations":[
{"lat":)" +
std::to_string(map.nodes["C"].second) + R"(,"lon":)" +
std::to_string(map.nodes["C"].first) +
R"(},
{"lat":)" +
std::to_string(map.nodes["E"].second) + R"(,"lon":)" +
std::to_string(map.nodes["E"].first) +
R"(}
],"costing":"auto","date_time":{"type":0},
"filters":{"attributes":["edge.length","edge.speed","edge.begin_shape_index",
"edge.end_shape_index","shape","shape_attributes.length","shape_attributes.time","shape_attributes.speed"],
"action":"include"}})",
nullptr, &api);
const auto& leg = api.trip().routes(0).legs(0);
auto shapes = midgard::decode<std::vector<valhalla::midgard::PointLL>>(leg.shape());
EXPECT_EQ(leg.node_size(), 2); // 1 edge
EXPECT_EQ(shapes.size(), 4);
// An attribute for each pair formed by the shape-points
EXPECT_EQ(leg.shape_attributes().time_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().length_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().speed_size(), shapes.size() - 1);
EXPECT_TRUE(map.nodes["C"].ApproximatelyEqual(shapes[0]));
{
auto b1 = map.nodes["C"].PointAlongSegment(map.nodes["E"], 100 / 255.0);
EXPECT_TRUE(b1.ApproximatelyEqual(shapes[1]));
}
{
auto b2 = map.nodes["C"].PointAlongSegment(map.nodes["E"], 200 / 255.0);
EXPECT_TRUE(b2.ApproximatelyEqual(shapes[2]));
}
EXPECT_TRUE(map.nodes["E"].ApproximatelyEqual(shapes[3]));
}
{
// Test partial CE
actor.route(
R"({"locations":[
{"lat":)" +
std::to_string(map.nodes["1"].second) + R"(,"lon":)" +
std::to_string(map.nodes["1"].first) +
R"(},
{"lat":)" +
std::to_string(map.nodes["E"].second) + R"(,"lon":)" +
std::to_string(map.nodes["E"].first) +
R"(}
],"costing":"auto","date_time":{"type":0},
"filters":{"attributes":["edge.length","edge.speed","edge.begin_shape_index",
"edge.end_shape_index","shape","shape_attributes.length","shape_attributes.time","shape_attributes.speed"],
"action":"include"}})",
nullptr, &api);
const auto& leg = api.trip().routes(0).legs(0);
auto shapes = midgard::decode<std::vector<valhalla::midgard::PointLL>>(leg.shape());
EXPECT_EQ(leg.node_size(), 2); // 1 edge
EXPECT_EQ(shapes.size(), 4);
// An attribute for each pair formed by the shape-points
EXPECT_EQ(leg.shape_attributes().time_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().length_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().speed_size(), shapes.size() - 1);
{ EXPECT_TRUE(map.nodes["1"].ApproximatelyEqual(shapes[0])); }
{
auto b1 = map.nodes["C"].PointAlongSegment(map.nodes["E"], 100 / 255.0);
EXPECT_TRUE(b1.ApproximatelyEqual(shapes[1]));
}
{
auto b2 = map.nodes["C"].PointAlongSegment(map.nodes["E"], 200 / 255.0);
EXPECT_TRUE(b2.ApproximatelyEqual(shapes[2]));
}
}
{
// Test multishape FE
actor.route(
R"({"locations":[
{"lat":)" +
std::to_string(map.nodes["F"].second) + R"(,"lon":)" +
std::to_string(map.nodes["F"].first) +
R"(},
{"lat":)" +
std::to_string(map.nodes["I"].second) + R"(,"lon":)" +
std::to_string(map.nodes["I"].first) +
R"(}
],"costing":"auto","date_time":{"type":0},
"filters":{"attributes":["edge.length","edge.speed","edge.begin_shape_index",
"edge.end_shape_index","shape","shape_attributes.length","shape_attributes.time","shape_attributes.speed"],
"action":"include"}})",
nullptr, &api);
const auto& leg = api.trip().routes(0).legs(0);
auto shapes = midgard::decode<std::vector<valhalla::midgard::PointLL>>(leg.shape());
EXPECT_EQ(leg.node_size(), 2); // 1 edge
EXPECT_EQ(shapes.size(), 6);
// An attribute for each pair formed by the shape-points
EXPECT_EQ(leg.shape_attributes().time_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().length_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().speed_size(), shapes.size() - 1);
EXPECT_TRUE(map.nodes["F"].ApproximatelyEqual(shapes[0]));
{
auto b1 = map.nodes["F"].PointAlongSegment(map.nodes["I"], 100 / 255.0);
EXPECT_TRUE(b1.ApproximatelyEqual(shapes[1]));
}
EXPECT_TRUE(map.nodes["G"].ApproximatelyEqual(shapes[2]));
{
auto b2 = map.nodes["F"].PointAlongSegment(map.nodes["I"], 200 / 255.0);
EXPECT_TRUE(b2.ApproximatelyEqual(shapes[3]));
}
EXPECT_TRUE(map.nodes["H"].ApproximatelyEqual(shapes[4]));
EXPECT_TRUE(map.nodes["I"].ApproximatelyEqual(shapes[5]));
}
{
// Test partial multishape 2->E
actor.route(
R"({"locations":[
{"lat":)" +
std::to_string(map.nodes["2"].second) + R"(,"lon":)" +
std::to_string(map.nodes["2"].first) +
R"(},
{"lat":)" +
std::to_string(map.nodes["E"].second) + R"(,"lon":)" +
std::to_string(map.nodes["E"].first) +
R"(}
],"costing":"auto","date_time":{"type":0},
"filters":{"attributes":["edge.length","edge.speed","edge.begin_shape_index",
"edge.end_shape_index","shape","shape_attributes.length","shape_attributes.time","shape_attributes.speed"],
"action":"include"}})",
nullptr, &api);
const auto& leg = api.trip().routes(0).legs(0);
auto shapes = midgard::decode<std::vector<valhalla::midgard::PointLL>>(leg.shape());
//{
// std::string buf;
// google::protobuf::util::JsonPrintOptions opt;
// opt.add_whitespace = true;
// google::protobuf::util::MessageToJsonString(leg, &buf, opt);
// std::cout << buf << std::endl;
// auto node_of_interest = "F";
// std::cout << "node['" << node_of_interest << "'] "
// << std::to_string(map.nodes[node_of_interest].first) << ", "
// << std::to_string(map.nodes[node_of_interest].second) << std::endl;
// node_of_interest = "I";
// std::cout << "node['" << node_of_interest << "'] "
// << std::to_string(map.nodes[node_of_interest].first) << ", "
// << std::to_string(map.nodes[node_of_interest].second) << std::endl;
// int i=0;
// for (auto& shape : shapes) {
// std::cout << "shape #" <<std::to_string(i) << ": "<< std::to_string(shape.first) << ", "
// << std::to_string(shape.second) << std::endl;
// ++i;
// }
//}
EXPECT_EQ(leg.node_size(), 3); // FI + IE
EXPECT_EQ(shapes.size(), 9);
// An attribute for each pair formed by the shape-points
EXPECT_EQ(leg.shape_attributes().time_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().length_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().speed_size(), shapes.size() - 1);
EXPECT_TRUE(map.nodes["2"].ApproximatelyEqual(shapes[0]));
{
auto b1 = map.nodes["F"].PointAlongSegment(map.nodes["I"], 100 / 255.0);
EXPECT_TRUE(b1.ApproximatelyEqual(shapes[1]));
}
EXPECT_TRUE(map.nodes["G"].ApproximatelyEqual(shapes[2]));
{
auto b2 = map.nodes["F"].PointAlongSegment(map.nodes["I"], 200 / 255.0);
EXPECT_TRUE(b2.ApproximatelyEqual(shapes[3]));
}
EXPECT_TRUE(map.nodes["H"].ApproximatelyEqual(shapes[4]));
EXPECT_TRUE(map.nodes["I"].ApproximatelyEqual(shapes[5]));
{
auto b2 = map.nodes["I"].PointAlongSegment(map.nodes["E"], 100 / 255.0);
EXPECT_TRUE(b2.ApproximatelyEqual(shapes[6]));
}
{
auto b2 = map.nodes["I"].PointAlongSegment(map.nodes["E"], 200 / 255.0);
EXPECT_TRUE(b2.ApproximatelyEqual(shapes[7]));
}
EXPECT_TRUE(map.nodes["E"].ApproximatelyEqual(shapes[8]));
}
}
// Test regression when cutting
{
{
valhalla::baldr::TrafficSpeed ts{
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
valhalla::baldr::UNKNOWN_TRAFFIC_SPEED_RAW,
0u,
0u,
0u,
0u,
0u,
0u,
};
ts.overall_encoded_speed = 36 >> 1;
ts.encoded_speed1 = 20 >> 1;
ts.congestion1 = baldr::MAX_CONGESTION_VAL - 1;
ts.breakpoint1 = 100;
ts.encoded_speed2 = 40 >> 1;
ts.congestion2 = 1;
// Regression is when breakpoint2 is set to 255,
// but speed3 is not set to UNKNOWN - we would incorrectly
// emit an additional 0-length slice in the API response
ts.breakpoint2 = 255;
// This needs to be set
ts.encoded_speed3 = 60 >> 1;
ts.congestion3 = 50;
auto cb_setter_speed = [&map, &ts](baldr::GraphReader& reader, baldr::TrafficTile& tile,
uint32_t index, baldr::TrafficSpeed* current) -> void {
baldr::GraphId tile_id(tile.header->tile_id);
auto BD = gurka::findEdge(reader, map.nodes, "BD", "D", tile_id);
current->breakpoint1 = 255;
if (std::get<1>(BD) != nullptr && std::get<0>(BD).id() == index) {
current->overall_encoded_speed = 0;
current->encoded_speed1 = 0;
} else {
*current = ts;
}
};
test::customize_live_traffic_data(map.config, cb_setter_speed);
}
auto clean_reader = test::make_clean_graphreader(map.config.get_child("mjolnir"));
tyr::actor_t actor(map.config, *clean_reader);
valhalla::Api api;
{
// Test the full edge CE
actor.route(
R"({"locations":[
{"lat":)" +
std::to_string(map.nodes["C"].second) + R"(,"lon":)" +
std::to_string(map.nodes["C"].first) +
R"(},
{"lat":)" +
std::to_string(map.nodes["E"].second) + R"(,"lon":)" +
std::to_string(map.nodes["E"].first) +
R"(}
],"costing":"auto","date_time":{"type":0},
"filters":{"attributes":["edge.length","edge.speed","edge.begin_shape_index",
"edge.end_shape_index","shape","shape_attributes.length","shape_attributes.time","shape_attributes.speed"],
"action":"include"}})",
nullptr, &api);
const auto& leg = api.trip().routes(0).legs(0);
auto shapes = midgard::decode<std::vector<valhalla::midgard::PointLL>>(leg.shape());
EXPECT_EQ(leg.node_size(), 2); // 1 edge
EXPECT_EQ(shapes.size(), 3); // The third record should not create a slice
// An attribute for each pair formed by the shape-points
EXPECT_EQ(leg.shape_attributes().time_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().length_size(), shapes.size() - 1);
EXPECT_EQ(leg.shape_attributes().speed_size(), shapes.size() - 1);
EXPECT_TRUE(map.nodes["C"].ApproximatelyEqual(shapes[0]));
{
auto b1 = map.nodes["C"].PointAlongSegment(map.nodes["E"], 100 / 255.0);
EXPECT_TRUE(b1.ApproximatelyEqual(shapes[1]));
}
EXPECT_TRUE(map.nodes["E"].ApproximatelyEqual(shapes[2]));
}
}
}
}
/*************************************************************/
class WaypointsOnClosuresTest : public ::testing::Test {
protected:
static gurka::map closure_map;
static int const default_speed;
static std::string const tile_dir;
static std::shared_ptr<baldr::GraphReader> clean_reader;
;
static void SetUpTestSuite() {
const std::string ascii_map = R"(
A-2-------D
| |
| |
| |
| |
B-1-------C
)";
const std::string speed_str = std::to_string(default_speed);
const gurka::ways ways = {{"AB", {{"highway", "primary"}, {"maxspeed", speed_str}}},
{"BC", {{"highway", "primary"}, {"maxspeed", speed_str}}},
{"CD", {{"highway", "primary"}, {"maxspeed", speed_str}}},
{"DA", {{"highway", "primary"}, {"maxspeed", speed_str}}}};
const auto layout = gurka::detail::map_to_coordinates(ascii_map, 10, {.05f, .2f});
closure_map = gurka::buildtiles(layout, ways, {}, {}, tile_dir);
closure_map.config.put("mjolnir.traffic_extract", tile_dir + "/traffic.tar");
test::build_live_traffic_data(closure_map.config);
clean_reader = test::make_clean_graphreader(closure_map.config.get_child("mjolnir"));
}
};
gurka::map WaypointsOnClosuresTest::closure_map = {};
const int WaypointsOnClosuresTest::default_speed = 36;
const std::string WaypointsOnClosuresTest::tile_dir = "test/data/traffic_waypoints_on_closures";
std::shared_ptr<baldr::GraphReader> WaypointsOnClosuresTest::clean_reader;
namespace {
inline void SetLiveSpeed(baldr::TrafficSpeed* live_speed, uint64_t speed) {
live_speed->breakpoint1 = 255;
live_speed->overall_encoded_speed = speed >> 1;
live_speed->encoded_speed1 = speed >> 1;
}
} // namespace
TEST_F(WaypointsOnClosuresTest, DepartPointAtClosure) {
const float eta_margin = 0.01f;
// start from an edge that is closed in a one direction
{
LiveTrafficCustomize close_edge = [](baldr::GraphReader& reader, baldr::TrafficTile& tile,
uint32_t index, baldr::TrafficSpeed* current) -> void {
baldr::GraphId tile_id(tile.header->tile_id);
auto BC = gurka::findEdge(reader, closure_map.nodes, "BC", "C", tile_id);
bool const is_bc = (std::get<1>(BC) != nullptr && std::get<0>(BC).id() == index);
SetLiveSpeed(current, (is_bc ? 0 : default_speed));
};
test::customize_live_traffic_data(closure_map.config, close_edge);
auto result = gurka::do_action(valhalla::Options::route, closure_map, {"1", "A"}, "auto",
{{"/date_time/type", "0"}}, clean_reader);
gurka::assert::raw::expect_path(result, {"BC", "AB"});
gurka::assert::raw::expect_eta(result, 7.f, eta_margin);
}
// start from an edge that is closed in a one direction
{
LiveTrafficCustomize close_edge = [](baldr::GraphReader& reader, baldr::TrafficTile& tile,
uint32_t index, baldr::TrafficSpeed* current) -> void {
baldr::GraphId tile_id(tile.header->tile_id);
auto CB = gurka::findEdge(reader, closure_map.nodes, "BC", "B", tile_id);
bool const is_cb = (std::get<1>(CB) != nullptr && std::get<0>(CB).id() == index);
SetLiveSpeed(current, (is_cb ? 0 : default_speed));
};
test::customize_live_traffic_data(closure_map.config, close_edge);
auto result = gurka::do_action(valhalla::Options::route, closure_map, {"1", "A"}, "auto",
{{"/date_time/type", "0"}}, clean_reader);
gurka::assert::raw::expect_path(result, {"BC", "CD", "DA"});
gurka::assert::raw::expect_eta(result, 23.f, eta_margin);
}
// depart point at closed edge (ignore):
// - closed edge should be ignored;
// - depart point should be matched to the nearest node;
{
LiveTrafficCustomize close_edge = [](baldr::GraphReader& reader, baldr::TrafficTile& tile,
uint32_t index, baldr::TrafficSpeed* current) -> void {
baldr::GraphId tile_id(tile.header->tile_id);
auto BC = gurka::findEdge(reader, closure_map.nodes, "BC", "C", tile_id);
auto CB = gurka::findEdge(reader, closure_map.nodes, "BC", "B", tile_id);
bool const is_bc = (std::get<1>(BC) != nullptr && std::get<0>(BC).id() == index);
bool const is_cb = (std::get<1>(CB) != nullptr && std::get<0>(CB).id() == index);
SetLiveSpeed(current, ((is_cb || is_bc) ? 0 : default_speed));
};
test::customize_live_traffic_data(closure_map.config, close_edge);
auto result = gurka::do_action(valhalla::Options::route, closure_map, {"1", "A"}, "auto",
{{"/date_time/type", "0"}}, clean_reader);
gurka::assert::raw::expect_path(result, {"AB"});
gurka::assert::raw::expect_eta(result, 5.f, eta_margin);
}
}
TEST_F(WaypointsOnClosuresTest, ArrivePointAtClosure) {
const float eta_margin = 0.01f;
// end at edge that is closed in a one direction
{
LiveTrafficCustomize close_edge = [](baldr::GraphReader& reader, baldr::TrafficTile& tile,
uint32_t index, baldr::TrafficSpeed* current) -> void {
baldr::GraphId tile_id(tile.header->tile_id);
auto DA = gurka::findEdge(reader, closure_map.nodes, "DA", "A", tile_id);
bool const is_da = (std::get<1>(DA) != nullptr && std::get<0>(DA).id() == index);
SetLiveSpeed(current, (is_da ? 0 : default_speed));
};
test::customize_live_traffic_data(closure_map.config, close_edge);
auto result = gurka::do_action(valhalla::Options::route, closure_map, {"B", "2"}, "auto",
{{"/date_time/type", "0"}}, clean_reader);
gurka::assert::raw::expect_path(result, {"AB", "DA"});
gurka::assert::raw::expect_eta(result, 7.f, eta_margin);
}
// end at edge that is closed in a one direction
{
LiveTrafficCustomize close_edge = [](baldr::GraphReader& reader, baldr::TrafficTile& tile,
uint32_t index, baldr::TrafficSpeed* current) -> void {
baldr::GraphId tile_id(tile.header->tile_id);
auto AD = gurka::findEdge(reader, closure_map.nodes, "DA", "D", tile_id);
bool const is_ad = (std::get<1>(AD) != nullptr && std::get<0>(AD).id() == index);
SetLiveSpeed(current, (is_ad ? 0 : default_speed));
};
test::customize_live_traffic_data(closure_map.config, close_edge);
auto result = gurka::do_action(valhalla::Options::route, closure_map, {"B", "2"}, "auto",
{{"/date_time/type", "0"}}, clean_reader);
gurka::assert::raw::expect_path(result, {"BC", "CD", "DA"});
gurka::assert::raw::expect_eta(result, 23.f, eta_margin);
}
// arrive point at closed edge (ignore):
// - closed edge should be ignored;
// - arrive point should be matched to the nearest node;
{
LiveTrafficCustomize close_edge = [](baldr::GraphReader& reader, baldr::TrafficTile& tile,
uint32_t index, baldr::TrafficSpeed* current) -> void {
baldr::GraphId tile_id(tile.header->tile_id);
auto DA = gurka::findEdge(reader, closure_map.nodes, "DA", "A", tile_id);
auto AD = gurka::findEdge(reader, closure_map.nodes, "DA", "D", tile_id);
bool const is_da = (std::get<1>(DA) != nullptr && std::get<0>(DA).id() == index);
bool const is_ad = (std::get<1>(AD) != nullptr && std::get<0>(AD).id() == index);
SetLiveSpeed(current, ((is_da || is_ad) ? 0 : default_speed));
};
test::customize_live_traffic_data(closure_map.config, close_edge);
auto result = gurka::do_action(valhalla::Options::route, closure_map, {"B", "2"}, "auto",
{{"/date_time/type", "0"}}, clean_reader);
gurka::assert::raw::expect_path(result, {"AB"});
gurka::assert::raw::expect_eta(result, 5.f, eta_margin);
}
}
TEST_F(WaypointsOnClosuresTest, IgnoreDepartPointAtClosure) {
// the edge is not closed
{
auto result = gurka::do_action(valhalla::Options::route, closure_map, {"1", "A"}, "auto",
{{"/date_time/type", "0"}}, clean_reader);
gurka::assert::raw::expect_path(result, {"BC", "AB"});
}
// the edge is closed in both directions
{
LiveTrafficCustomize close_edge = [](baldr::GraphReader& reader, baldr::TrafficTile& tile,
uint32_t index, baldr::TrafficSpeed* current) -> void {
baldr::GraphId tile_id(tile.header->tile_id);
auto BC = std::get<0>(gurka::findEdge(reader, closure_map.nodes, "BC", "C"));
auto CB = std::get<0>(gurka::findEdge(reader, closure_map.nodes, "BC", "B"));
bool should_close = (BC.Tile_Base() == tile_id && BC.id() == index) ||
(CB.Tile_Base() == tile_id && CB.id() == index);
SetLiveSpeed(current, should_close ? 0 : default_speed);
};
test::customize_live_traffic_data(closure_map.config, close_edge);
auto result = gurka::do_action(valhalla::Options::route, closure_map, {"1", "A"}, "auto",
{{"/date_time/type", "0"}}, clean_reader);
gurka::assert::raw::expect_path(result, {"AB"});
}