Skip to content

Commit 385178d

Browse files
committed
Some minor cleanups in GraphHopper&RoutingTemplates
1 parent 03d883d commit 385178d

File tree

3 files changed

+45
-45
lines changed

3 files changed

+45
-45
lines changed

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

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,12 @@
2929
import com.graphhopper.routing.RoutingAlgorithmFactorySimple;
3030
import com.graphhopper.routing.ch.CHPreparationHandler;
3131
import com.graphhopper.routing.ch.CHRoutingAlgorithmFactory;
32-
import com.graphhopper.routing.lm.LMConfig;
33-
import com.graphhopper.routing.lm.LMPreparationHandler;
3432
import com.graphhopper.routing.ev.DefaultEncodedValueFactory;
3533
import com.graphhopper.routing.ev.EncodedValueFactory;
3634
import com.graphhopper.routing.ev.EnumEncodedValue;
3735
import com.graphhopper.routing.ev.RoadEnvironment;
36+
import com.graphhopper.routing.lm.LMConfig;
37+
import com.graphhopper.routing.lm.LMPreparationHandler;
3838
import com.graphhopper.routing.querygraph.QueryGraph;
3939
import com.graphhopper.routing.subnetwork.PrepareRoutingSubnetworks;
4040
import com.graphhopper.routing.template.AlternativeRoutingTemplate;
@@ -1045,68 +1045,57 @@ public List<Path> calcPaths(GHRequest request, GHResponse ghRsp) {
10451045

10461046
try {
10471047
validateRequest(request);
1048-
PMap hints = request.getHints();
10491048
final boolean disableCH = getDisableCH(request.getHints());
10501049
final boolean disableLM = getDisableLM(request.getHints());
1051-
String algoStr = request.getAlgorithm();
1052-
if (algoStr.isEmpty())
1053-
algoStr = chPreparationHandler.isEnabled() && !disableCH ? DIJKSTRA_BI : ASTAR_BI;
1054-
10551050
Profile profile = profilesByName.get(request.getProfile());
1056-
if (profile == null) {
1051+
if (profile == null)
10571052
throw new IllegalArgumentException("The requested profile '" + request.getProfile() + "' does not exist.\nAvailable profiles: " + profilesByName.keySet());
1058-
}
1059-
if (!profile.isTurnCosts() && !request.getCurbsides().isEmpty()) {
1060-
List<String> turnCostProfiles = new ArrayList<>();
1061-
for (Profile p : profilesByName.values()) {
1062-
if (p.isTurnCosts()) {
1063-
turnCostProfiles.add(p.getName());
1064-
}
1065-
}
1053+
if (!profile.isTurnCosts() && !request.getCurbsides().isEmpty())
10661054
throw new IllegalArgumentException("To make use of the " + CURBSIDE + " parameter you need to use a profile that supports turn costs" +
1067-
"\nThe following profiles do support turn costs: " + turnCostProfiles);
1068-
}
1055+
"\nThe following profiles do support turn costs: " + getTurnCostProfiles());
10691056

10701057
// todo later: should we be able to control this using the edge_based parameter?
10711058
TraversalMode tMode = profile.isTurnCosts() ? TraversalMode.EDGE_BASED : TraversalMode.NODE_BASED;
1072-
List<GHPoint> points = request.getPoints();
10731059
RoutingAlgorithmFactory algorithmFactory = getAlgorithmFactory(profile.getName(), disableCH, disableLM);
10741060
Weighting weighting;
10751061
Graph graph = ghStorage;
10761062
if (chPreparationHandler.isEnabled() && !disableCH) {
10771063
if (!(algorithmFactory instanceof CHRoutingAlgorithmFactory))
10781064
throw new IllegalStateException("Although CH was enabled a non-CH algorithm factory was returned " + algorithmFactory);
10791065

1080-
if (hints.has(Routing.BLOCK_AREA))
1066+
if (request.getHints().has(Routing.BLOCK_AREA))
10811067
throw new IllegalArgumentException("When CH is enabled the " + Parameters.Routing.BLOCK_AREA + " cannot be specified");
10821068

10831069
CHConfig chConfig = ((CHRoutingAlgorithmFactory) algorithmFactory).getCHConfig();
10841070
weighting = chConfig.getWeighting();
10851071
graph = ghStorage.getCHGraph(chConfig);
10861072
} else {
1087-
checkNonChMaxWaypointDistance(points);
1088-
final int uTurnCostsInt = hints.getInt(Routing.U_TURN_COSTS, INFINITE_U_TURN_COSTS);
1073+
checkNonChMaxWaypointDistance(request.getPoints());
1074+
final int uTurnCostsInt = request.getHints().getInt(Routing.U_TURN_COSTS, INFINITE_U_TURN_COSTS);
10891075
if (uTurnCostsInt != INFINITE_U_TURN_COSTS && !tMode.isEdgeBased()) {
10901076
throw new IllegalArgumentException("Finite u-turn costs can only be used for edge-based routing, use `" + Routing.EDGE_BASED + "=true'");
10911077
}
10921078
FlagEncoder encoder = encodingManager.getEncoder(profile.getVehicle());
1093-
weighting = createWeighting(profile, hints);
1094-
if (hints.has(Routing.BLOCK_AREA))
1079+
weighting = createWeighting(profile, request.getHints());
1080+
if (request.getHints().has(Routing.BLOCK_AREA))
10951081
weighting = new BlockAreaWeighting(weighting, GraphEdgeIdFinder.createBlockArea(ghStorage, locationIndex,
1096-
points, hints, DefaultEdgeFilter.allEdges(encoder)));
1082+
request.getPoints(), request.getHints(), DefaultEdgeFilter.allEdges(encoder)));
10971083
}
10981084
ghRsp.addDebugInfo("tmode:" + tMode.toString());
10991085

