From f7ef63068b667988268ca9a0d5805dfd13ff3273 Mon Sep 17 00:00:00 2001 From: Yogurt4 Date: Mon, 15 Apr 2024 20:15:35 +0200 Subject: [PATCH] Restrict access to "destination" for living streets in Hungary (by law) (#2879) --- .../europe/HungaryCountryRule.java | 22 +++++++++++++++++++ .../util/countryrules/CountryRuleTest.java | 21 ++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/core/src/main/java/com/graphhopper/routing/util/countryrules/europe/HungaryCountryRule.java b/core/src/main/java/com/graphhopper/routing/util/countryrules/europe/HungaryCountryRule.java index 4955ab2aeaa..0161d3d9ea3 100644 --- a/core/src/main/java/com/graphhopper/routing/util/countryrules/europe/HungaryCountryRule.java +++ b/core/src/main/java/com/graphhopper/routing/util/countryrules/europe/HungaryCountryRule.java @@ -18,8 +18,10 @@ package com.graphhopper.routing.util.countryrules.europe; import com.graphhopper.reader.ReaderWay; +import com.graphhopper.routing.ev.RoadAccess; import com.graphhopper.routing.ev.RoadClass; import com.graphhopper.routing.ev.Toll; +import com.graphhopper.routing.util.TransportationMode; import com.graphhopper.routing.util.countryrules.CountryRule; /** @@ -29,6 +31,26 @@ */ public class HungaryCountryRule implements CountryRule { + @Override + public RoadAccess getAccess(ReaderWay readerWay, TransportationMode transportationMode, RoadAccess currentRoadAccess) { + // Pedestrian traffic and bicycles are not restricted + if (transportationMode == TransportationMode.FOOT || transportationMode == TransportationMode.BIKE) { + return currentRoadAccess; + } + + // Override only bogus "yes" and missing/other + if (currentRoadAccess != RoadAccess.YES && currentRoadAccess != RoadAccess.OTHER) { + return currentRoadAccess; + } + + RoadClass roadClass = RoadClass.find(readerWay.getTag("highway", "")); + if (roadClass == RoadClass.LIVING_STREET) { + return RoadAccess.DESTINATION; + } + + return currentRoadAccess; + } + @Override public Toll getToll(ReaderWay readerWay, Toll currentToll) { if (currentToll != Toll.MISSING) { diff --git a/core/src/test/java/com/graphhopper/routing/util/countryrules/CountryRuleTest.java b/core/src/test/java/com/graphhopper/routing/util/countryrules/CountryRuleTest.java index 274dcfd5f67..def53ce0ea1 100644 --- a/core/src/test/java/com/graphhopper/routing/util/countryrules/CountryRuleTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/countryrules/CountryRuleTest.java @@ -19,9 +19,11 @@ import com.graphhopper.reader.ReaderWay; import com.graphhopper.routing.ev.RoadAccess; +import com.graphhopper.routing.ev.Toll; import com.graphhopper.routing.util.TransportationMode; import com.graphhopper.routing.util.countryrules.europe.AustriaCountryRule; import com.graphhopper.routing.util.countryrules.europe.GermanyCountryRule; +import com.graphhopper.routing.util.countryrules.europe.HungaryCountryRule; import org.junit.jupiter.api.Test; @@ -44,6 +46,25 @@ void austria() { assertEquals(RoadAccess.DESTINATION, rule.getAccess(createReaderWay("living_street"), TransportationMode.CAR, RoadAccess.YES)); } + @Test + void hungary() { + HungaryCountryRule rule = new HungaryCountryRule(); + assertEquals(RoadAccess.YES, rule.getAccess(createReaderWay("primary"), TransportationMode.CAR, RoadAccess.YES)); + assertEquals(RoadAccess.DESTINATION, rule.getAccess(createReaderWay("living_street"), TransportationMode.CAR, RoadAccess.YES)); + assertEquals(RoadAccess.YES, rule.getAccess(createReaderWay("living_street"), TransportationMode.BIKE, RoadAccess.YES)); + assertEquals(RoadAccess.PRIVATE, rule.getAccess(createReaderWay("living_street"), TransportationMode.CAR, RoadAccess.PRIVATE)); + assertEquals(RoadAccess.PRIVATE, rule.getAccess(createReaderWay("living_street"), TransportationMode.BIKE, RoadAccess.PRIVATE)); + assertEquals(Toll.ALL, rule.getToll(createReaderWay("motorway"), Toll.MISSING)); + assertEquals(Toll.HGV, rule.getToll(createReaderWay("trunk"), Toll.MISSING)); + assertEquals(Toll.HGV, rule.getToll(createReaderWay("primary"), Toll.MISSING)); + assertEquals(Toll.MISSING, rule.getToll(createReaderWay("secondary"), Toll.MISSING)); + assertEquals(Toll.MISSING, rule.getToll(createReaderWay("residential"), Toll.MISSING)); + assertEquals(Toll.MISSING, rule.getToll(createReaderWay("service"), Toll.MISSING)); + assertEquals(Toll.ALL, rule.getToll(createReaderWay("service"), Toll.ALL)); + assertEquals(Toll.HGV, rule.getToll(createReaderWay("service"), Toll.HGV)); + assertEquals(Toll.NO, rule.getToll(createReaderWay("service"), Toll.NO)); + } + private ReaderWay createReaderWay(String highway) { ReaderWay readerWay = new ReaderWay(123L); readerWay.setTag("highway", highway);