Skip to content

Commit 1c6b5b6

Browse files
committed
dynamic assignment of flag bits to encoders
minimized encoders to use only required number of bits added safe_way flags refactored acceptWay methods to encodingManager removed all local encoder instantiations
1 parent 8c27e1e commit 1c6b5b6

File tree

59 files changed

+737
-554
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

59 files changed

+737
-554
lines changed

core/src/main/java/com/graphhopper/GHRequest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ public class GHRequest {
3737
private GHPlace from;
3838
private GHPlace to;
3939
private Map<String, Object> hints = new HashMap<String, Object>(5);
40-
private EdgePropertyEncoder encoder = new CarFlagEncoder();
40+
private String encoderName = "CAR";
4141
private WeightCalculation weightCalc = new ShortestCalc();
4242

4343
/**
@@ -114,12 +114,12 @@ public WeightCalculation type() {
114114
return weightCalc;
115115
}
116116

117-
public GHRequest vehicle(EdgePropertyEncoder encoder) {
118-
this.encoder = encoder;
117+
public GHRequest vehicle(String encoder) {
118+
this.encoderName = encoder;
119119
return this;
120120
}
121121

122-
public EdgePropertyEncoder vehicle() {
123-
return encoder;
122+
public String vehicle() {
123+
return encoderName;
124124
}
125125
}

core/src/main/java/com/graphhopper/GraphHopper.java

Lines changed: 27 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,8 @@
2222
import com.graphhopper.routing.Path;
2323
import com.graphhopper.routing.RoutingAlgorithm;
2424
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;
3627
import com.graphhopper.storage.Directory;
3728
import com.graphhopper.storage.Graph;
3829
import com.graphhopper.storage.GraphStorage;
@@ -100,7 +91,7 @@ public static void main(String[] strs) throws Exception {
10091
private int neighborUpdates = 20;
10192
// for OSM import:
10293
private String osmFile;
103-
private AcceptWay acceptWay = new AcceptWay("CAR");
94+
private EncodingManager encodingManager = null;
10495
private long expectedNodes = 10;
10596
private double wayPointMaxDistance = 1;
10697
private int workerThreads = -1;
@@ -118,13 +109,13 @@ public GraphHopper() {
118109
initIndex();
119110
}
120111

121-
public GraphHopper acceptWay(AcceptWay acceptWay) {
122-
this.acceptWay = acceptWay;
112+
public GraphHopper encodingManager( EncodingManager acceptWay ) {
113+
this.encodingManager = acceptWay;
123114
return this;
124115
}
125116

126-
public AcceptWay acceptWay() {
127-
return acceptWay;
117+
public EncodingManager encodingManager() {
118+
return encodingManager;
128119
}
129120

130121
public GraphHopper forServer() {
@@ -299,7 +290,7 @@ public GraphHopper init(CmdArgs args) throws IOException {
299290
// osm import
300291
wayPointMaxDistance = args.getDouble("osmreader.wayPointMaxDistance", 1);
301292
String type = args.get("osmreader.acceptWay", "CAR");
302-
acceptWay = new AcceptWay(type);
293+
encodingManager = new EncodingManager(type);
303294
workerThreads = args.getInt("osmreader.workerThreads", -1);
304295

305296
// index
@@ -351,15 +342,15 @@ protected OSMReader importOSM(String file) throws IOException {
351342

352343
logger.info("start creating graph from " + file);
353344
OSMReader reader = new OSMReader(graph, expectedNodes).workerThreads(workerThreads);
354-
reader.acceptWay(acceptWay);
345+
reader.encodingManager( encodingManager );
355346
reader.wayPointMaxDistance(wayPointMaxDistance);
356347

357-
properties.put("osmreader.acceptWay", acceptWay.toString());
348+
properties.put( "osmreader.acceptWay", encodingManager.encoderList() );
358349
properties.putCurrentVersions();
359350
String info = graph.getClass().getSimpleName()
360351
+ "|" + graph.directory().getClass().getSimpleName()
361352
+ "|" + properties.versionsToString();
362-
logger.info("using " + info + ", accepts:" + acceptWay + ", memory:" + Helper.memInfo());
353+
logger.info("using " + info + ", accepts:" + encodingManager + ", memory:" + Helper.memInfo());
363354
reader.osm2Graph(osmTmpFile);
364355
return reader;
365356
}
@@ -378,6 +369,8 @@ protected OSMReader importOSM(String file) throws IOException {
378369
public boolean load(String graphHopperFolder) {
379370
if (Helper.isEmpty(graphHopperFolder))
380371
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)");
381374
if (graph != null)
382375
throw new IllegalStateException("graph is already loaded");
383376

@@ -407,10 +400,10 @@ else if (graphHopperFolder.endsWith(".osm") || graphHopperFolder.endsWith(".xml"
407400

408401
properties = new StorableProperties(dir, "properties");
409402
if (chUsage) {
410-
graph = new LevelGraphStorage(dir);
403+
graph = new LevelGraphStorage(dir, encodingManager );
411404
PrepareContractionHierarchies tmpPrepareCH = new PrepareContractionHierarchies();
412405

413-
EdgePropertyEncoder encoder = acceptWay.getSingle();
406+
EdgePropertyEncoder encoder = encodingManager.getSingle();
414407
if (chFast)
415408
tmpPrepareCH.type(new FastestCalc(encoder));
416409
else
@@ -423,19 +416,19 @@ else if (graphHopperFolder.endsWith(".osm") || graphHopperFolder.endsWith(".xml"
423416
prepare = tmpPrepareCH;
424417
prepare.graph(graph);
425418
} 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() );
428421
}
429422

430423
if (!graph.loadExisting())
431424
return false;
432425

433426
properties.loadExisting();
434427

435-
// overwrite configured accept way
428+
// check encoding for compatiblity
436429
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:\nGraphhopper: " + encodingManager.encoderList() + "\nGraph: " + acceptStr );
439432
properties.checkVersions(false);
440433
if ("false".equals(properties.get("prepare.done")))
441434
prepare();
@@ -444,8 +437,8 @@ else if (graphHopperFolder.endsWith(".osm") || graphHopperFolder.endsWith(".xml"
444437
return true;
445438
}
446439

447-
private boolean supportsVehicle(EdgePropertyEncoder encoder) {
448-
return acceptWay.accepts(encoder);
440+
private boolean supportsVehicle( String encoder) {
441+
return encodingManager.accepts(encoder);
449442
}
450443

451444
@Override
@@ -455,11 +448,11 @@ public GHResponse route(GHRequest request) {
455448
GHResponse rsp = new GHResponse();
456449

457450
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()));
459452
return rsp;
460453
}
461454

462-
EdgeFilter edgeFilter = new DefaultEdgeFilter(request.vehicle());
455+
EdgeFilter edgeFilter = new DefaultEdgeFilter( encodingManager.getEncoder( request.vehicle()));
463456
int from = index.findClosest(request.from().lat, request.from().lon, edgeFilter).closestNode();
464457
int to = index.findClosest(request.to().lat, request.to().lon, edgeFilter).closestNode();
465458
String debug = "idLookup:" + sw.stop().getSeconds() + "s";
@@ -482,7 +475,7 @@ else if (request.algorithm().equals("astarbi"))
482475
// or use defaultAlgorithm here?
483476
rsp.addError(new IllegalStateException("Only dijkstrabi and astarbi is supported for LevelGraph (using contraction hierarchies)!"));
484477
} else {
485-
prepare = NoOpAlgorithmPreparation.createAlgoPrepare(graph, request.algorithm(), request.vehicle());
478+
prepare = NoOpAlgorithmPreparation.createAlgoPrepare( graph, request.algorithm(), encodingManager.getEncoder( request.vehicle() ) );
486479
algo = prepare.createAlgo();
487480
algo.type(request.type());
488481
}
@@ -545,9 +538,9 @@ public void prepare() {
545538
boolean tmpPrepare = doPrepare && prepare != null;
546539
properties.put("prepare.done", tmpPrepare);
547540
if (tmpPrepare) {
548-
if (prepare instanceof PrepareContractionHierarchies && acceptWay.countVehicles() > 1)
541+
if (prepare instanceof PrepareContractionHierarchies && encodingManager.countVehicles() > 1)
549542
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 );
551544
logger.info("calling prepare.doWork ... (" + Helper.memInfo() + ")");
552545
prepare.doWork();
553546
}

core/src/main/java/com/graphhopper/reader/OSMElement.java

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,6 @@ public boolean isType(int type) {
242242
/**
243243
* Only for testing
244244
*
245-
* @deprecated
246245
* @param tags
247246
*/
248247
public void setTags(Map<String, String> tags) {

core/src/main/java/com/graphhopper/reader/OSMReader.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
package com.graphhopper.reader;
2020

2121
import com.graphhopper.coll.LongIntMap;
22-
import com.graphhopper.routing.util.AcceptWay;
22+
import com.graphhopper.routing.util.EncodingManager;
2323
import com.graphhopper.storage.GraphStorage;
2424
import com.graphhopper.util.Helper;
2525
import static com.graphhopper.util.Helper.*;
@@ -43,13 +43,12 @@ public class OSMReader {
4343
private long skippedLocations;
4444
private GraphStorage graphStorage;
4545
private OSMReaderHelper helper;
46-
private AcceptWay acceptWay;
46+
private EncodingManager encodingManager = null;
4747
private int workerThreads = -1;
4848

4949
public OSMReader(GraphStorage storage, long expectedNodes) {
5050
this.graphStorage = storage;
5151
helper = new OSMReaderHelper(graphStorage, expectedNodes);
52-
acceptWay = new AcceptWay(AcceptWay.CAR);
5352
}
5453

5554
public OSMReader workerThreads(int numOfWorkers) {
@@ -58,6 +57,9 @@ public OSMReader workerThreads(int numOfWorkers) {
5857
}
5958

6059
public void osm2Graph(File osmFile) throws IOException {
60+
if( encodingManager == null )
61+
throw new IllegalStateException( "Encoding manager not set." );
62+
6163
long start = System.currentTimeMillis();
6264
preProcess(osmFile);
6365

@@ -118,7 +120,7 @@ boolean filterWay(OSMWay item) throws XMLStreamException {
118120
if (!item.hasTags())
119121
return false;
120122

121-
return acceptWay.accept(item) > 0;
123+
return encodingManager.accept(item) > 0;
122124
}
123125

124126
/**
@@ -180,9 +182,9 @@ public void processWay(OSMWay way) throws XMLStreamException {
180182
if (!way.hasTags())
181183
return;
182184

183-
int includeWay = acceptWay.accept(way);
185+
int includeWay = encodingManager.accept(way);
184186
if (includeWay > 0) {
185-
int flags = acceptWay.encodeTags(includeWay, way);
187+
int flags = encodingManager.encodeTags(includeWay, way);
186188
if (flags != 0)
187189
helper.addEdge(way.nodes(), flags);
188190
}
@@ -212,8 +214,8 @@ public GraphStorage graph() {
212214
/**
213215
* Specify the type of the path calculation (car, bike, ...).
214216
*/
215-
public OSMReader acceptWay(AcceptWay acceptWay) {
216-
this.acceptWay = acceptWay;
217+
public OSMReader encodingManager( EncodingManager acceptWay ) {
218+
this.encodingManager = acceptWay;
217219
return this;
218220
}
219221

core/src/main/java/com/graphhopper/routing/RoutingAlgorithmFactory.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ public RoutingAlgorithm createAlgo(Graph g, EdgePropertyEncoder encoder) {
5858
return new AStar(g, encoder);
5959
}
6060

61+
/*
6162
public static RoutingAlgorithm createAlgoForCar(String algoStr, Graph g, boolean shortest) {
6263
EdgePropertyEncoder carEncoder = new CarFlagEncoder();
6364
WeightCalculation weight;
@@ -71,4 +72,5 @@ public static RoutingAlgorithm createAlgoForCar(String algoStr, Graph g, boolean
7172
throw new RuntimeException(ex);
7273
}
7374
}
75+
*/
7476
}

core/src/main/java/com/graphhopper/routing/ch/PrepareContractionHierarchies.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,8 @@ public class PrepareContractionHierarchies extends AbstractAlgoPreparation<Prepa
9595
private int neighborUpdatePercentage = 10;
9696

9797
public PrepareContractionHierarchies() {
98-
type(new ShortestCalc()).vehicle(new CarFlagEncoder());
98+
type(new ShortestCalc());
99+
//vehicle(new CarFlagEncoder());
99100
originalEdges = new RAMDirectory().findCreate("originalEdges");
100101
originalEdges.create(1000);
101102
}
@@ -178,6 +179,10 @@ public PrepareContractionHierarchies removeHigher2LowerEdges(boolean removeHighe
178179

179180
@Override
180181
public PrepareContractionHierarchies doWork() {
182+
if( prepareEncoder == null )
183+
throw new IllegalStateException( "No vehicle encoder set." );
184+
if( prepareWeightCalc == null )
185+
throw new IllegalStateException( "No weight calculation set." );
181186
allSW.start();
182187
super.doWork();
183188
initFromGraph();

core/src/main/java/com/graphhopper/routing/rideshare/DijkstraTwoDrivers.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import com.graphhopper.routing.Path;
2323
import com.graphhopper.routing.util.CarFlagEncoder;
2424
import com.graphhopper.routing.util.EdgePropertyEncoder;
25+
import com.graphhopper.routing.util.EncodingManager;
2526
import com.graphhopper.storage.Graph;
2627
import com.graphhopper.storage.EdgeEntry;
2728

@@ -41,7 +42,7 @@ public class DijkstraTwoDrivers {
4142

4243
public DijkstraTwoDrivers(Graph graph) {
4344
this.graph = graph;
44-
this.carEncoder = new CarFlagEncoder();
45+
this.carEncoder = EncodingManager.instance().getEncoder( "CAR" );
4546
}
4647

4748
public void setDriverA(int fromA, int toA) {

0 commit comments

Comments
 (0)