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

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

checkstyle - fix CommentsIndentation errors

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