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.HashSet;
|
---|
7 | import java.util.Set;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.data.osm.Node;
|
---|
10 | import org.openstreetmap.josm.data.osm.Way;
|
---|
11 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
12 | import org.openstreetmap.josm.data.validation.Test;
|
---|
13 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
14 |
|
---|
15 | /**
|
---|
16 | * Checks for self-intersecting ways.
|
---|
17 | */
|
---|
18 | public class SelfIntersectingWay extends Test {
|
---|
19 |
|
---|
20 | protected static final int SELF_INTERSECT = 401;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Constructs a new {@code SelfIntersectingWay} test.
|
---|
24 | */
|
---|
25 | public SelfIntersectingWay() {
|
---|
26 | super(tr("Self-intersecting ways"),
|
---|
27 | tr("This test checks for ways " +
|
---|
28 | "that contain some of their nodes more than once."));
|
---|
29 | }
|
---|
30 |
|
---|
31 | @Override
|
---|
32 | public void visit(Way w) {
|
---|
33 | int last = w.getNodesCount();
|
---|
34 | if (last < 2)
|
---|
35 | return;
|
---|
36 | Set<Node> nodes = new HashSet<>();
|
---|
37 | nodes.add(w.firstNode());
|
---|
38 | int countFirst = 0;
|
---|
39 | int countLast = 0;
|
---|
40 | for (int i = 1; i < last; i++) {
|
---|
41 | Node n = w.getNode(i);
|
---|
42 | if (nodes.contains(n)) {
|
---|
43 | boolean ok = false;
|
---|
44 | if (n == w.firstNode()) {
|
---|
45 | if (countFirst++ == 0)
|
---|
46 | ok = true;
|
---|
47 | } else if (i + 1 == last) {
|
---|
48 | if (countLast++ == 0)
|
---|
49 | ok = true;
|
---|
50 | }
|
---|
51 | if (!ok || countFirst + countLast > 1) {
|
---|
52 | errors.add(TestError.builder(this, Severity.WARNING, SELF_INTERSECT)
|
---|
53 | .message(tr("Self-intersecting ways"))
|
---|
54 | .primitives(w)
|
---|
55 | .highlight(n)
|
---|
56 | .build());
|
---|
57 | break;
|
---|
58 | }
|
---|
59 | } else {
|
---|
60 | nodes.add(n);
|
---|
61 | }
|
---|
62 | }
|
---|
63 | }
|
---|
64 | }
|
---|