source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/UnclosedWays.java@ 8362

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

fix #11415 - Wrong warnings about unclosed ways

  • Property svn:eol-style set to native
File size: 7.2 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.marktr;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.text.MessageFormat;
8import java.util.Arrays;
9import java.util.Collections;
10import java.util.HashSet;
11import java.util.List;
12import java.util.Set;
13
14import org.openstreetmap.josm.data.osm.OsmPrimitive;
15import org.openstreetmap.josm.data.osm.OsmUtils;
16import org.openstreetmap.josm.data.osm.Relation;
17import org.openstreetmap.josm.data.osm.Way;
18import org.openstreetmap.josm.data.validation.Severity;
19import org.openstreetmap.josm.data.validation.Test;
20import org.openstreetmap.josm.data.validation.TestError;
21
22/**
23 * Check area type ways for errors
24 *
25 * @author stoecker
26 * @since 3669
27 */
28public class UnclosedWays extends Test {
29
30 /**
31 * Constructs a new {@code UnclosedWays} test.
32 */
33 public UnclosedWays() {
34 super(tr("Unclosed Ways"), tr("This tests if ways which should be circular are closed."));
35 }
36
37 /**
38 * A check performed by UnclosedWays test.
39 * @since 6390
40 */
41 private class UnclosedWaysCheck {
42 /** The unique numeric code for this check */
43 public final int code;
44 /** The OSM key checked */
45 public final String key;
46 /** The English message */
47 private final String engMessage;
48 /** The special values, to be ignored if ignore is set to true; to be considered only if ignore is set to false */
49 private final List<String> specialValues;
50 /** The boolean indicating if special values must be ignored or considered only */
51 private final boolean ignore;
52
53 /**
54 * Constructs a new {@code UnclosedWaysCheck}.
55 * @param code The unique numeric code for this check
56 * @param key The OSM key checked
57 * @param engMessage The English message
58 */
59 public UnclosedWaysCheck(int code, String key, String engMessage) {
60 this(code, key, engMessage, Collections.<String>emptyList());
61 }
62
63 /**
64 * Constructs a new {@code UnclosedWaysCheck}.
65 * @param code The unique numeric code for this check
66 * @param key The OSM key checked
67 * @param engMessage The English message
68 * @param ignoredValues The ignored values.
69 */
70 public UnclosedWaysCheck(int code, String key, String engMessage, List<String> ignoredValues) {
71 this(code, key, engMessage, ignoredValues, true);
72 }
73
74 /**
75 * Constructs a new {@code UnclosedWaysCheck}.
76 * @param code The unique numeric code for this check
77 * @param key The OSM key checked
78 * @param engMessage The English message
79 * @param specialValues The special values, to be ignored if ignore is set to true; to be considered only if ignore is set to false
80 * @param ignore indicates if special values must be ignored or considered only
81 */
82 public UnclosedWaysCheck(int code, String key, String engMessage, List<String> specialValues, boolean ignore) {
83 this.code = code;
84 this.key = key;
85 this.engMessage = engMessage;
86 this.specialValues = specialValues;
87 this.ignore = ignore;
88 }
89
90 /**
91 * Returns the test error of the given way, if any.
92 * @param w The way to check
93 * @return The test error if the way is erroneous, {@code null} otherwise
94 */
95 public final TestError getTestError(Way w) {
96 String value = w.get(key);
97 if (isValueErroneous(value)) {
98 String type = engMessage.contains("{0}") ? tr(engMessage, tr(value)) : tr(engMessage);
99 String etype = engMessage.contains("{0}") ? MessageFormat.format(engMessage, value) : engMessage;
100 return new TestError(UnclosedWays.this, Severity.WARNING, tr("Unclosed way"),
101 type, etype, code, Arrays.asList(w),
102 // The important parts of an unclosed way are the first and
103 // the last node which should be connected, therefore we highlight them
104 Arrays.asList(w.firstNode(), w.lastNode()));
105 }
106 return null;
107 }
108
109 protected boolean isValueErroneous(String value) {
110 return value != null && ignore != specialValues.contains(value);
111 }
112 }
113
114 /**
115 * A check performed by UnclosedWays test where the key is treated as boolean.
116 * @since 6390
117 */
118 private final class UnclosedWaysBooleanCheck extends UnclosedWaysCheck {
119
120 /**
121 * Constructs a new {@code UnclosedWaysBooleanCheck}.
122 * @param code The unique numeric code for this check
123 * @param key The OSM key checked
124 * @param engMessage The English message
125 */
126 public UnclosedWaysBooleanCheck(int code, String key, String engMessage) {
127 super(code, key, engMessage);
128 }
129
130 @Override
131 protected boolean isValueErroneous(String value) {
132 Boolean btest = OsmUtils.getOsmBoolean(value);
133 // Not a strict boolean comparison to handle building=house like a building=yes
134 return (btest != null && btest) || (btest == null && value != null);
135 }
136 }
137
138 private final UnclosedWaysCheck[] checks = {
139 new UnclosedWaysCheck(1101, "natural", marktr("natural type {0}"), Arrays.asList("coastline", "cliff", "tree_row", "ridge", "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}"), Arrays.asList("water_slide", "climbing")),
143 new UnclosedWaysCheck(1105, "tourism", marktr("tourism type {0}"), Arrays.asList("attraction", "artwork")),
144 new UnclosedWaysCheck(1106, "shop", marktr("shop type {0}")),
145 new UnclosedWaysCheck(1107, "leisure", marktr("leisure type {0}"), Arrays.asList("track", "slipway")),
146 new UnclosedWaysCheck(1108, "waterway", marktr("waterway type {0}"), Arrays.asList("riverbank"), false),
147 new UnclosedWaysCheck(1109, "boundary", marktr("boundary type {0}")),
148 new UnclosedWaysBooleanCheck(1120, "building", marktr("building")),
149 new UnclosedWaysBooleanCheck(1130, "area", marktr("area")),
150 };
151
152 /**
153 * Returns the set of checked OSM keys.
154 * @return The set of checked OSM keys.
155 * @since 6390
156 */
157 public Set<String> getCheckedKeys() {
158 Set<String> keys = new HashSet<>();
159 for (UnclosedWaysCheck c : checks) {
160 keys.add(c.key);
161 }
162 return keys;
163 }
164
165 @Override
166 public void visit(Way w) {
167
168 if (!w.isUsable() || w.isArea())
169 return;
170
171 for (OsmPrimitive parent: w.getReferrers()) {
172 if (parent instanceof Relation && ((Relation)parent).isMultipolygon())
173 return;
174 }
175
176 for (UnclosedWaysCheck c : checks) {
177 TestError error = c.getTestError(w);
178 if (error != null) {
179 errors.add(error);
180 return;
181 }
182 }
183 }
184}
Note: See TracBrowser for help on using the repository browser.