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

Last change on this file since 3703 was 3703, checked in by bastiK, 13 years ago

fixed #5703 - Redoing move after paste doesn't work correctly

  • Property svn:eol-style set to native
File size: 14.3 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.DataSet;
24import org.openstreetmap.josm.data.osm.Node;
25import org.openstreetmap.josm.data.osm.OsmUtils;
26import org.openstreetmap.josm.data.osm.QuadBuckets;
27import org.openstreetmap.josm.data.osm.Way;
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.ValidatorPreference;
32import org.openstreetmap.josm.gui.progress.ProgressMonitor;
33
34/**
35 * Tests if there are segments that crosses in the same layer
36 *
37 * @author frsantos
38 */
39public class UnconnectedWays extends Test {
40
41 protected static int UNCONNECTED_WAYS = 1301;
42 protected static final String PREFIX = ValidatorPreference.PREFIX + "." + UnconnectedWays.class.getSimpleName();
43
44 Set<MyWaySegment> ways;
45 Set<Node> endnodes; // nodes at end of way
46 Set<Node> endnodes_highway; // nodes at end of way
47 Set<Node> middlenodes; // nodes in middle of way
48 Set<Node> othernodes; // nodes appearing at least twice
49 //NodeSearchCache nodecache;
50 QuadBuckets<Node> nodecache;
51 Area ds_area;
52 DataSet ds;
53
54 double mindist;
55 double minmiddledist;
56
57 /**
58 * Constructor
59 */
60 public UnconnectedWays() {
61 super(tr("Unconnected ways."),
62 tr("This test checks if a way has an endpoint very near to another way."));
63 }
64
65 @Override
66 public void startTest(ProgressMonitor monitor) {
67 super.startTest(monitor);
68 ways = new HashSet<MyWaySegment>();
69 endnodes = new HashSet<Node>();
70 endnodes_highway = new HashSet<Node>();
71 middlenodes = new HashSet<Node>();
72 othernodes = new HashSet<Node>();
73 mindist = Main.pref.getDouble(PREFIX + ".node_way_distance", 10.0)/6378135.0;
74 minmiddledist = Main.pref.getDouble(PREFIX + ".way_way_distance", 0.0)/6378135.0;
75 this.ds = Main.main.getCurrentDataSet();
76 this.ds_area = ds.getDataSourceArea();
77 }
78
79 @Override
80 public void endTest() {
81 //Area a = Main.ds.getDataSourceArea();
82 Map<Node, Way> map = new HashMap<Node, Way>();
83 //long last = -1;
84 for (int iter = 0; iter < 1; iter++) {
85 //last = System.currentTimeMillis();
86 long last_print = -1;
87 int nr = 0;
88 Collection<MyWaySegment> tmp_ways = ways;
89 for (MyWaySegment s : tmp_ways) {
90 nr++;
91 long now = System.currentTimeMillis();
92 if (now - last_print > 200) {
93 //System.err.println("processing segment nr: " + nr + " of " + ways.size());
94 last_print = now;
95 }
96 for (Node en : s.nearbyNodes(mindist)) {
97 if (en == null || !s.highway || !endnodes_highway.contains(en)) {
98 continue;
99 }
100 if ("turning_circle".equals(en.get("highway"))
101 || "bus_stop".equals(en.get("highway"))
102 || "buffer_stop".equals(en.get("railway"))
103 || OsmUtils.isTrue(en.get("noexit"))
104 || en.hasKey("barrier")) {
105 continue;
106 }
107 // There's a small false-positive here. Imagine an intersection
108 // like a 't'. If the top part of the 't' is short enough, it
109 // will trigger the node at the very top of the 't' to be unconnected
110 // to the way that "crosses" the 't'. We should probably check that
111 // the ways to which 'en' belongs are not connected to 's.w'.
112 map.put(en, s.w);
113 }
114 }
115 //System.out.println("p1 elapsed: " + (System.currentTimeMillis()-last));
116 //last = System.currentTimeMillis();
117 }
118 for (Map.Entry<Node, Way> error : map.entrySet()) {
119 errors.add(new TestError(this, Severity.WARNING,
120 tr("Way end node near other highway"),
121 UNCONNECTED_WAYS,
122 Arrays.asList(error.getKey(), error.getValue())));
123 }
124 map.clear();
125 for (MyWaySegment s : ways) {
126 for (Node en : s.nearbyNodes(mindist)) {
127 if (endnodes_highway.contains(en) && !s.highway && !s.isArea()) {
128 map.put(en, s.w);
129 } else if (endnodes.contains(en) && !s.isArea()) {
130 map.put(en, s.w);
131 }
132 }
133 }
134 //System.out.println("p2 elapsed: " + (System.currentTimeMillis()-last));
135 //last = System.currentTimeMillis();
136 for (Map.Entry<Node, Way> error : map.entrySet()) {
137 errors.add(new TestError(this, Severity.WARNING,
138 tr("Way end node near other way"),
139 UNCONNECTED_WAYS,
140 Arrays.asList(error.getKey(), error.getValue())));
141 }
142 /* the following two use a shorter distance */
143 if (minmiddledist > 0.0) {
144 map.clear();
145 for (MyWaySegment s : ways) {
146 for (Node en : s.nearbyNodes(minmiddledist)) {
147 if (!middlenodes.contains(en)) {
148 continue;
149 }
150 map.put(en, s.w);
151 }
152 }
153 //System.out.println("p3 elapsed: " + (System.currentTimeMillis()-last));
154 //last = System.currentTimeMillis();
155 for (Map.Entry<Node, Way> error : map.entrySet()) {
156 errors.add(new TestError(this, Severity.OTHER,
157 tr("Way node near other way"),
158 UNCONNECTED_WAYS,
159 Arrays.asList(error.getKey(), error.getValue())));
160 }
161 map.clear();
162 for (MyWaySegment s : ways) {
163 for (Node en : s.nearbyNodes(minmiddledist)) {
164 if (!othernodes.contains(en)) {
165 continue;
166 }
167 map.put(en, s.w);
168 }
169 }
170 //System.out.println("p4 elapsed: " + (System.currentTimeMillis()-last));
171 //last = System.currentTimeMillis();
172 for (Map.Entry<Node, Way> error : map.entrySet()) {
173 errors.add(new TestError(this, Severity.OTHER,
174 tr("Connected way end node near other way"),
175 UNCONNECTED_WAYS,
176 Arrays.asList(error.getKey(), error.getValue())));
177 }
178 }
179 ways = null;
180 endnodes = null;
181 super.endTest();
182 //System.out.println("p99 elapsed: " + (System.currentTimeMillis()-last));
183 //last = System.currentTimeMillis();
184 }
185
186 private class MyWaySegment {
187 private final Line2D line;
188 public final Way w;
189 public final boolean isAbandoned;
190 public final boolean isBoundary;
191 public final boolean highway;
192 private final double len;
193 private Set<Node> nearbyNodeCache;
194 double nearbyNodeCacheDist = -1.0;
195 final Node n1;
196 final Node n2;
197
198 public MyWaySegment(Way w, Node n1, Node n2) {
199 this.w = w;
200 String railway = w.get("railway");
201 String highway = w.get("highway");
202 this.isAbandoned = "abandoned".equals(railway) || OsmUtils.isTrue(w.get("disused"));
203 this.highway = (highway != null || railway != null) && !isAbandoned;
204 this.isBoundary = !this.highway && "administrative".equals(w.get("boundary"));
205 line = new Line2D.Double(n1.getEastNorth().east(), n1.getEastNorth().north(),
206 n2.getEastNorth().east(), n2.getEastNorth().north());
207 len = line.getP1().distance(line.getP2());
208 this.n1 = n1;
209 this.n2 = n2;
210 }
211
212 public boolean nearby(Node n, double dist) {
213 if (w == null) {
214 Main.debug("way null");
215 return false;
216 }
217 if (w.containsNode(n))
218 return false;
219 if (OsmUtils.isTrue(n.get("noexit")))
220 return false;
221 EastNorth coord = n.getEastNorth();
222 if (coord == null)
223 return false;
224 Point2D p = new Point2D.Double(coord.east(), coord.north());
225 if (line.getP1().distance(p) > len+dist)
226 return false;
227 if (line.getP2().distance(p) > len+dist)
228 return false;
229 return line.ptSegDist(p) < dist;
230 }
231
232 public List<LatLon> getBounds(double fudge) {
233 double x1 = n1.getCoor().lon();
234 double x2 = n2.getCoor().lon();
235 if (x1 > x2) {
236 double tmpx = x1;
237 x1 = x2;
238 x2 = tmpx;
239 }
240 double y1 = n1.getCoor().lat();
241 double y2 = n2.getCoor().lat();
242 if (y1 > y2) {
243 double tmpy = y1;
244 y1 = y2;
245 y2 = tmpy;
246 }
247 LatLon topLeft = new LatLon(y2+fudge, x1-fudge);
248 LatLon botRight = new LatLon(y1-fudge, x2+fudge);
249 List<LatLon> ret = new ArrayList<LatLon>();
250 ret.add(topLeft);
251 ret.add(botRight);
252 return ret;
253 }
254
255 public Collection<Node> nearbyNodes(double dist) {
256 // If you're looking for nodes that are farther
257 // away that we looked for last time, the cached
258 // result is no good
259 if (dist > nearbyNodeCacheDist) {
260 //if (nearbyNodeCacheDist != -1)
261 // System.out.println("destroyed MyWaySegment nearby node cache:" + dist + " > " + nearbyNodeCacheDist);
262 nearbyNodeCache = null;
263 }
264 if (nearbyNodeCache != null) {
265 // If we've cached an aread greater than the
266 // one now being asked for...
267 if (nearbyNodeCacheDist > dist) {
268 //System.out.println("had to trim MyWaySegment nearby node cache.");
269 // Used the cached result and trim out
270 // the nodes that are not in the smaller
271 // area, but keep the old larger cache.
272 Set<Node> trimmed = new HashSet<Node>(nearbyNodeCache);
273 for (Node n : new HashSet<Node>(nearbyNodeCache)) {
274 if (!nearby(n, dist)) {
275 trimmed.remove(n);
276 }
277 }
278 return trimmed;
279 }
280 return nearbyNodeCache;
281 }
282 /*
283 * We know that any point near the line must be at
284 * least as close as the other end of the line, plus
285 * a little fudge for the distance away ('dist').
286 */
287
288 // This needs to be a hash set because the searches
289 // overlap a bit and can return duplicate nodes.
290 nearbyNodeCache = null;
291 List<LatLon> bounds = this.getBounds(dist);
292 List<Node> found_nodes = ds.searchNodes(new BBox(bounds.get(0), bounds.get(1)));
293 if (found_nodes == null)
294 return Collections.emptySet();
295
296 for (Node n : found_nodes) {
297 if (!nearby(n, dist) ||
298 (ds_area != null && !ds_area.contains(n.getCoor()))) {
299 continue;
300 }
301 // It is actually very rare for us to find a node
302 // so defer as much of the work as possible, like
303 // allocating the hash set
304 if (nearbyNodeCache == null) {
305 nearbyNodeCache = new HashSet<Node>();
306 }
307 nearbyNodeCache.add(n);
308 }
309 nearbyNodeCacheDist = dist;
310 if (nearbyNodeCache == null) {
311 nearbyNodeCache = Collections.emptySet();
312 }
313 return nearbyNodeCache;
314 }
315
316 public boolean isArea() {
317 return w.hasKey("landuse")
318 || w.hasKey("leisure")
319 || w.hasKey("amenity")
320 || w.hasKey("building");
321 }
322 }
323
324 List<MyWaySegment> getWaySegments(Way w) {
325 List<MyWaySegment> ret = new ArrayList<MyWaySegment>();
326 if (!w.isUsable()
327 || w.hasKey("barrier")
328 || "cliff".equals(w.get("natural")))
329 return ret;
330
331 int size = w.getNodesCount();
332 if (size < 2)
333 return ret;
334 for (int i = 1; i < size; ++i) {
335 if(i < size-1) {
336 addNode(w.getNode(i), middlenodes);
337 }
338 MyWaySegment ws = new MyWaySegment(w, w.getNode(i-1), w.getNode(i));
339 if (ws.isBoundary || ws.isAbandoned) {
340 continue;
341 }
342 ret.add(ws);
343 }
344 return ret;
345 }
346
347 @Override
348 public void visit(Way w) {
349 ways.addAll(getWaySegments(w));
350 Set<Node> set = endnodes;
351 if (w.hasKey("highway") || w.hasKey("railway")) {
352 set = endnodes_highway;
353 }
354 addNode(w.firstNode(), set);
355 addNode(w.lastNode(), set);
356 }
357
358 @Override
359 public void visit(Node n) {
360 }
361
362 private void addNode(Node n, Set<Node> s) {
363 boolean m = middlenodes.contains(n);
364 boolean e = endnodes.contains(n);
365 boolean eh = endnodes_highway.contains(n);
366 boolean o = othernodes.contains(n);
367 if (!m && !e && !o && !eh) {
368 s.add(n);
369 } else if (!o) {
370 othernodes.add(n);
371 if (e) {
372 endnodes.remove(n);
373 } else if (eh) {
374 endnodes_highway.remove(n);
375 } else {
376 middlenodes.remove(n);
377 }
378 }
379 }
380}
Note: See TracBrowser for help on using the repository browser.