source: josm/trunk/src/org/openstreetmap/josm/data/validation/tests/PowerLines.java@ 5300

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

fix #7812 - warn about missing power=tower/pole within powerlines

File size: 3.4 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.util.HashMap;
7import java.util.Map;
8
9import org.openstreetmap.josm.command.ChangePropertyCommand;
10import org.openstreetmap.josm.command.Command;
11import org.openstreetmap.josm.data.osm.Node;
12import org.openstreetmap.josm.data.osm.Way;
13import org.openstreetmap.josm.data.validation.Severity;
14import org.openstreetmap.josm.data.validation.Test;
15import org.openstreetmap.josm.data.validation.TestError;
16
17public class PowerLines extends Test {
18
19 protected static final int POWER_LINES = 2501;
20
21 protected Map<Way, String> towerPoleTagMap = new HashMap<Way, String>();
22
23 public PowerLines() {
24 super(tr("Power lines"), tr("Checks for nodes in power lines that do not have a power=tower/pole tag."));
25 }
26
27 @Override
28 public void visit(Way w) {
29 if (w.isUsable() && isPowerLine(w)) {
30 String fixValue = null;
31 boolean erroneous = false;
32 boolean canFix = false;
33 for (Node n : w.getNodes()) {
34 if (!isPowerTower(n)) {
35 errors.add(new PowerLineError(n, w));
36 erroneous = true;
37 } else if (fixValue == null) {
38 // First tower/pole tag found, remember it
39 fixValue = n.get("power");
40 canFix = true;
41 } else if (!fixValue.equals(n.get("power"))) {
42 // The power line contains both "tower" and "pole" -> cannot fix this error
43 canFix = false;
44 }
45 }
46 if (erroneous && canFix) {
47 towerPoleTagMap.put(w, fixValue);
48 }
49 }
50 }
51
52 @Override
53 public Command fixError(TestError testError) {
54 if (isFixable(testError)) {
55 return new ChangePropertyCommand(
56 testError.getPrimitives().iterator().next(),
57 "power", towerPoleTagMap.get(((PowerLineError)testError).line));
58 }
59 return null;
60 }
61
62 @Override
63 public boolean isFixable(TestError testError) {
64 return testError instanceof PowerLineError && towerPoleTagMap.containsKey(((PowerLineError)testError).line);
65 }
66
67 /**
68 * Determines if the specified way denotes a power line.
69 * @param w The way to be tested
70 * @return True if power key is set and equal to line/minor_line
71 */
72 protected static final boolean isPowerLine(Way w) {
73 String v = w.get("power");
74 return v != null && (v.equals("line") || v.equals("minor_line"));
75 }
76
77 /**
78 * Determines if the specified node denotes a power tower/pole.
79 * @param w The node to be tested
80 * @return True if power key is set and equal to tower/pole
81 */
82 protected static final boolean isPowerTower(Node n) {
83 String v = n.get("power");
84 return v != null && (v.equals("tower") || v.equals("pole"));
85 }
86
87 protected class PowerLineError extends TestError {
88 public final Way line;
89 public PowerLineError(Node n, Way line) {
90 super(PowerLines.this, Severity.WARNING,
91 tr("Missing power tower/pole within power line"), POWER_LINES, n);
92 this.line = line;
93 }
94 }
95}
Note: See TracBrowser for help on using the repository browser.