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.Map;
|
---|
7 | import java.util.List;
|
---|
8 | import java.util.HashMap;
|
---|
9 | import java.util.HashSet;
|
---|
10 | import java.util.ArrayList;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.Node;
|
---|
13 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
14 | import org.openstreetmap.josm.data.validation.Test;
|
---|
15 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
16 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
17 |
|
---|
18 | public class NodesWithSameName extends Test {
|
---|
19 | protected static int SAME_NAME = 801;
|
---|
20 |
|
---|
21 | private Map<String, List<Node>> namesToNodes;
|
---|
22 |
|
---|
23 | public NodesWithSameName() {
|
---|
24 | super(tr("Nodes with same name"),
|
---|
25 | tr("This test finds nodes that have the same name (might be duplicates)."));
|
---|
26 | }
|
---|
27 |
|
---|
28 | @Override public void startTest(ProgressMonitor monitor) {
|
---|
29 | super.startTest(monitor);
|
---|
30 | namesToNodes = new HashMap<String, List<Node>>();
|
---|
31 | }
|
---|
32 |
|
---|
33 | @Override public void visit(Node n) {
|
---|
34 | if (!n.isUsable()) return;
|
---|
35 |
|
---|
36 | String name = n.get("name");
|
---|
37 | String sign = n.get("traffic_sign");
|
---|
38 | String highway = n.get("highway");
|
---|
39 | if (name == null
|
---|
40 | || (sign != null && sign.equals("city_limit"))
|
---|
41 | || (highway != null && highway.equals("bus_stop")))
|
---|
42 | return;
|
---|
43 |
|
---|
44 | List<Node> nodes = namesToNodes.get(name);
|
---|
45 | if (nodes == null) {
|
---|
46 | namesToNodes.put(name, nodes = new ArrayList<Node>());
|
---|
47 | }
|
---|
48 |
|
---|
49 | nodes.add(n);
|
---|
50 | }
|
---|
51 |
|
---|
52 | @Override public void endTest() {
|
---|
53 | for (List<Node> nodes : namesToNodes.values()) {
|
---|
54 | if (nodes.size() > 1) {
|
---|
55 | // Report the same-name nodes, unless each has a unique ref=*.
|
---|
56 | HashSet<String> refs = new HashSet<String>();
|
---|
57 |
|
---|
58 | for (Node n : nodes) {
|
---|
59 | String ref = n.get("ref");
|
---|
60 | if (ref == null || !refs.add(ref)) {
|
---|
61 | errors.add(new TestError(this, Severity.OTHER,
|
---|
62 | tr("Nodes with same name"), SAME_NAME, nodes));
|
---|
63 | break;
|
---|
64 | }
|
---|
65 | }
|
---|
66 | }
|
---|
67 | }
|
---|
68 | super.endTest();
|
---|
69 | namesToNodes = null;
|
---|
70 | }
|
---|
71 | }
|
---|