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 various hazmat tags (#1711)
* Add separate hazmat categories * Add support for OSMs `hazmat` tag * Add support for OSMs `hazmat:water` tag
- Loading branch information
Showing
10 changed files
with
238 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
22 changes: 22 additions & 0 deletions
22
core/src/main/java/com/graphhopper/routing/profiles/Hazmat.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,22 @@ | ||
package com.graphhopper.routing.profiles; | ||
|
||
/** | ||
* Defines general restrictions for the transport of hazardous materials.<br> | ||
* If not tagged it will be {@link #YES} | ||
*/ | ||
public enum Hazmat { | ||
YES("yes"), NO("no"); | ||
|
||
public static final String KEY = "hazmat"; | ||
|
||
private final String name; | ||
|
||
Hazmat(String name) { | ||
this.name = name; | ||
} | ||
|
||
@Override | ||
public String toString() { | ||
return name; | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
core/src/main/java/com/graphhopper/routing/profiles/HazmatWater.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,22 @@ | ||
package com.graphhopper.routing.profiles; | ||
|
||
/** | ||
* Defines general restrictions for the transport of goods through water protection areas.<br> | ||
* If not tagged it will be {@link #YES} | ||
*/ | ||
public enum HazmatWater { | ||
YES("yes"), PERMISSIVE("permissive"), NO("no"); | ||
|
||
public static final String KEY = "hazmat_water"; | ||
|
||
private final String name; | ||
|
||
HazmatWater(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
36 changes: 36 additions & 0 deletions
36
core/src/main/java/com/graphhopper/routing/util/parsers/OSMHazmatParser.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,36 @@ | ||
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.Hazmat; | ||
import com.graphhopper.routing.util.EncodingManager.Access; | ||
import com.graphhopper.storage.IntsRef; | ||
|
||
public class OSMHazmatParser implements TagParser { | ||
|
||
private final EnumEncodedValue<Hazmat> hazEnc; | ||
|
||
public OSMHazmatParser() { | ||
this.hazEnc = new EnumEncodedValue<>(Hazmat.KEY, Hazmat.class); | ||
} | ||
|
||
@Override | ||
public void createEncodedValues(EncodedValueLookup lookup, | ||
List<EncodedValue> registerNewEncodedValue) { | ||
registerNewEncodedValue.add(hazEnc); | ||
} | ||
|
||
@Override | ||
public IntsRef handleWayTags(IntsRef edgeFlags, ReaderWay readerWay, Access access, | ||
long relationFlags) { | ||
if (readerWay.hasTag("hazmat", "no")) { | ||
hazEnc.setEnum(false, edgeFlags, Hazmat.NO); | ||
} | ||
return edgeFlags; | ||
} | ||
|
||
} |
38 changes: 38 additions & 0 deletions
38
core/src/main/java/com/graphhopper/routing/util/parsers/OSMHazmatWaterParser.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,38 @@ | ||
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.HazmatWater; | ||
import com.graphhopper.routing.util.EncodingManager.Access; | ||
import com.graphhopper.storage.IntsRef; | ||
|
||
public class OSMHazmatWaterParser implements TagParser { | ||
|
||
private final EnumEncodedValue<HazmatWater> hazWaterEnc; | ||
|
||
public OSMHazmatWaterParser() { | ||
this.hazWaterEnc = new EnumEncodedValue<>(HazmatWater.KEY, HazmatWater.class); | ||
} | ||
|
||
@Override | ||
public void createEncodedValues(EncodedValueLookup lookup, | ||
List<EncodedValue> registerNewEncodedValue) { | ||
registerNewEncodedValue.add(hazWaterEnc); | ||
} | ||
|
||
@Override | ||
public IntsRef handleWayTags(IntsRef edgeFlags, ReaderWay readerWay, Access access, | ||
long relationFlags) { | ||
if (readerWay.hasTag("hazmat:water", "no")) { | ||
hazWaterEnc.setEnum(false, edgeFlags, HazmatWater.NO); | ||
} else if (readerWay.hasTag("hazmat:water", "permissive")) { | ||
hazWaterEnc.setEnum(false, edgeFlags, HazmatWater.PERMISSIVE); | ||
} | ||
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
54 changes: 54 additions & 0 deletions
54
core/src/test/java/com/graphhopper/routing/util/parsers/OSMHazmatParserTest.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,54 @@ | ||
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.Hazmat; | ||
import com.graphhopper.routing.util.EncodingManager; | ||
import com.graphhopper.storage.IntsRef; | ||
|
||
public class OSMHazmatParserTest { | ||
|
||
private EncodingManager em; | ||
private EnumEncodedValue<Hazmat> hazEnc; | ||
private OSMHazmatParser parser; | ||
|
||
@Before | ||
public void setUp() { | ||
parser = new OSMHazmatParser(); | ||
em = new EncodingManager.Builder().add(parser).build(); | ||
hazEnc = em.getEnumEncodedValue(Hazmat.KEY, Hazmat.class); | ||
} | ||
|
||
@Test | ||
public void testSimpleTags() { | ||
ReaderWay readerWay = new ReaderWay(1); | ||
IntsRef intsRef = em.createEdgeFlags(); | ||
readerWay.setTag("hazmat", "no"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(Hazmat.NO, hazEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay.setTag("hazmat", "yes"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(Hazmat.YES, hazEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay.setTag("hazmat", "designated"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(Hazmat.YES, hazEnc.getEnum(false, intsRef)); | ||
} | ||
|
||
@Test | ||
public void testNoNPE() { | ||
ReaderWay readerWay = new ReaderWay(1); | ||
IntsRef intsRef = em.createEdgeFlags(); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(Hazmat.YES, hazEnc.getEnum(false, intsRef)); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
core/src/test/java/com/graphhopper/routing/util/parsers/OSMHazmatWaterParserTest.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,54 @@ | ||
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.HazmatWater; | ||
import com.graphhopper.routing.util.EncodingManager; | ||
import com.graphhopper.storage.IntsRef; | ||
|
||
public class OSMHazmatWaterParserTest { | ||
|
||
private EncodingManager em; | ||
private EnumEncodedValue<HazmatWater> hazWaterEnc; | ||
private OSMHazmatWaterParser parser; | ||
|
||
@Before | ||
public void setUp() { | ||
parser = new OSMHazmatWaterParser(); | ||
em = new EncodingManager.Builder().add(parser).build(); | ||
hazWaterEnc = em.getEnumEncodedValue(HazmatWater.KEY, HazmatWater.class); | ||
} | ||
|
||
@Test | ||
public void testSimpleTags() { | ||
ReaderWay readerWay = new ReaderWay(1); | ||
IntsRef intsRef = em.createEdgeFlags(); | ||
readerWay.setTag("hazmat:water", "no"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatWater.NO, hazWaterEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay.setTag("hazmat:water", "yes"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatWater.YES, hazWaterEnc.getEnum(false, intsRef)); | ||
|
||
intsRef = em.createEdgeFlags(); | ||
readerWay.setTag("hazmat:water", "permissive"); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatWater.PERMISSIVE, hazWaterEnc.getEnum(false, intsRef)); | ||
} | ||
|
||
@Test | ||
public void testNoNPE() { | ||
ReaderWay readerWay = new ReaderWay(1); | ||
IntsRef intsRef = em.createEdgeFlags(); | ||
parser.handleWayTags(intsRef, readerWay, WAY, 0); | ||
assertEquals(HazmatWater.YES, hazWaterEnc.getEnum(false, intsRef)); | ||
} | ||
} |