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.HashSet;
|
---|
7 | import java.util.Map;
|
---|
8 | import java.util.Set;
|
---|
9 |
|
---|
10 | import org.openstreetmap.josm.Main;
|
---|
11 | import org.openstreetmap.josm.command.Command;
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
14 | import org.openstreetmap.josm.data.osm.RelationMember;
|
---|
15 | import org.openstreetmap.josm.data.osm.Way;
|
---|
16 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
17 | import org.openstreetmap.josm.data.validation.Test;
|
---|
18 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
19 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Checks for untagged ways
|
---|
23 | *
|
---|
24 | * @author frsantos
|
---|
25 | */
|
---|
26 | public 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 | }
|
---|