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

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

fix #11503 - support oneway:bicycle in turn restriction test

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