22
22
import com .graphhopper .routing .Path ;
23
23
import com .graphhopper .routing .RoutingAlgorithm ;
24
24
import com .graphhopper .routing .ch .PrepareContractionHierarchies ;
25
- import com .graphhopper .routing .util .AcceptWay ;
26
- import com .graphhopper .routing .util .AlgorithmPreparation ;
27
- import com .graphhopper .routing .util .CarFlagEncoder ;
28
- import com .graphhopper .routing .util .DefaultEdgeFilter ;
29
- import com .graphhopper .routing .util .EdgeFilter ;
30
- import com .graphhopper .routing .util .FastestCalc ;
31
- import com .graphhopper .routing .util .EdgePropertyEncoder ;
32
- import com .graphhopper .routing .util .NoOpAlgorithmPreparation ;
33
- import com .graphhopper .routing .util .PrepareRoutingSubnetworks ;
34
- import com .graphhopper .routing .util .RoutingAlgorithmSpecialAreaTests ;
35
- import com .graphhopper .routing .util .ShortestCalc ;
25
+ import com .graphhopper .routing .util .*;
26
+ import com .graphhopper .routing .util .EncodingManager ;
36
27
import com .graphhopper .storage .Directory ;
37
28
import com .graphhopper .storage .Graph ;
38
29
import com .graphhopper .storage .GraphStorage ;
@@ -100,7 +91,7 @@ public static void main(String[] strs) throws Exception {
100
91
private int neighborUpdates = 20 ;
101
92
// for OSM import:
102
93
private String osmFile ;
103
- private AcceptWay acceptWay = new AcceptWay ( "CAR" ) ;
94
+ private EncodingManager encodingManager = null ;
104
95
private long expectedNodes = 10 ;
105
96
private double wayPointMaxDistance = 1 ;
106
97
private int workerThreads = -1 ;
@@ -118,13 +109,13 @@ public GraphHopper() {
118
109
initIndex ();
119
110
}
120
111
121
- public GraphHopper acceptWay ( AcceptWay acceptWay ) {
122
- this .acceptWay = acceptWay ;
112
+ public GraphHopper encodingManager ( EncodingManager acceptWay ) {
113
+ this .encodingManager = acceptWay ;
123
114
return this ;
124
115
}
125
116
126
- public AcceptWay acceptWay () {
127
- return acceptWay ;
117
+ public EncodingManager encodingManager () {
118
+ return encodingManager ;
128
119
}
129
120
130
121
public GraphHopper forServer () {
@@ -299,7 +290,7 @@ public GraphHopper init(CmdArgs args) throws IOException {
299
290
// osm import
300
291
wayPointMaxDistance = args .getDouble ("osmreader.wayPointMaxDistance" , 1 );
301
292
String type = args .get ("osmreader.acceptWay" , "CAR" );
302
- acceptWay = new AcceptWay (type );
293
+ encodingManager = new EncodingManager (type );
303
294
workerThreads = args .getInt ("osmreader.workerThreads" , -1 );
304
295
305
296
// index
@@ -351,15 +342,15 @@ protected OSMReader importOSM(String file) throws IOException {
351
342
352
343
logger .info ("start creating graph from " + file );
353
344
OSMReader reader = new OSMReader (graph , expectedNodes ).workerThreads (workerThreads );
354
- reader .acceptWay ( acceptWay );
345
+ reader .encodingManager ( encodingManager );
355
346
reader .wayPointMaxDistance (wayPointMaxDistance );
356
347
357
- properties .put ("osmreader.acceptWay" , acceptWay . toString () );
348
+ properties .put ( "osmreader.acceptWay" , encodingManager . encoderList () );
358
349
properties .putCurrentVersions ();
359
350
String info = graph .getClass ().getSimpleName ()
360
351
+ "|" + graph .directory ().getClass ().getSimpleName ()
361
352
+ "|" + properties .versionsToString ();
362
- logger .info ("using " + info + ", accepts:" + acceptWay + ", memory:" + Helper .memInfo ());
353
+ logger .info ("using " + info + ", accepts:" + encodingManager + ", memory:" + Helper .memInfo ());
363
354
reader .osm2Graph (osmTmpFile );
364
355
return reader ;
365
356
}
@@ -378,6 +369,8 @@ protected OSMReader importOSM(String file) throws IOException {
378
369
public boolean load (String graphHopperFolder ) {
379
370
if (Helper .isEmpty (graphHopperFolder ))
380
371
throw new IllegalStateException ("graphHopperLocation is not specified. call init before" );
372
+ if ( encodingManager == null )
373
+ throw new IllegalStateException ("No vehicles are defined (no encoding manager set)" );
381
374
if (graph != null )
382
375
throw new IllegalStateException ("graph is already loaded" );
383
376
@@ -407,10 +400,10 @@ else if (graphHopperFolder.endsWith(".osm") || graphHopperFolder.endsWith(".xml"
407
400
408
401
properties = new StorableProperties (dir , "properties" );
409
402
if (chUsage ) {
410
- graph = new LevelGraphStorage (dir );
403
+ graph = new LevelGraphStorage (dir , encodingManager );
411
404
PrepareContractionHierarchies tmpPrepareCH = new PrepareContractionHierarchies ();
412
405
413
- EdgePropertyEncoder encoder = acceptWay .getSingle ();
406
+ EdgePropertyEncoder encoder = encodingManager .getSingle ();
414
407
if (chFast )
415
408
tmpPrepareCH .type (new FastestCalc (encoder ));
416
409
else
@@ -423,19 +416,19 @@ else if (graphHopperFolder.endsWith(".osm") || graphHopperFolder.endsWith(".xml"
423
416
prepare = tmpPrepareCH ;
424
417
prepare .graph (graph );
425
418
} else {
426
- graph = new GraphStorage (dir );
427
- prepare = NoOpAlgorithmPreparation .createAlgoPrepare (graph , defaultAlgorithm , new CarFlagEncoder () );
419
+ graph = new GraphStorage (dir , encodingManager );
420
+ prepare = NoOpAlgorithmPreparation .createAlgoPrepare ( graph , defaultAlgorithm , encodingManager . getFirst () );
428
421
}
429
422
430
423
if (!graph .loadExisting ())
431
424
return false ;
432
425
433
426
properties .loadExisting ();
434
427
435
- // overwrite configured accept way
428
+ // check encoding for compatiblity
436
429
String acceptStr = properties .get ("osmreader.acceptWay" );
437
- if (!acceptStr .isEmpty ())
438
- acceptWay = new AcceptWay ( acceptStr );
430
+ if (!acceptStr .isEmpty () && ! encodingManager . encoderList (). equals ( acceptStr ) )
431
+ throw new IllegalStateException ( "Encoding does not match: \n Graphhopper: " + encodingManager . encoderList () + " \n Graph: " + acceptStr );
439
432
properties .checkVersions (false );
440
433
if ("false" .equals (properties .get ("prepare.done" )))
441
434
prepare ();
@@ -444,8 +437,8 @@ else if (graphHopperFolder.endsWith(".osm") || graphHopperFolder.endsWith(".xml"
444
437
return true ;
445
438
}
446
439
447
- private boolean supportsVehicle (EdgePropertyEncoder encoder ) {
448
- return acceptWay .accepts (encoder );
440
+ private boolean supportsVehicle ( String encoder ) {
441
+ return encodingManager .accepts (encoder );
449
442
}
450
443
451
444
@ Override
@@ -455,11 +448,11 @@ public GHResponse route(GHRequest request) {
455
448
GHResponse rsp = new GHResponse ();
456
449
457
450
if (!supportsVehicle (request .vehicle ())) {
458
- rsp .addError (new IllegalArgumentException ("Vehicle " + request .vehicle () + " unsupported. Supported are: " + acceptWay ()));
451
+ rsp .addError (new IllegalArgumentException ("Vehicle " + request .vehicle () + " unsupported. Supported are: " + encodingManager ()));
459
452
return rsp ;
460
453
}
461
454
462
- EdgeFilter edgeFilter = new DefaultEdgeFilter (request .vehicle ());
455
+ EdgeFilter edgeFilter = new DefaultEdgeFilter ( encodingManager . getEncoder ( request .vehicle () ));
463
456
int from = index .findClosest (request .from ().lat , request .from ().lon , edgeFilter ).closestNode ();
464
457
int to = index .findClosest (request .to ().lat , request .to ().lon , edgeFilter ).closestNode ();
465
458
String debug = "idLookup:" + sw .stop ().getSeconds () + "s" ;
@@ -482,7 +475,7 @@ else if (request.algorithm().equals("astarbi"))
482
475
// or use defaultAlgorithm here?
483
476
rsp .addError (new IllegalStateException ("Only dijkstrabi and astarbi is supported for LevelGraph (using contraction hierarchies)!" ));
484
477
} else {
485
- prepare = NoOpAlgorithmPreparation .createAlgoPrepare (graph , request .algorithm (), request .vehicle ());
478
+ prepare = NoOpAlgorithmPreparation .createAlgoPrepare ( graph , request .algorithm (), encodingManager . getEncoder ( request .vehicle () ) );
486
479
algo = prepare .createAlgo ();
487
480
algo .type (request .type ());
488
481
}
@@ -545,9 +538,9 @@ public void prepare() {
545
538
boolean tmpPrepare = doPrepare && prepare != null ;
546
539
properties .put ("prepare.done" , tmpPrepare );
547
540
if (tmpPrepare ) {
548
- if (prepare instanceof PrepareContractionHierarchies && acceptWay .countVehicles () > 1 )
541
+ if (prepare instanceof PrepareContractionHierarchies && encodingManager .countVehicles () > 1 )
549
542
throw new IllegalArgumentException ("Contraction hierarchies preparation "
550
- + "requires (at the moment) only one vehicle. But was:" + acceptWay );
543
+ + "requires (at the moment) only one vehicle. But was:" + encodingManager );
551
544
logger .info ("calling prepare.doWork ... (" + Helper .memInfo () + ")" );
552
545
prepare .doWork ();
553
546
}
0 commit comments