Skip to content

Commit

Permalink
make changing precision possible for DecimalDetails and AverageSpeedD…
Browse files Browse the repository at this point in the history
…etails for #1798
  • Loading branch information
karussell committed Nov 23, 2019
1 parent ef8b191 commit ce97613
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,30 +9,39 @@
public class AverageSpeedDetails extends AbstractPathDetailsBuilder {

private final Weighting weighting;
private final double precision;
private double decimalValue = -1;
// will include the turn time penalty
private int prevEdgeId = -1;

public AverageSpeedDetails(Weighting weighting) {
this(weighting, 0.1);
}

/**
* @param precision e.g. 0.1 to avoid creating too many path details, i.e. round the speed to the specified precision
* before detecting a change.
*/
public AverageSpeedDetails(Weighting weighting, double precision) {
super(AVERAGE_SPEED);
this.weighting = weighting;
this.precision = precision;
}

@Override
protected Object getCurrentValue() {
if (Double.isInfinite(decimalValue))
throw new IllegalStateException("average_speed must not be infinite");

return decimalValue;
}

@Override
public boolean isEdgeDifferentToLastEdge(EdgeIteratorState edge) {
double tmpVal = edge.getDistance() / weighting.calcMillis(edge, false, prevEdgeId) * 3600;
if (Double.isInfinite(tmpVal))
throw new IllegalStateException("average_speed was infinite for " + edge.fetchWayGeometry(3));

prevEdgeId = edge.getEdge();
// avoid creating too many path details => round the speed to 0.01 precision and include it only if:
if (Math.abs(tmpVal - decimalValue) >= 0.1) {
this.decimalValue = Helper.round2(tmpVal);
if (Math.abs(tmpVal - decimalValue) >= precision) {
this.decimalValue = Math.round(tmpVal / precision) * precision;
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,23 @@ public class DecimalDetails extends AbstractPathDetailsBuilder {
private final DecimalEncodedValue ev;
private double decimalValue = -1;
private final String infinityJsonValue;
private final double precision;

public DecimalDetails(String name, DecimalEncodedValue ev) {
this(name, ev, null);
this(name, ev, null, 0.001);
}

/**
* DecimalEncodedValue can return infinity as default value, but JSON cannot include this
* https://stackoverflow.com/a/9218955/194609
* @param infinityJsonValue DecimalEncodedValue can return infinity as default value, but JSON cannot include this
* https://stackoverflow.com/a/9218955/194609 so we need a special string to handle this or null.
* @param precision e.g. 0.1 to avoid creating too many path details, i.e. round the speed to the specified precision
* * before detecting a change.
*/
public DecimalDetails(String name, DecimalEncodedValue ev, String infinityJsonValue) {
public DecimalDetails(String name, DecimalEncodedValue ev, String infinityJsonValue, double precision) {
super(name);
this.ev = ev;
this.infinityJsonValue = infinityJsonValue;
this.precision = precision;
}

@Override
Expand All @@ -51,8 +55,8 @@ protected Object getCurrentValue() {
@Override
public boolean isEdgeDifferentToLastEdge(EdgeIteratorState edge) {
double tmpVal = edge.get(ev);
if (Math.abs(tmpVal - decimalValue) > 0.0001) {
this.decimalValue = tmpVal;
if (Math.abs(tmpVal - decimalValue) >= precision) {
this.decimalValue = Double.isInfinite(tmpVal) ? tmpVal : Math.round(tmpVal / precision) * precision;
return true;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,10 @@ public void testPathDetails() {
assertTrue(pathDetails.containsKey("edge_id"));
assertTrue(pathDetails.containsKey("time"));
List<PathDetail> averageSpeedList = pathDetails.get("average_speed");
assertEquals(13, averageSpeedList.size());
assertEquals(30.00, averageSpeedList.get(0).getValue());
assertEquals(14, averageSpeedList.size());
assertEquals(30.0, averageSpeedList.get(0).getValue());
assertEquals(14, averageSpeedList.get(0).getLength());
assertEquals(60.06, averageSpeedList.get(1).getValue());
assertEquals(60.1, averageSpeedList.get(1).getValue());
assertEquals(5, averageSpeedList.get(1).getLength());

List<PathDetail> edgeIdDetails = pathDetails.get("edge_id");
Expand Down Expand Up @@ -277,9 +277,9 @@ public void testPathDetailsWithoutGraphHopperWeb() {
JsonNode details = path.get("details");
assertTrue(details.has("average_speed"));
JsonNode averageSpeed = details.get("average_speed");
assertEquals(30.00, averageSpeed.get(0).get(2).asDouble(), .01);
assertEquals(14, averageSpeed.get(0).get(1).asInt());
assertEquals(60.06, averageSpeed.get(1).get(2).asDouble(), .01);
assertEquals(30.0, averageSpeed.get(0).get(2).asDouble(), .1);
assertEquals(14, averageSpeed.get(0).get(1).asInt(), .1);
assertEquals(60.1, averageSpeed.get(1).get(2).asDouble(), .1);
assertEquals(19, averageSpeed.get(1).get(1).asInt());
assertTrue(details.has("edge_id"));
JsonNode edgeIds = details.get("edge_id");
Expand Down

0 comments on commit ce97613

Please sign in to comment.