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

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

fix copyright/license headers globally (missed a few files)

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