-
Notifications
You must be signed in to change notification settings - Fork 1.5k
/
AbstractWeighting.java
115 lines (98 loc) · 4.63 KB
/
AbstractWeighting.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
/*
* 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.weighting;
import com.graphhopper.routing.profiles.BooleanEncodedValue;
import com.graphhopper.routing.profiles.DecimalEncodedValue;
import com.graphhopper.routing.util.FlagEncoder;
import com.graphhopper.routing.util.HintsMap;
import com.graphhopper.util.EdgeIteratorState;
import static com.graphhopper.util.Helper.toLowerCase;
/**
* @author Peter Karich
*/
public abstract class AbstractWeighting implements Weighting {
protected final FlagEncoder flagEncoder;
protected final DecimalEncodedValue avSpeedEnc;
protected final BooleanEncodedValue accessEnc;
protected AbstractWeighting(FlagEncoder encoder) {
this.flagEncoder = encoder;
if (!flagEncoder.isRegistered())
throw new IllegalStateException("Make sure you add the FlagEncoder " + flagEncoder + " to an EncodingManager before using it elsewhere");
if (!isValidName(getName()))
throw new IllegalStateException("Not a valid name for a Weighting: " + getName());
avSpeedEnc = encoder.getAverageSpeedEnc();
accessEnc = encoder.getAccessEnc();
}
@Override
public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) {
// special case for loop edges: since they do not have a meaningful direction we always need to read them in
// forward direction
if (edgeState.getBaseNode() == edgeState.getAdjNode()) {
reverse = false;
}
if (reverse && !edgeState.getReverse(accessEnc) || !reverse && !edgeState.get(accessEnc))
throw new IllegalStateException("Calculating time should not require to read speed from edge in wrong direction. " +
"(" + edgeState.getBaseNode() + " - " + edgeState.getAdjNode() + ") "
+ edgeState.fetchWayGeometry(3) + ", dist: " + edgeState.getDistance() + " "
+ "Reverse:" + reverse + ", fwd:" + edgeState.get(accessEnc) + ", bwd:" + edgeState.getReverse(accessEnc) + ", fwd-speed: " + edgeState.get(avSpeedEnc) + ", bwd-speed: " + edgeState.getReverse(avSpeedEnc));
double speed = reverse ? edgeState.getReverse(avSpeedEnc) : edgeState.get(avSpeedEnc);
if (Double.isInfinite(speed) || Double.isNaN(speed) || speed < 0)
throw new IllegalStateException("Invalid speed stored in edge! " + speed);
if (speed == 0)
throw new IllegalStateException("Speed cannot be 0 for unblocked edge, use access properties to mark edge blocked! Should only occur for shortest path calculation. See #242.");
return (long) (edgeState.getDistance() * 3600 / speed);
}
@Override
public boolean matches(HintsMap reqMap) {
return getName().equals(reqMap.getWeighting()) && flagEncoder.toString().equals(reqMap.getVehicle());
}
@Override
public FlagEncoder getFlagEncoder() {
return flagEncoder;
}
@Override
public int hashCode() {
int hash = 7;
hash = 71 * hash + toString().hashCode();
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
final Weighting other = (Weighting) obj;
return toString().equals(other.toString());
}
static final boolean isValidName(String name) {
if (name == null || name.isEmpty())
return false;
return name.matches("[\\|_a-z]+");
}
/**
* Replaces all characters which are not numbers, characters or underscores with underscores
*/
public static String weightingToFileName(Weighting w, boolean edgeBased) {
return toLowerCase(w.toString()).replaceAll("\\|", "_") + (edgeBased ? "_edge" : "_node");
}
@Override
public String toString() {
return getName() + "|" + flagEncoder;
}
}