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

Last change on this file since 3669 was 3669, checked in by bastiK, 15 years ago

add validator plugin to josm core. Original author: Francisco R. Santos (frsantos); major contributions by bilbo, daeron, delta_foxtrot, imi, jttt, jrreid, gabriel, guggis, pieren, rrankin, skela, stoecker, stotz and others

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