source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/UnconnectedWays.java@ 6763

Last change on this file since 6763 was 6763, checked in by simon04, 10 years ago

fix #9617 - False positive in unconnected power ways

  • Property svn:eol-style set to native
File size: 16.9 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.awt.geom.Area;
7import java.awt.geom.Line2D;
8import java.awt.geom.Point2D;
9import java.util.ArrayList;
10import java.util.Arrays;
11import java.util.Collection;
12import java.util.Collections;
13import java.util.HashMap;
14import java.util.HashSet;
15import java.util.List;
16import java.util.Map;
17import java.util.Set;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.data.coor.EastNorth;
21import org.openstreetmap.josm.data.coor.LatLon;
22import org.openstreetmap.josm.data.osm.BBox;
23import org.openstreetmap.josm.data.osm.Node;
24import org.openstreetmap.josm.data.osm.OsmPrimitive;
25import org.openstreetmap.josm.data.osm.QuadBuckets;
26import org.openstreetmap.josm.data.osm.Way;
27import org.openstreetmap.josm.data.validation.Severity;
28import org.openstreetmap.josm.data.validation.Test;
29import org.openstreetmap.josm.data.validation.TestError;
30import org.openstreetmap.josm.gui.preferences.validator.ValidatorPreference;
31import org.openstreetmap.josm.gui.progress.ProgressMonitor;
32
33/**
34 * Tests if there are segments that crosses in the same layer.
35 * <br/>
36 * This class is abstract since highway/railway/waterway/… ways must be handled separately.
37 * An actual implementation must override {@link #isPrimitiveUsable(OsmPrimitive)}
38 * to denote which kind of primitives can be handled.
39 *
40 * @author frsantos
41 */
42public abstract class UnconnectedWays extends Test {
43
44 /**
45 * Unconnected highways test.
46 */
47 public static class UnconnectedHighways extends UnconnectedWays {
48
49 /**
50 * Constructs a new {@code UnconnectedHighways} test.
51 */
52 public UnconnectedHighways() {
53 super(tr("Unconnected highways"));
54 }
55
56 @Override
57 public boolean isPrimitiveUsable(OsmPrimitive p) {
58 return super.isPrimitiveUsable(p) && p.hasKey("highway");
59 }
60 }
61
62 /**
63 * Unconnected railways test.
64 */
65 public static class UnconnectedRailways extends UnconnectedWays {
66
67 /**
68 * Constructs a new {@code UnconnectedRailways} test.
69 */
70 public UnconnectedRailways() {
71 super(tr("Unconnected railways"));
72 }
73
74 @Override
75 public boolean isPrimitiveUsable(OsmPrimitive p) {
76 return super.isPrimitiveUsable(p) && p.hasKey("railway");
77 }
78 }
79
80 /**
81 * Unconnected waterways test.
82 */
83 public static class UnconnectedWaterways extends UnconnectedWays {
84
85 /**
86 * Constructs a new {@code UnconnectedWaterways} test.
87 */
88 public UnconnectedWaterways() {
89 super(tr("Unconnected waterways"));
90 }
91
92 @Override
93 public boolean isPrimitiveUsable(OsmPrimitive p) {
94 return super.isPrimitiveUsable(p) && p.hasKey("waterway");
95 }
96 }
97
98 /**
99 * Unconnected natural/landuse test.
100 */
101 public static class UnconnectedNaturalOrLanduse extends UnconnectedWays {
102
103 /**
104 * Constructs a new {@code UnconnectedNaturalOrLanduse} test.
105 */
106 public UnconnectedNaturalOrLanduse() {
107 super(tr("Unconnected natural lands and landuses"));
108 }
109
110 @Override
111 public boolean isPrimitiveUsable(OsmPrimitive p) {
112 return super.isPrimitiveUsable(p) && (p.hasKey("natural") || p.hasKey("landuse"));
113 }
114 }
115
116 /**
117 * Unconnected power ways test.
118 */
119 public static class UnconnectedPower extends UnconnectedWays {
120
121 /**
122 * Constructs a new {@code UnconnectedPower} test.
123 */
124 public UnconnectedPower() {
125 super(tr("Unconnected power ways"));
126 }
127
128 @Override
129 public boolean isPrimitiveUsable(OsmPrimitive p) {
130 return super.isPrimitiveUsable(p) && p.hasTag("power", "line", "minor_line", "cable");
131 }
132 }
133
134 protected static final int UNCONNECTED_WAYS = 1301;
135 protected static final String PREFIX = ValidatorPreference.PREFIX + "." + UnconnectedWays.class.getSimpleName();
136
137 private Set<MyWaySegment> ways;
138 private QuadBuckets<Node> endnodes; // nodes at end of way
139 private QuadBuckets<Node> endnodes_highway; // nodes at end of way
140 private QuadBuckets<Node> middlenodes; // nodes in middle of way
141 private Set<Node> othernodes; // nodes appearing at least twice
142 private Area dsArea;
143
144 private double mindist;
145 private double minmiddledist;
146
147 /**
148 * Constructs a new {@code UnconnectedWays} test.
149 * @param title The test title
150 * @since 6691
151 */
152 public UnconnectedWays(String title) {
153 super(title, tr("This test checks if a way has an endpoint very near to another way."));
154 }
155
156 @Override
157 public void startTest(ProgressMonitor monitor) {
158 super.startTest(monitor);
159 ways = new HashSet<MyWaySegment>();
160 endnodes = new QuadBuckets<Node>();
161 endnodes_highway = new QuadBuckets<Node>();
162 middlenodes = new QuadBuckets<Node>();
163 othernodes = new HashSet<Node>();
164 mindist = Main.pref.getDouble(PREFIX + ".node_way_distance", 10.0);
165 minmiddledist = Main.pref.getDouble(PREFIX + ".way_way_distance", 0.0);
166 dsArea = Main.main == null || !Main.main.hasEditLayer() ? null : Main.main.getCurrentDataSet().getDataSourceArea();
167 }
168
169 @Override
170 public void endTest() {
171 Map<Node, Way> map = new HashMap<Node, Way>();
172 for (int iter = 0; iter < 1; iter++) {
173 for (MyWaySegment s : ways) {
174 Collection<Node> nearbyNodes = s.nearbyNodes(mindist);
175 for (Node en : nearbyNodes) {
176 if (en == null || !s.highway || !endnodes_highway.contains(en)) {
177 continue;
178 }
179 if (en.hasTag("highway", "turning_circle", "bus_stop")
180 || en.hasTag("amenity", "parking_entrance")
181 || en.hasTag("railway", "buffer_stop")
182 || en.isKeyTrue("noexit")
183 || en.hasKey("entrance")
184 || en.hasKey("barrier")) {
185 continue;
186 }
187 // to handle intersections of 't' shapes and similar
188 if (en.isConnectedTo(s.w.getNodes(), 3 /* hops */, null)) {
189 continue;
190 }
191 map.put(en, s.w);
192 }
193 if(isCanceled())
194 return;
195 }
196 }
197 for (Map.Entry<Node, Way> error : map.entrySet()) {
198 errors.add(new TestError(this, Severity.WARNING,
199 tr("Way end node near other highway"),
200 UNCONNECTED_WAYS,
201 Arrays.asList(error.getKey(), error.getValue()),
202 Arrays.asList(error.getKey())));
203 }
204 map.clear();
205 for (MyWaySegment s : ways) {
206 if(isCanceled())
207 return;
208 for (Node en : s.nearbyNodes(mindist)) {
209 if (en.isConnectedTo(s.w.getNodes(), 3 /* hops */, null)) {
210 continue;
211 }
212 if (endnodes_highway.contains(en) && !s.highway && !s.w.concernsArea()) {
213 map.put(en, s.w);
214 } else if (endnodes.contains(en) && !s.w.concernsArea()) {
215 map.put(en, s.w);
216 }
217 }
218 }
219 for (Map.Entry<Node, Way> error : map.entrySet()) {
220 errors.add(new TestError(this, Severity.WARNING,
221 tr("Way end node near other way"),
222 UNCONNECTED_WAYS,
223 Arrays.asList(error.getKey(), error.getValue()),
224 Arrays.asList(error.getKey())));
225 }
226 /* the following two use a shorter distance */
227 if (minmiddledist > 0.0) {
228 map.clear();
229 for (MyWaySegment s : ways) {
230 if(isCanceled())
231 return;
232 for (Node en : s.nearbyNodes(minmiddledist)) {
233 if (en.isConnectedTo(s.w.getNodes(), 3 /* hops */, null)) {
234 continue;
235 }
236 if (!middlenodes.contains(en)) {
237 continue;
238 }
239 map.put(en, s.w);
240 }
241 }
242 for (Map.Entry<Node, Way> error : map.entrySet()) {
243 errors.add(new TestError(this, Severity.OTHER,
244 tr("Way node near other way"),
245 UNCONNECTED_WAYS,
246 Arrays.asList(error.getKey(), error.getValue()),
247 Arrays.asList(error.getKey())));
248 }
249 map.clear();
250 for (MyWaySegment s : ways) {
251 for (Node en : s.nearbyNodes(minmiddledist)) {
252 if (en.isConnectedTo(s.w.getNodes(), 3 /* hops */, null)) {
253 continue;
254 }
255 if(isCanceled())
256 return;
257 if (!othernodes.contains(en)) {
258 continue;
259 }
260 map.put(en, s.w);
261 }
262 }
263 for (Map.Entry<Node, Way> error : map.entrySet()) {
264 errors.add(new TestError(this, Severity.OTHER,
265 tr("Connected way end node near other way"),
266 UNCONNECTED_WAYS,
267 Arrays.asList(error.getKey(), error.getValue()),
268 Arrays.asList(error.getKey())));
269 }
270 }
271 ways = null;
272 endnodes = null;
273 endnodes_highway = null;
274 middlenodes = null;
275 othernodes = null;
276 dsArea = null;
277 super.endTest();
278 }
279
280 private class MyWaySegment {
281 private final Line2D line;
282 public final Way w;
283 public final boolean isAbandoned;
284 public final boolean isBoundary;
285 public final boolean highway;
286 private final double len;
287 private Set<Node> nearbyNodeCache;
288 double nearbyNodeCacheDist = -1.0;
289 final Node n1;
290 final Node n2;
291
292 public MyWaySegment(Way w, Node n1, Node n2) {
293 this.w = w;
294 String railway = w.get("railway");
295 String highway = w.get("highway");
296 this.isAbandoned = "abandoned".equals(railway) || w.isKeyTrue("disused");
297 this.highway = (highway != null || railway != null) && !isAbandoned;
298 this.isBoundary = !this.highway && "administrative".equals(w.get("boundary"));
299 line = new Line2D.Double(n1.getEastNorth().east(), n1.getEastNorth().north(),
300 n2.getEastNorth().east(), n2.getEastNorth().north());
301 len = line.getP1().distance(line.getP2());
302 this.n1 = n1;
303 this.n2 = n2;
304 }
305
306 public boolean nearby(Node n, double dist) {
307 if (w == null) {
308 Main.debug("way null");
309 return false;
310 }
311 if (w.containsNode(n))
312 return false;
313 if (n.isKeyTrue("noexit"))
314 return false;
315 EastNorth coord = n.getEastNorth();
316 if (coord == null)
317 return false;
318 Point2D p = new Point2D.Double(coord.east(), coord.north());
319 if (line.getP1().distance(p) > len+dist)
320 return false;
321 if (line.getP2().distance(p) > len+dist)
322 return false;
323 return line.ptSegDist(p) < dist;
324 }
325
326 public List<LatLon> getBounds(double fudge) {
327 double x1 = n1.getCoor().lon();
328 double x2 = n2.getCoor().lon();
329 if (x1 > x2) {
330 double tmpx = x1;
331 x1 = x2;
332 x2 = tmpx;
333 }
334 double y1 = n1.getCoor().lat();
335 double y2 = n2.getCoor().lat();
336 if (y1 > y2) {
337 double tmpy = y1;
338 y1 = y2;
339 y2 = tmpy;
340 }
341 LatLon topLeft = new LatLon(y2+fudge, x1-fudge);
342 LatLon botRight = new LatLon(y1-fudge, x2+fudge);
343 List<LatLon> ret = new ArrayList<LatLon>(2);
344 ret.add(topLeft);
345 ret.add(botRight);
346 return ret;
347 }
348
349 public Collection<Node> nearbyNodes(double dist) {
350 // If you're looking for nodes that are farther
351 // away that we looked for last time, the cached
352 // result is no good
353 if (dist > nearbyNodeCacheDist) {
354 nearbyNodeCache = null;
355 }
356 if (nearbyNodeCache != null) {
357 // If we've cached an area greater than the
358 // one now being asked for...
359 if (nearbyNodeCacheDist > dist) {
360 // Used the cached result and trim out
361 // the nodes that are not in the smaller
362 // area, but keep the old larger cache.
363 Set<Node> trimmed = new HashSet<Node>(nearbyNodeCache);
364 Set<Node> initial = new HashSet<Node>(nearbyNodeCache);
365 for (Node n : initial) {
366 if (!nearby(n, dist)) {
367 trimmed.remove(n);
368 }
369 }
370 return trimmed;
371 }
372 return nearbyNodeCache;
373 }
374 /*
375 * We know that any point near the line must be at
376 * least as close as the other end of the line, plus
377 * a little fudge for the distance away ('dist').
378 */
379
380 // This needs to be a hash set because the searches
381 // overlap a bit and can return duplicate nodes.
382 nearbyNodeCache = null;
383 List<LatLon> bounds = this.getBounds(dist);
384 List<Node> found_nodes = endnodes_highway.search(new BBox(bounds.get(0), bounds.get(1)));
385 found_nodes.addAll(endnodes.search(new BBox(bounds.get(0), bounds.get(1))));
386
387 for (Node n : found_nodes) {
388 if (!nearby(n, dist) || !n.getCoor().isIn(dsArea)) {
389 continue;
390 }
391 // It is actually very rare for us to find a node
392 // so defer as much of the work as possible, like
393 // allocating the hash set
394 if (nearbyNodeCache == null) {
395 nearbyNodeCache = new HashSet<Node>();
396 }
397 nearbyNodeCache.add(n);
398 }
399 nearbyNodeCacheDist = dist;
400 if (nearbyNodeCache == null) {
401 nearbyNodeCache = Collections.emptySet();
402 }
403 return nearbyNodeCache;
404 }
405 }
406
407 List<MyWaySegment> getWaySegments(Way w) {
408 List<MyWaySegment> ret = new ArrayList<MyWaySegment>();
409 if (!w.isUsable()
410 || w.hasKey("barrier")
411 || w.hasTag("natural", "cliff"))
412 return ret;
413
414 int size = w.getNodesCount();
415 if (size < 2)
416 return ret;
417 for (int i = 1; i < size; ++i) {
418 if(i < size-1) {
419 addNode(w.getNode(i), middlenodes);
420 }
421 Node a = w.getNode(i-1);
422 Node b = w.getNode(i);
423 if (a.isDrawable() && b.isDrawable()) {
424 MyWaySegment ws = new MyWaySegment(w, a, b);
425 if (ws.isBoundary || ws.isAbandoned) {
426 continue;
427 }
428 ret.add(ws);
429 }
430 }
431 return ret;
432 }
433
434 @Override
435 public void visit(Way w) {
436 if (w.getNodesCount() > 0 // do not consider empty ways
437 && !w.hasKey("addr:interpolation") // ignore addr:interpolation ways as they are not physical features and most of the time very near the associated highway, which is perfectly normal, see #9332
438 && !w.hasTag("highway", "platform") && !w.hasTag("railway", "platform") // similarly for public transport platforms
439 ) {
440 ways.addAll(getWaySegments(w));
441 QuadBuckets<Node> set = endnodes;
442 if (w.hasKey("highway") || w.hasKey("railway")) {
443 set = endnodes_highway;
444 }
445 addNode(w.firstNode(), set);
446 addNode(w.lastNode(), set);
447 }
448 }
449
450 private void addNode(Node n, QuadBuckets<Node> s) {
451 boolean m = middlenodes.contains(n);
452 boolean e = endnodes.contains(n);
453 boolean eh = endnodes_highway.contains(n);
454 boolean o = othernodes.contains(n);
455 if (!m && !e && !o && !eh) {
456 s.add(n);
457 } else if (!o) {
458 othernodes.add(n);
459 if (e) {
460 endnodes.remove(n);
461 } else if (eh) {
462 endnodes_highway.remove(n);
463 } else {
464 middlenodes.remove(n);
465 }
466 }
467 }
468}
Note: See TracBrowser for help on using the repository browser.