Repository: graphhopper/graphhopper
Pull request #1339 (2018-04-13 14:02:53)

Issue 1305 - Distance Details


Patch:
diff --git a/core/src/main/java/com/graphhopper/util/Parameters.java b/core/src/main/java/com/graphhopper/util/Parameters.java
index bf112ad42..0cbfb8f2c 100644
--- a/core/src/main/java/com/graphhopper/util/Parameters.java
+++ b/core/src/main/java/com/graphhopper/util/Parameters.java
@@ -197,6 +197,7 @@ public class Parameters {
         public static final String STREET_NAME = "street_name";
         public static final String EDGE_ID = "edge_id";
         public static final String TIME = "time";
+        public static final String DISTANCE = "distance";
     }
 
     public static final class PT {
diff --git a/core/src/main/java/com/graphhopper/util/details/DistanceDetails.java b/core/src/main/java/com/graphhopper/util/details/DistanceDetails.java
new file mode 100644
index 000000000..9de183bc6
--- /dev/null
+++ b/core/src/main/java/com/graphhopper/util/details/DistanceDetails.java
@@ -0,0 +1,31 @@
+package com.graphhopper.util.details;
+
+import com.graphhopper.routing.weighting.Weighting;
+import com.graphhopper.util.EdgeIteratorState;
+
+import static com.graphhopper.util.Parameters.DETAILS.DISTANCE;
+
+public class DistanceDetails extends AbstractPathDetailsBuilder {
+
+    private int edgeId = -1;
+    private double distance = 0;
+
+    public DistanceDetails() {
+        super(DISTANCE);
+    }
+
+    @Override
+    public boolean isEdgeDifferentToLastEdge(EdgeIteratorState edge) {
+        if (edge.getEdge() != edgeId) {
+            edgeId = edge.getEdge();
+            distance = edge.getDistance();
+            return true;
+        }
+        return false;
+    }
+
+    @Override
+    public Object getCurrentValue() {
+        return this.distance;
+    }
+}
\ No newline at end of file
diff --git a/core/src/main/java/com/graphhopper/util/details/PathDetailsBuilderFactory.java b/core/src/main/java/com/graphhopper/util/details/PathDetailsBuilderFactory.java
index 702319f92..290323e19 100644
--- a/core/src/main/java/com/graphhopper/util/details/PathDetailsBuilderFactory.java
+++ b/core/src/main/java/com/graphhopper/util/details/PathDetailsBuilderFactory.java
@@ -47,6 +47,9 @@ public class PathDetailsBuilderFactory {
         if (requestedPathDetails.contains(TIME))
             builders.add(new TimeDetails(weighting));
 
+        if (requestedPathDetails.contains(DISTANCE))
+            builders.add(new DistanceDetails());
+
         if (requestedPathDetails.size() != builders.size()) {
             throw new IllegalArgumentException("You requested the details " + requestedPathDetails + " but we could only find " + builders);
         }


Test patch:
diff --git a/core/src/test/java/com/graphhopper/routing/PathTest.java b/core/src/test/java/com/graphhopper/routing/PathTest.java
index aca2e77a5..21c485759 100644
--- a/core/src/test/java/com/graphhopper/routing/PathTest.java
+++ b/core/src/test/java/com/graphhopper/routing/PathTest.java
@@ -360,7 +360,7 @@ public class PathTest {
         assertEquals(4, timeDetails.size());
         assertEquals(400L, timeDetails.get(0).getValue());
         assertEquals(200L, timeDetails.get(1).getValue());
-        assertEquals(1800L, timeDetails.get(2).getValue());
+        assertEquals(3600L, timeDetails.get(2).getValue());
         assertEquals(400L, timeDetails.get(3).getValue());
 
         assertEquals(0, timeDetails.get(0).getFirst());
@@ -370,6 +370,21 @@ public class PathTest {
         assertEquals(4, timeDetails.get(3).getLast());
     }
 
+    @Test
+    public void testCalcDistanceDetails() {
+        Path p = new Dijkstra(pathDetailGraph, new ShortestWeighting(encoder), TraversalMode.NODE_BASED).calcPath(1, 5);
+        assertTrue(p.isFound());
+
+        Map<String, List<PathDetail>> details = p.calcDetails(Arrays.asList(new String[]{DISTANCE}), new PathDetailsBuilderFactory(), 0);
+        assertTrue(details.size() == 1);
+
+        List<PathDetail> distanceDetails = details.get(DISTANCE);
+        assertEquals(5D, distanceDetails.get(0).getValue());
+        assertEquals(5D, distanceDetails.get(1).getValue());
+        assertEquals(10D, distanceDetails.get(2).getValue());
+        assertEquals(5D, distanceDetails.get(3).getValue());
+    }
+
     /**
      * case with one edge being not an exit
      */
@@ -868,7 +883,7 @@ public class PathTest {
         tmpEdge.setFlags(carManager.handleWayTags(w, carManager.acceptWay(w), 0));
 
         w.setTag("maxspeed", "10");
-        tmpEdge = g.edge(3, 4, 5, true).setName("3-4");
+        tmpEdge = g.edge(3, 4, 10, true).setName("3-4");
         tmpEdge.setFlags(carManager.handleWayTags(w, carManager.acceptWay(w), 0));
 
         return g;