1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.marktr;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 |
|
---|
7 | import java.util.ArrayList;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.Collections;
|
---|
10 | import java.util.HashMap;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.Iterator;
|
---|
13 | import java.util.LinkedHashSet;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Map;
|
---|
17 | import java.util.Map.Entry;
|
---|
18 | import java.util.Set;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.actions.MergeNodesAction;
|
---|
22 | import org.openstreetmap.josm.command.Command;
|
---|
23 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
24 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
25 | import org.openstreetmap.josm.data.osm.Hash;
|
---|
26 | import org.openstreetmap.josm.data.osm.Node;
|
---|
27 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
28 | import org.openstreetmap.josm.data.osm.OsmPrimitiveType;
|
---|
29 | import org.openstreetmap.josm.data.osm.Storage;
|
---|
30 | import org.openstreetmap.josm.data.osm.Way;
|
---|
31 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
32 | import org.openstreetmap.josm.data.validation.Test;
|
---|
33 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
34 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
35 | import org.openstreetmap.josm.tools.MultiMap;
|
---|
36 |
|
---|
37 | /**
|
---|
38 | * Tests if there are duplicate nodes
|
---|
39 | *
|
---|
40 | * @author frsantos
|
---|
41 | */
|
---|
42 | public class DuplicateNode extends Test {
|
---|
43 |
|
---|
44 | private static class NodeHash implements Hash<Object, Object> {
|
---|
45 |
|
---|
46 | private double precision = Main.pref.getDouble("validator.duplicatenodes.precision", 0.);
|
---|
47 |
|
---|
48 | private LatLon roundCoord(LatLon coor) {
|
---|
49 | return new LatLon(
|
---|
50 | Math.round(coor.lat() / precision) * precision,
|
---|
51 | Math.round(coor.lon() / precision) * precision
|
---|
52 | );
|
---|
53 | }
|
---|
54 |
|
---|
55 | @SuppressWarnings("unchecked")
|
---|
56 | private LatLon getLatLon(Object o) {
|
---|
57 | if (o instanceof Node) {
|
---|
58 | LatLon coor = ((Node) o).getCoor();
|
---|
59 | if (coor == null)
|
---|
60 | return null;
|
---|
61 | if (precision == 0)
|
---|
62 | return coor.getRoundedToOsmPrecision();
|
---|
63 | return roundCoord(coor);
|
---|
64 | } else if (o instanceof List<?>) {
|
---|
65 | LatLon coor = ((List<Node>) o).get(0).getCoor();
|
---|
66 | if (coor == null)
|
---|
67 | return null;
|
---|
68 | if (precision == 0)
|
---|
69 | return coor.getRoundedToOsmPrecision();
|
---|
70 | return roundCoord(coor);
|
---|
71 | } else
|
---|
72 | throw new AssertionError();
|
---|
73 | }
|
---|
74 |
|
---|
75 | @Override
|
---|
76 | public boolean equals(Object k, Object t) {
|
---|
77 | LatLon coorK = getLatLon(k);
|
---|
78 | LatLon coorT = getLatLon(t);
|
---|
79 | return coorK == coorT || (coorK != null && coorT != null && coorK.equals(coorT));
|
---|
80 | }
|
---|
81 |
|
---|
82 | @Override
|
---|
83 | public int getHashCode(Object k) {
|
---|
84 | LatLon coorK = getLatLon(k);
|
---|
85 | return coorK == null ? 0 : coorK.hashCode();
|
---|
86 | }
|
---|
87 | }
|
---|
88 |
|
---|
89 | protected static final int DUPLICATE_NODE = 1;
|
---|
90 | protected static final int DUPLICATE_NODE_MIXED = 2;
|
---|
91 | protected static final int DUPLICATE_NODE_OTHER = 3;
|
---|
92 | protected static final int DUPLICATE_NODE_BUILDING = 10;
|
---|
93 | protected static final int DUPLICATE_NODE_BOUNDARY = 11;
|
---|
94 | protected static final int DUPLICATE_NODE_HIGHWAY = 12;
|
---|
95 | protected static final int DUPLICATE_NODE_LANDUSE = 13;
|
---|
96 | protected static final int DUPLICATE_NODE_NATURAL = 14;
|
---|
97 | protected static final int DUPLICATE_NODE_POWER = 15;
|
---|
98 | protected static final int DUPLICATE_NODE_RAILWAY = 16;
|
---|
99 | protected static final int DUPLICATE_NODE_WATERWAY = 17;
|
---|
100 |
|
---|
101 | /** The map of potential duplicates.
|
---|
102 | *
|
---|
103 | * If there is exactly one node for a given pos, the map includes a pair <pos, Node>.
|
---|
104 | * If there are multiple nodes for a given pos, the map includes a pair
|
---|
105 | * <pos, NodesByEqualTagsMap>
|
---|
106 | */
|
---|
107 | private Storage<Object> potentialDuplicates;
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Constructor
|
---|
111 | */
|
---|
112 | public DuplicateNode() {
|
---|
113 | super(tr("Duplicated nodes"),
|
---|
114 | tr("This test checks that there are no nodes at the very same location."));
|
---|
115 | }
|
---|
116 |
|
---|
117 | @Override
|
---|
118 | public void startTest(ProgressMonitor monitor) {
|
---|
119 | super.startTest(monitor);
|
---|
120 | potentialDuplicates = new Storage<>(new NodeHash());
|
---|
121 | }
|
---|
122 |
|
---|
123 | @SuppressWarnings("unchecked")
|
---|
124 | @Override
|
---|
125 | public void endTest() {
|
---|
126 | for (Object v: potentialDuplicates) {
|
---|
127 | if (v instanceof Node) {
|
---|
128 | // just one node at this position. Nothing to report as error
|
---|
129 | continue;
|
---|
130 | }
|
---|
131 |
|
---|
132 | // multiple nodes at the same position -> check if all nodes have a distinct elevation
|
---|
133 | List<Node> nodes = (List<Node>) v;
|
---|
134 | Set<String> eles = new HashSet<>();
|
---|
135 | for (Node n : nodes) {
|
---|
136 | String ele = n.get("ele");
|
---|
137 | if (ele != null) {
|
---|
138 | eles.add(ele);
|
---|
139 | }
|
---|
140 | }
|
---|
141 | if (eles.size() == nodes.size()) {
|
---|
142 | // All nodes at this position have a distinct elevation.
|
---|
143 | // This is normal in some particular cases (for example, geodesic points in France)
|
---|
144 | // Do not report this as an error
|
---|
145 | continue;
|
---|
146 | }
|
---|
147 |
|
---|
148 | // report errors
|
---|
149 | errors.addAll(buildTestErrors(this, nodes));
|
---|
150 | }
|
---|
151 | super.endTest();
|
---|
152 | potentialDuplicates = null;
|
---|
153 | }
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * Returns the list of "duplicate nodes" errors for the given selection of node and parent test
|
---|
157 | * @param parentTest The parent test of returned errors
|
---|
158 | * @param nodes The nodes selction to look into
|
---|
159 | * @return the list of "duplicate nodes" errors
|
---|
160 | */
|
---|
161 | public List<TestError> buildTestErrors(Test parentTest, List<Node> nodes) {
|
---|
162 | List<TestError> errors = new ArrayList<>();
|
---|
163 |
|
---|
164 | MultiMap<Map<String, String>, OsmPrimitive> mm = new MultiMap<>();
|
---|
165 | for (Node n: nodes) {
|
---|
166 | mm.put(n.getKeys(), n);
|
---|
167 | }
|
---|
168 |
|
---|
169 | Map<String, Boolean> typeMap = new HashMap<>();
|
---|
170 | String[] types = {"none", "highway", "railway", "waterway", "boundary", "power", "natural", "landuse", "building"};
|
---|
171 |
|
---|
172 | // check whether we have multiple nodes at the same position with the same tag set
|
---|
173 | for (Iterator<Map<String, String>> it = mm.keySet().iterator(); it.hasNext();) {
|
---|
174 | Map<String, String> tagSet = it.next();
|
---|
175 | if (mm.get(tagSet).size() > 1) {
|
---|
176 |
|
---|
177 | for (String type: types) {
|
---|
178 | typeMap.put(type, false);
|
---|
179 | }
|
---|
180 |
|
---|
181 | for (OsmPrimitive p : mm.get(tagSet)) {
|
---|
182 | if (p.getType() == OsmPrimitiveType.NODE) {
|
---|
183 | Node n = (Node) p;
|
---|
184 | List<OsmPrimitive> lp = n.getReferrers();
|
---|
185 | for (OsmPrimitive sp: lp) {
|
---|
186 | if (sp.getType() == OsmPrimitiveType.WAY) {
|
---|
187 | boolean typed = false;
|
---|
188 | Way w = (Way) sp;
|
---|
189 | Map<String, String> keys = w.getKeys();
|
---|
190 | for (String type: typeMap.keySet()) {
|
---|
191 | if (keys.containsKey(type)) {
|
---|
192 | typeMap.put(type, true);
|
---|
193 | typed = true;
|
---|
194 | }
|
---|
195 | }
|
---|
196 | if (!typed) {
|
---|
197 | typeMap.put("none", true);
|
---|
198 | }
|
---|
199 | }
|
---|
200 | }
|
---|
201 |
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | int nbType = 0;
|
---|
206 | for (Entry<String, Boolean> entry: typeMap.entrySet()) {
|
---|
207 | if (entry.getValue()) {
|
---|
208 | nbType++;
|
---|
209 | }
|
---|
210 | }
|
---|
211 |
|
---|
212 | if (nbType > 1) {
|
---|
213 | String msg = marktr("Mixed type duplicated nodes");
|
---|
214 | errors.add(new TestError(
|
---|
215 | parentTest,
|
---|
216 | Severity.WARNING,
|
---|
217 | tr("Duplicated nodes"),
|
---|
218 | tr(msg),
|
---|
219 | msg,
|
---|
220 | DUPLICATE_NODE_MIXED,
|
---|
221 | mm.get(tagSet)
|
---|
222 | ));
|
---|
223 | } else if (typeMap.get("highway")) {
|
---|
224 | String msg = marktr("Highway duplicated nodes");
|
---|
225 | errors.add(new TestError(
|
---|
226 | parentTest,
|
---|
227 | Severity.ERROR,
|
---|
228 | tr("Duplicated nodes"),
|
---|
229 | tr(msg),
|
---|
230 | msg,
|
---|
231 | DUPLICATE_NODE_HIGHWAY,
|
---|
232 | mm.get(tagSet)
|
---|
233 | ));
|
---|
234 | } else if (typeMap.get("railway")) {
|
---|
235 | String msg = marktr("Railway duplicated nodes");
|
---|
236 | errors.add(new TestError(
|
---|
237 | parentTest,
|
---|
238 | Severity.ERROR,
|
---|
239 | tr("Duplicated nodes"),
|
---|
240 | tr(msg),
|
---|
241 | msg,
|
---|
242 | DUPLICATE_NODE_RAILWAY,
|
---|
243 | mm.get(tagSet)
|
---|
244 | ));
|
---|
245 | } else if (typeMap.get("waterway")) {
|
---|
246 | String msg = marktr("Waterway duplicated nodes");
|
---|
247 | errors.add(new TestError(
|
---|
248 | parentTest,
|
---|
249 | Severity.ERROR,
|
---|
250 | tr("Duplicated nodes"),
|
---|
251 | tr(msg),
|
---|
252 | msg,
|
---|
253 | DUPLICATE_NODE_WATERWAY,
|
---|
254 | mm.get(tagSet)
|
---|
255 | ));
|
---|
256 | } else if (typeMap.get("boundary")) {
|
---|
257 | String msg = marktr("Boundary duplicated nodes");
|
---|
258 | errors.add(new TestError(
|
---|
259 | parentTest,
|
---|
260 | Severity.ERROR,
|
---|
261 | tr("Duplicated nodes"),
|
---|
262 | tr(msg),
|
---|
263 | msg,
|
---|
264 | DUPLICATE_NODE_BOUNDARY,
|
---|
265 | mm.get(tagSet)
|
---|
266 | ));
|
---|
267 | } else if (typeMap.get("power")) {
|
---|
268 | String msg = marktr("Power duplicated nodes");
|
---|
269 | errors.add(new TestError(
|
---|
270 | parentTest,
|
---|
271 | Severity.ERROR,
|
---|
272 | tr("Duplicated nodes"),
|
---|
273 | tr(msg),
|
---|
274 | msg,
|
---|
275 | DUPLICATE_NODE_POWER,
|
---|
276 | mm.get(tagSet)
|
---|
277 | ));
|
---|
278 | } else if (typeMap.get("natural")) {
|
---|
279 | String msg = marktr("Natural duplicated nodes");
|
---|
280 | errors.add(new TestError(
|
---|
281 | parentTest,
|
---|
282 | Severity.ERROR,
|
---|
283 | tr("Duplicated nodes"),
|
---|
284 | tr(msg),
|
---|
285 | msg,
|
---|
286 | DUPLICATE_NODE_NATURAL,
|
---|
287 | mm.get(tagSet)
|
---|
288 | ));
|
---|
289 | } else if (typeMap.get("building")) {
|
---|
290 | String msg = marktr("Building duplicated nodes");
|
---|
291 | errors.add(new TestError(
|
---|
292 | parentTest,
|
---|
293 | Severity.ERROR,
|
---|
294 | tr("Duplicated nodes"),
|
---|
295 | tr(msg),
|
---|
296 | msg,
|
---|
297 | DUPLICATE_NODE_BUILDING,
|
---|
298 | mm.get(tagSet)
|
---|
299 | ));
|
---|
300 | } else if (typeMap.get("landuse")) {
|
---|
301 | String msg = marktr("Landuse duplicated nodes");
|
---|
302 | errors.add(new TestError(
|
---|
303 | parentTest,
|
---|
304 | Severity.ERROR,
|
---|
305 | tr("Duplicated nodes"),
|
---|
306 | tr(msg),
|
---|
307 | msg,
|
---|
308 | DUPLICATE_NODE_LANDUSE,
|
---|
309 | mm.get(tagSet)
|
---|
310 | ));
|
---|
311 | } else {
|
---|
312 | String msg = marktr("Other duplicated nodes");
|
---|
313 | errors.add(new TestError(
|
---|
314 | parentTest,
|
---|
315 | Severity.WARNING,
|
---|
316 | tr("Duplicated nodes"),
|
---|
317 | tr(msg),
|
---|
318 | msg,
|
---|
319 | DUPLICATE_NODE_OTHER,
|
---|
320 | mm.get(tagSet)
|
---|
321 | ));
|
---|
322 |
|
---|
323 | }
|
---|
324 | it.remove();
|
---|
325 | }
|
---|
326 | }
|
---|
327 |
|
---|
328 | // check whether we have multiple nodes at the same position with
|
---|
329 | // differing tag sets
|
---|
330 | //
|
---|
331 | if (!mm.isEmpty()) {
|
---|
332 | List<OsmPrimitive> duplicates = new ArrayList<>();
|
---|
333 | for (Set<OsmPrimitive> l: mm.values()) {
|
---|
334 | duplicates.addAll(l);
|
---|
335 | }
|
---|
336 | if (duplicates.size() > 1) {
|
---|
337 | errors.add(new TestError(
|
---|
338 | parentTest,
|
---|
339 | Severity.WARNING,
|
---|
340 | tr("Nodes at same position"),
|
---|
341 | DUPLICATE_NODE,
|
---|
342 | duplicates
|
---|
343 | ));
|
---|
344 | }
|
---|
345 | }
|
---|
346 | return errors;
|
---|
347 | }
|
---|
348 |
|
---|
349 | @SuppressWarnings("unchecked")
|
---|
350 | @Override
|
---|
351 | public void visit(Node n) {
|
---|
352 | if (n.isUsable()) {
|
---|
353 | if (potentialDuplicates.get(n) == null) {
|
---|
354 | // in most cases there is just one node at a given position. We
|
---|
355 | // avoid to create an extra object and add remember the node
|
---|
356 | // itself at this position
|
---|
357 | potentialDuplicates.put(n);
|
---|
358 | } else if (potentialDuplicates.get(n) instanceof Node) {
|
---|
359 | // we have an additional node at the same position. Create an extra
|
---|
360 | // object to keep track of the nodes at this position.
|
---|
361 | //
|
---|
362 | Node n1 = (Node) potentialDuplicates.get(n);
|
---|
363 | List<Node> nodes = new ArrayList<>(2);
|
---|
364 | nodes.add(n1);
|
---|
365 | nodes.add(n);
|
---|
366 | potentialDuplicates.put(nodes);
|
---|
367 | } else if (potentialDuplicates.get(n) instanceof List<?>) {
|
---|
368 | // we have multiple nodes at the same position.
|
---|
369 | //
|
---|
370 | List<Node> nodes = (List<Node>) potentialDuplicates.get(n);
|
---|
371 | nodes.add(n);
|
---|
372 | }
|
---|
373 | }
|
---|
374 | }
|
---|
375 |
|
---|
376 | /**
|
---|
377 | * Merge the nodes into one.
|
---|
378 | * Copied from UtilsPlugin.MergePointsAction
|
---|
379 | */
|
---|
380 | @Override
|
---|
381 | public Command fixError(TestError testError) {
|
---|
382 | if (!isFixable(testError)) return null;
|
---|
383 | Collection<OsmPrimitive> sel = new LinkedList<OsmPrimitive>(testError.getPrimitives());
|
---|
384 | Set<Node> nodes = new LinkedHashSet<>(OsmPrimitive.getFilteredList(sel, Node.class));
|
---|
385 |
|
---|
386 | // Filter nodes that have already been deleted (see #5764 and #5773)
|
---|
387 | for (Iterator<Node> it = nodes.iterator(); it.hasNext();) {
|
---|
388 | if (it.next().isDeleted()) {
|
---|
389 | it.remove();
|
---|
390 | }
|
---|
391 | }
|
---|
392 |
|
---|
393 | // Merge only if at least 2 nodes remain
|
---|
394 | if (nodes.size() >= 2) {
|
---|
395 | // Use first existing node or first node if all nodes are new
|
---|
396 | Node target = null;
|
---|
397 | for (Node n: nodes) {
|
---|
398 | if (!n.isNew()) {
|
---|
399 | target = n;
|
---|
400 | break;
|
---|
401 | }
|
---|
402 | }
|
---|
403 | if (target == null) {
|
---|
404 | target = nodes.iterator().next();
|
---|
405 | }
|
---|
406 |
|
---|
407 | if (DeleteCommand.checkAndConfirmOutlyingDelete(nodes, Collections.singleton(target)))
|
---|
408 | return MergeNodesAction.mergeNodes(Main.main.getEditLayer(), nodes, target);
|
---|
409 | }
|
---|
410 |
|
---|
411 | return null; // undoRedo handling done in mergeNodes
|
---|
412 | }
|
---|
413 |
|
---|
414 | @Override
|
---|
415 | public boolean isFixable(TestError testError) {
|
---|
416 | if (!(testError.getTester() instanceof DuplicateNode)) return false;
|
---|
417 | // never merge nodes with different tags.
|
---|
418 | if (testError.getCode() == DUPLICATE_NODE) return false;
|
---|
419 | // everything else is ok to merge
|
---|
420 | return true;
|
---|
421 | }
|
---|
422 | }
|
---|