source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/Highways.java@ 7273

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

fix #10204 - restrict roundabout test to classified highways

File size: 10.5 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.HashMap;
9import java.util.HashSet;
10import java.util.Iterator;
11import java.util.List;
12import java.util.Locale;
13import java.util.Map;
14
15import org.openstreetmap.josm.command.ChangePropertyCommand;
16import org.openstreetmap.josm.command.Command;
17import org.openstreetmap.josm.data.osm.Node;
18import org.openstreetmap.josm.data.osm.OsmPrimitive;
19import org.openstreetmap.josm.data.osm.OsmUtils;
20import org.openstreetmap.josm.data.osm.Way;
21import org.openstreetmap.josm.data.validation.Severity;
22import org.openstreetmap.josm.data.validation.Test;
23import org.openstreetmap.josm.data.validation.TestError;
24import org.openstreetmap.josm.tools.Predicate;
25import org.openstreetmap.josm.tools.Utils;
26
27/**
28 * Test that performs semantic checks on highways.
29 * @since 5902
30 */
31public class Highways extends Test {
32
33 protected static final int WRONG_ROUNDABOUT_HIGHWAY = 2701;
34 protected static final int MISSING_PEDESTRIAN_CROSSING = 2702;
35 protected static final int SOURCE_MAXSPEED_UNKNOWN_COUNTRY_CODE = 2703;
36 protected static final int SOURCE_MAXSPEED_UNKNOWN_CONTEXT = 2704;
37 protected static final int SOURCE_MAXSPEED_CONTEXT_MISMATCH_VS_MAXSPEED = 2705;
38 protected static final int SOURCE_MAXSPEED_CONTEXT_MISMATCH_VS_HIGHWAY = 2706;
39 protected static final int SOURCE_WRONG_LINK = 2707;
40
41 /**
42 * Classified highways in order of importance
43 */
44 protected static final List<String> CLASSIFIED_HIGHWAYS = Arrays.asList(
45 "motorway", "motorway_link",
46 "trunk", "trunk_link",
47 "primary", "primary_link",
48 "secondary", "secondary_link",
49 "tertiary", "tertiary_link",
50 "unclassified",
51 "residential",
52 "living_street");
53
54 protected static final List<String> KNOWN_SOURCE_MAXSPEED_CONTEXTS = Arrays.asList(
55 "urban", "rural", "zone", "zone30", "zone:30", "nsl_single", "nsl_dual", "motorway", "trunk", "living_street", "bicycle_road");
56
57 protected static final List<String> ISO_COUNTRIES = Arrays.asList(Locale.getISOCountries());
58
59 boolean leftByPedestrians = false;
60 boolean leftByCyclists = false;
61 boolean leftByCars = false;
62 int pedestrianWays = 0;
63 int cyclistWays = 0;
64 int carsWays = 0;
65
66 /**
67 * Constructs a new {@code Highways} test.
68 */
69 public Highways() {
70 super(tr("Highways"), tr("Performs semantic checks on highways."));
71 }
72
73 protected class WrongRoundaboutHighway extends TestError {
74
75 public final String correctValue;
76
77 public WrongRoundaboutHighway(Way w, String key) {
78 super(Highways.this, Severity.WARNING,
79 tr("Incorrect roundabout (highway: {0} instead of {1})", w.get("highway"), key),
80 WRONG_ROUNDABOUT_HIGHWAY, w);
81 this.correctValue = key;
82 }
83 }
84
85 @Override
86 public void visit(Node n) {
87 if (n.isUsable()) {
88 if (!n.hasTag("highway", "crossing") && !n.hasTag("crossing", "no") && n.isReferredByWays(2)) {
89 testMissingPedestrianCrossing(n);
90 }
91 if (n.hasKey("source:maxspeed")) {
92 // Check maxspeed but not context against highway for nodes as maxspeed is not set on highways here but on signs, speed cameras, etc.
93 testSourceMaxspeed(n, false);
94 }
95 }
96 }
97
98 @Override
99 public void visit(Way w) {
100 if (w.isUsable()) {
101 if (w.hasKey("highway") && CLASSIFIED_HIGHWAYS.contains(w.get("highway")) && w.hasKey("junction") && "roundabout".equals(w.get("junction"))) {
102 testWrongRoundabout(w);
103 }
104 if (w.hasKey("source:maxspeed")) {
105 // Check maxspeed, including context against highway
106 testSourceMaxspeed(w, true);
107 }
108 testHighwayLink(w);
109 }
110 }
111
112 private void testWrongRoundabout(Way w) {
113 Map<String, List<Way>> map = new HashMap<>();
114 // Count all highways (per type) connected to this roundabout, except links
115 // As roundabouts are closed ways, take care of not processing the first/last node twice
116 for (Node n : new HashSet<>(w.getNodes())) {
117 for (Way h : Utils.filteredCollection(n.getReferrers(), Way.class)) {
118 String value = h.get("highway");
119 if (h != w && value != null && !value.endsWith("_link")) {
120 List<Way> list = map.get(value);
121 if (list == null) {
122 map.put(value, list = new ArrayList<>());
123 }
124 list.add(h);
125 }
126 }
127 }
128 // The roundabout should carry the highway tag of its two biggest highways
129 for (String s : CLASSIFIED_HIGHWAYS) {
130 List<Way> list = map.get(s);
131 if (list != null && list.size() >= 2) {
132 // Except when a single road is connected, but with two oneway segments
133 Boolean oneway1 = OsmUtils.getOsmBoolean(list.get(0).get("oneway"));
134 Boolean oneway2 = OsmUtils.getOsmBoolean(list.get(1).get("oneway"));
135 if (list.size() > 2 || oneway1 == null || oneway2 == null || !oneway1 || !oneway2) {
136 // Error when the highway tags do not match
137 if (!w.get("highway").equals(s)) {
138 errors.add(new WrongRoundaboutHighway(w, s));
139 }
140 break;
141 }
142 }
143 }
144 }
145
146 public static boolean isHighwayLinkOkay(final Way way) {
147 final String highway = way.get("highway");
148 if (highway == null || !highway.endsWith("_link")) {
149 return true;
150 }
151
152 final HashSet<OsmPrimitive> referrers = new HashSet<>();
153 referrers.addAll(way.firstNode().getReferrers());
154 referrers.addAll(way.lastNode().getReferrers());
155
156 return Utils.exists(Utils.filteredCollection(referrers, Way.class), new Predicate<Way>() {
157 @Override
158 public boolean evaluate(final Way otherWay) {
159 return !way.equals(otherWay) && otherWay.hasTag("highway", highway, highway.replaceAll("_link$", ""));
160 }
161 });
162 }
163
164 private void testHighwayLink(final Way way) {
165 if (!isHighwayLinkOkay(way)) {
166 errors.add(new TestError(this, Severity.WARNING,
167 tr("Highway link is not linked to adequate highway/link"), SOURCE_WRONG_LINK, way));
168 }
169 }
170
171 private void testMissingPedestrianCrossing(Node n) {
172 leftByPedestrians = false;
173 leftByCyclists = false;
174 leftByCars = false;
175 pedestrianWays = 0;
176 cyclistWays = 0;
177 carsWays = 0;
178
179 for (Way w : OsmPrimitive.getFilteredList(n.getReferrers(), Way.class)) {
180 String highway = w.get("highway");
181 if (highway != null) {
182 if ("footway".equals(highway) || "path".equals(highway)) {
183 handlePedestrianWay(n, w);
184 if (w.hasTag("bicycle", "yes", "designated")) {
185 handleCyclistWay(n, w);
186 }
187 } else if ("cycleway".equals(highway)) {
188 handleCyclistWay(n, w);
189 if (w.hasTag("foot", "yes", "designated")) {
190 handlePedestrianWay(n, w);
191 }
192 } else if (CLASSIFIED_HIGHWAYS.contains(highway)) {
193 // Only look at classified highways for now:
194 // - service highways support is TBD (see #9141 comments)
195 // - roads should be determined first. Another warning is raised anyway
196 handleCarWay(n, w);
197 }
198 if ((leftByPedestrians || leftByCyclists) && leftByCars) {
199 errors.add(new TestError(this, Severity.OTHER, tr("Missing pedestrian crossing information"), MISSING_PEDESTRIAN_CROSSING, n));
200 return;
201 }
202 }
203 }
204 }
205
206 private void handleCarWay(Node n, Way w) {
207 carsWays++;
208 if (!w.isFirstLastNode(n) || carsWays > 1) {
209 leftByCars = true;
210 }
211 }
212
213 private void handleCyclistWay(Node n, Way w) {
214 cyclistWays++;
215 if (!w.isFirstLastNode(n) || cyclistWays > 1) {
216 leftByCyclists = true;
217 }
218 }
219
220 private void handlePedestrianWay(Node n, Way w) {
221 pedestrianWays++;
222 if (!w.isFirstLastNode(n) || pedestrianWays > 1) {
223 leftByPedestrians = true;
224 }
225 }
226
227 private void testSourceMaxspeed(OsmPrimitive p, boolean testContextHighway) {
228 String value = p.get("source:maxspeed");
229 if (value.matches("[A-Z]{2}:.+")) {
230 int index = value.indexOf(':');
231 // Check country
232 String country = value.substring(0, index);
233 if (!ISO_COUNTRIES.contains(country)) {
234 errors.add(new TestError(this, Severity.WARNING, tr("Unknown country code: {0}", country), SOURCE_MAXSPEED_UNKNOWN_COUNTRY_CODE, p));
235 }
236 // Check context
237 String context = value.substring(index+1);
238 if (!KNOWN_SOURCE_MAXSPEED_CONTEXTS.contains(context)) {
239 errors.add(new TestError(this, Severity.WARNING, tr("Unknown source:maxspeed context: {0}", context), SOURCE_MAXSPEED_UNKNOWN_CONTEXT, p));
240 }
241 // TODO: Check coherence of context against maxspeed
242 // TODO: Check coherence of context against highway
243 }
244 }
245
246 @Override
247 public boolean isFixable(TestError testError) {
248 return testError instanceof WrongRoundaboutHighway;
249 }
250
251 @Override
252 public Command fixError(TestError testError) {
253 if (testError instanceof WrongRoundaboutHighway) {
254 // primitives list can be empty if all primitives have been purged
255 Iterator<? extends OsmPrimitive> it = testError.getPrimitives().iterator();
256 if (it.hasNext()) {
257 return new ChangePropertyCommand(it.next(),
258 "highway", ((WrongRoundaboutHighway) testError).correctValue);
259 }
260 }
261 return null;
262 }
263}
Note: See TracBrowser for help on using the repository browser.