/* * 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.util.FlagEncoder; import com.graphhopper.util.EdgeIteratorState; import com.graphhopper.util.PMap; import com.graphhopper.util.Parameters.Routing; /** * Calculates the fastest route with the specified vehicle (VehicleEncoder). Calculates the weight * in seconds. * <p> * * @author Peter Karich */ public class FastestWeighting extends AbstractWeighting { /** * Converting to seconds is not necessary but makes adding other penalties easier (e.g. turn * costs or traffic light costs etc) */ protected final static double SPEED_CONV = 3.6; private final double headingPenalty; private final long headingPenaltyMillis; private final double maxSpeed; public FastestWeighting(FlagEncoder encoder, PMap pMap) { super(encoder); headingPenalty = pMap.getDouble(Routing.HEADING_PENALTY, Routing.DEFAULT_HEADING_PENALTY); headingPenaltyMillis = Math.round(headingPenalty * 1000); maxSpeed = encoder.getMaxSpeed() / SPEED_CONV; } public FastestWeighting(FlagEncoder encoder) { this(encoder, new PMap(0)); } @Override public double getMinWeight(double distance) { return distance / maxSpeed; } @Override public double calcWeight(EdgeIteratorState edge, boolean reverse, int prevOrNextEdgeId) { double speed = reverse ? flagEncoder.getReverseSpeed(edge.getFlags()) : flagEncoder.getSpeed(edge.getFlags()); if (speed == 0) return Double.POSITIVE_INFINITY; double time = edge.getDistance() / speed * SPEED_CONV; // add direction penalties at start/stop/via points boolean unfavoredEdge = edge.getBool(EdgeIteratorState.K_UNFAVORED_EDGE, false); if (unfavoredEdge) time += headingPenalty; return time; } @Override public long calcMillis(EdgeIteratorState edgeState, boolean reverse, int prevOrNextEdgeId) { // TODO move this to AbstractWeighting? long time = 0; boolean unfavoredEdge = edgeState.getBool(EdgeIteratorState.K_UNFAVORED_EDGE, false); if (unfavoredEdge) time += headingPenaltyMillis; return time + super.calcMillis(edgeState, reverse, prevOrNextEdgeId); } @Override public String getName() { return "fastest"; } }