forked from graphhopper/graphhopper
-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from GIScience/Interface_speedcalculator
Make SpeedCalculator an interface and ConditionalSpeedCalculator an i…
- Loading branch information
Showing
3 changed files
with
87 additions
and
76 deletions.
There are no files selected for viewing
82 changes: 82 additions & 0 deletions
82
core/src/main/java/com/graphhopper/routing/util/ConditionalSpeedCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
package com.graphhopper.routing.util; | ||
|
||
import ch.poole.conditionalrestrictionparser.ConditionalRestrictionParser; | ||
import ch.poole.conditionalrestrictionparser.Restriction; | ||
import com.graphhopper.routing.EdgeKeys; | ||
import com.graphhopper.routing.profiles.BooleanEncodedValue; | ||
import com.graphhopper.routing.profiles.DecimalEncodedValue; | ||
import com.graphhopper.util.DateTimeHelper; | ||
import com.graphhopper.storage.ConditionalEdgesMap; | ||
import com.graphhopper.storage.GraphHopperStorage; | ||
import com.graphhopper.util.EdgeIteratorState; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.time.ZonedDateTime; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* Calculate time-dependent conditional speed | ||
* | ||
* @author Andrzej Oles | ||
*/ | ||
public class ConditionalSpeedCalculator implements SpeedCalculator{ | ||
protected final DecimalEncodedValue avSpeedEnc; | ||
|
||
// time-dependent stuff | ||
private final BooleanEncodedValue conditionalEnc; | ||
private final ConditionalEdgesMap conditionalEdges; | ||
private final DateTimeHelper dateTimeHelper; | ||
|
||
public ConditionalSpeedCalculator(GraphHopperStorage graph, FlagEncoder encoder) { | ||
avSpeedEnc = encoder.getAverageSpeedEnc(); | ||
|
||
// time-dependent stuff | ||
EncodingManager encodingManager = graph.getEncodingManager(); | ||
String encoderName = encodingManager.getKey(encoder, "conditional_speed"); | ||
|
||
if (!encodingManager.hasEncodedValue(encoderName)) { | ||
throw new IllegalStateException("No conditional speed associated with the flag encoder"); | ||
} | ||
|
||
conditionalEnc = encodingManager.getBooleanEncodedValue(encoderName); | ||
conditionalEdges = graph.getConditionalSpeed(encoder); | ||
|
||
this.dateTimeHelper = new DateTimeHelper(graph); | ||
} | ||
|
||
public double getSpeed(EdgeIteratorState edge, boolean reverse, long time) { | ||
double speed = reverse ? edge.getReverse(avSpeedEnc) : edge.get(avSpeedEnc); | ||
|
||
// retrieve time-dependent maxspeed here | ||
if (time != -1 && edge.get(conditionalEnc)) { | ||
ZonedDateTime zonedDateTime = dateTimeHelper.getZonedDateTime(edge, time); | ||
int edgeId = EdgeKeys.getOriginalEdge(edge); | ||
String value = conditionalEdges.getValue(edgeId); | ||
double maxSpeed = getSpeed(value, zonedDateTime); | ||
if (maxSpeed >= 0) | ||
return maxSpeed * 0.9; | ||
} | ||
|
||
return speed; | ||
} | ||
|
||
private double getSpeed(String conditional, ZonedDateTime zonedDateTime) { | ||
try { | ||
ConditionalRestrictionParser crparser = new ConditionalRestrictionParser(new ByteArrayInputStream(conditional.getBytes())); | ||
ArrayList<Restriction> restrictions = crparser.restrictions(); | ||
|
||
// iterate over restrictions starting from the last one in order to match to the most specific one | ||
for (int i = restrictions.size() - 1 ; i >= 0; i--) { | ||
Restriction restriction = restrictions.get(i); | ||
// stop as soon as time matches the combined conditions | ||
if (TimeDependentConditionalEvaluator.match(restriction.getConditions(), zonedDateTime)) { | ||
return AbstractFlagEncoder.parseSpeed(restriction.getValue()); | ||
} | ||
} | ||
} catch (ch.poole.conditionalrestrictionparser.ParseException e) { | ||
//nop | ||
} | ||
return -1; | ||
} | ||
|
||
} |
79 changes: 4 additions & 75 deletions
79
core/src/main/java/com/graphhopper/routing/util/SpeedCalculator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,83 +1,12 @@ | ||
package com.graphhopper.routing.util; | ||
|
||
import ch.poole.conditionalrestrictionparser.ConditionalRestrictionParser; | ||
import ch.poole.conditionalrestrictionparser.Restriction; | ||
import com.graphhopper.routing.EdgeKeys; | ||
import com.graphhopper.routing.profiles.BooleanEncodedValue; | ||
import com.graphhopper.routing.profiles.DecimalEncodedValue; | ||
import com.graphhopper.util.DateTimeHelper; | ||
import com.graphhopper.storage.ConditionalEdgesMap; | ||
import com.graphhopper.storage.GraphHopperStorage; | ||
import com.graphhopper.util.EdgeIteratorState; | ||
|
||
import java.io.ByteArrayInputStream; | ||
import java.time.ZonedDateTime; | ||
import java.util.ArrayList; | ||
|
||
/** | ||
* Calculate time-dependent conditional speed | ||
* Common interface for time-dependent speed calculation | ||
* | ||
* @author Andrzej Oles | ||
* @author Hendrik Leuschner | ||
*/ | ||
public class SpeedCalculator { | ||
//protected final FlagEncoder flagEncoder; | ||
protected final DecimalEncodedValue avSpeedEnc; | ||
|
||
// time-dependent stuff | ||
private final BooleanEncodedValue conditionalEnc; | ||
private final ConditionalEdgesMap conditionalEdges; | ||
private final DateTimeHelper dateTimeHelper; | ||
|
||
public SpeedCalculator(GraphHopperStorage graph, FlagEncoder encoder) { | ||
avSpeedEnc = encoder.getAverageSpeedEnc(); | ||
|
||
// time-dependent stuff | ||
EncodingManager encodingManager = graph.getEncodingManager(); | ||
String encoderName = encodingManager.getKey(encoder, "conditional_speed"); | ||
|
||
if (!encodingManager.hasEncodedValue(encoderName)) { | ||
throw new IllegalStateException("No conditional speed associated with the flag encoder"); | ||
} | ||
|
||
conditionalEnc = encodingManager.getBooleanEncodedValue(encoderName); | ||
conditionalEdges = graph.getConditionalSpeed(encoder); | ||
|
||
this.dateTimeHelper = new DateTimeHelper(graph); | ||
} | ||
|
||
public double getSpeed(EdgeIteratorState edge, boolean reverse, long time) { | ||
double speed = reverse ? edge.getReverse(avSpeedEnc) : edge.get(avSpeedEnc); | ||
|
||
// retrieve time-dependent maxspeed here | ||
if (time != -1 && edge.get(conditionalEnc)) { | ||
ZonedDateTime zonedDateTime = dateTimeHelper.getZonedDateTime(edge, time); | ||
int edgeId = EdgeKeys.getOriginalEdge(edge); | ||
String value = conditionalEdges.getValue(edgeId); | ||
double maxSpeed = getSpeed(value, zonedDateTime); | ||
if (maxSpeed >= 0) | ||
return maxSpeed * 0.9; | ||
} | ||
|
||
return speed; | ||
} | ||
|
||
private double getSpeed(String conditional, ZonedDateTime zonedDateTime) { | ||
try { | ||
ConditionalRestrictionParser crparser = new ConditionalRestrictionParser(new ByteArrayInputStream(conditional.getBytes())); | ||
ArrayList<Restriction> restrictions = crparser.restrictions(); | ||
|
||
// iterate over restrictions starting from the last one in order to match to the most specific one | ||
for (int i = restrictions.size() - 1 ; i >= 0; i--) { | ||
Restriction restriction = restrictions.get(i); | ||
// stop as soon as time matches the combined conditions | ||
if (TimeDependentConditionalEvaluator.match(restriction.getConditions(), zonedDateTime)) { | ||
return AbstractFlagEncoder.parseSpeed(restriction.getValue()); | ||
} | ||
} | ||
} catch (ch.poole.conditionalrestrictionparser.ParseException e) { | ||
//nop | ||
} | ||
return -1; | ||
} | ||
|
||
public interface SpeedCalculator { | ||
double getSpeed(EdgeIteratorState edge, boolean reverse, long time); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters