source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/TurnrestrictionTest.java@ 14501

Last change on this file since 14501 was 14501, checked in by GerdP, 5 years ago

see #17057 : revert changes from r14494

  • Property svn:eol-style set to native
File size: 9.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.List;
8
9import org.openstreetmap.josm.data.osm.Node;
10import org.openstreetmap.josm.data.osm.OsmPrimitive;
11import org.openstreetmap.josm.data.osm.Relation;
12import org.openstreetmap.josm.data.osm.RelationMember;
13import org.openstreetmap.josm.data.osm.Way;
14import org.openstreetmap.josm.data.validation.Severity;
15import org.openstreetmap.josm.data.validation.Test;
16import org.openstreetmap.josm.data.validation.TestError;
17
18/**
19 * Checks if turnrestrictions are valid
20 * @since 3669
21 */
22public class TurnrestrictionTest extends Test {
23
24 protected static final int NO_VIA = 1801;
25 protected static final int NO_FROM = 1802;
26 protected static final int NO_TO = 1803;
27 protected static final int MORE_VIA = 1804;
28 protected static final int MORE_FROM = 1805;
29 protected static final int MORE_TO = 1806;
30 protected static final int UNKNOWN_ROLE = 1807;
31 protected static final int UNKNOWN_TYPE = 1808;
32 protected static final int FROM_VIA_NODE = 1809;
33 protected static final int TO_VIA_NODE = 1810;
34 protected static final int FROM_VIA_WAY = 1811;
35 protected static final int TO_VIA_WAY = 1812;
36 protected static final int MIX_VIA = 1813;
37 protected static final int UNCONNECTED_VIA = 1814;
38 protected static final int SUPERFLUOUS = 1815;
39 protected static final int FROM_EQUALS_TO = 1816;
40
41 /**
42 * Constructs a new {@code TurnrestrictionTest}.
43 */
44 public TurnrestrictionTest() {
45 super(tr("Turnrestrictions"), tr("This test checks if turnrestrictions are valid."));
46 }
47
48 @Override
49 public void visit(Relation r) {
50 if (!r.hasTag("type", "restriction"))
51 return;
52
53 Way fromWay = null;
54 Way toWay = null;
55 List<OsmPrimitive> via = new ArrayList<>();
56
57 boolean morefrom = false;
58 boolean moreto = false;
59 boolean morevia = false;
60 boolean mixvia = false;
61
62 /* find the "from", "via" and "to" elements */
63 for (RelationMember m : r.getMembers()) {
64 if (m.getMember().isIncomplete())
65 return;
66
67 List<OsmPrimitive> l = new ArrayList<>();
68 l.add(r);
69 l.add(m.getMember());
70 if (m.isWay()) {
71 Way w = m.getWay();
72 if (w.getNodesCount() < 2) {
73 continue;
74 }
75
76 switch (m.getRole()) {
77 case "from":
78 if (fromWay != null) {
79 morefrom = true;
80 } else {
81 fromWay = w;
82 }
83 break;
84 case "to":
85 if (toWay != null) {
86 moreto = true;
87 } else {
88 toWay = w;
89 }
90 break;
91 case "via":
92 if (!via.isEmpty() && via.get(0) instanceof Node) {
93 mixvia = true;
94 } else {
95 via.add(w);
96 }
97 break;
98 default:
99 errors.add(TestError.builder(this, Severity.WARNING, UNKNOWN_ROLE)
100 .message(tr("Unknown role"))
101 .primitives(l)
102 .highlight(m.getMember())
103 .build());
104 }
105 } else if (m.isNode()) {
106 Node n = m.getNode();
107 if ("via".equals(m.getRole())) {
108 if (!via.isEmpty()) {
109 if (via.get(0) instanceof Node) {
110 morevia = true;
111 } else {
112 mixvia = true;
113 }
114 } else {
115 via.add(n);
116 }
117 } else {
118 errors.add(TestError.builder(this, Severity.WARNING, UNKNOWN_ROLE)
119 .message(tr("Unknown role"))
120 .primitives(l)
121 .highlight(m.getMember())
122 .build());
123 }
124 } else {
125 errors.add(TestError.builder(this, Severity.WARNING, UNKNOWN_TYPE)
126 .message(tr("Unknown member type"))
127 .primitives(l)
128 .highlight(m.getMember())
129 .build());
130 }
131 }
132 if (morefrom) {
133 errors.add(TestError.builder(this, Severity.ERROR, MORE_FROM)
134 .message(tr("More than one \"from\" way found"))
135 .primitives(r)
136 .build());
137 }
138 if (moreto) {
139 errors.add(TestError.builder(this, Severity.ERROR, MORE_TO)
140 .message(tr("More than one \"to\" way found"))
141 .primitives(r)
142 .build());
143 }
144 if (morevia) {
145 errors.add(TestError.builder(this, Severity.ERROR, MORE_VIA)
146 .message(tr("More than one \"via\" node found"))
147 .primitives(r)
148 .build());
149 }
150 if (mixvia) {
151 errors.add(TestError.builder(this, Severity.ERROR, MIX_VIA)
152 .message(tr("Cannot mix node and way for role \"via\""))
153 .primitives(r)
154 .build());
155 }
156
157 if (fromWay == null) {
158 errors.add(TestError.builder(this, Severity.ERROR, NO_FROM)
159 .message(tr("No \"from\" way found"))
160 .primitives(r)
161 .build());
162 return;
163 }
164 if (toWay == null) {
165 errors.add(TestError.builder(this, Severity.ERROR, NO_TO)
166 .message(tr("No \"to\" way found"))
167 .primitives(r)
168 .build());
169 return;
170 }
171 if (fromWay.equals(toWay)) {
172 Severity severity = r.hasTag("restriction", "no_u_turn") ? Severity.OTHER : Severity.WARNING;
173 errors.add(TestError.builder(this, severity, FROM_EQUALS_TO)
174 .message(tr("\"from\" way equals \"to\" way"))
175 .primitives(r)
176 .build());
177 }
178 if (via.isEmpty()) {
179 errors.add(TestError.builder(this, Severity.ERROR, NO_VIA)
180 .message(tr("No \"via\" node or way found"))
181 .primitives(r)
182 .build());
183 return;
184 }
185
186 if (via.get(0) instanceof Node) {
187 final Node viaNode = (Node) via.get(0);
188 final Way viaPseudoWay = new Way();
189 viaPseudoWay.addNode(viaNode);
190 checkIfConnected(fromWay, viaPseudoWay,
191 tr("The \"from\" way does not start or end at a \"via\" node."), FROM_VIA_NODE);
192 if (toWay.isOneway() != 0 && viaNode.equals(toWay.lastNode(true))) {
193 errors.add(TestError.builder(this, Severity.WARNING, SUPERFLUOUS)
194 .message(tr("Superfluous turnrestriction as \"to\" way is oneway"))
195 .primitives(r)
196 .build());
197 return;
198 }
199 checkIfConnected(viaPseudoWay, toWay,
200 tr("The \"to\" way does not start or end at a \"via\" node."), TO_VIA_NODE);
201 } else {
202 // check if consecutive ways are connected: from/via[0], via[i-1]/via[i], via[last]/to
203 checkIfConnected(fromWay, (Way) via.get(0),
204 tr("The \"from\" and the first \"via\" way are not connected."), FROM_VIA_WAY);
205 if (via.size() > 1) {
206 for (int i = 1; i < via.size(); i++) {
207 Way previous = (Way) via.get(i - 1);
208 Way current = (Way) via.get(i);
209 checkIfConnected(previous, current,
210 tr("The \"via\" ways are not connected."), UNCONNECTED_VIA);
211 }
212 }
213 if (toWay.isOneway() != 0 && ((Way) via.get(via.size() - 1)).isFirstLastNode(toWay.lastNode(true))) {
214 errors.add(TestError.builder(this, Severity.WARNING, SUPERFLUOUS)
215 .message(tr("Superfluous turnrestriction as \"to\" way is oneway"))
216 .primitives(r)
217 .build());
218 return;
219 }
220 checkIfConnected((Way) via.get(via.size() - 1), toWay,
221 tr("The last \"via\" and the \"to\" way are not connected."), TO_VIA_WAY);
222 }
223 }
224
225 private static boolean isFullOneway(Way w) {
226 return w.isOneway() != 0 && !w.hasTag("oneway:bicycle", "no");
227 }
228
229 private void checkIfConnected(Way previous, Way current, String msg, int code) {
230 boolean c;
231 if (isFullOneway(previous) && isFullOneway(current)) {
232 // both oneways: end/start node must be equal
233 c = previous.lastNode(true).equals(current.firstNode(true));
234 } else if (isFullOneway(previous)) {
235 // previous way is oneway: end of previous must be start/end of current
236 c = current.isFirstLastNode(previous.lastNode(true));
237 } else if (isFullOneway(current)) {
238 // current way is oneway: start of current must be start/end of previous
239 c = previous.isFirstLastNode(current.firstNode(true));
240 } else {
241 // otherwise: start/end of previous must be start/end of current
242 c = current.isFirstLastNode(previous.firstNode()) || current.isFirstLastNode(previous.lastNode());
243 }
244 if (!c) {
245 errors.add(TestError.builder(this, Severity.ERROR, code)
246 .message(msg)
247 .primitives(previous, current)
248 .build());
249 }
250 }
251}
Note: See TracBrowser for help on using the repository browser.