source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/UntaggedWay.java@ 8510

Last change on this file since 8510 was 8510, checked in by Don-vip, 9 years ago

checkstyle: enable relevant whitespace checks and fix them

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.HashSet;
7import java.util.Map;
8import java.util.Set;
9
10import org.openstreetmap.josm.Main;
11import org.openstreetmap.josm.command.Command;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.Relation;
14import org.openstreetmap.josm.data.osm.RelationMember;
15import org.openstreetmap.josm.data.osm.Way;
16import org.openstreetmap.josm.data.validation.Severity;
17import org.openstreetmap.josm.data.validation.Test;
18import org.openstreetmap.josm.data.validation.TestError;
19import org.openstreetmap.josm.gui.progress.ProgressMonitor;
20
21/**
22 * Checks for untagged ways
23 *
24 * @author frsantos
25 */
26public class UntaggedWay extends Test {
27
28 /** Empty way error */
29 protected static final int EMPTY_WAY = 301;
30 /** Untagged way error */
31 protected static final int UNTAGGED_WAY = 302;
32 /** Unnamed way error */
33 protected static final int UNNAMED_WAY = 303;
34 /** One node way error */
35 protected static final int ONE_NODE_WAY = 304;
36 /** Unnamed junction error */
37 protected static final int UNNAMED_JUNCTION = 305;
38 /** Untagged, but commented way error */
39 protected static final int COMMENTED_WAY = 306;
40
41 private Set<Way> waysUsedInRelations;
42
43 /** Ways that must have a name */
44 public static final Set<String> NAMED_WAYS = new HashSet<>();
45 static {
46 NAMED_WAYS.add("motorway");
47 NAMED_WAYS.add("trunk");
48 NAMED_WAYS.add("primary");
49 NAMED_WAYS.add("secondary");
50 NAMED_WAYS.add("tertiary");
51 NAMED_WAYS.add("residential");
52 NAMED_WAYS.add("pedestrian");
53 }
54
55 /** Whitelist of roles allowed to reference an untagged way */
56 public static final Set<String> WHITELIST = new HashSet<>();
57 static {
58 WHITELIST.add("outer");
59 WHITELIST.add("inner");
60 WHITELIST.add("perimeter");
61 WHITELIST.add("edge");
62 WHITELIST.add("outline");
63 }
64
65 /**
66 * Constructor
67 */
68 public UntaggedWay() {
69 super(tr("Untagged, empty and one node ways"),
70 tr("This test checks for untagged, empty and one node ways."));
71 }
72
73 @Override
74 public void visit(Way w) {
75 if (!w.isUsable())
76 return;
77
78 Map<String, String> tags = w.getKeys();
79 if (!tags.isEmpty()) {
80 String highway = tags.get("highway");
81 if (highway != null && NAMED_WAYS.contains(highway) && !tags.containsKey("name") && !tags.containsKey("ref")
82 && !"yes".equals(tags.get("noname"))) {
83 boolean isJunction = false;
84 boolean hasName = false;
85 for (String key : w.keySet()) {
86 hasName = key.startsWith("name:") || key.endsWith("_name") || key.endsWith("_ref");
87 if (hasName) {
88 break;
89 }
90 if ("junction".equals(key)) {
91 isJunction = true;
92 break;
93 }
94 }
95
96 if (!hasName && !isJunction) {
97 errors.add(new TestError(this, Severity.WARNING, tr("Unnamed ways"), UNNAMED_WAY, w));
98 } else if (isJunction) {
99 errors.add(new TestError(this, Severity.OTHER, tr("Unnamed junction"), UNNAMED_JUNCTION, w));
100 }
101 }
102 }
103
104 if (!w.isTagged() && !waysUsedInRelations.contains(w)) {
105 if (w.hasKeys()) {
106 errors.add(new TestError(this, Severity.WARNING, tr("Untagged ways (commented)"), COMMENTED_WAY, w));
107 } else {
108 errors.add(new TestError(this, Severity.WARNING, tr("Untagged ways"), UNTAGGED_WAY, w));
109 }
110 }
111
112 if (w.getNodesCount() == 0) {
113 errors.add(new TestError(this, Severity.ERROR, tr("Empty ways"), EMPTY_WAY, w));
114 } else if (w.getNodesCount() == 1) {
115 errors.add(new TestError(this, Severity.ERROR, tr("One node ways"), ONE_NODE_WAY, w));
116 }
117 }
118
119 @Override
120 public void startTest(ProgressMonitor monitor) {
121 super.startTest(monitor);
122 waysUsedInRelations = new HashSet<>();
123 for (Relation r : Main.main.getCurrentDataSet().getRelations()) {
124 if (r.isUsable()) {
125 for (RelationMember m : r.getMembers()) {
126 if (r.isMultipolygon() || WHITELIST.contains(m.getRole())) {
127 OsmPrimitive member = m.getMember();
128 if (member instanceof Way && member.isUsable() && !member.isTagged()) {
129 waysUsedInRelations.add((Way) member);
130 }
131 }
132 }
133 }
134 }
135 }
136
137 @Override
138 public void endTest() {
139 waysUsedInRelations = null;
140 super.endTest();
141 }
142
143 @Override
144 public boolean isFixable(TestError testError) {
145 if (testError.getTester() instanceof UntaggedWay)
146 return testError.getCode() == EMPTY_WAY
147 || testError.getCode() == ONE_NODE_WAY;
148
149 return false;
150 }
151
152 @Override
153 public Command fixError(TestError testError) {
154 return deletePrimitivesIfNeeded(testError.getPrimitives());
155 }
156
157 @Override
158 public boolean isPrimitiveUsable(OsmPrimitive p) {
159 return p.isUsable();
160 }
161}
Note: See TracBrowser for help on using the repository browser.