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

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

fix #10223 - false positive for "Missing pedestrian crossing information"

File size: 10.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.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("crossing", "no")
89 && !(n.hasKey("crossing") && (n.hasTag("highway", "crossing") || n.hasTag("highway", "traffic_signals")))
90 && n.isReferredByWays(2)) {
91 testMissingPedestrianCrossing(n);
92 }
93 if (n.hasKey("source:maxspeed")) {
94 // Check maxspeed but not context against highway for nodes as maxspeed is not set on highways here but on signs, speed cameras, etc.
95 testSourceMaxspeed(n, false);
96 }
97 }
98 }
99
100 @Override
101 public void visit(Way w) {
102 if (w.isUsable()) {
103 if (w.hasKey("highway") && CLASSIFIED_HIGHWAYS.contains(w.get("highway")) && w.hasKey("junction") && "roundabout".equals(w.get("junction"))) {
104 testWrongRoundabout(w);
105 }
106 if (w.hasKey("source:maxspeed")) {
107 // Check maxspeed, including context against highway
108 testSourceMaxspeed(w, true);
109 }
110 testHighwayLink(w);
111 }
112 }
113
114 private void testWrongRoundabout(Way w) {
115 Map<String, List<Way>> map = new HashMap<>();
116 // Count all highways (per type) connected to this roundabout, except links
117 // As roundabouts are closed ways, take care of not processing the first/last node twice
118 for (Node n : new HashSet<>(w.getNodes())) {
119 for (Way h : Utils.filteredCollection(n.getReferrers(), Way.class)) {
120 String value = h.get("highway");
121 if (h != w && value != null && !value.endsWith("_link")) {
122 List<Way> list = map.get(value);
123 if (list == null) {
124 map.put(value, list = new ArrayList<>());
125 }
126 list.add(h);
127 }
128 }
129 }
130 // The roundabout should carry the highway tag of its two biggest highways
131 for (String s : CLASSIFIED_HIGHWAYS) {
132 List<Way> list = map.get(s);
133 if (list != null && list.size() >= 2) {
134 // Except when a single road is connected, but with two oneway segments
135 Boolean oneway1 = OsmUtils.getOsmBoolean(list.get(0).get("oneway"));
136 Boolean oneway2 = OsmUtils.getOsmBoolean(list.get(1).get("oneway"));
137 if (list.size() > 2 || oneway1 == null || oneway2 == null || !oneway1 || !oneway2) {
138 // Error when the highway tags do not match
139 if (!w.get("highway").equals(s)) {
140 errors.add(new WrongRoundaboutHighway(w, s));
141 }
142 break;
143 }
144 }
145 }
146 }
147
148 public static boolean isHighwayLinkOkay(final Way way) {
149 final String highway = way.get("highway");
150 if (highway == null || !highway.endsWith("_link")) {
151 return true;
152 }
153
154 final HashSet<OsmPrimitive> referrers = new HashSet<>();
155
156 if (way.isClosed()) {
157 // for closed way we need to check all adjacent ways
158 for (Node n: way.getNodes()) {
159 referrers.addAll(n.getReferrers());
160 }
161 } else {
162 referrers.addAll(way.firstNode().getReferrers());
163 referrers.addAll(way.lastNode().getReferrers());
164 }
165
166 return Utils.exists(Utils.filteredCollection(referrers, Way.class), new Predicate<Way>() {
167 @Override
168 public boolean evaluate(final Way otherWay) {
169 return !way.equals(otherWay) && otherWay.hasTag("highway", highway, highway.replaceAll("_link$", ""));
170 }
171 });
172 }
173
174 private void testHighwayLink(final Way way) {
175 if (!isHighwayLinkOkay(way)) {
176 errors.add(new TestError(this, Severity.WARNING,
177 tr("Highway link is not linked to adequate highway/link"), SOURCE_WRONG_LINK, way));
178 }
179 }
180
181 private void testMissingPedestrianCrossing(Node n) {
182 leftByPedestrians = false;
183 leftByCyclists = false;
184 leftByCars = false;
185 pedestrianWays = 0;
186 cyclistWays = 0;
187 carsWays = 0;
188
189 for (Way w : OsmPrimitive.getFilteredList(n.getReferrers(), Way.class)) {
190 String highway = w.get("highway");
191 if (highway != null) {
192 if ("footway".equals(highway) || "path".equals(highway)) {
193 handlePedestrianWay(n, w);
194 if (w.hasTag("bicycle", "yes", "designated")) {
195 handleCyclistWay(n, w);
196 }
197 } else if ("cycleway".equals(highway)) {
198 handleCyclistWay(n, w);
199 if (w.hasTag("foot", "yes", "designated")) {
200 handlePedestrianWay(n, w);
201 }
202 } else if (CLASSIFIED_HIGHWAYS.contains(highway)) {
203 // Only look at classified highways for now:
204 // - service highways support is TBD (see #9141 comments)
205 // - roads should be determined first. Another warning is raised anyway
206 handleCarWay(n, w);
207 }
208 if ((leftByPedestrians || leftByCyclists) && leftByCars) {
209 errors.add(new TestError(this, Severity.OTHER, tr("Missing pedestrian crossing information"), MISSING_PEDESTRIAN_CROSSING, n));
210 return;
211 }
212 }
213 }
214 }
215
216 private void handleCarWay(Node n, Way w) {
217 carsWays++;
218 if (!w.isFirstLastNode(n) || carsWays > 1) {
219 leftByCars = true;
220 }
221 }
222
223 private void handleCyclistWay(Node n, Way w) {
224 cyclistWays++;
225 if (!w.isFirstLastNode(n) || cyclistWays > 1) {
226 leftByCyclists = true;
227 }
228 }
229
230 private void handlePedestrianWay(Node n, Way w) {
231 pedestrianWays++;
232 if (!w.isFirstLastNode(n) || pedestrianWays > 1) {
233 leftByPedestrians = true;
234 }
235 }
236
237 private void testSourceMaxspeed(OsmPrimitive p, boolean testContextHighway) {
238 String value = p.get("source:maxspeed");
239 if (value.matches("[A-Z]{2}:.+")) {
240 int index = value.indexOf(':');
241 // Check country
242 String country = value.substring(0, index);
243 if (!ISO_COUNTRIES.contains(country)) {
244 errors.add(new TestError(this, Severity.WARNING, tr("Unknown country code: {0}", country), SOURCE_MAXSPEED_UNKNOWN_COUNTRY_CODE, p));
245 }
246 // Check context
247 String context = value.substring(index+1);
248 if (!KNOWN_SOURCE_MAXSPEED_CONTEXTS.contains(context)) {
249 errors.add(new TestError(this, Severity.WARNING, tr("Unknown source:maxspeed context: {0}", context), SOURCE_MAXSPEED_UNKNOWN_CONTEXT, p));
250 }
251 // TODO: Check coherence of context against maxspeed
252 // TODO: Check coherence of context against highway
253 }
254 }
255
256 @Override
257 public boolean isFixable(TestError testError) {
258 return testError instanceof WrongRoundaboutHighway;
259 }
260
261 @Override
262 public Command fixError(TestError testError) {
263 if (testError instanceof WrongRoundaboutHighway) {
264 // primitives list can be empty if all primitives have been purged
265 Iterator<? extends OsmPrimitive> it = testError.getPrimitives().iterator();
266 if (it.hasNext()) {
267 return new ChangePropertyCommand(it.next(),
268 "highway", ((WrongRoundaboutHighway) testError).correctValue);
269 }
270 }
271 return null;
272 }
273}
Note: See TracBrowser for help on using the repository browser.