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

Last change on this file since 5359 was 5199, checked in by simon04, 13 years ago

fix #7546 - add validation warning Superfluous turnrestriction as "to" way is oneway

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