Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion core/src/main/java/com/graphhopper/GraphHopper.java
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,10 @@ protected void initCHPrepare()
prepare.setGraph(graph);
}

protected Weighting createWeighting( String weighting, FlagEncoder encoder )
/**
* public for test only
*/
public Weighting createWeighting( String weighting, FlagEncoder encoder )
{
// ignore case
weighting = weighting.toLowerCase();
Expand Down
10 changes: 9 additions & 1 deletion core/src/main/java/com/graphhopper/reader/OSMTurnRelation.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,13 @@
import java.util.Collection;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedList;
import java.util.Map;
import java.util.Set;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.graphhopper.routing.util.TurnCostEncoder;
import com.graphhopper.util.EdgeExplorer;
import com.graphhopper.util.EdgeIterator;
Expand All @@ -17,6 +21,7 @@
*/
public class OSMTurnRelation
{
static Logger logger = LoggerFactory.getLogger(OSMTurnRelation.class);

enum Type
{
Expand Down Expand Up @@ -87,7 +92,10 @@ public Collection<TurnCostTableEntry> getRestrictionAsEntries( TurnCostEncoder e
{
if (viaNodeId == OSMReader.EMPTY)
{
throw new IllegalArgumentException("Unknown node osm id");
// could this happen due to a problem in the OSM data?
//throw new IllegalArgumentException("Unknown node osm id "+viaOsm);
logger.warn("Unknown node osm id:"+viaOsm);
return new LinkedList<OSMTurnRelation.TurnCostTableEntry>();
}

int edgeIdFrom = EdgeIterator.NO_EDGE;
Expand Down
11 changes: 10 additions & 1 deletion core/src/main/java/com/graphhopper/routing/AStar.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ public AStar( Graph g, FlagEncoder encoder, Weighting weighting )
super(g, encoder, weighting);
initCollections(1000);
setApproximation(true);

// for turn restrictions
// Note: if turn restrictions are enabled during the test com.graphhopper.routing.RoutingAlgorithmIT.testPerformance()
// it will fail
if (weighting instanceof TurnWeighting)
if (((TurnWeighting)weighting).isEnabledTurnRestrictions() || ((TurnWeighting)weighting).isEnabledTurnRestrictions())
setTraversalMode(AbstractRoutingAlgorithm.TRAVERSAL_MODE.EDGE_BASED_DIRECTION_SENSITIVE);


}

/**
Expand Down Expand Up @@ -118,7 +127,7 @@ private Path runAlgo()

if (weighting instanceof TurnWeighting)
{
alreadyVisitedWeight += ((TurnWeighting) weighting).calcTurnWeight(currEdge.edge, neighborNode, iter.getEdge(), false);
alreadyVisitedWeight += ((TurnWeighting) weighting).calcTurnWeight(currEdge.edge, iter.getBaseNode(), iter.getEdge(), false);
}

AStarEdge nEdge = fromMap.get(iterationKey);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,90 +1,98 @@
package com.graphhopper.routing.util;

import com.graphhopper.storage.TurnCostStorage;

/**
* Provides the storage required by turn cost calculation
*
* @author Karl Hübner
*/
public abstract class AbstractTurnWeighting implements TurnWeighting
{

private boolean enabledTurnRestrictions = false;
private boolean enabledTurnCosts = false;

/**
* Storage, which contains the turn flags
*/
protected TurnCostStorage turnCostStorage;

/**
* Encoder, which decodes the turn flags
*/
protected TurnCostEncoder turnCostEncoder;

public AbstractTurnWeighting( TurnCostEncoder encoder )
{
this.turnCostEncoder = encoder;
}

/**
* Is required to inject the storage containing the turn flags
*/
@Override
public void initTurnWeighting( TurnCostStorage turnCostStorage )
{
this.turnCostStorage = turnCostStorage;
}

/**
* enables/disables the turn weight / restrictions
*/
@Override
public void setEnableTurnWeighting( boolean turnRestrictions, boolean turnCosts )
{
this.enabledTurnRestrictions = turnRestrictions;
this.enabledTurnCosts = turnCosts;
}

@Override
public boolean isEnabledTurnCosts()
{
return enabledTurnCosts;
}

@Override
public boolean isEnabledTurnRestrictions()
{
return enabledTurnRestrictions;
}

@Override
public double calcTurnWeight( int edgeFrom, int nodeVia, int edgeTo, boolean reverse )
{
if (!isEnabledTurnCosts() && !isEnabledTurnRestrictions())
{
return 0;
}

if (turnCostStorage == null)
{
throw new AssertionError("No storage set to calculate turn weight");
}
if (turnCostEncoder == null)
{
throw new AssertionError("No encoder set to calculate turn weight");
}

if (reverse)
{
return calcTurnWeight(edgeTo, nodeVia, edgeFrom);
} else
{
return calcTurnWeight(edgeFrom, nodeVia, edgeTo);
}
}

protected abstract double calcTurnWeight( int edgeTo, int nodeVia, int edgeFrom );

}
package com.graphhopper.routing.util;

import com.graphhopper.storage.TurnCostStorage;

/**
* Provides the storage required by turn cost calculation
*
* @author Karl Hübner
*/
public abstract class AbstractTurnWeighting implements TurnWeighting
{

private boolean enabledTurnRestrictions = false;
private boolean enabledTurnCosts = false;

/**
* Storage, which contains the turn flags
*/
protected TurnCostStorage turnCostStorage;

/**
* Encoder, which decodes the turn flags
*/
protected TurnCostEncoder turnCostEncoder;

public AbstractTurnWeighting( TurnCostEncoder encoder )
{
this.turnCostEncoder = encoder;
}

/**
* Is required to inject the storage containing the turn flags
*/
@Override
public void initTurnWeighting( TurnCostStorage turnCostStorage )
{
this.turnCostStorage = turnCostStorage;
}

/**
* enables/disables the turn weight / restrictions
*/
@Override
public void setEnableTurnWeighting( boolean turnRestrictions, boolean turnCosts )
{
this.enabledTurnRestrictions = turnRestrictions;
this.enabledTurnCosts = turnCosts;
}

@Override
public boolean isEnabledTurnCosts()
{
return enabledTurnCosts;
}

@Override
public boolean isEnabledTurnRestrictions()
{
return enabledTurnRestrictions;
}

@Override
public double calcTurnWeight( int edgeFrom, int nodeVia, int edgeTo, boolean reverse )
{
// FIXME:
// when commented : astarbi and dijkstrabi are failing in com.graphhopper.routing.RoutingAlgorithmIT.testMoscowTurnRestrictions()
// when not commented : com.graphhopper.routing.DijkstraBidirectionRefTest.testCalcIfEmptyWay()
// if (edgeFrom==edgeTo){
// // prevent U turn in A* bidirectional EDGE_BASED
// return Double.MAX_VALUE;
// }

if (!isEnabledTurnCosts() && !isEnabledTurnRestrictions())
{
return 0;
}

if (turnCostStorage == null)
{
throw new AssertionError("No storage set to calculate turn weight");
}
if (turnCostEncoder == null)
{
throw new AssertionError("No encoder set to calculate turn weight");
}

if (reverse)
{
return calcTurnWeight(edgeTo, nodeVia, edgeFrom);
} else
{
return calcTurnWeight(edgeFrom, nodeVia, edgeTo);
}
}

protected abstract double calcTurnWeight( int edgeTo, int nodeVia, int edgeFrom );

}
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public String toString()
@Override
protected double calcTurnWeight( int edgeFrom, int nodeVia, int edgeTo )
{
int turnFlags = turnCostStorage.getTurnCosts(edgeFrom, nodeVia, edgeTo);
int turnFlags = turnCostStorage.getTurnCosts(nodeVia, edgeFrom, edgeTo);
if (isEnabledTurnRestrictions() && turnCostEncoder.isTurnRestricted(turnFlags))
{
//we only consider turn restrictions in shortest calculation
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,18 @@
import com.graphhopper.storage.index.LocationIndex;
import com.graphhopper.storage.LevelGraph;
import com.graphhopper.util.StopWatch;

import static com.graphhopper.routing.util.NoOpAlgorithmPreparation.*;

import com.graphhopper.storage.*;
import com.graphhopper.storage.index.LocationIndexTreeSC;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Map.Entry;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -114,16 +119,26 @@ public ME( AlgorithmPreparation ap, LocationIndex idx )
}

public static Collection<Entry<AlgorithmPreparation, LocationIndex>> createAlgos( Graph g,
LocationIndex idx, FlagEncoder encoder, boolean withCh, Weighting weighting, EncodingManager manager )
LocationIndex idx, FlagEncoder encoder,boolean withCh, Weighting weighting, EncodingManager manager ){
return createAlgos(g, idx, encoder, getAlgoNames(), withCh, weighting, manager);
}
public static List<String> getAlgoNames(){
List<String>algoNames=Arrays.asList(
"astar"
//,"dijkstraOneToMany"
,"astarbi"
,"dijkstraNativebi"
,"dijkstrabi");
return algoNames;
}

public static Collection<Entry<AlgorithmPreparation, LocationIndex>> createAlgos( Graph g,
LocationIndex idx, FlagEncoder encoder, List<String>algoNames,boolean withCh, Weighting weighting, EncodingManager manager )
{
// List<Entry<AlgorithmPreparation, LocationIndex>> prepare = new ArrayList<Entry<AlgorithmPreparation, LocationIndex>>();
List<Entry<AlgorithmPreparation, LocationIndex>> prepare = new ArrayList<Entry<AlgorithmPreparation, LocationIndex>>();
prepare.add(new ME(createAlgoPrepare(g, "astar", encoder, weighting), idx));
// prepare.add(new ME(createAlgoPrepare(g, "dijkstraOneToMany", encoder, weighting), idx));
prepare.add(new ME(createAlgoPrepare(g, "astarbi", encoder, weighting), idx));
prepare.add(new ME(createAlgoPrepare(g, "dijkstraNativebi", encoder, weighting), idx));
prepare.add(new ME(createAlgoPrepare(g, "dijkstrabi", encoder, weighting), idx));
prepare.add(new ME(createAlgoPrepare(g, "dijkstra", encoder, weighting), idx));
for(String algoName:algoNames)
prepare.add(new ME(createAlgoPrepare(g, algoName, encoder, weighting), idx));

if (withCh)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ public String toString()
@Override
protected double calcTurnWeight( int edgeFrom, int nodeVia, int edgeTo )
{
int turnFlags = turnCostStorage.getTurnCosts(edgeFrom, nodeVia, edgeTo);
int turnFlags = turnCostStorage.getTurnCosts(nodeVia, edgeFrom, edgeTo);
if (isEnabledTurnRestrictions() && turnCostEncoder.isTurnRestricted(turnFlags))
{
//we only consider turn restrictions in shortest calculation
Expand Down
Loading