Ticket #20832: 20832.2.patch

File 20832.2.patch, 4.0 KB (added by GerdP, 2 years ago)
  • src/org/openstreetmap/josm/data/validation/tests/TurnrestrictionTest.java

     
    5151            "no_entry", "no_exit"
    5252        );
    5353
     54    // This should really be replaced with a transport mode hierarchy, i.e. #18383
     55    private static final List<String> SUPPORTED_VEHICLE_TYPES = Arrays.asList(
     56            "agricultural", "bicycle", "bus", "caravan", "foot", "hazmat",
     57            "hgv", "horse", "motor_vehicle", "motorcar", "motorcycle",
     58            "psv", "vehicle"
     59    );
     60
     61    /** A string for "restriction" */
     62    private static final String RESTRICTION_STRING = "restriction";
     63    /** A string for "conditional" */
     64    private static final String CONDITIONAL_STRING = "conditional";
     65
    5466    /**
    5567     * Constructs a new {@code TurnrestrictionTest}.
    5668     */
     
    5870        super(tr("Turn restrictions"), tr("This test checks if turn restrictions are valid."));
    5971    }
    6072
    61     private static boolean hasSupportedRestrictionTag(Relation r) {
    62         if (r.hasTag("restriction", SUPPORTED_RESTRICTIONS))
    63             return true;
    64         String conditionalValue = r.get("restriction:conditional");
    65         if (conditionalValue != null) {
     73    /**
     74     * Check for any restriction tag (e.g., "restriction:mode:conditional" or "restriction:conditional)
     75     * @param key The key to check
     76     * @param value The value to check
     77     * @return {@code true} if the tag is supported
     78     */
     79    private static boolean restrictionTagIsSupported(final String key, final String value) {
     80        if (!key.startsWith("restriction:"))
     81            return false;
     82        String[] parts = key.split(":");
     83        if (parts.length > 3)
     84            return false;
     85        if (parts.length == 3 && !SUPPORTED_VEHICLE_TYPES.contains(parts[1]))
     86            return false;
     87        if (key.endsWith(CONDITIONAL_STRING)) {
     88            // restriction:conditional=* or restriction:mode:condition=*
    6689            try {
    67                 List<ConditionalValue> values = ConditionalValue.parse(conditionalValue);
    68                 return !values.isEmpty() && SUPPORTED_RESTRICTIONS.contains(values.get(0).restrictionValue);
     90                final List<ConditionalValue> conditionalValues = ConditionalValue.parse(value);
     91                return !conditionalValues.isEmpty() && conditionalValues.stream()
     92                        .anyMatch(conditional -> SUPPORTED_RESTRICTIONS.contains(conditional.restrictionValue));
    6993            } catch (ConditionalParsingException e) {
    7094                Logging.trace(e);
    7195            }
     96        } else if (SUPPORTED_VEHICLE_TYPES.contains(parts[1])) {
     97            // restriction:mode=*
     98            return SUPPORTED_RESTRICTIONS.contains(value);
    7299        }
    73100        return false;
    74101    }
    75102
     103    private static boolean hasSupportedRestrictionTag(Relation r) {
     104        if (r.hasTag(RESTRICTION_STRING, SUPPORTED_RESTRICTIONS))
     105            return true;
     106        if (r.hasTag("restriction:bicycle", "give_way", "stop"))
     107            return true;
     108        return r.getKeys().entrySet().stream()
     109                .anyMatch(entry -> restrictionTagIsSupported(entry.getKey(), entry.getValue()));
     110    }
     111
    76112    @Override
    77113    public void visit(Relation r) {
    78         if (!r.hasTag("type", "restriction"))
     114        if (!r.hasTag("type", RESTRICTION_STRING))
    79115            return;
    80116
    81117        if (!hasSupportedRestrictionTag(r)) {
     
    224260            return;
    225261        }
    226262        if (fromWay.equals(toWay)) {
    227             Severity severity = r.hasTag("restriction", "no_u_turn") ? Severity.OTHER : Severity.WARNING;
     263            Severity severity = r.hasTag(RESTRICTION_STRING, "no_u_turn") ? Severity.OTHER : Severity.WARNING;
    228264            errors.add(TestError.builder(this, severity, FROM_EQUALS_TO)
    229265                    .message(tr("\"from\" way equals \"to\" way"))
    230266                    .primitives(r)