1086+
String algoStr = request.getAlgorithm();
1087+
if (algoStr.isEmpty())
1088+
algoStr = chPreparationHandler.isEnabled() && !disableCH ? DIJKSTRA_BI : ASTAR_BI;
11001089
RoutingTemplate routingTemplate = createRoutingTemplate(request, ghRsp, algoStr, weighting);
11011090

11021091
StopWatch sw = new StopWatch().start();
1103-
List<QueryResult> qResults = routingTemplate.lookup(points);
1092+
List<QueryResult> qResults = routingTemplate.lookup(request.getPoints());
11041093
ghRsp.addDebugInfo("idLookup:" + sw.stop().getSeconds() + "s");
11051094
if (ghRsp.hasErrors())
11061095
return Collections.emptyList();
11071096

11081097
QueryGraph queryGraph = QueryGraph.create(graph, qResults);
1109-
int maxVisitedNodesForRequest = hints.getInt(Routing.MAX_VISITED_NODES, routingConfig.getMaxVisitedNodes());
1098+
int maxVisitedNodesForRequest = request.getHints().getInt(Routing.MAX_VISITED_NODES, routingConfig.getMaxVisitedNodes());
11101099
if (maxVisitedNodesForRequest > routingConfig.getMaxVisitedNodes())
11111100
throw new IllegalArgumentException("The max_visited_nodes parameter has to be below or equal to:" + routingConfig.getMaxVisitedNodes());
11121101

@@ -1115,15 +1104,15 @@ public List<Path> calcPaths(GHRequest request, GHResponse ghRsp) {
11151104
traversalMode(tMode).
11161105
weighting(weighting).
11171106
maxVisitedNodes(maxVisitedNodesForRequest).
1118-
hints(hints).
1107+
hints(request.getHints()).
11191108
build();
11201109

11211110
// do the actual route calculation !
11221111
List<Path> altPaths = routingTemplate.calcPaths(queryGraph, algorithmFactory, algoOpts);
11231112

1124-
boolean tmpEnableInstructions = hints.getBool(Routing.INSTRUCTIONS, encodingManager.isEnableInstructions());
1125-
boolean tmpCalcPoints = hints.getBool(Routing.CALC_POINTS, routingConfig.isCalcPoints());
1126-
double wayPointMaxDistance = hints.getDouble(Routing.WAY_POINT_MAX_DISTANCE, 1d);
1113+
boolean tmpEnableInstructions = request.getHints().getBool(Routing.INSTRUCTIONS, encodingManager.isEnableInstructions());
1114+
boolean tmpCalcPoints = request.getHints().getBool(Routing.CALC_POINTS, routingConfig.isCalcPoints());
1115+
double wayPointMaxDistance = request.getHints().getDouble(Routing.WAY_POINT_MAX_DISTANCE, 1d);
11271116

11281117
DouglasPeucker peucker = new DouglasPeucker().setMaxDistance(wayPointMaxDistance);
11291118
PathMerger pathMerger = new PathMerger(queryGraph.getBaseGraph(), weighting).
@@ -1144,6 +1133,16 @@ public List<Path> calcPaths(GHRequest request, GHResponse ghRsp) {
11441133
}
11451134
}
11461135

