Skip to content

Commit

Permalink
ModeAccessParser: remove private by default and make restriction valu…
Browse files Browse the repository at this point in the history
…es configurable
  • Loading branch information
karussell committed Mar 13, 2024
1 parent b5a27c1 commit 93cf366
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@
import com.graphhopper.routing.util.*;
import com.graphhopper.routing.util.parsers.*;

import java.util.Arrays;
import java.util.stream.Collectors;

public class DefaultImportRegistry implements ImportRegistry {
@Override
public ImportUnit createImportUnit(String name) {
Expand Down Expand Up @@ -206,13 +209,15 @@ else if (BikeNetwork.KEY.equals(name) || MtbNetwork.KEY.equals(name) || FootNetw

else if (BusAccess.KEY.equals(name))
return ImportUnit.create(name, props -> BusAccess.create(),
(lookup, props) -> new ModeAccessParser(TransportationMode.BUS, lookup.getBooleanEncodedValue(BusAccess.KEY), lookup.getBooleanEncodedValue(Roundabout.KEY)),
(lookup, props) -> new ModeAccessParser(TransportationMode.BUS, lookup.getBooleanEncodedValue(BusAccess.KEY),
lookup.getBooleanEncodedValue(Roundabout.KEY), Arrays.stream(props.getString("restrictions", "").split(";")).filter(s -> !s.isEmpty()).collect(Collectors.toList())),
"roundabout"
);

else if (HovAccess.KEY.equals(name))
return ImportUnit.create(name, props -> HovAccess.create(),
(lookup, props) -> new ModeAccessParser(TransportationMode.HOV, lookup.getBooleanEncodedValue(HovAccess.KEY), lookup.getBooleanEncodedValue(Roundabout.KEY)),
(lookup, props) -> new ModeAccessParser(TransportationMode.HOV, lookup.getBooleanEncodedValue(HovAccess.KEY),
lookup.getBooleanEncodedValue(Roundabout.KEY), Arrays.stream(props.getString("restrictions", "").split(";")).filter(s -> !s.isEmpty()).collect(Collectors.toList())),
"roundabout"
);
else if (FootRoadAccessConditional.KEY.equals(name))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,22 +12,24 @@
public class ModeAccessParser implements TagParser {

private final static Set<String> onewaysForward = new HashSet<>(Arrays.asList("yes", "true", "1"));
private final static Set<String> restrictedValues = new HashSet<>(Arrays.asList("no", "restricted", "military", "emergency", "private", "permit"));
private final Set<String> restrictedValues;
private final List<String> restrictionKeys;
private final List<String> vehicleForward;
private final List<String> vehicleBackward;
private final List<String> ignoreOnewayKeys;
private final BooleanEncodedValue accessEnc;
private final BooleanEncodedValue roundaboutEnc;

public ModeAccessParser(TransportationMode mode, BooleanEncodedValue accessEnc, BooleanEncodedValue roundaboutEnc) {
public ModeAccessParser(TransportationMode mode, BooleanEncodedValue accessEnc, BooleanEncodedValue roundaboutEnc, List<String> restrictions) {
this.accessEnc = accessEnc;
this.roundaboutEnc = roundaboutEnc;
restrictionKeys = OSMRoadAccessParser.toOSMRestrictions(mode);
vehicleForward = restrictionKeys.stream().map(r -> r + ":forward").toList();
vehicleBackward = restrictionKeys.stream().map(r -> r + ":backward").toList();

ignoreOnewayKeys = restrictionKeys.stream().map(r -> "oneway:" + r).toList();
restrictedValues = new HashSet<>(restrictions.isEmpty() ? Arrays.asList("no", "restricted", "military", "emergency") : restrictions);
if (restrictedValues.contains(""))
throw new IllegalArgumentException("restriction values cannot contain empty string");
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static org.junit.jupiter.api.Assertions.assertFalse;
Expand All @@ -16,7 +17,7 @@
class ModeAccessParserTest {

private final EncodingManager em = new EncodingManager.Builder().add(Roundabout.create()).add(BusAccess.create()).build();
private final ModeAccessParser parser = new ModeAccessParser(TransportationMode.BUS, em.getBooleanEncodedValue(BusAccess.KEY), em.getBooleanEncodedValue(Roundabout.KEY));
private final ModeAccessParser parser = new ModeAccessParser(TransportationMode.BUS, em.getBooleanEncodedValue(BusAccess.KEY), em.getBooleanEncodedValue(Roundabout.KEY), List.of());
private final BooleanEncodedValue busAccessEnc = em.getBooleanEncodedValue(BusAccess.KEY);

@Test
Expand All @@ -30,6 +31,18 @@ public void testAccess() {
assertTrue(busAccessEnc.getBool(true, edgeId, edgeIntAccess));
}

@Test
public void testPrivate() {
ReaderWay way = new ReaderWay(1);
way.setTag("highway", "primary");
way.setTag("access", "private");
EdgeIntAccess edgeIntAccess = new ArrayEdgeIntAccess(em.getIntsForFlags());
int edgeId = 0;
parser.handleWayTags(edgeId, edgeIntAccess, way, null);
assertTrue(busAccessEnc.getBool(false, edgeId, edgeIntAccess));
assertTrue(busAccessEnc.getBool(true, edgeId, edgeIntAccess));
}

@Test
public void testOneway() {
ReaderWay way = new ReaderWay(1);
Expand Down Expand Up @@ -179,7 +192,7 @@ public void testPsvYes() {
public void testMotorcycleYes() {
BooleanEncodedValue mcAccessEnc = new SimpleBooleanEncodedValue("motorcycle_access", true);
EncodingManager mcEM = new EncodingManager.Builder().add(mcAccessEnc).add(Roundabout.create()).build();
ModeAccessParser mcParser = new ModeAccessParser(TransportationMode.MOTORCYCLE, mcAccessEnc, mcEM.getBooleanEncodedValue(Roundabout.KEY));
ModeAccessParser mcParser = new ModeAccessParser(TransportationMode.MOTORCYCLE, mcAccessEnc, mcEM.getBooleanEncodedValue(Roundabout.KEY), List.of());

int edgeId = 0;
EdgeIntAccess access = new ArrayEdgeIntAccess(1);
Expand Down

0 comments on commit 93cf366

Please sign in to comment.