Clean access restriction usage.

This commit is contained in:
Mikael Capelle 2018-03-01 14:20:42 +01:00
parent a51e16c3dd
commit 25da6079e6
4 changed files with 131 additions and 76 deletions

View File

@ -1,11 +1,83 @@
package org.insa.graph; package org.insa.graph;
import java.util.Map;
/** /**
* Class containing information for road that may be shared by multiple arcs. * Class containing information for road that may be shared by multiple arcs.
* *
*/ */
public class RoadInformation { 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<AccessMode, Boolean> 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<AccessMode, Boolean> 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. * Road type.
*/ */
@ -28,35 +100,11 @@ public class RoadInformation {
COASTLINE COASTLINE
} }
/**
* Access mode.
*/
public enum AccessMode {
FOOT, BICYCLE, SMALL_MOTORCYCLE, MOTORCYCLE, MOTORCAR, BUS
}
// Some masks...
@SuppressWarnings("unused")
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;
// Type of the road (see above). // Type of the road (see above).
private final RoadType type; private final RoadType type;
// Access information // Access information
private final int access; private final AccessRestriction access;
// One way road? // One way road?
private final boolean oneway; private final boolean oneway;
@ -67,8 +115,8 @@ public class RoadInformation {
// Name of the road. // Name of the road.
private final String name; private final String name;
public RoadInformation(RoadType roadType, int access, boolean isOneWay, int maxSpeed, public RoadInformation(RoadType roadType, AccessRestriction access, boolean isOneWay,
String name) { int maxSpeed, String name) {
this.type = roadType; this.type = roadType;
this.access = access; this.access = access;
this.oneway = isOneWay; this.oneway = isOneWay;
@ -81,48 +129,8 @@ public class RoadInformation {
/** /**
* @return true if this is a private road. * @return true if this is a private road.
*/ */
public boolean isPrivate() { public AccessRestriction getAccessRestrictions() {
return (this.access & MASK_PRIVATE) != 0; return this.access;
}
/**
* @return true if this road is reserved for public transport.
*/
public boolean isPublicTransport() {
return (this.access & MASK_PUBLIC_TRANSPORT) != 0;
}
/**
* @param mode
*
* @return true if this road is allowed for the specified mode.
*/
public boolean isAllowedFor(AccessMode mode) {
if ((this.access & MASK_UNKNOWN) != 0) {
return true;
}
int maskedAccess = 0;
switch (mode) {
case FOOT:
maskedAccess = access & MASK_FOOT;
break;
case BICYCLE:
maskedAccess = access & MASK_BICYCLE;
break;
case SMALL_MOTORCYCLE:
maskedAccess = access & MASK_SMALL_MOTORCYCLE;
break;
case MOTORCYCLE:
maskedAccess = access & MASK_MOTORCYCLE;
break;
case MOTORCAR:
maskedAccess = access & MASK_MOTORCAR;
break;
case BUS:
maskedAccess = access & MASK_BUS;
break;
}
return maskedAccess != 0;
} }
/** /**

View File

@ -10,6 +10,7 @@ import org.insa.graph.Graph;
import org.insa.graph.Node; import org.insa.graph.Node;
import org.insa.graph.Point; import org.insa.graph.Point;
import org.insa.graph.RoadInformation; import org.insa.graph.RoadInformation;
import org.insa.graph.RoadInformation.AccessRestriction;
import org.insa.graph.RoadInformation.RoadType; import org.insa.graph.RoadInformation.RoadType;
public class BinaryGraphReaderInsa2016 extends BinaryReader implements GraphReader { public class BinaryGraphReaderInsa2016 extends BinaryReader implements GraphReader {
@ -189,8 +190,8 @@ public class BinaryGraphReaderInsa2016 extends BinaryReader implements GraphRead
private RoadInformation readRoadInformation() throws IOException { private RoadInformation readRoadInformation() throws IOException {
char type = (char) dis.readUnsignedByte(); char type = (char) dis.readUnsignedByte();
int x = dis.readUnsignedByte(); int x = dis.readUnsignedByte();
return new RoadInformation(toRoadType(type), 0, (x & 0x80) > 0, (x & 0x7F) * 5, return new RoadInformation(toRoadType(type), new AccessRestriction(), (x & 0x80) > 0,
dis.readUTF()); (x & 0x7F) * 5, dis.readUTF());
} }
} }

View File

@ -4,12 +4,16 @@ import java.io.DataInputStream;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;
import org.insa.graph.Arc; import org.insa.graph.Arc;
import org.insa.graph.Graph; import org.insa.graph.Graph;
import org.insa.graph.Node; import org.insa.graph.Node;
import org.insa.graph.Point; import org.insa.graph.Point;
import org.insa.graph.RoadInformation; import org.insa.graph.RoadInformation;
import org.insa.graph.RoadInformation.AccessRestriction;
import org.insa.graph.RoadInformation.AccessMode;
import org.insa.graph.RoadInformation.RoadType; import org.insa.graph.RoadInformation.RoadType;
public class BinaryGraphReaderInsa2018 extends BinaryReader implements GraphReader { public class BinaryGraphReaderInsa2018 extends BinaryReader implements GraphReader {
@ -18,6 +22,48 @@ public class BinaryGraphReaderInsa2018 extends BinaryReader implements GraphRead
private static final int VERSION = 5; private static final int VERSION = 5;
private static final int MAGIC_NUMBER = 0x208BC3B3; private static final int MAGIC_NUMBER = 0x208BC3B3;
// 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.
*
* @param access
* @return
*/
protected static AccessRestriction toAccessInformation(int access) {
// Unknown -> Return default access information.
if ((access & MASK_UNKNOWN) != 0) {
return new AccessRestriction();
}
Map<AccessMode, Boolean> allowedModes = new EnumMap<>(AccessMode.class);
allowedModes.put(AccessMode.FOOT, (access & MASK_FOOT) != 0);
allowedModes.put(AccessMode.BICYCLE, (access & MASK_BICYCLE) != 0);
allowedModes.put(AccessMode.SMALL_MOTORCYCLE, (access & MASK_SMALL_MOTORCYCLE) != 0);
allowedModes.put(AccessMode.MOTORCYCLE, (access & MASK_MOTORCYCLE) != 0);
allowedModes.put(AccessMode.MOTORCAR, (access & MASK_MOTORCAR) != 0);
allowedModes.put(AccessMode.BUS, (access & MASK_BUS) != 0);
return new AccessRestriction((access & MASK_PRIVATE) != 0,
(access & MASK_PUBLIC_TRANSPORT) != 0, allowedModes);
}
/** /**
* Convert a character to its corresponding road type. * Convert a character to its corresponding road type.
* *
@ -27,7 +73,7 @@ public class BinaryGraphReaderInsa2018 extends BinaryReader implements GraphRead
* *
* @see http://wiki.openstreetmap.org/wiki/Highway_tag_usage. * @see http://wiki.openstreetmap.org/wiki/Highway_tag_usage.
*/ */
public static RoadType toRoadType(char ch) { protected static RoadType toRoadType(char ch) {
switch (ch) { switch (ch) {
case 'a': case 'a':
return RoadType.MOTORWAY; return RoadType.MOTORWAY;
@ -194,9 +240,9 @@ public class BinaryGraphReaderInsa2018 extends BinaryReader implements GraphRead
private RoadInformation readRoadInformation() throws IOException { private RoadInformation readRoadInformation() throws IOException {
char type = (char) dis.readUnsignedByte(); char type = (char) dis.readUnsignedByte();
int x = dis.readUnsignedByte(); int x = dis.readUnsignedByte();
int access = 0xFF00; AccessRestriction access = new AccessRestriction();
if (getCurrentVersion() >= 6) { if (getCurrentVersion() >= 6) {
access = dis.readUnsignedShort(); access = toAccessInformation(dis.readUnsignedShort());
} }
return new RoadInformation(toRoadType(type), access, (x & 0x80) > 0, (x & 0x7F) * 5, return new RoadInformation(toRoadType(type), access, (x & 0x80) > 0, (x & 0x7F) * 5,
dis.readUTF()); dis.readUTF());

View File

@ -31,8 +31,8 @@ public class PathTest {
public static void initAll() throws IOException { public static void initAll() throws IOException {
// 10 and 20 meters per seconds // 10 and 20 meters per seconds
RoadInformation speed10 = new RoadInformation(RoadType.ROAD, 0, true, 36, ""), RoadInformation speed10 = new RoadInformation(RoadType.ROAD, null, true, 36, ""),
speed20 = new RoadInformation(RoadType.ROAD, 0, true, 72, ""); speed20 = new RoadInformation(RoadType.ROAD, null, true, 72, "");
// Create nodes // Create nodes
nodes = new Node[5]; nodes = new Node[5];