com.graphhopper.routing.weighting.AbstractWeighting Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of graphhopper-core Show documentation
Show all versions of graphhopper-core Show documentation
GraphHopper is a fast and memory efficient Java road routing engine
working seamlessly with OpenStreetMap data.
/*
* 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.ev.BooleanEncodedValue;
import com.graphhopper.routing.ev.DecimalEncodedValue;
import com.graphhopper.util.EdgeIteratorState;
import com.graphhopper.util.FetchMode;
import static com.graphhopper.routing.weighting.TurnCostProvider.NO_TURN_COST_PROVIDER;
/**
* @author Peter Karich
*/
public abstract class AbstractWeighting implements Weighting {
protected final BooleanEncodedValue accessEnc;
protected final DecimalEncodedValue speedEnc;
private final TurnCostProvider turnCostProvider;
protected AbstractWeighting(BooleanEncodedValue accessEnc, DecimalEncodedValue speedEnc, TurnCostProvider turnCostProvider) {
if (!Weighting.isValidName(getName()))
throw new IllegalStateException("Not a valid name for a Weighting: " + getName());
this.accessEnc = accessEnc;
this.speedEnc = speedEnc;
this.turnCostProvider = turnCostProvider;
}
@Override
public long calcEdgeMillis(EdgeIteratorState edgeState, boolean reverse) {
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(FetchMode.ALL) + ", dist: " + edgeState.getDistance() + " "
+ "Reverse:" + reverse + ", fwd:" + edgeState.get(accessEnc) + ", bwd:" + edgeState.getReverse(accessEnc) + ", fwd-speed: " + edgeState.get(speedEnc) + ", bwd-speed: " + edgeState.getReverse(speedEnc));
double speed = reverse ? edgeState.getReverse(speedEnc) : edgeState.get(speedEnc);
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 Math.round(edgeState.getDistance() / speed * 3.6 * 1000);
}
@Override
public double calcTurnWeight(int inEdge, int viaNode, int outEdge) {
return turnCostProvider.calcTurnWeight(inEdge, viaNode, outEdge);
}
@Override
public long calcTurnMillis(int inEdge, int viaNode, int outEdge) {
return turnCostProvider.calcTurnMillis(inEdge, viaNode, outEdge);
}
@Override
public boolean hasTurnCosts() {
return turnCostProvider != NO_TURN_COST_PROVIDER;
}
@Override
public String toString() {
return getName() + "|" + speedEnc.getName();
}
}