Repository: graphhopper/graphhopper
Pull request #1541 (2019-02-11 16:02:13)

Cannot set a blocked edge to accessible again
This PR highlights the issue described in #1540.


Patch:
diff --git a/core/src/main/java/com/graphhopper/storage/change/ChangeGraphHelper.java b/core/src/main/java/com/graphhopper/storage/change/ChangeGraphHelper.java
index d1a1c2cb2..248064119 100644
--- a/core/src/main/java/com/graphhopper/storage/change/ChangeGraphHelper.java
+++ b/core/src/main/java/com/graphhopper/storage/change/ChangeGraphHelper.java
@@ -93,7 +93,7 @@ public class ChangeGraphHelper {
         BooleanEncodedValue accessEnc = encoder.getAccessEnc();
         DecimalEncodedValue avSpeedEnc = encoder.getAverageSpeedEnc();
         long updates = 0;
-        EdgeFilter filter = DefaultEdgeFilter.allEdges(encoder);
+        EdgeFilter filter = EdgeFilter.ALL_EDGES;
         GHIntHashSet edges = new GHIntHashSet();
         if (jsonFeature.hasGeometry()) {
             graphBrowser.fillEdgeIDs(edges, jsonFeature.getGeometry(), filter);


Test patch:
diff --git a/core/src/test/java/com/graphhopper/storage/change/ChangeGraphHelperTest.java b/core/src/test/java/com/graphhopper/storage/change/ChangeGraphHelperTest.java
index 35926b18c..3feea2b09 100644
--- a/core/src/test/java/com/graphhopper/storage/change/ChangeGraphHelperTest.java
+++ b/core/src/test/java/com/graphhopper/storage/change/ChangeGraphHelperTest.java
@@ -3,11 +3,15 @@ package com.graphhopper.storage.change;
 import com.graphhopper.jackson.Jackson;
 import com.graphhopper.json.geo.JsonFeatureCollection;
 import com.graphhopper.routing.AbstractRoutingAlgorithmTester;
+import com.graphhopper.routing.Dijkstra;
+import com.graphhopper.routing.Path;
 import com.graphhopper.routing.profiles.BooleanEncodedValue;
 import com.graphhopper.routing.profiles.DecimalEncodedValue;
 import com.graphhopper.routing.util.AllEdgesIterator;
 import com.graphhopper.routing.util.EncodingManager;
 import com.graphhopper.routing.util.FlagEncoder;
+import com.graphhopper.routing.util.TraversalMode;
+import com.graphhopper.routing.weighting.ShortestWeighting;
 import com.graphhopper.storage.GraphBuilder;
 import com.graphhopper.storage.GraphHopperStorage;
 import com.graphhopper.storage.RAMDirectory;
@@ -76,4 +80,56 @@ public class ChangeGraphHelperTest {
         assertTrue(newSpeed < defaultSpeed);
         assertFalse(GHUtility.getEdge(graph, 3, 4).get(accessEnc));
     }
+
+    @Test
+    public void testRevertChanges() throws IOException {
+        // 0-1-2
+        // | |
+        // 3-4
+        graph.edge(0, 1, 1, true);
+        graph.edge(1, 2, 1, true);
+        graph.edge(3, 4, 1, true);
+        graph.edge(0, 3, 1, true);
+        graph.edge(1, 4, 1, true);
+        AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 0, 0.01, 0.00);
+        AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 1, 0.01, 0.01);
+        AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 2, 0.01, 0.02);
+        AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 3, 0.00, 0.00);
+        AbstractRoutingAlgorithmTester.updateDistancesFor(graph, 4, 0.00, 0.01);
+        LocationIndex locationIndex = new LocationIndexTree(graph, new RAMDirectory()).prepareIndex();
+
+        FlagEncoder encoder = encodingManager.getEncoder("car");
+        Path p = new Dijkstra(graph, new ShortestWeighting(encoder), TraversalMode.NODE_BASED)
+                .calcPath(0, 2);
+        assertTrue(p.isFound());
+
+        double distanceBeforeBlock = p.getDistance();
+        assertEquals(2223, distanceBeforeBlock, 1);
+
+        // Block the edge 0-1
+        Reader reader = new InputStreamReader(getClass().getResourceAsStream("overlaydata2.json"), Helper.UTF_CS);
+        ChangeGraphHelper instance = new ChangeGraphHelper(graph, locationIndex);
+        JsonFeatureCollection collection = Jackson.newObjectMapper().readValue(reader, JsonFeatureCollection.class);
+        long updates = instance.applyChanges(encodingManager, collection.getFeatures());
+        assertEquals(1, updates);
+
+        p = new Dijkstra(graph, new ShortestWeighting(encoder), TraversalMode.NODE_BASED)
+                .calcPath(0, 2);
+        assertTrue(p.isFound());
+
+        double distanceAfterBlock = p.getDistance();
+        assertEquals(4447, distanceAfterBlock, 1);
+
+        // Unblock the edge 0-1
+        reader = new InputStreamReader(getClass().getResourceAsStream("overlaydata3.json"), Helper.UTF_CS);
+        collection = Jackson.newObjectMapper().readValue(reader, JsonFeatureCollection.class);
+        updates = instance.applyChanges(encodingManager, collection.getFeatures());
+        assertEquals(1, updates);
+
+        p = new Dijkstra(graph, new ShortestWeighting(encoder), TraversalMode.NODE_BASED)
+                .calcPath(0, 2);
+        assertTrue(p.isFound());
+
+        assertEquals(distanceBeforeBlock, p.getDistance(), 1);
+    }
 }
diff --git a/core/src/test/resources/com/graphhopper/storage/change/overlaydata2.json b/core/src/test/resources/com/graphhopper/storage/change/overlaydata2.json
new file mode 100644
index 000000000..72253eba6
--- /dev/null
+++ b/core/src/test/resources/com/graphhopper/storage/change/overlaydata2.json
@@ -0,0 +1,14 @@
+{
+    "type": "FeatureCollection",
+    "features": [{
+        "type": "Feature",
+        "geometry": {
+            "type": "Point",
+            "coordinates": [0.005, 0.01]
+        },
+        "properties": {
+            "vehicles": ["car"],
+            "access": false
+        }
+    }]
+}
\ No newline at end of file
diff --git a/core/src/test/resources/com/graphhopper/storage/change/overlaydata3.json b/core/src/test/resources/com/graphhopper/storage/change/overlaydata3.json
new file mode 100644
index 000000000..b1d42babe
--- /dev/null
+++ b/core/src/test/resources/com/graphhopper/storage/change/overlaydata3.json
@@ -0,0 +1,14 @@
+{
+    "type": "FeatureCollection",
+    "features": [{
+        "type": "Feature",
+        "geometry": {
+            "type": "Point",
+            "coordinates": [0.005, 0.01]
+        },
+        "properties": {
+            "vehicles": ["car"],
+            "access": true
+        }
+    }]
+}
\ No newline at end of file