Skip to content

Commit

Permalink
Merge pull request #31 from GIScience/Interface_speedcalculator
Browse files Browse the repository at this point in the history
Make SpeedCalculator an interface and ConditionalSpeedCalculator an i…
  • Loading branch information
takb authored Oct 9, 2020
2 parents f7c2d46 + 99f871a commit 1a65759
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 76 deletions.
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;
}

}
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);
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public TimeDependentFastestWeighting(FlagEncoder encoder, PMap map, GraphHopperS
headingPenalty = map.getDouble(Routing.HEADING_PENALTY, Routing.DEFAULT_HEADING_PENALTY);
maxSpeed = encoder.getMaxSpeed() / SPEED_CONV;

this.speedCalculator = new SpeedCalculator(graph, encoder);
this.speedCalculator = new ConditionalSpeedCalculator(graph, encoder);
}

@Override
Expand Down

0 comments on commit 1a65759

Please sign in to comment.