| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.data.validation.tests;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 5 |
|
|---|
| 6 | import java.util.Arrays;
|
|---|
| 7 | import java.util.Set;
|
|---|
| 8 | import java.util.stream.Collectors;
|
|---|
| 9 |
|
|---|
| 10 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 11 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 12 | import org.openstreetmap.josm.data.validation.Severity;
|
|---|
| 13 | import org.openstreetmap.josm.data.validation.Test;
|
|---|
| 14 | import org.openstreetmap.josm.data.validation.TestError;
|
|---|
| 15 | import org.openstreetmap.josm.tools.Logging;
|
|---|
| 16 | import org.openstreetmap.josm.tools.Utils;
|
|---|
| 17 |
|
|---|
| 18 | /**
|
|---|
| 19 | * Test that validates {@code lane:} tags.
|
|---|
| 20 | * @since 6592
|
|---|
| 21 | */
|
|---|
| 22 | public class Lanes extends Test.TagTest {
|
|---|
| 23 |
|
|---|
| 24 | private static final String[] BLACKLIST = {
|
|---|
| 25 | "source:lanes",
|
|---|
| 26 | "note:lanes",
|
|---|
| 27 | "proposed:lanes",
|
|---|
| 28 | "source:proposed:lanes",
|
|---|
| 29 | "piste:lanes",
|
|---|
| 30 | };
|
|---|
| 31 |
|
|---|
| 32 | /**
|
|---|
| 33 | * Constructs a new {@code Lanes} test.
|
|---|
| 34 | */
|
|---|
| 35 | public Lanes() {
|
|---|
| 36 | super(tr("Lane tags"), tr("Test that validates ''lane:'' tags."));
|
|---|
| 37 | }
|
|---|
| 38 |
|
|---|
| 39 | static int getLanesCount(String value) {
|
|---|
| 40 | return value.isEmpty() ? 0 : value.replaceAll("[^|]", "").length() + 1;
|
|---|
| 41 | }
|
|---|
| 42 |
|
|---|
| 43 | protected void checkNumberOfLanesByKey(final OsmPrimitive p, String lanesKey, String message) {
|
|---|
| 44 | final Set<Integer> lanesCount =
|
|---|
| 45 | p.keys()
|
|---|
| 46 | .filter(x -> x.endsWith(":" + lanesKey))
|
|---|
| 47 | .filter(x -> !Arrays.asList(BLACKLIST).contains(x))
|
|---|
| 48 | .map(key -> getLanesCount(p.get(key)))
|
|---|
| 49 | .collect(Collectors.toSet());
|
|---|
| 50 |
|
|---|
| 51 | if (lanesCount.size() > 1) {
|
|---|
| 52 | // if not all numbers are the same
|
|---|
| 53 | errors.add(TestError.builder(this, Severity.WARNING, 3100)
|
|---|
| 54 | .message(message)
|
|---|
| 55 | .primitives(p)
|
|---|
| 56 | .build());
|
|---|
| 57 | } else if (lanesCount.size() == 1 && p.hasKey(lanesKey)) {
|
|---|
| 58 | // ensure that lanes <= *:lanes
|
|---|
| 59 | try {
|
|---|
| 60 | if (Integer.parseInt(p.get(lanesKey)) > lanesCount.iterator().next()) {
|
|---|
| 61 | errors.add(TestError.builder(this, Severity.WARNING, 3100)
|
|---|
| 62 | .message(tr("Number of {0} greater than {1}", lanesKey, "*:" + lanesKey))
|
|---|
| 63 | .primitives(p)
|
|---|
| 64 | .build());
|
|---|
| 65 | }
|
|---|
| 66 | } catch (NumberFormatException e) {
|
|---|
| 67 | Logging.debug(e.getMessage());
|
|---|
| 68 | }
|
|---|
| 69 | }
|
|---|
| 70 | }
|
|---|
| 71 |
|
|---|
| 72 | protected void checkNumberOfLanes(final OsmPrimitive p) {
|
|---|
| 73 | final String lanes = p.get("lanes");
|
|---|
| 74 | if (lanes == null) return;
|
|---|
| 75 | final String forward = Utils.firstNonNull(p.get("lanes:forward"), "0");
|
|---|
| 76 | final String backward = Utils.firstNonNull(p.get("lanes:backward"), "0");
|
|---|
| 77 | try {
|
|---|
| 78 | if (Integer.parseInt(lanes) < Integer.parseInt(forward) + Integer.parseInt(backward)) {
|
|---|
| 79 | errors.add(TestError.builder(this, Severity.WARNING, 3101)
|
|---|
| 80 | .message(tr("Number of {0} greater than {1}", tr("{0}+{1}", "lanes:forward", "lanes:backward"), "lanes"))
|
|---|
| 81 | .primitives(p)
|
|---|
| 82 | .build());
|
|---|
| 83 | }
|
|---|
| 84 | } catch (NumberFormatException e) {
|
|---|
| 85 | Logging.debug(e.getMessage());
|
|---|
| 86 | }
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | @Override
|
|---|
| 90 | public void check(OsmPrimitive p) {
|
|---|
| 91 | checkNumberOfLanesByKey(p, "lanes", tr("Number of lane dependent values inconsistent"));
|
|---|
| 92 | checkNumberOfLanesByKey(p, "lanes:forward", tr("Number of lane dependent values inconsistent in forward direction"));
|
|---|
| 93 | checkNumberOfLanesByKey(p, "lanes:backward", tr("Number of lane dependent values inconsistent in backward direction"));
|
|---|
| 94 | checkNumberOfLanes(p);
|
|---|
| 95 | }
|
|---|
| 96 |
|
|---|
| 97 | @Override
|
|---|
| 98 | public boolean isPrimitiveUsable(OsmPrimitive p) {
|
|---|
| 99 | return p.isTagged() && p instanceof Way && p.hasTag("highway") && super.isPrimitiveUsable(p);
|
|---|
| 100 | }
|
|---|
| 101 | }
|
|---|