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

Last change on this file since 4058 was 3673, checked in by bastiK, 13 years ago

(hopefully) fix some coding errors

  • Property svn:eol-style set to native
File size: 6.6 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.Collections;
8
9import org.openstreetmap.josm.data.osm.Node;
10import org.openstreetmap.josm.data.osm.OsmPrimitive;
11import org.openstreetmap.josm.data.osm.OsmUtils;
12import org.openstreetmap.josm.data.osm.Relation;
13import org.openstreetmap.josm.data.osm.RelationMember;
14import org.openstreetmap.josm.data.osm.Way;
15import org.openstreetmap.josm.data.validation.Severity;
16import org.openstreetmap.josm.data.validation.Test;
17import org.openstreetmap.josm.data.validation.TestError;
18
19public class TurnrestrictionTest extends Test {
20
21 protected static final int NO_VIA = 1801;
22 protected static final int NO_FROM = 1802;
23 protected static final int NO_TO = 1803;
24 protected static final int MORE_VIA = 1804;
25 protected static final int MORE_FROM = 1805;
26 protected static final int MORE_TO = 1806;
27 protected static final int UNKNOWN_ROLE = 1807;
28 protected static final int UNKNOWN_TYPE = 1808;
29 protected static final int FROM_VIA_NODE = 1809;
30 protected static final int TO_VIA_NODE = 1810;
31 protected static final int FROM_VIA_WAY = 1811;
32 protected static final int TO_VIA_WAY = 1812;
33
34 public TurnrestrictionTest() {
35 super(tr("Turnrestriction"), tr("This test checks if turnrestrictions are valid"));
36 }
37
38 @Override
39 public void visit(Relation r) {
40 if (!"restriction".equals(r.get("type")))
41 return;
42
43 Way fromWay = null;
44 Way toWay = null;
45 OsmPrimitive via = null;
46
47 boolean morefrom = false;
48 boolean moreto = false;
49 boolean morevia = false;
50
51 /* find the "from", "via" and "to" elements */
52 for (RelationMember m : r.getMembers()) {
53 if (m.getMember().isIncomplete())
54 return;
55
56 ArrayList<OsmPrimitive> l = new ArrayList<OsmPrimitive>();
57 l.add(r);
58 l.add(m.getMember());
59 if (m.isWay()) {
60 Way w = m.getWay();
61 if (w.getNodesCount() < 2) {
62 continue;
63 }
64
65 if ("from".equals(m.getRole())) {
66 if (fromWay != null) {
67 morefrom = true;
68 } else {
69 fromWay = w;
70 }
71 } else if ("to".equals(m.getRole())) {
72 if (toWay != null) {
73 moreto = true;
74 } else {
75 toWay = w;
76 }
77 } else if ("via".equals(m.getRole())) {
78 if (via != null) {
79 morevia = true;
80 } else {
81 via = w;
82 }
83 } else {
84 errors.add(new TestError(this, Severity.WARNING, tr("Unknown role"), UNKNOWN_ROLE,
85 l, Collections.singletonList(m)));
86 }
87 } else if (m.isNode()) {
88 Node n = m.getNode();
89 if ("via".equals(m.getRole())) {
90 if (via != null) {
91 morevia = true;
92 } else {
93 via = n;
94 }
95 } else {
96 errors.add(new TestError(this, Severity.WARNING, tr("Unknown role"), UNKNOWN_ROLE,
97 l, Collections.singletonList(m)));
98 }
99 } else {
100 errors.add(new TestError(this, Severity.WARNING, tr("Unknown member type"), UNKNOWN_TYPE,
101 l, Collections.singletonList(m)));
102 }
103 }
104 if (morefrom) {
105 errors.add(new TestError(this, Severity.ERROR, tr("More than one \"from\" way found"), MORE_FROM, r));
106 }
107 if (moreto) {
108 errors.add(new TestError(this, Severity.ERROR, tr("More than one \"to\" way found"), MORE_TO, r));
109 }
110 if (morevia) {
111 errors.add(new TestError(this, Severity.ERROR, tr("More than one \"via\" way found"), MORE_VIA, r));
112 }
113
114 if (fromWay == null) {
115 errors.add(new TestError(this, Severity.ERROR, tr("No \"from\" way found"), NO_FROM, r));
116 return;
117 }
118 if (toWay == null) {
119 errors.add(new TestError(this, Severity.ERROR, tr("No \"to\" way found"), NO_TO, r));
120 return;
121 }
122 if (via == null) {
123 errors.add(new TestError(this, Severity.ERROR, tr("No \"via\" node or way found"), NO_VIA, r));
124 return;
125 }
126
127 Node viaNode;
128 if (via instanceof Node) {
129 viaNode = (Node) via;
130 if (!fromWay.isFirstLastNode(viaNode)) {
131 errors.add(new TestError(this, Severity.ERROR,
132 tr("The \"from\" way does not start or end at a \"via\" node"), FROM_VIA_NODE, r));
133 return;
134 }
135 if (!toWay.isFirstLastNode(viaNode)) {
136 errors.add(new TestError(this, Severity.ERROR,
137 tr("The \"to\" way does not start or end at a \"via\" node"), TO_VIA_NODE, r));
138 return;
139 }
140 } else {
141 Way viaWay = (Way) via;
142 Node firstNode = viaWay.firstNode();
143 Node lastNode = viaWay.lastNode();
144 Boolean onewayvia = false;
145
146 String onewayviastr = viaWay.get("oneway");
147 if (onewayviastr != null) {
148 if ("-1".equals(onewayviastr)) {
149 onewayvia = true;
150 Node tmp = firstNode;
151 firstNode = lastNode;
152 lastNode = tmp;
153 } else {
154 onewayvia = OsmUtils.getOsmBoolean(onewayviastr);
155 if (onewayvia == null) {
156 onewayvia = false;
157 }
158 }
159 }
160
161 if (fromWay.isFirstLastNode(firstNode)) {
162 viaNode = firstNode;
163 } else if (!onewayvia && fromWay.isFirstLastNode(lastNode)) {
164 viaNode = lastNode;
165 } else {
166 errors.add(new TestError(this, Severity.ERROR,
167 tr("The \"from\" way does not start or end at a \"via\" way."), FROM_VIA_WAY, r));
168 return;
169 }
170 if (!toWay.isFirstLastNode(viaNode == firstNode ? lastNode : firstNode)) {
171 errors.add(new TestError(this, Severity.ERROR,
172 tr("The \"to\" way does not start or end at a \"via\" way."), TO_VIA_WAY, r));
173 }
174 }
175 }
176}
Note: See TracBrowser for help on using the repository browser.