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.Collections;
|
---|
8 | import java.util.Iterator;
|
---|
9 | import java.util.List;
|
---|
10 |
|
---|
11 | import org.openstreetmap.josm.command.ChangeNodesCommand;
|
---|
12 | import org.openstreetmap.josm.command.Command;
|
---|
13 | import org.openstreetmap.josm.data.osm.Node;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
15 | import org.openstreetmap.josm.data.osm.Way;
|
---|
16 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
17 | import org.openstreetmap.josm.data.validation.Test;
|
---|
18 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Checks for ways with identical consecutive nodes.
|
---|
22 | * @since 3669
|
---|
23 | */
|
---|
24 | public class DuplicatedWayNodes extends Test {
|
---|
25 | protected static final int DUPLICATE_WAY_NODE = 501;
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Constructs a new {@code DuplicatedWayNodes} test.
|
---|
29 | */
|
---|
30 | public DuplicatedWayNodes() {
|
---|
31 | super(tr("Duplicated way nodes"),
|
---|
32 | tr("Checks for ways with identical consecutive nodes."));
|
---|
33 | }
|
---|
34 |
|
---|
35 | @Override
|
---|
36 | public void visit(Way w) {
|
---|
37 | if (!w.isUsable()) return;
|
---|
38 |
|
---|
39 | Node lastN = null;
|
---|
40 | for (Node n : w.getNodes()) {
|
---|
41 | if (lastN == null) {
|
---|
42 | lastN = n;
|
---|
43 | continue;
|
---|
44 | }
|
---|
45 | if (lastN == n) {
|
---|
46 | errors.add(TestError.builder(this, Severity.ERROR, DUPLICATE_WAY_NODE)
|
---|
47 | .message(tr("Duplicated way nodes"))
|
---|
48 | .primitives(w)
|
---|
49 | .highlight(n)
|
---|
50 | .build());
|
---|
51 | break;
|
---|
52 | }
|
---|
53 | lastN = n;
|
---|
54 | }
|
---|
55 | }
|
---|
56 |
|
---|
57 | @Override
|
---|
58 | public Command fixError(TestError testError) {
|
---|
59 | // primitives list can be empty if all primitives have been purged
|
---|
60 | Iterator<? extends OsmPrimitive> it = testError.getPrimitives().iterator();
|
---|
61 | if (it.hasNext()) {
|
---|
62 | Way w = (Way) it.next();
|
---|
63 | Node lastN = null;
|
---|
64 | List<Node> modNodes = new ArrayList<>();
|
---|
65 | for (Node n : w.getNodes()) {
|
---|
66 | if (n != lastN) {
|
---|
67 | modNodes.add(n);
|
---|
68 | }
|
---|
69 | lastN = n;
|
---|
70 | }
|
---|
71 | if (modNodes.size() < 2)
|
---|
72 | // Empty way, delete
|
---|
73 | return deletePrimitivesIfNeeded(Collections.singleton(w));
|
---|
74 | else
|
---|
75 | return new ChangeNodesCommand(w, modNodes);
|
---|
76 | }
|
---|
77 | return null;
|
---|
78 | }
|
---|
79 |
|
---|
80 | @Override
|
---|
81 | public boolean isFixable(TestError testError) {
|
---|
82 | return testError.getTester() instanceof DuplicatedWayNodes;
|
---|
83 | }
|
---|
84 | }
|
---|