Repository: graphhopper/graphhopper
Pull request #796 (2016-09-16 15:50:02)

Issue #713 part 1: store tunnel and bridge tags.
This PR adds a few utility methods to check if the edge is a tunnel or a bridge edge.

karussell: Thanks! Also for moving this to the new encoder!

For performance reasons it is very wise that you put the int values into a separate field :). Now I fear we'll produce a big cluttered class at the end. But we'll see if those method are necessary (or if we move this to the weighting instead) and fix later.

Patch:
diff --git a/core/src/main/java/com/graphhopper/routing/util/DataFlagEncoder.java b/core/src/main/java/com/graphhopper/routing/util/DataFlagEncoder.java
index b01fdd8d6..59b8d00c0 100644
--- a/core/src/main/java/com/graphhopper/routing/util/DataFlagEncoder.java
+++ b/core/src/main/java/com/graphhopper/routing/util/DataFlagEncoder.java
@@ -63,6 +63,8 @@ public class DataFlagEncoder extends AbstractFlagEncoder {
     private final Map<String, Integer> highwayMap = new HashMap<>();
     private final List<String> transportModeList = new ArrayList<>();
     private final Map<String, Integer> transportModeMap = new HashMap<>();
+    private final int transportModeTunnelValue;
+    private final int transportModeBridgeValue;
     private long bit0;
     private EncodedDoubleValue carFwdMaxspeedEncoder;
     private EncodedDoubleValue carBwdMaxspeedEncoder;
@@ -101,6 +103,8 @@ public class DataFlagEncoder extends AbstractFlagEncoder {
         for (String tm : transportModeList) {
             transportModeMap.put(tm, counter++);
         }
+        transportModeTunnelValue = transportModeMap.get("tunnel");
+        transportModeBridgeValue = transportModeMap.get("bridge");
 
         List<String> surfaceList = Arrays.asList("_default", "asphalt", "unpaved", "paved", "gravel",
                 "ground", "dirt", "grass", "concrete", "paving_stones", "sand", "compacted", "cobblestone", "mud", "ice");
@@ -315,6 +319,14 @@ public class DataFlagEncoder extends AbstractFlagEncoder {
     public int getTransportMode(EdgeIteratorState edge) {
         return (int) transportModeEncoder.getValue(edge.getFlags());
     }
+    
+    public boolean isTransportModeTunnel(EdgeIteratorState edge) {
+    	return transportModeEncoder.getValue(edge.getFlags()) == this.transportModeTunnelValue;
+    }
+
+    public boolean isTransportModeBridge(EdgeIteratorState edge) {
+    	return transportModeEncoder.getValue(edge.getFlags()) == this.transportModeBridgeValue;
+    }
 
     public String getTransportModeAsString(EdgeIteratorState edge) {
         int val = getTransportMode(edge);


Test patch:
diff --git a/core/src/test/java/com/graphhopper/routing/util/DataFlagEncoderTest.java b/core/src/test/java/com/graphhopper/routing/util/DataFlagEncoderTest.java
index 2a8945b98..a80b25a47 100644
--- a/core/src/test/java/com/graphhopper/routing/util/DataFlagEncoderTest.java
+++ b/core/src/test/java/com/graphhopper/routing/util/DataFlagEncoderTest.java
@@ -56,6 +56,52 @@ public class DataFlagEncoderTest {
         edge = GHUtility.createMockedEdgeIteratorState(0, flags);
         assertEquals("_default", encoder.getHighwayAsString(edge));
     }
+    
+    @Test
+    public void testTunnel() {
+        ReaderWay osmWay = new ReaderWay(0);
+        osmWay.setTag("highway", "primary");
+        osmWay.setTag("tunnel", "yes");
+        long flags = encoder.handleWayTags(osmWay, 1, 0);
+        EdgeIteratorState edge = GHUtility.createMockedEdgeIteratorState(0, flags);
+        assertEquals("primary", encoder.getHighwayAsString(edge));
+        assertEquals("tunnel", encoder.getTransportModeAsString(edge));
+        assertTrue(encoder.isTransportModeTunnel(edge));
+        assertFalse(encoder.isTransportModeBridge(edge));
+
+        osmWay = new ReaderWay(0);
+        osmWay.setTag("highway", "primary");
+        osmWay.setTag("tunnel", "yes");
+        osmWay.setTag("bridge", "yes");
+        flags = encoder.handleWayTags(osmWay, 1, 0);
+        edge = GHUtility.createMockedEdgeIteratorState(0, flags);
+        assertEquals("bridge", encoder.getTransportModeAsString(edge));
+        assertFalse(encoder.isTransportModeTunnel(edge));
+        assertTrue(encoder.isTransportModeBridge(edge));
+    }
+    
+    @Test
+    public void testBridge() {
+        ReaderWay osmWay = new ReaderWay(0);
+        osmWay.setTag("highway", "primary");
+        osmWay.setTag("bridge", "yes");
+        long flags = encoder.handleWayTags(osmWay, 1, 0);
+        EdgeIteratorState edge = GHUtility.createMockedEdgeIteratorState(0, flags);
+        assertEquals("primary", encoder.getHighwayAsString(edge));
+        assertEquals("bridge", encoder.getTransportModeAsString(edge));
+        assertFalse(encoder.isTransportModeTunnel(edge));
+        assertTrue(encoder.isTransportModeBridge(edge));
+
+        osmWay = new ReaderWay(0);
+        osmWay.setTag("highway", "primary");
+        osmWay.setTag("bridge", "yes");
+        osmWay.setTag("tunnel", "yes");
+        flags = encoder.handleWayTags(osmWay, 1, 0);
+        edge = GHUtility.createMockedEdgeIteratorState(0, flags);
+        assertEquals("bridge", encoder.getTransportModeAsString(edge));
+        assertFalse(encoder.isTransportModeTunnel(edge));
+        assertTrue(encoder.isTransportModeBridge(edge));
+    }    
 
     @Test
     public void testHighwaySpeed() {