From e83332e9089ae439a2e12469daaa5f25f78f0f09 Mon Sep 17 00:00:00 2001 From: Peter Date: Mon, 18 Nov 2024 13:01:49 +0100 Subject: [PATCH] bug fix for max_weight parsing and fixes for max_weight_except --- CHANGELOG.md | 1 + .../routing/ev/MaxWeightExcept.java | 10 +++-- .../util/parsers/OSMMaxWeightParser.java | 2 +- .../parsers/helpers/OSMValueExtractor.java | 2 +- .../parsers/MaxWeightExceptParserTest.java | 6 +-- .../util/parsers/OSMMaxWeightParserTest.java | 39 +++++++++++-------- 6 files changed, 35 insertions(+), 25 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c879faffa24..5bea7a9464f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,6 @@ ### 11.0 [not yet released] +- max_weight_except: changed NONE to MISSING - the list of restrictions for BIKE returned from OSMRoadAccessParser.toOSMRestrictions is again `[bicycle, vehicle, access]` and not `[bicycle, access]` like before #2981 - road_access now contains value of highest transportation mode for CAR, i.e. access=private, motorcar=yes will now return YES and not PRIVATE - car.json by default avoids private roads diff --git a/core/src/main/java/com/graphhopper/routing/ev/MaxWeightExcept.java b/core/src/main/java/com/graphhopper/routing/ev/MaxWeightExcept.java index d25cc79e476..bcc2cf0d72a 100644 --- a/core/src/main/java/com/graphhopper/routing/ev/MaxWeightExcept.java +++ b/core/src/main/java/com/graphhopper/routing/ev/MaxWeightExcept.java @@ -8,7 +8,7 @@ */ public enum MaxWeightExcept { - NONE, DELIVERY, DESTINATION, FORESTRY; + MISSING, DELIVERY, DESTINATION, FORESTRY; public static final String KEY = "max_weight_except"; @@ -23,12 +23,16 @@ public String toString() { public static MaxWeightExcept find(String name) { if (name == null || name.isEmpty()) - return NONE; + return MISSING; + + // "maxweight:conditional=none @ private" is rare and seems to be known from a few mappers only + if (name.equalsIgnoreCase("permit") || name.equalsIgnoreCase("private")) + return DELIVERY; try { return MaxWeightExcept.valueOf(Helper.toUpperCase(name)); } catch (IllegalArgumentException ex) { - return NONE; + return MISSING; } } } diff --git a/core/src/main/java/com/graphhopper/routing/util/parsers/OSMMaxWeightParser.java b/core/src/main/java/com/graphhopper/routing/util/parsers/OSMMaxWeightParser.java index bfd3548cbc2..0043cc90e07 100644 --- a/core/src/main/java/com/graphhopper/routing/util/parsers/OSMMaxWeightParser.java +++ b/core/src/main/java/com/graphhopper/routing/util/parsers/OSMMaxWeightParser.java @@ -44,7 +44,7 @@ public OSMMaxWeightParser(DecimalEncodedValue weightEncoder) { public void handleWayTags(int edgeId, EdgeIntAccess edgeIntAccess, ReaderWay way, IntsRef relationFlags) { OSMValueExtractor.extractTons(edgeId, edgeIntAccess, way, weightEncoder, MAX_WEIGHT_TAGS); - // vehicle:conditional no @ (weight > 7.5) + // vehicle:conditional = no @ (weight > 7.5) for (String restriction : HGV_RESTRICTIONS) { String value = way.getTag(restriction, ""); if (value.startsWith("no") && value.indexOf("@") < 6) { // no,none[ ]@ diff --git a/core/src/main/java/com/graphhopper/routing/util/parsers/helpers/OSMValueExtractor.java b/core/src/main/java/com/graphhopper/routing/util/parsers/helpers/OSMValueExtractor.java index 82caff0034e..08bf8474973 100644 --- a/core/src/main/java/com/graphhopper/routing/util/parsers/helpers/OSMValueExtractor.java +++ b/core/src/main/java/com/graphhopper/routing/util/parsers/helpers/OSMValueExtractor.java @@ -52,7 +52,7 @@ public static double conditionalWeightToTons(String value) { } if (index > 0) { int lastIndex = value.indexOf(')', index); // (value) or value - if (lastIndex < 0) lastIndex = value.length() - 1; + if (lastIndex < 0) lastIndex = value.length(); if (lastIndex > index) return OSMValueExtractor.stringToTons(value.substring(index, lastIndex)); } diff --git a/core/src/test/java/com/graphhopper/routing/util/parsers/MaxWeightExceptParserTest.java b/core/src/test/java/com/graphhopper/routing/util/parsers/MaxWeightExceptParserTest.java index 2c769627d04..1604238a7f9 100644 --- a/core/src/test/java/com/graphhopper/routing/util/parsers/MaxWeightExceptParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/parsers/MaxWeightExceptParserTest.java @@ -44,7 +44,7 @@ public void testConditionalTags() { readerWay.setTag("highway", "primary"); readerWay.setTag("hgv:conditional", "no @ (weight > 7.5)"); parser.handleWayTags(edgeId, edgeIntAccess, readerWay, relFlags); - assertEquals(MaxWeightExcept.NONE, mwEnc.getEnum(false, edgeId, edgeIntAccess)); + assertEquals(MaxWeightExcept.MISSING, mwEnc.getEnum(false, edgeId, edgeIntAccess)); // weight=5 is missing edgeIntAccess = new ArrayEdgeIntAccess(1); @@ -52,7 +52,7 @@ public void testConditionalTags() { readerWay.setTag("highway", "primary"); readerWay.setTag("vehicle:conditional", "delivery @ (weight > 5)"); parser.handleWayTags(edgeId, edgeIntAccess, readerWay, relFlags); - assertEquals(MaxWeightExcept.NONE, mwEnc.getEnum(false, edgeId, edgeIntAccess)); + assertEquals(MaxWeightExcept.MISSING, mwEnc.getEnum(false, edgeId, edgeIntAccess)); edgeIntAccess = new ArrayEdgeIntAccess(1); readerWay.clearTags(); @@ -70,4 +70,4 @@ public void testConditionalTags() { parser.handleWayTags(edgeId, edgeIntAccess, readerWay, relFlags); assertEquals(MaxWeightExcept.DESTINATION, mwEnc.getEnum(false, edgeId, edgeIntAccess)); } -} \ No newline at end of file +} diff --git a/core/src/test/java/com/graphhopper/routing/util/parsers/OSMMaxWeightParserTest.java b/core/src/test/java/com/graphhopper/routing/util/parsers/OSMMaxWeightParserTest.java index 847c8c2b2eb..61b80ad95f1 100644 --- a/core/src/test/java/com/graphhopper/routing/util/parsers/OSMMaxWeightParserTest.java +++ b/core/src/test/java/com/graphhopper/routing/util/parsers/OSMMaxWeightParserTest.java @@ -24,42 +24,47 @@ public void setUp() { @Test public void testSimpleTags() { - EdgeIntAccess edgeIntAccess = new ArrayEdgeIntAccess(1); ReaderWay readerWay = new ReaderWay(1); readerWay.setTag("highway", "primary"); readerWay.setTag("maxweight", "5"); - parser.handleWayTags(edgeId, edgeIntAccess, readerWay, relFlags); - assertEquals(5.0, mwEnc.getDecimal(false, edgeId, edgeIntAccess), .01); + assertEquals(5.0, getMaxWeight(readerWay), .01); // if value is beyond the maximum then do not use infinity instead fallback to more restrictive maximum - edgeIntAccess = new ArrayEdgeIntAccess(1); readerWay.setTag("maxweight", "54"); - parser.handleWayTags(edgeId, edgeIntAccess, readerWay, relFlags); - assertEquals(51, mwEnc.getDecimal(false, edgeId, edgeIntAccess), .01); + assertEquals(51, getMaxWeight(readerWay), .01); } @Test public void testConditionalTags() { - EdgeIntAccess edgeIntAccess = new ArrayEdgeIntAccess(1); ReaderWay readerWay = new ReaderWay(1); readerWay.setTag("highway", "primary"); + readerWay.setTag("access:conditional", "no @ (weight > 7.5)"); + assertEquals(7.5, getMaxWeight(readerWay), .01); + + readerWay.setTag("access:conditional", "no @ weight > 7"); + assertEquals(7, getMaxWeight(readerWay), .01); + + readerWay = new ReaderWay(1); + readerWay.setTag("highway", "primary"); readerWay.setTag("hgv:conditional", "no @ (weight > 7.5)"); - parser.handleWayTags(edgeId, edgeIntAccess, readerWay, relFlags); - assertEquals(7.5, mwEnc.getDecimal(false, edgeId, edgeIntAccess), .01); + assertEquals(7.5, getMaxWeight(readerWay), .01); - edgeIntAccess = new ArrayEdgeIntAccess(1); + readerWay = new ReaderWay(1); + readerWay.setTag("highway", "primary"); readerWay.setTag("hgv:conditional", "none @ (weight > 10t)"); - parser.handleWayTags(edgeId, edgeIntAccess, readerWay, relFlags); - assertEquals(10, mwEnc.getDecimal(false, edgeId, edgeIntAccess), .01); + assertEquals(10, getMaxWeight(readerWay), .01); - edgeIntAccess = new ArrayEdgeIntAccess(1); readerWay.setTag("hgv:conditional", "no@ (weight > 7)"); - parser.handleWayTags(edgeId, edgeIntAccess, readerWay, relFlags); - assertEquals(7, mwEnc.getDecimal(false, edgeId, edgeIntAccess), .01); + assertEquals(7, getMaxWeight(readerWay), .01); - edgeIntAccess = new ArrayEdgeIntAccess(1); readerWay.setTag("hgv:conditional", "no @ (maxweight > 6)"); // allow different tagging + assertEquals(6, getMaxWeight(readerWay), .01); + } + + double getMaxWeight(ReaderWay readerWay) { + EdgeIntAccess edgeIntAccess = new ArrayEdgeIntAccess(1); + int edgeId = 0; parser.handleWayTags(edgeId, edgeIntAccess, readerWay, relFlags); - assertEquals(6, mwEnc.getDecimal(false, edgeId, edgeIntAccess), .01); + return mwEnc.getDecimal(false, edgeId, edgeIntAccess); } }