From 3ee2984e5243d85677fa03de4dd17cd5f72b3fd7 Mon Sep 17 00:00:00 2001 From: Holt59 Date: Sun, 4 Mar 2018 13:17:20 +0100 Subject: [PATCH] Start modification restrictions. --- .../org/insa/graph/AccessRestrictions.java | 51 ++++++++++++ src/main/org/insa/graph/RoadInformation.java | 83 ++----------------- .../graph/io/BinaryGraphReaderInsa2018.java | 59 ++++++++----- 3 files changed, 92 insertions(+), 101 deletions(-) create mode 100644 src/main/org/insa/graph/AccessRestrictions.java diff --git a/src/main/org/insa/graph/AccessRestrictions.java b/src/main/org/insa/graph/AccessRestrictions.java new file mode 100644 index 0000000..89c1f29 --- /dev/null +++ b/src/main/org/insa/graph/AccessRestrictions.java @@ -0,0 +1,51 @@ +package org.insa.graph; + +import java.util.EnumMap; +import java.util.EnumSet; + +public class AccessRestrictions { + + public enum AccessMode { + + // Specific modes + FOOT, BICYCLE, SMALL_MOTORCYCLE, AGRICULTURAL, MOTORCYCLE, MOTORCAR, HEAVY_GOODS, PUBLIC_TRANSPORT; + + // All available modes + public static final EnumSet ALL = EnumSet.allOf(AccessMode.class); + + // Vehicle + public static final EnumSet VEHICLE = EnumSet.range(AccessMode.BICYCLE, + AccessMode.PUBLIC_TRANSPORT); + + // Motor vehicle + public static final EnumSet MOTOR_VEHICLE = EnumSet.range(AccessMode.SMALL_MOTORCYCLE, + AccessMode.PUBLIC_TRANSPORT); + } + + public enum AccessRestriction { + ALLOWED, FORBIDDEN, PRIVATE, DESTINATION, FORESTRY, UNKNOWN + } + + // Map mode -> restriction + private final EnumMap restrictions; + + /** + * Create new access restrictions with unknown restrictions. + */ + public AccessRestrictions() { + this.restrictions = new EnumMap<>(AccessMode.class); + for (AccessMode mode: AccessMode.values()) { + this.restrictions.put(mode, AccessRestriction.UNKNOWN); + } + } + + /** + * Create a new instance of access restrictions with the given restrictions. + * + * @param restrictions + */ + public AccessRestrictions(EnumMap restrictions) { + this.restrictions = restrictions; + } + +} diff --git a/src/main/org/insa/graph/RoadInformation.java b/src/main/org/insa/graph/RoadInformation.java index a90cc83..1764ed6 100644 --- a/src/main/org/insa/graph/RoadInformation.java +++ b/src/main/org/insa/graph/RoadInformation.java @@ -1,83 +1,11 @@ package org.insa.graph; -import java.util.Map; - /** * Class containing information for road that may be shared by multiple arcs. * */ public class RoadInformation { - /** - * Access mode. - */ - public enum AccessMode { - FOOT, BICYCLE, SMALL_MOTORCYCLE, MOTORCYCLE, MOTORCAR, BUS - } - - /** - * Class containing access restriction information. - * - */ - public static class AccessRestriction { - - // Private road - private boolean is_private = false; - - // Public transport restricted. - private boolean is_publicTransport = false; - - // Map Enum -> Allowed. - private Map allowedModes; - - /** - * Construct a new AccessInformation with unknown information: Not private, not - * public transport and all modes are allowed. - */ - public AccessRestriction() { - - } - - /** - * @param isPrivate - * @param isPublicTransport - * @param allowedModes - */ - public AccessRestriction(boolean isPrivate, boolean isPublicTransport, - Map allowedModes) { - this.is_private = isPrivate; - this.is_publicTransport = isPublicTransport; - this.allowedModes = allowedModes; - } - - /** - * @return true if this is a private road. - */ - public boolean isPrivate() { - return is_private; - } - - /** - * @return true if this road is reserved for public transport. - */ - public boolean isPublicTransport() { - return is_publicTransport; - } - - /** - * @param mode - * - * @return true if this road is allowed for the specified mode. - */ - public boolean isAllowedFor(AccessMode mode) { - if (this.allowedModes == null) { - return true; - } - return this.allowedModes.getOrDefault(mode, false); - } - - } - /** * Road type. */ @@ -104,7 +32,7 @@ public class RoadInformation { private final RoadType type; // Access information - private final AccessRestriction access; + private final AccessRestrictions access; // One way road? private final boolean oneway; @@ -115,8 +43,7 @@ public class RoadInformation { // Name of the road. private final String name; - public RoadInformation(RoadType roadType, AccessRestriction access, boolean isOneWay, - int maxSpeed, String name) { + public RoadInformation(RoadType roadType, AccessRestrictions access, boolean isOneWay, int maxSpeed, String name) { this.type = roadType; this.access = access; this.oneway = isOneWay; @@ -129,7 +56,7 @@ public class RoadInformation { /** * @return true if this is a private road. */ - public AccessRestriction getAccessRestrictions() { + public AccessRestrictions getAccessRestrictions() { return this.access; } @@ -163,6 +90,7 @@ public class RoadInformation { /* * (non-Javadoc) + * * @see java.lang.Object#toString() */ @Override @@ -174,8 +102,7 @@ public class RoadInformation { if (getType() == RoadType.MOTORWAY) { typeAsString = "highway"; } - return typeAsString + " : " + getName() + " " + (isOneWay() ? " (oneway) " : "") + maxSpeed - + " km/h (max.)"; + return typeAsString + " : " + getName() + " " + (isOneWay() ? " (oneway) " : "") + maxSpeed + " km/h (max.)"; } } diff --git a/src/main/org/insa/graph/io/BinaryGraphReaderInsa2018.java b/src/main/org/insa/graph/io/BinaryGraphReaderInsa2018.java index a107d80..1d38017 100644 --- a/src/main/org/insa/graph/io/BinaryGraphReaderInsa2018.java +++ b/src/main/org/insa/graph/io/BinaryGraphReaderInsa2018.java @@ -7,14 +7,14 @@ import java.util.Collections; import java.util.EnumMap; import java.util.Map; +import org.insa.graph.AccessRestrictions; +import org.insa.graph.AccessRestrictions.AccessRestriction; import org.insa.graph.Arc; import org.insa.graph.Graph; import org.insa.graph.GraphInformation; import org.insa.graph.Node; import org.insa.graph.Point; import org.insa.graph.RoadInformation; -import org.insa.graph.RoadInformation.AccessMode; -import org.insa.graph.RoadInformation.AccessRestriction; import org.insa.graph.RoadInformation.RoadType; public class BinaryGraphReaderInsa2018 extends BinaryReader implements GraphReader { @@ -26,29 +26,39 @@ public class BinaryGraphReaderInsa2018 extends BinaryReader implements GraphRead // Length of the map id field (in bytes) protected static final int MAP_ID_FIELD_LENGTH = 32; - // Some masks... - private static final int MASK_UNKNOWN = 0x01; - private static final int MASK_PRIVATE = 0x02; - @SuppressWarnings("unused") - private static final int MASK_AGRICULTURAL = 0x04; - @SuppressWarnings("unused") - private static final int MASK_SERVICE = 0x08; - private static final int MASK_PUBLIC_TRANSPORT = 0x10; - - private static final int MASK_FOOT = 0x01 << 8; - private static final int MASK_BICYCLE = 0x02 << 8; - private static final int MASK_MOTORCYCLE = 0x0C << 8; - private static final int MASK_SMALL_MOTORCYCLE = 0x08 << 8; - private static final int MASK_MOTORCAR = 0x10 << 8; - private static final int MASK_BUS = 0x20 << 8; - /** - * Create a new access information by parsing the given value. + * Create a new access information by parsing the given value (V6 version). * * @param access * @return */ - protected static AccessRestriction toAccessInformation(int access) { + protected static AccessRestrictions toAccessInformationV7(long access) { + + } + + /** + * Create a new access information by parsing the given value (V6 version). + * + * @param access + * @return + */ + protected static AccessRestrictions toAccessInformationV6(int access) { + + // Some masks... + final int MASK_UNKNOWN = 0x01; + final int MASK_PRIVATE = 0x02; + @SuppressWarnings("unused") + final int MASK_AGRICULTURAL = 0x04; + @SuppressWarnings("unused") + final int MASK_SERVICE = 0x08; + final int MASK_PUBLIC_TRANSPORT = 0x10; + + final int MASK_FOOT = 0x01 << 8; + final int MASK_BICYCLE = 0x02 << 8; + final int MASK_MOTORCYCLE = 0x0C << 8; + final int MASK_SMALL_MOTORCYCLE = 0x08 << 8; + final int MASK_MOTORCAR = 0x10 << 8; + final int MASK_BUS = 0x20 << 8; // Unknown -> Return default access information. if ((access & MASK_UNKNOWN) != 0) { @@ -259,9 +269,12 @@ public class BinaryGraphReaderInsa2018 extends BinaryReader implements GraphRead private RoadInformation readRoadInformation() throws IOException { char type = (char) dis.readUnsignedByte(); int x = dis.readUnsignedByte(); - AccessRestriction access = new AccessRestriction(); - if (getCurrentVersion() >= 6) { - access = toAccessInformation(dis.readUnsignedShort()); + AccessRestrictions access = new AccessRestrictions(); + if (getCurrentVersion() >= 7) { + access = toAccessInformationV7(dis.readLong()); + } + else if (getCurrentVersion() >= 6) { + access = toAccessInformationV6(dis.readUnsignedShort()); } return new RoadInformation(toRoadType(type), access, (x & 0x80) > 0, (x & 0x7F) * 5, dis.readUTF()); }