1136+
private List<String> getTurnCostProfiles() {
1137+
List<String> turnCostProfiles = new ArrayList<>();
1138+
for (Profile p : profilesByName.values()) {
1139+
if (p.isTurnCosts()) {
1140+
turnCostProfiles.add(p.getName());
1141+
}
1142+
}
1143+
return turnCostProfiles;
1144+
}
1145+
11471146
protected void validateRequest(GHRequest request) {
11481147
if (Helper.isEmpty(request.getProfile()))
11491148
throw new IllegalArgumentException("You need to specify a profile to perform a routing request, see docs/core/profiles.md");

core/src/main/java/com/graphhopper/routing/template/AlternativeRoutingTemplate.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,9 +74,9 @@ public void finish(PathMerger pathMerger, Translation tr) {
7474

7575
// if alternative route calculation was done then create the responses from single paths
7676
PointList wpList = getWaypoints();
77-
altResponse.setWaypoints(wpList);
78-
ghResponse.add(altResponse);
79-
pathMerger.doWork(altResponse, Collections.singletonList(pathList.get(0)), lookup, tr);
77+
pathWrapper.setWaypoints(wpList);
78+
ghResponse.add(pathWrapper);
79+
pathMerger.doWork(pathWrapper, Collections.singletonList(pathList.get(0)), lookup, tr);
8080
for (int index = 1; index < pathList.size(); index++) {
8181
PathWrapper tmpAltRsp = new PathWrapper();
8282
tmpAltRsp.setWaypoints(wpList);

core/src/main/java/com/graphhopper/routing/template/ViaRoutingTemplate.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ public class ViaRoutingTemplate extends AbstractRoutingTemplate implements Routi
5656
protected final GHResponse ghResponse;
5757
// result from route
5858
protected List<Path> pathList;
59-
protected final PathWrapper altResponse = new PathWrapper();
59+
protected final PathWrapper pathWrapper = new PathWrapper();
6060
private final EnumEncodedValue<RoadClass> roadClassEnc;
6161
private final EnumEncodedValue<RoadEnvironment> roadEnvEnc;
6262

@@ -113,7 +113,6 @@ public List<Path> calcPaths(QueryGraph queryGraph, RoutingAlgorithmFactory algoF
113113
}
114114
}
115115

116-
final boolean forceCurbsides = ghRequest.getHints().getBool(Routing.FORCE_CURBSIDE, true);
117116
QueryResult fromQResult = queryResults.get(0);
118117
StopWatch sw;
119118
for (int placeIndex = 1; placeIndex < pointsCount; placeIndex++) {
@@ -155,6 +154,7 @@ public List<Path> calcPaths(QueryGraph queryGraph, RoutingAlgorithmFactory algoF
155154
final String toCurbside = ghRequest.getCurbsides().get(placeIndex);
156155
int sourceOutEdge = DirectionResolverResult.getOutEdge(directions.get(placeIndex - 1), fromCurbside);
157156
int targetInEdge = DirectionResolverResult.getInEdge(directions.get(placeIndex), toCurbside);
157+
final boolean forceCurbsides = ghRequest.getHints().getBool(Routing.FORCE_CURBSIDE, true);
158158
sourceOutEdge = ignoreThrowOrAcceptImpossibleCurbsides(sourceOutEdge, placeIndex - 1, forceCurbsides);
159159
targetInEdge = ignoreThrowOrAcceptImpossibleCurbsides(targetInEdge, placeIndex, forceCurbsides);
160160

@@ -183,17 +183,18 @@ public List<Path> calcPaths(QueryGraph queryGraph, RoutingAlgorithmFactory algoF
183183
if (tmpPathList.isEmpty())
184184
throw new IllegalStateException("At least one path has to be returned for " + fromQResult + " -> " + toQResult);
185185

186-
int idx = 0;
187-
for (Path path : tmpPathList) {
186+
// todo: can tmpPathList ever have more than one path here? and would it even be correct to add them all
187+
// to pathList then?
188+
for (int i = 0; i < tmpPathList.size(); i++) {
189+
Path path = tmpPathList.get(i);
188190
if (path.getTime() < 0)
189-
throw new RuntimeException("Time was negative " + path.getTime() + " for index " + idx + ". Please report as bug and include:" + ghRequest);
191+
throw new RuntimeException("Time was negative " + path.getTime() + " for index " + i + ". Please report as bug and include:" + ghRequest);
190192

191193
pathList.add(path);
192194
debug += ", " + path.getDebugInfo();
193-
idx++;
194195
}
195196

196-
altResponse.addDebugInfo(debug);
197+
pathWrapper.addDebugInfo(debug);
197198

198199
// reset all direction enforcements in queryGraph to avoid influencing next path
199200
queryGraph.clearUnfavoredStatus();
@@ -202,7 +203,7 @@ public List<Path> calcPaths(QueryGraph queryGraph, RoutingAlgorithmFactory algoF
202203
throw new IllegalArgumentException("No path found due to maximum nodes exceeded " + algoOpts.getMaxVisitedNodes());
203204

204205
visitedNodesSum += algo.getVisitedNodes();
205-
altResponse.addDebugInfo("visited nodes sum: " + visitedNodesSum);
206+
pathWrapper.addDebugInfo("visited nodes sum: " + visitedNodesSum);
206207
fromQResult = toQResult;
207208
}
208209

@@ -232,9 +233,9 @@ public void finish(PathMerger pathMerger, Translation tr) {
232233
if (ghRequest.getPoints().size() - 1 != pathList.size())
233234
throw new RuntimeException("There should be exactly one more points than paths. points:" + ghRequest.getPoints().size() + ", paths:" + pathList.size());
234235

235-
altResponse.setWaypoints(getWaypoints());
236-
ghResponse.add(altResponse);
237-
pathMerger.doWork(altResponse, pathList, lookup, tr);
236+
pathWrapper.setWaypoints(getWaypoints());
237+
ghResponse.add(pathWrapper);
238+
pathMerger.doWork(pathWrapper, pathList, lookup, tr);
238239
}
239240

240241
}

0 commit comments

Comments
 (0)