-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for EU tunnel codes (#1786)
* Define EU tunnel codes * Add a parser for EU tunnel codes * Add a junit test for the tunnel code parser
- Loading branch information
Showing
7 changed files
with
276 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
core/src/main/java/com/graphhopper/routing/profiles/HazmatTunnel.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.graphhopper.routing.profiles; | ||
|
||
/** | ||
* Defines the degree of restriction for the transport of hazardous goods through tunnels.<br> | ||
* If not tagged it will be {@link #A} | ||
* | ||
* @see https://wiki.openstreetmap.org/wiki/Key:hazmat#Tunnel_restrictions | ||
*/ | ||
public enum HazmatTunnel { | ||
/** driving with any dangerous goods allowed */ | ||
A("A"), | ||
/** no goods with very large explosion range */ | ||
B("B"), | ||
/** no goods with large explosion or poisoning range */ | ||
C("C"), | ||
/** no goods which threaten a large explosion, poisoning or fire */ | ||
D("D"), | ||
/** forbids all dangerous goods except: UN 2919,3291, 3331, 3359, 3373 */ | ||
E("E"); | ||
|
||
public static final String KEY = "hazmat_tunnel"; | ||
|
||
private final String name; | ||
|
||
HazmatTunnel(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
core/src/main/java/com/graphhopper/routing/util/parsers/OSMHazmatTunnelParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
package com.graphhopper.routing.util.parsers; | ||
|
||
import java.util.List; | ||
|
||
import com.graphhopper.reader.ReaderWay; | ||
import com.graphhopper.routing.profiles.EncodedValue; | ||
import com.graphhopper.routing.profiles.EncodedValueLookup; | ||
import com.graphhopper.routing.profiles.EnumEncodedValue; | ||
import com.graphhopper.routing.profiles.HazmatTunnel; | ||
import com.graphhopper.routing.util.EncodingManager.Access; | ||
import com.graphhopper.storage.IntsRef; | ||
|
||
public class OSMHazmatTunnelParser implements TagParser { | ||
|
||
private static final String[] TUNNEL_CATEGORY_NAMES; | ||
static { | ||
HazmatTunnel[] categories = HazmatTunnel.values(); | ||
TUNNEL_CATEGORY_NAMES = new String[categories.length]; | ||
for (int i = 0; i < categories.length; i++) { | ||
TUNNEL_CATEGORY_NAMES[i] = categories[i].name(); | ||
} | ||
} | ||
|
||
private final EnumEncodedValue<HazmatTunnel> hazTunnelEnc; | ||
|
||
public OSMHazmatTunnelParser() { | ||
this.hazTunnelEnc = new EnumEncodedValue<>(HazmatTunnel.KEY, HazmatTunnel.class); | ||
} | ||
|
||
@Override | ||
public void createEncodedValues(EncodedValueLookup lookup, | ||
List<EncodedValue> registerNewEncodedValue) { | ||
registerNewEncodedValue.add(hazTunnelEnc); | ||
} | ||
|
||
@Override | ||
public IntsRef handleWayTags(IntsRef edgeFlags, ReaderWay readerWay, Access access, | ||
long relationFlags) { | ||
|
||
if (readerWay.hasTag("hazmat:adr_tunnel_cat", TUNNEL_CATEGORY_NAMES)) { | ||
HazmatTunnel code = HazmatTunnel.valueOf(readerWay.getTag("hazmat:adr_tunnel_cat")); | ||
hazTunnelEnc.setEnum(false, edgeFlags, code); | ||
} else if (readerWay.hasTag("hazmat:tunnel_cat", TUNNEL_CATEGORY_NAMES)) { | ||
HazmatTunnel code = HazmatTunnel.valueOf(readerWay.getTag("hazmat:tunnel_cat")); | ||
hazTunnelEnc.setEnum(false, edgeFlags, code); | ||
} else if (readerWay.hasTag("tunnel", "yes")) { | ||
HazmatTunnel[] codes = HazmatTunnel.values(); | ||
for (int i = codes.length - 1; i >= 0; i--) { | ||
if (readerWay.hasTag("hazmat:" + codes[i].name(), "no")) { | ||
hazTunnelEnc.setEnum(false, edgeFlags, codes[i]); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
return edgeFlags; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
179 changes: 179 additions & 0 deletions
179
core/src/test/java/com/graphhopper/routing/util/parsers/OSMHazmatTunnelParserTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,179 @@ | ||
package com.graphhopper.routing.util.parsers; | ||
|
||
import static com.graphhopper.routing.util.EncodingManager.Access.WAY; | ||
import static org.junit.Assert.assertEquals; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
|
||
import com.graphhopper.reader.ReaderWay; | ||
import com.graphhopper.routing.profiles.EnumEncodedValue; | ||
import com.graphhopper.routing.profiles.HazmatTunnel; | ||
import com.graphhopper.routing.util.EncodingManager; | ||
import com.graphhopper.storage.IntsRef; | ||
|
||
public class OSMHazmatTunnelParserTest { | ||
|
||
private EncodingManager em; | ||
private EnumEncodedValue<HazmatTunnel> hazTunnelEnc; | ||
private OSMHazmatTunnelParser parser; | ||
|
||
@Before | ||
public void setUp() { | ||
parser = new OSMHazmatTunnelParser(); | ||
em = new EncodingManager.Builder().add(parser).build(); | ||
hazTunnelEnc = em.getEnumEncodedValue(HazmatTunnel.KEY, HazmatTunnel.class); | ||
} | ||
|
||
@Test | ||
public void testADRTunnelCat() { | ||
IntsRef intsRef = em.createEdgeFlags(); | ||
ReaderWay readerWay = new ReaderWay(1); | ||
readerWay.setTag("hazmat:adr_tunnel_cat", "A"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.A, hazTunnelEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay = new ReaderWay(1); | ||
readerWay.setTag("hazmat:adr_tunnel_cat", "B"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.B, hazTunnelEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay = new ReaderWay(1); | ||
readerWay.setTag("hazmat:adr_tunnel_cat", "C"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.C, hazTunnelEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay = new ReaderWay(1); | ||
readerWay.setTag("hazmat:adr_tunnel_cat", "D"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.D, hazTunnelEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay = new ReaderWay(1); | ||
readerWay.setTag("hazmat:adr_tunnel_cat", "E"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.E, hazTunnelEnc.getEnum(false, intsRef)); | ||
} | ||
|
||
@Test | ||
public void testTunnelCat() { | ||
IntsRef intsRef = em.createEdgeFlags(); | ||
ReaderWay readerWay = new ReaderWay(1); | ||
readerWay.setTag("hazmat:tunnel_cat", "A"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.A, hazTunnelEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay = new ReaderWay(1); | ||
readerWay.setTag("hazmat:tunnel_cat", "B"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.B, hazTunnelEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay = new ReaderWay(1); | ||
readerWay.setTag("hazmat:tunnel_cat", "C"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.C, hazTunnelEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay = new ReaderWay(1); | ||
readerWay.setTag("hazmat:tunnel_cat", "D"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.D, hazTunnelEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay = new ReaderWay(1); | ||
readerWay.setTag("hazmat:tunnel_cat", "E"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.E, hazTunnelEnc.getEnum(false, intsRef)); | ||
} | ||
|
||
@Test | ||
public void testHazmatSubtags() { | ||
IntsRef intsRef = em.createEdgeFlags(); | ||
ReaderWay readerWay = new ReaderWay(1); | ||
readerWay.setTag("tunnel", "yes"); | ||
readerWay.setTag("hazmat:A", "no"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.A, hazTunnelEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay = new ReaderWay(1); | ||
readerWay.setTag("tunnel", "yes"); | ||
readerWay.setTag("hazmat:B", "no"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.B, hazTunnelEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay = new ReaderWay(1); | ||
readerWay.setTag("tunnel", "yes"); | ||
readerWay.setTag("hazmat:C", "no"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.C, hazTunnelEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay = new ReaderWay(1); | ||
readerWay.setTag("tunnel", "yes"); | ||
readerWay.setTag("hazmat:D", "no"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.D, hazTunnelEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay = new ReaderWay(1); | ||
readerWay.setTag("tunnel", "yes"); | ||
readerWay.setTag("hazmat:E", "no"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.E, hazTunnelEnc.getEnum(false, intsRef)); | ||
} | ||
|
||
@Test | ||
public void testOrder() { | ||
IntsRef intsRef = em.createEdgeFlags(); | ||
ReaderWay readerWay = new ReaderWay(1); | ||
readerWay.setTag("tunnel", "yes"); | ||
readerWay.setTag("hazmat:A", "no"); | ||
readerWay.setTag("hazmat:B", "no"); | ||
readerWay.setTag("hazmat:C", "no"); | ||
readerWay.setTag("hazmat:D", "no"); | ||
readerWay.setTag("hazmat:E", "no"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.E, hazTunnelEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay = new ReaderWay(1); | ||
readerWay.setTag("tunnel", "yes"); | ||
readerWay.setTag("hazmat:A", "no"); | ||
readerWay.setTag("hazmat:B", "no"); | ||
readerWay.setTag("hazmat:C", "no"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.C, hazTunnelEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay = new ReaderWay(1); | ||
readerWay.setTag("tunnel", "yes"); | ||
readerWay.setTag("hazmat:B", "no"); | ||
readerWay.setTag("hazmat:E", "no"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.E, hazTunnelEnc.getEnum(false, intsRef)); | ||
} | ||
|
||
@Test | ||
public void testIgnoreNonTunnelSubtags() { | ||
IntsRef intsRef = em.createEdgeFlags(); | ||
ReaderWay readerWay = new ReaderWay(1); | ||
readerWay.setTag("hazmat:B", "no"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.A, hazTunnelEnc.getEnum(false, intsRef)); | ||
} | ||
|
||
@Test | ||
public void testNoNPE() { | ||
ReaderWay readerWay = new ReaderWay(1); | ||
IntsRef intsRef = em.createEdgeFlags(); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatTunnel.A, hazTunnelEnc.getEnum(false, intsRef)); | ||
} | ||
} |