/
GraphHopperTest.java
855 lines (768 loc) · 31 KB
/
GraphHopperTest.java
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
/*
* Licensed to GraphHopper and Peter Karich under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper licenses this file to you under the Apache License,
* Version 2.0 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.graphhopper;
import com.graphhopper.reader.DataReader;
import com.graphhopper.routing.*;
import com.graphhopper.routing.ch.PrepareContractionHierarchies;
import com.graphhopper.routing.util.*;
import com.graphhopper.storage.*;
import com.graphhopper.storage.index.QueryResult;
import com.graphhopper.util.CmdArgs;
import com.graphhopper.util.Helper;
import com.graphhopper.util.Instruction;
import com.graphhopper.util.shapes.GHPoint;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicReference;
import static org.junit.Assert.*;
/**
* @author Peter Karich
*/
public class GraphHopperTest
{
private static final String ghLoc = "./target/tmp/ghosm";
private static final String testOsm = "./src/test/resources/com/graphhopper/reader/test-osm.xml";
private static final String testOsm3 = "./src/test/resources/com/graphhopper/reader/test-osm3.xml";
private GraphHopper instance;
@Before
public void setUp()
{
Helper.removeDir(new File(ghLoc));
}
@After
public void tearDown()
{
if (instance != null)
instance.close();
Helper.removeDir(new File(ghLoc));
}
@Test
public void testLoadOSM()
{
GraphHopper closableInstance = new GraphHopper().setStoreOnFlush(true).
setEncodingManager(new EncodingManager("CAR")).
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm);
closableInstance.importOrLoad();
GHResponse rsp = closableInstance.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4));
assertFalse(rsp.hasErrors());
assertEquals(3, rsp.getPoints().getSize());
closableInstance.close();
// no encoding manager necessary
closableInstance = new GraphHopper().setStoreOnFlush(true);
assertTrue(closableInstance.load(ghLoc));
rsp = closableInstance.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4));
assertFalse(rsp.hasErrors());
assertEquals(3, rsp.getPoints().getSize());
closableInstance.close();
try
{
rsp = closableInstance.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4));
assertTrue(false);
} catch (Exception ex)
{
assertEquals("You need to create a new GraphHopper instance as it is already closed", ex.getMessage());
}
try
{
QueryResult qr = closableInstance.getLocationIndex().findClosest(51.2492152, 9.4317166, EdgeFilter.ALL_EDGES);
assertTrue(false);
} catch (Exception ex)
{
assertEquals("You need to create a new LocationIndex instance as it is already closed", ex.getMessage());
}
}
@Test
public void testLoadOSMNoCH()
{
GraphHopper gh = new GraphHopper().setStoreOnFlush(true).
setCHEnable(false).
setEncodingManager(new EncodingManager("CAR")).
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm);
gh.importOrLoad();
GHResponse rsp = gh.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4));
assertFalse(rsp.hasErrors());
assertEquals(3, rsp.getPoints().getSize());
gh.close();
gh = new GraphHopper().setStoreOnFlush(true).
setCHEnable(false).
setEncodingManager(new EncodingManager("CAR"));
assertTrue(gh.load(ghLoc));
rsp = gh.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4));
assertFalse(rsp.hasErrors());
assertEquals(3, rsp.getPoints().getSize());
gh.close();
}
@Test
public void testLoadingWithDifferentCHConfig_issue471()
{
// with CH should not be loadable without CH configured
GraphHopper gh = new GraphHopper().setStoreOnFlush(true).
setEncodingManager(new EncodingManager("CAR")).
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm);
gh.importOrLoad();
GHResponse rsp = gh.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4));
assertFalse(rsp.hasErrors());
assertEquals(3, rsp.getPoints().getSize());
gh.close();
gh = new GraphHopper().setStoreOnFlush(true).
setCHEnable(false).
setEncodingManager(new EncodingManager("CAR"));
try
{
gh.load(ghLoc);
assertTrue(false);
} catch (Exception ex)
{
assertTrue(ex.getMessage(), ex.getMessage().startsWith("Configured graph.chWeightings:"));
}
Helper.removeDir(new File(ghLoc));
// without CH should not be loadable with CH enabled
gh = new GraphHopper().setStoreOnFlush(true).
setCHEnable(false).
setEncodingManager(new EncodingManager("CAR")).
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm);
gh.importOrLoad();
rsp = gh.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4));
assertFalse(rsp.hasErrors());
assertEquals(3, rsp.getPoints().getSize());
gh.close();
gh = new GraphHopper().setStoreOnFlush(true).
setEncodingManager(new EncodingManager("CAR"));
try
{
gh.load(ghLoc);
assertTrue(false);
} catch (Exception ex)
{
assertTrue(ex.getMessage(), ex.getMessage().startsWith("Configured graph.chWeightings:"));
}
}
@Test
public void testAllowMultipleReadingInstances()
{
GraphHopper instance1 = new GraphHopper().setStoreOnFlush(true).
setEncodingManager(new EncodingManager("CAR")).
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm);
instance1.importOrLoad();
GraphHopper instance2 = new GraphHopper().setStoreOnFlush(true).
setEncodingManager(new EncodingManager("CAR")).
setOSMFile(testOsm);
instance2.load(ghLoc);
GraphHopper instance3 = new GraphHopper().setStoreOnFlush(true).
setEncodingManager(new EncodingManager("CAR")).
setOSMFile(testOsm);
instance3.load(ghLoc);
instance1.close();
instance2.close();
instance3.close();
}
@Test
public void testDoNotAllowWritingAndLoadingAtTheSameTime() throws Exception
{
final CountDownLatch latch1 = new CountDownLatch(1);
final CountDownLatch latch2 = new CountDownLatch(1);
final GraphHopper instance1 = new GraphHopper()
{
@Override
protected DataReader importData() throws IOException
{
try
{
latch2.countDown();
latch1.await(3, TimeUnit.SECONDS);
} catch (InterruptedException ex)
{
}
return super.importData();
}
}.setStoreOnFlush(true).
setEncodingManager(new EncodingManager("CAR")).
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm);
final AtomicReference<Exception> ar = new AtomicReference<Exception>();
Thread thread = new Thread()
{
@Override
public void run()
{
try
{
instance1.importOrLoad();
} catch (Exception ex)
{
ar.set(ex);
}
}
};
thread.start();
GraphHopper instance2 = new GraphHopper().setStoreOnFlush(true).
setEncodingManager(new EncodingManager("CAR")).
setOSMFile(testOsm);
try
{
// let thread reach the CountDownLatch
latch2.await(3, TimeUnit.SECONDS);
// now importOrLoad should have create a lock which this load call does not like
instance2.load(ghLoc);
assertTrue(false);
} catch (RuntimeException ex)
{
assertNotNull(ex);
assertTrue(ex.getMessage(), ex.getMessage().startsWith("To avoid reading partial data"));
} finally
{
instance2.close();
latch1.countDown();
// make sure the import process wasn't interrupted and no other error happened
thread.join();
}
if (ar.get() != null)
assertNull(ar.get().getMessage(), ar.get());
instance1.close();
}
@Test
public void testPrepare()
{
instance = new GraphHopper().
setStoreOnFlush(false).
setEncodingManager(new EncodingManager("CAR")).
setCHWeighting("shortest").
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm);
instance.importOrLoad();
GHResponse rsp = instance.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4).
setAlgorithm(AlgorithmOptions.DIJKSTRA_BI));
assertFalse(rsp.hasErrors());
assertEquals(Helper.createPointList(51.249215, 9.431716, 52.0, 9.0, 51.2, 9.4), rsp.getPoints());
assertEquals(3, rsp.getPoints().getSize());
}
@Test
public void testSortedGraph_noCH()
{
instance = new GraphHopper().setStoreOnFlush(false).
setSortGraph(true).
setEncodingManager(new EncodingManager("CAR")).
setCHEnable(false).
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm);
instance.importOrLoad();
GHResponse rsp = instance.route(new GHRequest(51.2492152, 9.4317166, 51.2, 9.4).
setAlgorithm(AlgorithmOptions.DIJKSTRA_BI));
assertFalse(rsp.hasErrors());
assertEquals(3, rsp.getPoints().getSize());
assertEquals(new GHPoint(51.24921503475044, 9.431716451757769), rsp.getPoints().toGHPoint(0));
assertEquals(new GHPoint(52.0, 9.0), rsp.getPoints().toGHPoint(1));
assertEquals(new GHPoint(51.199999850988384, 9.39999970197677), rsp.getPoints().toGHPoint(2));
GHRequest req = new GHRequest(51.2492152, 9.4317166, 51.2, 9.4);
boolean old = instance.enableInstructions;
req.getHints().put("instructions", true);
instance.route(req);
assertEquals(old, instance.enableInstructions);
req.getHints().put("instructions", false);
instance.route(req);
assertEquals("route method should not change instance field", old, instance.enableInstructions);
}
@Test
public void testFootAndCar()
{
// now all ways are imported
instance = new GraphHopper().setStoreOnFlush(false).
setEncodingManager(new EncodingManager("CAR,FOOT")).
setCHEnable(false).
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm3);
instance.importOrLoad();
assertEquals(5, instance.getGraphHopperStorage().getNodes());
assertEquals(8, instance.getGraphHopperStorage().getAllEdges().getMaxId());
// A to D
GHResponse rsp = instance.route(new GHRequest(11.1, 50, 11.3, 51).setVehicle(EncodingManager.CAR));
assertFalse(rsp.hasErrors());
assertFalse(rsp.hasErrors());
assertEquals(3, rsp.getPoints().getSize());
// => found A and D
assertEquals(50, rsp.getPoints().getLongitude(0), 1e-3);
assertEquals(11.1, rsp.getPoints().getLatitude(0), 1e-3);
assertEquals(51, rsp.getPoints().getLongitude(2), 1e-3);
assertEquals(11.3, rsp.getPoints().getLatitude(2), 1e-3);
// A to D not allowed for foot. But the location index will choose a node close to D accessible to FOOT
rsp = instance.route(new GHRequest(11.1, 50, 11.3, 51).setVehicle(EncodingManager.FOOT));
assertFalse(rsp.hasErrors());
assertEquals(2, rsp.getPoints().getSize());
// => found a point on edge A-B
assertEquals(11.680, rsp.getPoints().getLatitude(1), 1e-3);
assertEquals(50.644, rsp.getPoints().getLongitude(1), 1e-3);
// A to E only for foot
rsp = instance.route(new GHRequest(11.1, 50, 10, 51).setVehicle(EncodingManager.FOOT));
assertFalse(rsp.hasErrors());
assertEquals(2, rsp.getPoints().size());
// A D E for car
rsp = instance.route(new GHRequest(11.1, 50, 10, 51).setVehicle(EncodingManager.CAR));
assertFalse(rsp.hasErrors());
assertEquals(3, rsp.getPoints().getSize());
}
@Test
public void testFailsForWrongConfig() throws IOException
{
instance = new GraphHopper().init(
new CmdArgs().
put("osmreader.osm", testOsm3).
put("osmreader.dataaccess", "RAM").
put("graph.flagEncoders", "FOOT,CAR").
put("prepare.chWeighting", "no")).
setGraphHopperLocation(ghLoc);
instance.importOrLoad();
assertEquals(5, instance.getGraphHopperStorage().getNodes());
instance.close();
// different config (flagEncoder list)
try
{
GraphHopper tmpGH = new GraphHopper().init(
new CmdArgs().
put("osmreader.osm", testOsm3).
put("osmreader.dataaccess", "RAM").
put("graph.flagEncoders", "FOOT").
put("prepare.chWeighting", "no")).
setOSMFile(testOsm3);
tmpGH.load(ghLoc);
assertTrue(false);
} catch (Exception ex)
{
}
// different order is no longer okay, see #350
try
{
GraphHopper tmpGH = new GraphHopper().init(new CmdArgs().
put("osmreader.osm", testOsm3).
put("osmreader.dataaccess", "RAM").
put("prepare.chWeighting", "no").
put("graph.flagEncoders", "CAR,FOOT")).
setOSMFile(testOsm3);
tmpGH.load(ghLoc);
assertTrue(false);
} catch (Exception ex)
{
}
}
@Test
public void testNoNPE_ifLoadNotSuccessful()
{
// missing import of graph
instance = new GraphHopper().
setStoreOnFlush(true).
setEncodingManager(new EncodingManager("CAR"));
try
{
assertFalse(instance.load(ghLoc));
instance.route(new GHRequest(10, 40, 12, 32));
assertTrue(false);
} catch (IllegalStateException ex)
{
assertEquals("Call load or importOrLoad before routing", ex.getMessage());
}
}
@Test
public void testFailsForMissingParameters() throws IOException
{
// missing load of graph
instance = new GraphHopper();
try
{
instance.setOSMFile(testOsm).importData();
assertTrue(false);
} catch (IllegalStateException ex)
{
assertEquals("Load graph before importing OSM data", ex.getMessage());
}
// missing graph location
instance = new GraphHopper();
try
{
instance.importOrLoad();
assertTrue(false);
} catch (IllegalStateException ex)
{
assertEquals("graphHopperLocation is not specified. call init before", ex.getMessage());
}
// missing OSM file to import
instance = new GraphHopper().
setStoreOnFlush(true).
setEncodingManager(new EncodingManager("CAR")).
setGraphHopperLocation(ghLoc);
try
{
instance.importOrLoad();
assertTrue(false);
} catch (IllegalStateException ex)
{
assertEquals("Couldn't load from existing folder: " + ghLoc
+ " but also cannot import from OSM file as it wasn't specified!", ex.getMessage());
}
// missing encoding manager
instance = new GraphHopper().
setStoreOnFlush(true).
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm3);
try
{
instance.importOrLoad();
assertTrue(false);
} catch (IllegalStateException ex)
{
assertTrue(ex.getMessage(), ex.getMessage().startsWith("Cannot load properties to fetch EncodingManager"));
}
// Import is possible even if no storeOnFlush is specified BUT here we miss the OSM file
instance = new GraphHopper().
setStoreOnFlush(false).
setEncodingManager(new EncodingManager("CAR")).
setGraphHopperLocation(ghLoc);
try
{
instance.importOrLoad();
assertTrue(false);
} catch (Exception ex)
{
assertEquals("Couldn't load from existing folder: " + ghLoc
+ " but also cannot import from OSM file as it wasn't specified!", ex.getMessage());
}
}
@Test
public void testFootOnly()
{
// now only footable ways are imported => no A D C and B D E => the other both ways have pillar nodes!
instance = new GraphHopper().setStoreOnFlush(false).
setEncodingManager(new EncodingManager("FOOT")).
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm3);
instance.importOrLoad();
assertEquals(2, instance.getGraphHopperStorage().getNodes());
assertEquals(2, instance.getGraphHopperStorage().getAllEdges().getMaxId());
// A to E only for foot
GHResponse res = instance.route(new GHRequest(11.1, 50, 11.2, 52.01).setVehicle(EncodingManager.FOOT));
assertFalse(res.hasErrors());
assertEquals(Helper.createPointList(11.1, 50, 10, 51, 11.2, 52), res.getPoints());
}
@Test
public void testPrepareOnly()
{
instance = new GraphHopper().setStoreOnFlush(true).
setCHWeighting("shortest").
setEncodingManager(new EncodingManager("FOOT")).
setDoPrepare(false).
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm3);
instance.importOrLoad();
instance.close();
instance = new GraphHopper().setStoreOnFlush(true).
setCHWeighting("shortest").
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm3);
// wrong encoding manager
instance.setEncodingManager(new EncodingManager("CAR"));
try
{
instance.load(ghLoc);
assertTrue(false);
} catch (IllegalStateException ex)
{
assertTrue(ex.getMessage(), ex.getMessage().startsWith("Encoding does not match:"));
}
// use the encoding manager from the graph
instance = new GraphHopper().setStoreOnFlush(true).
setEncodingManager(new EncodingManager("FOOT")).
setCHWeighting("shortest").
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm3);
instance.load(ghLoc);
}
@Test
public void testVia()
{
instance = new GraphHopper().setStoreOnFlush(true).
init(new CmdArgs().
put("osmreader.osm", testOsm3).
put("prepare.minNetworkSize", "1").
put("graph.flagEncoders", "CAR")).
setGraphHopperLocation(ghLoc);
instance.importOrLoad();
// A -> B -> C
GHPoint first = new GHPoint(11.1, 50);
GHPoint second = new GHPoint(12, 51);
GHPoint third = new GHPoint(11.2, 51.9);
GHResponse rsp12 = instance.route(new GHRequest().addPoint(first).addPoint(second));
assertFalse("should find 1->2", rsp12.hasErrors());
assertEquals(147930.5, rsp12.getDistance(), .1);
GHResponse rsp23 = instance.route(new GHRequest().addPoint(second).addPoint(third));
assertFalse("should find 2->3", rsp23.hasErrors());
assertEquals(176608.9, rsp23.getDistance(), .1);
GHResponse rsp = instance.route(new GHRequest().addPoint(first).addPoint(second).addPoint(third));
assertFalse(rsp.hasErrors());
assertFalse("should find 1->2->3", rsp.hasErrors());
assertEquals(rsp12.getDistance() + rsp23.getDistance(), rsp.getDistance(), 1e-6);
assertEquals(5, rsp.getPoints().getSize());
assertEquals(5, rsp.getInstructions().size());
assertEquals(Instruction.REACHED_VIA, rsp.getInstructions().get(1).getSign());
}
@Test
public void testGetPathsDirectionEnforcement1()
{
// Test enforce start direction
// Note: This Test does not pass for CH enabled
instance = createSquareGraphInstance(false);
// Start in middle of edge 4-5
GHPoint start = new GHPoint(0.0015, 0.002);
// End at middle of edge 2-3
GHPoint end = new GHPoint(0.002, 0.0005);
// Test enforce south start direction; expected nodes (9)-5-8-3-(10)
GHRequest req = new GHRequest().addPoint(start, 180.).addPoint(end);
GHResponse response = new GHResponse();
List<Path> paths = instance.getPaths(req, response);
assertArrayEquals(new int[]
{
9, 5, 8, 3, 10
}, paths.get(0).calcNodes().toArray());
}
@Test
public void testGetPathsDirectionEnforcement2()
{
// Test enforce start & end direction
instance = createSquareGraphInstance(false);
// Start in middle of edge 4-5
GHPoint start = new GHPoint(0.0015, 0.002);
// End at middle of edge 2-3
GHPoint end = new GHPoint(0.002, 0.0005);
// Test enforce south start direction and east end direction ; expected nodes (9)-5-8-1-2-(10)
GHRequest req = new GHRequest().addPoint(start, 180.).addPoint(end, 90.);
GHResponse response = new GHResponse();
List<Path> paths = instance.getPaths(req, response);
assertArrayEquals(new int[]
{
9, 5, 8, 1, 2, 10
}, paths.get(0).calcNodes().toArray());
// Test uni-directional case
req.setAlgorithm(AlgorithmOptions.DIJKSTRA);
response = new GHResponse();
paths = instance.getPaths(req, response);
assertArrayEquals(new int[]
{
9, 5, 8, 1, 2, 10
}, paths.get(0).calcNodes().toArray());
}
@Test
public void testGetPathsDirectionEnforcement3()
{
instance = createSquareGraphInstance(false);
// Start in middle of edge 4-5
GHPoint start = new GHPoint(0.0015, 0.002);
// End at middle of edge 2-3
GHPoint end = new GHPoint(0.002, 0.0005);
// Via Point betweeen 8-7
GHPoint via = new GHPoint(0.0005, 0.001);
GHRequest req = new GHRequest().addPoint(start).addPoint(via, 0.).addPoint(end);
GHResponse response = new GHResponse();
List<Path> paths = instance.getPaths(req, response);
assertArrayEquals(new int[]
{
10, 5, 6, 7, 11
}, paths.get(0).calcNodes().toArray());
}
@Test
public void testGetPathsDirectionEnforcement4()
{
// Test straight via routing
instance = createSquareGraphInstance(false);
// Start in middle of edge 4-5
GHPoint start = new GHPoint(0.0015, 0.002);
// End at middle of edge 2-3
GHPoint end = new GHPoint(0.002, 0.0005);
// Via Point betweeen 8-3
GHPoint via = new GHPoint(0.0015, 0.001);
GHRequest req = new GHRequest().addPoint(start).addPoint(via).addPoint(end);
req.getHints().put("pass_through", true);
GHResponse response = new GHResponse();
List<Path> paths = instance.getPaths(req, response);
assertArrayEquals(new int[]
{
10, 4, 3, 11
}, paths.get(0).calcNodes().toArray());
assertArrayEquals(new int[]
{
11, 8, 1, 2, 9
}, paths.get(1).calcNodes().toArray());
}
@Test
public void testGetPathsDirectionEnforcement5()
{
// Test independence of previous enforcement for subsequent pathes
instance = createSquareGraphInstance(false);
// Start in middle of edge 4-5
GHPoint start = new GHPoint(0.0015, 0.002);
// End at middle of edge 2-3
GHPoint end = new GHPoint(0.002, 0.0005);
// First go south and than come from west to via-point at 7-6. Then go back over previously punished (11)-4 edge
GHPoint via = new GHPoint(0.000, 0.0015);
GHRequest req = new GHRequest().addPoint(start, 0.).addPoint(via, 3.14 / 2).addPoint(end);
req.getHints().put("pass_through", true);
GHResponse response = new GHResponse();
List<Path> paths = instance.getPaths(req, response);
assertArrayEquals(new int[]
{
10, 4, 3, 8, 7, 9
}, paths.get(0).calcNodes().toArray());
assertArrayEquals(new int[]
{
9, 6, 5, 10, 4, 3, 11
}, paths.get(1).calcNodes().toArray());
}
@Test
public void testGetPathsDirectionEnforcement6()
{
// Test if query results at tower nodes are ignored
instance = createSquareGraphInstance(false);
// QueryPoints directly on TowerNodes
GHPoint start = new GHPoint(0, 0);
GHPoint via = new GHPoint(0.002, 0.000);
GHPoint end = new GHPoint(0.002, 0.002);
GHRequest req = new GHRequest().addPoint(start, 90.).addPoint(via, 270.).addPoint(end, 270.);
GHResponse response = new GHResponse();
List<Path> paths = instance.getPaths(req, response);
assertArrayEquals(new int[]
{
0, 1, 2
}, paths.get(0).calcNodes().toArray());
assertArrayEquals(new int[]
{
2, 3, 4
}, paths.get(1).calcNodes().toArray());
}
private GraphHopper createSquareGraphInstance( boolean withCH )
{
CarFlagEncoder carEncoder = new CarFlagEncoder();
EncodingManager encodingManager = new EncodingManager(carEncoder);
Weighting weighting = new FastestWeighting(carEncoder);
GraphHopperStorage g = new GraphHopperStorage(Collections.singletonList(weighting), new RAMDirectory(), encodingManager,
false, new GraphExtension.NoOpExtension()).
create(20);
// 2---3---4
// / | \
// 1----8----5
// / | /
// 0----7---6
NodeAccess na = g.getNodeAccess();
na.setNode(0, 0.000, 0.000);
na.setNode(1, 0.001, 0.000);
na.setNode(2, 0.002, 0.000);
na.setNode(3, 0.002, 0.001);
na.setNode(4, 0.002, 0.002);
na.setNode(5, 0.001, 0.002);
na.setNode(6, 0.000, 0.002);
na.setNode(7, 0.000, 0.001);
na.setNode(8, 0.001, 0.001);
g.edge(0, 1, 100, true);
g.edge(1, 2, 100, true);
g.edge(2, 3, 100, true);
g.edge(3, 4, 100, true);
g.edge(4, 5, 100, true);
g.edge(5, 6, 100, true);
g.edge(6, 7, 100, true);
g.edge(7, 0, 100, true);
g.edge(1, 8, 110, true);
g.edge(3, 8, 110, true);
g.edge(5, 8, 110, true);
g.edge(7, 8, 110, true);
GraphHopper tmp = new GraphHopper().
putAlgorithmFactory(weighting, null).
setCHEnable(withCH).
setCHWeighting("fastest").
setEncodingManager(encodingManager);
tmp.setGraphHopperStorage(g);
tmp.postProcessing();
return tmp;
}
@Test
public void testCustomFactoryForNoneCH()
{
CarFlagEncoder carEncoder = new CarFlagEncoder();
EncodingManager em = new EncodingManager(carEncoder);
Weighting weighting = new FastestWeighting(carEncoder);
instance = new GraphHopper().setStoreOnFlush(false).
setCHEnable(false).
setEncodingManager(em).
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm);
RoutingAlgorithmFactory af = new RoutingAlgorithmFactorySimple();
instance.putAlgorithmFactory(weighting, af);
instance.importOrLoad();
assertTrue(af == instance.getAlgorithmFactory(weighting));
// test that hints are passwed to algorithm opts
final AtomicInteger cnt = new AtomicInteger(0);
instance.putAlgorithmFactory(weighting, new RoutingAlgorithmFactorySimple()
{
@Override
public RoutingAlgorithm createAlgo( Graph g, AlgorithmOptions opts )
{
cnt.addAndGet(1);
assertFalse(opts.getHints().getBool("test", true));
return super.createAlgo(g, opts);
}
});
GHRequest req = new GHRequest(51.2492152, 9.4317166, 51.2, 9.4);
req.getHints().put("test", false);
instance.route(req);
assertEquals(1, cnt.get());
}
@Test
public void testMultipleCHPreparationsInParallel()
{
// try all parallelization modes
for (int threadCount = 1; threadCount < 6; threadCount++)
{
EncodingManager em = new EncodingManager(Arrays.asList(new CarFlagEncoder(), new MotorcycleFlagEncoder(),
new MountainBikeFlagEncoder(), new RacingBikeFlagEncoder(), new FootFlagEncoder()),
8);
GraphHopper tmpGH = new GraphHopper().setStoreOnFlush(false).
setEncodingManager(em).
setGraphHopperLocation(ghLoc).
setOSMFile(testOsm).setCHPrepareThreads(threadCount);
tmpGH.importOrLoad();
assertEquals(5, tmpGH.getAlgorithmFactories().size());
for (RoutingAlgorithmFactory raf : tmpGH.getAlgorithmFactories())
{
PrepareContractionHierarchies pch = (PrepareContractionHierarchies) raf;
assertTrue("Preparation wasn't run! [" + threadCount + "]", pch.isPrepared());
String key = "prepare.date." + CHGraphImpl.weightingToFileName(pch.getWeighting());
String value = tmpGH.getGraphHopperStorage().getProperties().get(key);
assertTrue(key + " should contain finish time/date [" + threadCount + "]", !value.isEmpty());
}
tmpGH.close();
}
}
}