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.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collections;
|
---|
9 | import java.util.HashSet;
|
---|
10 | import java.util.Set;
|
---|
11 |
|
---|
12 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
14 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
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.mappaint.ElemStyles;
|
---|
20 |
|
---|
21 | /**
|
---|
22 | * Check area type ways for errors
|
---|
23 | *
|
---|
24 | * @author stoecker
|
---|
25 | * @since 3669
|
---|
26 | */
|
---|
27 | public class UnclosedWays extends Test {
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Constructs a new {@code UnclosedWays} test.
|
---|
31 | */
|
---|
32 | public UnclosedWays() {
|
---|
33 | super(tr("Unclosed Ways"), tr("This tests if ways which should be circular are closed."));
|
---|
34 | }
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * A check performed by UnclosedWays test.
|
---|
38 | * @since 6390
|
---|
39 | */
|
---|
40 | private static class UnclosedWaysCheck {
|
---|
41 | /** The unique numeric code for this check */
|
---|
42 | public final int code;
|
---|
43 | /** The OSM key checked */
|
---|
44 | public final String key;
|
---|
45 | /** The English message */
|
---|
46 | private final String engMessage;
|
---|
47 | /** The special values, to be ignored if ignore is set to true; to be considered only if ignore is set to false */
|
---|
48 | private final Set<String> specialValues;
|
---|
49 | /** The boolean indicating if special values must be ignored or considered only */
|
---|
50 | private final boolean ignore;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * Constructs a new {@code UnclosedWaysCheck}.
|
---|
54 | * @param code The unique numeric code for this check
|
---|
55 | * @param key The OSM key checked
|
---|
56 | * @param engMessage The English message
|
---|
57 | */
|
---|
58 | UnclosedWaysCheck(int code, String key, String engMessage) {
|
---|
59 | this(code, key, engMessage, Collections.<String>emptySet());
|
---|
60 | }
|
---|
61 |
|
---|
62 | /**
|
---|
63 | * Constructs a new {@code UnclosedWaysCheck}.
|
---|
64 | * @param code The unique numeric code for this check
|
---|
65 | * @param key The OSM key checked
|
---|
66 | * @param engMessage The English message
|
---|
67 | * @param ignoredValues The ignored values.
|
---|
68 | */
|
---|
69 | UnclosedWaysCheck(int code, String key, String engMessage, Set<String> ignoredValues) {
|
---|
70 | this(code, key, engMessage, ignoredValues, true);
|
---|
71 | }
|
---|
72 |
|
---|
73 | /**
|
---|
74 | * Constructs a new {@code UnclosedWaysCheck}.
|
---|
75 | * @param code The unique numeric code for this check
|
---|
76 | * @param key The OSM key checked
|
---|
77 | * @param engMessage The English message
|
---|
78 | * @param specialValues The special values, to be ignored if ignore is set to true; to be considered only if ignore is set to false
|
---|
79 | * @param ignore indicates if special values must be ignored or considered only
|
---|
80 | */
|
---|
81 | UnclosedWaysCheck(int code, String key, String engMessage, Set<String> specialValues, boolean ignore) {
|
---|
82 | this.code = code;
|
---|
83 | this.key = key;
|
---|
84 | this.engMessage = engMessage;
|
---|
85 | this.specialValues = specialValues;
|
---|
86 | this.ignore = ignore;
|
---|
87 | }
|
---|
88 |
|
---|
89 | /**
|
---|
90 | * Returns the test error of the given way, if any.
|
---|
91 | * @param w The way to check
|
---|
92 | * @param test parent test
|
---|
93 | * @return The test error if the way is erroneous, {@code null} otherwise
|
---|
94 | */
|
---|
95 | public final TestError getTestError(Way w, UnclosedWays test) {
|
---|
96 | String value = w.get(key);
|
---|
97 | if (isValueErroneous(value)) {
|
---|
98 | return TestError.builder(test, Severity.WARNING, code)
|
---|
99 | .message(tr("Unclosed way"), engMessage, engMessage.contains("{0}") ? new Object[]{value} : new Object[]{})
|
---|
100 | .primitives(w)
|
---|
101 | .highlight(Arrays.asList(w.firstNode(), w.lastNode()))
|
---|
102 | .build();
|
---|
103 | }
|
---|
104 | return null;
|
---|
105 | }
|
---|
106 |
|
---|
107 | protected boolean isValueErroneous(String value) {
|
---|
108 | return value != null && ignore != specialValues.contains(value);
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * A check performed by UnclosedWays test where the key is treated as boolean.
|
---|
114 | * @since 6390
|
---|
115 | */
|
---|
116 | private static final class UnclosedWaysBooleanCheck extends UnclosedWaysCheck {
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Constructs a new {@code UnclosedWaysBooleanCheck}.
|
---|
120 | * @param code The unique numeric code for this check
|
---|
121 | * @param key The OSM key checked
|
---|
122 | * @param engMessage The English message
|
---|
123 | */
|
---|
124 | UnclosedWaysBooleanCheck(int code, String key, String engMessage) {
|
---|
125 | super(code, key, engMessage);
|
---|
126 | }
|
---|
127 |
|
---|
128 | @Override
|
---|
129 | protected boolean isValueErroneous(String value) {
|
---|
130 | Boolean btest = OsmUtils.getOsmBoolean(value);
|
---|
131 | // Not a strict boolean comparison to handle building=house like a building=yes
|
---|
132 | return (btest != null && btest) || (btest == null && value != null);
|
---|
133 | }
|
---|
134 | }
|
---|
135 |
|
---|
136 | private static final UnclosedWaysCheck[] checks = {
|
---|
137 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
138 | // list contains natural tag allowed on unclosed ways as well as those only allowed on nodes to avoid
|
---|
139 | // duplicate warnings
|
---|
140 | new UnclosedWaysCheck(1101, "natural", marktr("natural type {0}"),
|
---|
141 | new HashSet<>(Arrays.asList("arete", "bay", "cave", "cliff", "coastline", "gorge", "gully", "peak",
|
---|
142 | "ridge", "saddle", "strait", "tree", "tree_row", "valley", "volcano"))),
|
---|
143 |
|
---|
144 | new UnclosedWaysCheck(1102, "landuse", marktr("landuse type {0}")),
|
---|
145 | new UnclosedWaysCheck(1103, "amenities", marktr("amenities type {0}")),
|
---|
146 | new UnclosedWaysCheck(1104, "sport", marktr("sport type {0}"),
|
---|
147 | new HashSet<>(Arrays.asList("water_slide", "climbing", "skiing", "toboggan", "bobsleigh", "karting"))),
|
---|
148 | new UnclosedWaysCheck(1105, "tourism", marktr("tourism type {0}"),
|
---|
149 | new HashSet<>(Arrays.asList("attraction", "artwork"))),
|
---|
150 | new UnclosedWaysCheck(1106, "shop", marktr("shop type {0}")),
|
---|
151 | new UnclosedWaysCheck(1107, "leisure", marktr("leisure type {0}"),
|
---|
152 | new HashSet<>(Arrays.asList("track", "slipway"))),
|
---|
153 | new UnclosedWaysCheck(1108, "waterway", marktr("waterway type {0}"),
|
---|
154 | new HashSet<>(Arrays.asList("riverbank")), false),
|
---|
155 | new UnclosedWaysCheck(1109, "boundary", marktr("boundary type {0}")),
|
---|
156 | new UnclosedWaysCheck(1110, "area:highway", marktr("area:highway type {0}")),
|
---|
157 | new UnclosedWaysBooleanCheck(1120, "building", marktr("building")),
|
---|
158 | new UnclosedWaysBooleanCheck(1130, "area", marktr("area")),
|
---|
159 | // 1131: Area style way is not closed
|
---|
160 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
161 | };
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Returns the set of checked OSM keys.
|
---|
165 | * @return The set of checked OSM keys.
|
---|
166 | * @since 6390
|
---|
167 | */
|
---|
168 | public Set<String> getCheckedKeys() {
|
---|
169 | Set<String> keys = new HashSet<>();
|
---|
170 | for (UnclosedWaysCheck c : checks) {
|
---|
171 | keys.add(c.key);
|
---|
172 | }
|
---|
173 | return keys;
|
---|
174 | }
|
---|
175 |
|
---|
176 | @Override
|
---|
177 | public void visit(Way w) {
|
---|
178 |
|
---|
179 | if (!w.isUsable() || w.isArea())
|
---|
180 | return;
|
---|
181 |
|
---|
182 | for (OsmPrimitive parent: w.getReferrers()) {
|
---|
183 | if (parent instanceof Relation && ((Relation) parent).isMultipolygon())
|
---|
184 | return;
|
---|
185 | }
|
---|
186 |
|
---|
187 | for (UnclosedWaysCheck c : checks) {
|
---|
188 | TestError error = c.getTestError(w, this);
|
---|
189 | if (error != null) {
|
---|
190 | errors.add(error);
|
---|
191 | return;
|
---|
192 | }
|
---|
193 | }
|
---|
194 | // code 1131: other area style ways
|
---|
195 | if (ElemStyles.hasOnlyAreaElements(w) && !w.getNodes().isEmpty()) {
|
---|
196 | errors.add(TestError.builder(this, Severity.WARNING, 1131)
|
---|
197 | .message(tr("Unclosed way"), marktr("Area style way is not closed"), new Object())
|
---|
198 | .primitives(w)
|
---|
199 | .highlight(Arrays.asList(w.firstNode(), w.lastNode()))
|
---|
200 | .build());
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|