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