Repository navigation
Closed
Description
When using the function addTurnInfo(int fromEdge, int viaNode, int toEdge, long turnFlags) a second time on the same edges, it doesn't update the turn costs.
When trying to receive them by calling getTurnCostFlags(int edgeFrom, int nodeVia, int edgeTo) you'll get the first cost entry - not the 'updated' one.
Add the following test to EdgeBasedRoutingAlgorithmTest shows that effect.
@Test
public void testTurnCostsUpdateBug() {
final GraphHopperStorage g = createStorage(createEncodingManager(false));
initGraph(g);
TurnCostExtension tcs = (TurnCostExtension) g.getExtension();
long tflags = carEncoder.getTurnFlags(false, 1);
tcs.addTurnInfo(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 3).getEdge(), tflags);
tflags = carEncoder.getTurnFlags(false, 2);
tcs.addTurnInfo(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 3).getEdge(), tflags);
assertEquals(tflags, tcs.getTurnCostFlags(getEdge(g, 5, 2).getEdge(), 2, getEdge(g, 2, 3).getEdge()));
}
i know this problem could be solved by simply using the correct turnCosts in the first place, but in some cases this might be the easier way.
This hack is fixing the problem by iterating all turnCostEntries and using the maximum.
private long nextCostFlags(int edgeFrom, int nodeVia, int edgeTo) {
int turnCostIndex = nodeAccess.getAdditionalNodeField(nodeVia);
+ int cost = -1;
int i = 0;
for (; i < 1000; i++) {
if (turnCostIndex == NO_TURN_ENTRY)
@@ -168,7 +168,8 @@ public class TurnCostExtension implements GraphExtension {
long turnCostPtr = (long) turnCostIndex * turnCostsEntryBytes;
if (edgeFrom == turnCosts.getInt(turnCostPtr + TC_FROM)) {
if (edgeTo == turnCosts.getInt(turnCostPtr + TC_TO))
- return turnCosts.getInt(turnCostPtr + TC_FLAGS);
+ if (cost < turnCosts.getInt(turnCostPtr + TC_FLAGS))
+ cost = turnCosts.getInt(turnCostPtr + TC_FLAGS);
}
int nextTurnCostIndex = turnCosts.getInt(turnCostPtr + TC_NEXT);
@@ -177,6 +178,9 @@ public class TurnCostExtension implements GraphExtension {
turnCostIndex = nextTurnCostIndex;
}
+ if (cost >= 0)
+ return cost;
+
// so many turn restrictions on one node? here is something wrong
if (i >= 1000)
throw new IllegalStateException("something went wrong: there seems to be no end of the turn cost-list!?");
return EMPTY_FLAGS;
}
I guess the clean solution would be an update mechanism when adding the turnCosts.
Reacted by Peter
Activity
Nakaner commentedon Jan 22, 2018
I observed a problem which is also caused by this "bug".
If you want to have an EncodingManager with multiple FlagEncoders which use turn costs, it will only work with the FlagEncoder whose turn costs you wrote first to the TurnCostExtension because these costs will be returned later for all FlagEncoders.
This test will pass:
But it will fail if I try to write the turn costs of both encoders separately:
A user can circumvent this "bug" by writing all turn costs information with only one single call of
TurnCostExtension.addTurnInfo(int, int, long)and doing a bitwise OR on all flags to be written.I am currently thinking how to fix this bug nicely without just adding a warning in the documenation of
TurnCostExtension.addTurnInfo(int, int, long).karussell commentedon Jan 22, 2018
Thanks for pointing this out. Before we add a new turn info we must somehow check existing stuff and overwrite them (or due to multiple FlagEncoders we have to properly merge them). The cool thing would be if we could rewrite this turn cost part with #1112 in mind or afterwards or even fix this with #1112.
update turn info if called multiple times, fixes #1107