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

Last change on this file since 7509 was 7509, checked in by stoecker, 10 years ago

remove tabs

  • Property svn:eol-style set to native
File size: 5.4 KB
Line 
1// License: GPL. See LICENSE file for details.
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 boolean isRoundabout = false;
83 boolean hasName = false;
84 for (String key : w.keySet()) {
85 hasName = key.startsWith("name:") || key.endsWith("_name") || key.endsWith("_ref");
86 if (hasName) {
87 break;
88 }
89 if ("junction".equals(key)) {
90 isRoundabout = "roundabout".equals(w.get("junction"));
91 break;
92 }
93 }
94
95 if (!hasName && !isRoundabout) {
96 errors.add(new TestError(this, Severity.WARNING, tr("Unnamed ways"), UNNAMED_WAY, w));
97 } else if (isRoundabout) {
98 errors.add(new TestError(this, Severity.WARNING, tr("Unnamed junction"), UNNAMED_JUNCTION, w));
99 }
100 }
101 }
102
103 if (!w.isTagged() && !waysUsedInRelations.contains(w)) {
104 if (w.hasKeys()) {
105 errors.add(new TestError(this, Severity.WARNING, tr("Untagged ways (commented)"), COMMENTED_WAY, w));
106 } else {
107 errors.add(new TestError(this, Severity.WARNING, tr("Untagged ways"), UNTAGGED_WAY, w));
108 }
109 }
110
111 if (w.getNodesCount() == 0) {
112 errors.add(new TestError(this, Severity.ERROR, tr("Empty ways"), EMPTY_WAY, w));
113 } else if (w.getNodesCount() == 1) {
114 errors.add(new TestError(this, Severity.ERROR, tr("One node ways"), ONE_NODE_WAY, w));
115 }
116 }
117
118 @Override
119 public void startTest(ProgressMonitor monitor) {
120 super.startTest(monitor);
121 waysUsedInRelations = new HashSet<>();
122 for (Relation r : Main.main.getCurrentDataSet().getRelations()) {
123 if (r.isUsable()) {
124 for (RelationMember m : r.getMembers()) {
125 if (r.isMultipolygon() || WHITELIST.contains(m.getRole())) {
126 OsmPrimitive member = m.getMember();
127 if (member instanceof Way && member.isUsable() && !member.isTagged()) {
128 waysUsedInRelations.add((Way)member);
129 }
130 }
131 }
132 }
133 }
134 }
135
136 @Override
137 public void endTest() {
138 waysUsedInRelations = null;
139 super.endTest();
140 }
141
142 @Override
143 public boolean isFixable(TestError testError) {
144 if (testError.getTester() instanceof UntaggedWay)
145 return testError.getCode() == EMPTY_WAY
146 || testError.getCode() == ONE_NODE_WAY;
147
148 return false;
149 }
150
151 @Override
152 public Command fixError(TestError testError) {
153 return deletePrimitivesIfNeeded(testError.getPrimitives());
154 }
155
156 @Override
157 public boolean isPrimitiveUsable(OsmPrimitive p) {
158 return p.isUsable();
159 }
160}
Note: See TracBrowser for help on using the repository browser.