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.text.MessageFormat;
|
---|
8 | import java.util.Arrays;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.HashSet;
|
---|
11 | import java.util.Set;
|
---|
12 |
|
---|
13 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
14 | import org.openstreetmap.josm.data.osm.OsmUtils;
|
---|
15 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
16 | import org.openstreetmap.josm.data.osm.Way;
|
---|
17 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
18 | import org.openstreetmap.josm.data.validation.Test;
|
---|
19 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
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 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 | * @return The test error if the way is erroneous, {@code null} otherwise
|
---|
93 | */
|
---|
94 | public final TestError getTestError(Way w) {
|
---|
95 | String value = w.get(key);
|
---|
96 | if (isValueErroneous(value)) {
|
---|
97 | String type = engMessage.contains("{0}") ? tr(engMessage, tr(value)) : tr(engMessage);
|
---|
98 | String etype = engMessage.contains("{0}") ? MessageFormat.format(engMessage, value) : engMessage;
|
---|
99 | return new TestError(UnclosedWays.this, Severity.WARNING, tr("Unclosed way"),
|
---|
100 | type, etype, code, Arrays.asList(w),
|
---|
101 | // The important parts of an unclosed way are the first and
|
---|
102 | // the last node which should be connected, therefore we highlight them
|
---|
103 | Arrays.asList(w.firstNode(), w.lastNode()));
|
---|
104 | }
|
---|
105 | return null;
|
---|
106 | }
|
---|
107 |
|
---|
108 | protected boolean isValueErroneous(String value) {
|
---|
109 | return value != null && ignore != specialValues.contains(value);
|
---|
110 | }
|
---|
111 | }
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * A check performed by UnclosedWays test where the key is treated as boolean.
|
---|
115 | * @since 6390
|
---|
116 | */
|
---|
117 | private final class UnclosedWaysBooleanCheck extends UnclosedWaysCheck {
|
---|
118 |
|
---|
119 | /**
|
---|
120 | * Constructs a new {@code UnclosedWaysBooleanCheck}.
|
---|
121 | * @param code The unique numeric code for this check
|
---|
122 | * @param key The OSM key checked
|
---|
123 | * @param engMessage The English message
|
---|
124 | */
|
---|
125 | UnclosedWaysBooleanCheck(int code, String key, String engMessage) {
|
---|
126 | super(code, key, engMessage);
|
---|
127 | }
|
---|
128 |
|
---|
129 | @Override
|
---|
130 | protected boolean isValueErroneous(String value) {
|
---|
131 | Boolean btest = OsmUtils.getOsmBoolean(value);
|
---|
132 | // Not a strict boolean comparison to handle building=house like a building=yes
|
---|
133 | return (btest != null && btest) || (btest == null && value != null);
|
---|
134 | }
|
---|
135 | }
|
---|
136 |
|
---|
137 | private final UnclosedWaysCheck[] checks = {
|
---|
138 | new UnclosedWaysCheck(1101, "natural", marktr("natural type {0}"),
|
---|
139 | new HashSet<>(Arrays.asList("cave", "coastline", "cliff", "tree_row", "ridge", "valley", "arete", "gorge"))),
|
---|
140 | new UnclosedWaysCheck(1102, "landuse", marktr("landuse type {0}")),
|
---|
141 | new UnclosedWaysCheck(1103, "amenities", marktr("amenities type {0}")),
|
---|
142 | new UnclosedWaysCheck(1104, "sport", marktr("sport type {0}"),
|
---|
143 | new HashSet<>(Arrays.asList("water_slide", "climbing"))),
|
---|
144 | new UnclosedWaysCheck(1105, "tourism", marktr("tourism type {0}"),
|
---|
145 | new HashSet<>(Arrays.asList("attraction", "artwork"))),
|
---|
146 | new UnclosedWaysCheck(1106, "shop", marktr("shop type {0}")),
|
---|
147 | new UnclosedWaysCheck(1107, "leisure", marktr("leisure type {0}"),
|
---|
148 | new HashSet<>(Arrays.asList("track", "slipway"))),
|
---|
149 | new UnclosedWaysCheck(1108, "waterway", marktr("waterway type {0}"),
|
---|
150 | new HashSet<>(Arrays.asList("riverbank")), false),
|
---|
151 | new UnclosedWaysCheck(1109, "boundary", marktr("boundary type {0}")),
|
---|
152 | new UnclosedWaysBooleanCheck(1120, "building", marktr("building")),
|
---|
153 | new UnclosedWaysBooleanCheck(1130, "area", marktr("area")),
|
---|
154 | };
|
---|
155 |
|
---|
156 | /**
|
---|
157 | * Returns the set of checked OSM keys.
|
---|
158 | * @return The set of checked OSM keys.
|
---|
159 | * @since 6390
|
---|
160 | */
|
---|
161 | public Set<String> getCheckedKeys() {
|
---|
162 | Set<String> keys = new HashSet<>();
|
---|
163 | for (UnclosedWaysCheck c : checks) {
|
---|
164 | keys.add(c.key);
|
---|
165 | }
|
---|
166 | return keys;
|
---|
167 | }
|
---|
168 |
|
---|
169 | @Override
|
---|
170 | public void visit(Way w) {
|
---|
171 |
|
---|
172 | if (!w.isUsable() || w.isArea())
|
---|
173 | return;
|
---|
174 |
|
---|
175 | for (OsmPrimitive parent: w.getReferrers()) {
|
---|
176 | if (parent instanceof Relation && ((Relation) parent).isMultipolygon())
|
---|
177 | return;
|
---|
178 | }
|
---|
179 |
|
---|
180 | for (UnclosedWaysCheck c : checks) {
|
---|
181 | TestError error = c.getTestError(w);
|
---|
182 | if (error != null) {
|
---|
183 | errors.add(error);
|
---|
184 | return;
|
---|
185 | }
|
---|
186 | }
|
---|
187 | }
|
---|
188 | }
|
---|