| 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.ArrayList; |
| 7 | import java.util.List; |
| 8 | import java.util.Map.Entry; |
| 9 | import java.util.regex.Pattern; |
| 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 MULLTIPLE_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 Pattern KEY_PATTERN = Pattern.compile(".*[:]?direction"); |
| 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 direction")); |
| 35 | } |
| 36 | |
| 37 | @Override |
| 38 | public void visit(Node n) { |
| 39 | if (!n.isUsable() || !n.isTagged()) |
| 40 | return; |
| 41 | for (Entry<String, String> entry : n.getKeys().entrySet()) { |
| 42 | if (("forward".equals(entry.getValue()) || "backward".equals(entry.getValue())) |
| 43 | && KEY_PATTERN.matcher(entry.getKey()).matches()) { |
| 44 | checkParents(n, entry.getKey()); |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | |
| 49 | private void checkParents(Node n, String key) { |
| 50 | final List<Way> ways = new ArrayList<>(); |
| 51 | boolean hasWays = false; |
| 52 | for (Way w : n.getParentWays()) { |
| 53 | if (w.hasKey("highway", "railway", "waterway")) { |
| 54 | ways.add(w); |
| 55 | } |
| 56 | hasWays = true; |
| 57 | } |
| 58 | |
| 59 | if (ways.isEmpty() && !n.isOutsideDownloadArea() && n.getDataSet().getDataSourceArea() != null) { |
| 60 | if (!hasWays) { |
| 61 | errors.add(TestError.builder(this, Severity.WARNING, NO_WAY_CODE) |
| 62 | .primitives(n) |
| 63 | .message(tr("Unconnected node with {0}", key)).build()); |
| 64 | } |
| 65 | } else if (ways.size() == 1) { |
| 66 | Way w = ways.get(0); |
| 67 | if (w.firstNode() == n || w.lastNode() == n) { |
| 68 | errors.add(TestError.builder(this, Severity.WARNING, END_NODE_CODE) |
| 69 | .primitives(n, w) |
| 70 | .highlight(n) |
| 71 | .message(tr("Node with {0} on end of way", key)).build()); |
| 72 | } |
| 73 | } else if (ways.size() > 1) { |
| 74 | List<OsmPrimitive> primitives = new ArrayList<>(); |
| 75 | primitives.add(n); |
| 76 | primitives.addAll(ways); |
| 77 | errors.add(TestError.builder(this, Severity.WARNING, MULLTIPLE_WAYS_CODE) |
| 78 | .primitives(primitives) |
| 79 | .highlight(n) |
| 80 | .message(tr("Node with {0} on a connection of multiple ways", key)).build()); |
| 81 | } |
| 82 | } |
| 83 | } |