| 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.marktr; |
| 5 | import static org.openstreetmap.josm.tools.I18n.tr; |
| 6 | |
| 7 | import java.util.ArrayList; |
| 8 | import java.util.List; |
| 9 | import java.util.Map.Entry; |
| 10 | |
| 11 | import org.openstreetmap.josm.data.osm.Node; |
| 12 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| 13 | import org.openstreetmap.josm.data.osm.Way; |
| 14 | import org.openstreetmap.josm.data.validation.Severity; |
| 15 | import org.openstreetmap.josm.data.validation.Test; |
| 16 | import org.openstreetmap.josm.data.validation.TestError; |
| 17 | |
| 18 | /** |
| 19 | * Find nodes with direction tag and invalid number of parent ways or position in way. See #20019. |
| 20 | * @author Gerd Petermann |
| 21 | * @since xxx |
| 22 | */ |
| 23 | public class DirectionNodes extends Test { |
| 24 | private static final int MULTIPLE_WAYS_CODE = 4000; |
| 25 | private static final int END_NODE_CODE = 4001; |
| 26 | private static final int NO_WAY_CODE = 4002; |
| 27 | |
| 28 | private static final String DIR_VERIF_PROBLEM_MSG = tr("Invalid usage of direction on node"); |
| 29 | |
| 30 | /** |
| 31 | * Construct a new {@code DirectionNodes} object |
| 32 | */ |
| 33 | public DirectionNodes() { |
| 34 | super(tr("Direction nodes"), tr("Check for nodes which have a 'forward' or 'backward' direction")); |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public void visit(Node n) { |
| 39 | if (!n.isUsable() || !n.isTagged()) |
| 40 | return; |
| 41 | for (Entry<String, String> tag : n.getKeys().entrySet()) { |
| 42 | if (("forward".equals(tag.getValue()) || "backward".equals(tag.getValue()) |
| 43 | && ("direction".equals(tag.getKey()) || tag.getKey().endsWith(":direction")))) { |
| 44 | checkParents(n, tag.toString()); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private static boolean isSuitableParentWay(Way w) { |
| 50 | return w.hasKey("highway", "railway", "waterway") || w.hasTag("man_made", "pipeline"); |
| 51 | } |
| 52 | |
| 53 | private void checkParents(Node n, String tag) { |
| 54 | final List<Way> ways = new ArrayList<>(); |
| 55 | int count = 0; |
| 56 | for (Way w : n.getParentWays()) { |
| 57 | if (isSuitableParentWay(w)) { |
| 58 | ways.add(w); |
| 59 | } |
| 60 | count++; |
| 61 | } |
| 62 | boolean needsParentWays = n.isNew() || (!n.isOutsideDownloadArea() && n.getDataSet().getDataSourceArea() != null); |
| 63 | TestError.Builder builder = null; |
| 64 | if (ways.isEmpty() && needsParentWays) { |
| 65 | if (count == 0) { |
| 66 | builder = TestError.builder(this, Severity.WARNING, NO_WAY_CODE).message(DIR_VERIF_PROBLEM_MSG, |
| 67 | marktr("Unconnected node with {0}"), tag); |
| 68 | } |
| 69 | |
| 70 | } else if (ways.size() == 1) { |
| 71 | Way w = ways.get(0); |
| 72 | if (w.firstNode() == n || w.lastNode() == n) { |
| 73 | builder = TestError.builder(this, Severity.WARNING, END_NODE_CODE).message(DIR_VERIF_PROBLEM_MSG, |
| 74 | marktr("Node with {0} on end of way"), tag); |
| 75 | } |
| 76 | } else if (ways.size() > 1) { |
| 77 | builder = TestError.builder(this, Severity.WARNING, MULTIPLE_WAYS_CODE).message(DIR_VERIF_PROBLEM_MSG, |
| 78 | marktr("Node with {0} on a connection of multiple ways"), tag); |
| 79 | } |
| 80 | if (builder != null) { |
| 81 | List<OsmPrimitive> primitives = new ArrayList<>(); |
| 82 | primitives.add(n); |
| 83 | primitives.addAll(ways); |
| 84 | errors.add(builder.primitives(primitives).highlight(n).build()); |
| 85 | } |
| 86 | } |
| 87 | } |