1 | // License: GPL. See LICENSE file for details.
|
---|
2 | package org.openstreetmap.josm.data.validation.tests;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
5 |
|
---|
6 | import java.util.HashMap;
|
---|
7 | import java.util.Map;
|
---|
8 |
|
---|
9 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
10 | import org.openstreetmap.josm.command.Command;
|
---|
11 | import org.openstreetmap.josm.data.osm.Node;
|
---|
12 | import org.openstreetmap.josm.data.osm.Way;
|
---|
13 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
14 | import org.openstreetmap.josm.data.validation.Test;
|
---|
15 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
16 |
|
---|
17 | public 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 | }
|
---|