-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
DefaultWeightingFactory.java
121 lines (110 loc) · 7.12 KB
/
DefaultWeightingFactory.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
/*
* Licensed to GraphHopper GmbH under one or more contributor
* license agreements. See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*
* GraphHopper GmbH 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.routing;
import com.graphhopper.config.Profile;
import com.graphhopper.routing.ev.*;
import com.graphhopper.routing.util.EncodingManager;
import com.graphhopper.routing.weighting.*;
import com.graphhopper.routing.weighting.custom.CustomModelParser;
import com.graphhopper.routing.weighting.custom.CustomProfile;
import com.graphhopper.routing.weighting.custom.CustomWeighting;
import com.graphhopper.storage.BaseGraph;
import com.graphhopper.util.CustomModel;
import com.graphhopper.util.PMap;
import com.graphhopper.util.Parameters;
import static com.graphhopper.routing.weighting.TurnCostProvider.NO_TURN_COST_PROVIDER;
import static com.graphhopper.routing.weighting.Weighting.INFINITE_U_TURN_COSTS;
import static com.graphhopper.util.Helper.toLowerCase;
public class DefaultWeightingFactory implements WeightingFactory {
private final BaseGraph graph;
private final EncodingManager encodingManager;
public DefaultWeightingFactory(BaseGraph graph, EncodingManager encodingManager) {
this.graph = graph;
this.encodingManager = encodingManager;
}
@Override
public Weighting createWeighting(Profile profile, PMap requestHints, boolean disableTurnCosts) {
// Merge profile hints with request hints, the request hints take precedence.
// Note that so far we do not check if overwriting the profile hints actually works with the preparation
// for LM/CH. Later we should also limit the number of parameters that can be used to modify the profile.
// todo: since we are not dealing with block_area here yet we cannot really apply any merging rules
// for it, see discussion here: https://github.com/graphhopper/graphhopper/pull/1958#discussion_r395462901
PMap hints = new PMap();
hints.putAll(profile.getHints());
hints.putAll(requestHints);
final String vehicle = profile.getVehicle();
TurnCostProvider turnCostProvider;
if (profile.isTurnCosts() && !disableTurnCosts) {
DecimalEncodedValue turnCostEnc = encodingManager.getDecimalEncodedValue(TurnCost.key(vehicle));
if (turnCostEnc == null)
throw new IllegalArgumentException("Vehicle " + vehicle + " does not support turn costs");
int uTurnCosts = hints.getInt(Parameters.Routing.U_TURN_COSTS, INFINITE_U_TURN_COSTS);
turnCostProvider = new DefaultTurnCostProvider(turnCostEnc, graph.getTurnCostStorage(), uTurnCosts);
} else {
turnCostProvider = NO_TURN_COST_PROVIDER;
}
String weightingStr = toLowerCase(profile.getWeighting());
if (weightingStr.isEmpty())
throw new IllegalArgumentException("You have to specify a weighting");
Weighting weighting = null;
BooleanEncodedValue accessEnc = encodingManager.getBooleanEncodedValue(EncodingManager.getKey(vehicle, "access"));
DecimalEncodedValue speedEnc = encodingManager.getDecimalEncodedValue(EncodingManager.getKey(vehicle, "average_speed"));
DecimalEncodedValue priorityEnc = encodingManager.hasEncodedValue(EncodingManager.getKey(vehicle, "priority"))
? encodingManager.getDecimalEncodedValue(EncodingManager.getKey(vehicle, "priority"))
: null;
if (CustomWeighting.NAME.equalsIgnoreCase(weightingStr)) {
if (!(profile instanceof CustomProfile))
throw new IllegalArgumentException("custom weighting requires a CustomProfile but was profile=" + profile.getName());
CustomModel queryCustomModel = requestHints.getObject(CustomModel.KEY, null);
CustomProfile customProfile = (CustomProfile) profile;
queryCustomModel = CustomModel.merge(customProfile.getCustomModel(), queryCustomModel);
// todonow: this used to be encoder.getMaxSpeed()!
double maxSpeed = speedEnc.getMaxDecimal();
weighting = CustomModelParser.createWeighting(accessEnc, speedEnc,
priorityEnc, maxSpeed, encodingManager, turnCostProvider, queryCustomModel);
} else if ("shortest".equalsIgnoreCase(weightingStr)) {
weighting = new ShortestWeighting(accessEnc, speedEnc, turnCostProvider);
} else if ("fastest".equalsIgnoreCase(weightingStr)) {
if (!encodingManager.hasEncodedValue(RoadAccess.KEY))
throw new IllegalArgumentException("fastest weighting requires road_access");
EnumEncodedValue<RoadAccess> roadAccessEnc = encodingManager.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class);
if (priorityEnc != null)
weighting = new PriorityWeighting(accessEnc, speedEnc, priorityEnc, roadAccessEnc, hints, turnCostProvider);
else
weighting = new FastestWeighting(accessEnc, speedEnc, roadAccessEnc, hints, turnCostProvider);
} else if ("curvature".equalsIgnoreCase(weightingStr)) {
DecimalEncodedValue curvatureEnc = encodingManager.hasEncodedValue(EncodingManager.getKey(vehicle, "curvature"))
? encodingManager.getDecimalEncodedValue(EncodingManager.getKey(vehicle, "curvature"))
: null;
if (curvatureEnc == null || priorityEnc == null)
throw new IllegalArgumentException("curvature weighting requires curvature and priority, but not found for " + vehicle);
if (!encodingManager.hasEncodedValue(RoadAccess.KEY))
throw new IllegalArgumentException("curvature weighting requires road_access");
EnumEncodedValue<RoadAccess> roadAccessEnc = encodingManager.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class);
weighting = new CurvatureWeighting(accessEnc, speedEnc, priorityEnc, curvatureEnc, roadAccessEnc, hints, turnCostProvider);
} else if ("short_fastest".equalsIgnoreCase(weightingStr)) {
if (!encodingManager.hasEncodedValue(RoadAccess.KEY))
throw new IllegalArgumentException("curvature weighting requires road_access");
EnumEncodedValue<RoadAccess> roadAccessEnc = encodingManager.getEnumEncodedValue(RoadAccess.KEY, RoadAccess.class);
weighting = new ShortFastestWeighting(accessEnc, speedEnc, roadAccessEnc, hints, turnCostProvider);
}
if (weighting == null)
throw new IllegalArgumentException("Weighting '" + weightingStr + "' not supported");
return weighting;
}
}