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