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.tr;
|
---|
5 |
|
---|
6 | import java.util.ArrayList;
|
---|
7 | import java.util.Arrays;
|
---|
8 | import java.util.Collection;
|
---|
9 | import java.util.HashMap;
|
---|
10 | import java.util.Iterator;
|
---|
11 | import java.util.List;
|
---|
12 | import java.util.Map;
|
---|
13 |
|
---|
14 | import org.openstreetmap.josm.Main;
|
---|
15 | import org.openstreetmap.josm.command.ChangePropertyCommand;
|
---|
16 | import org.openstreetmap.josm.command.Command;
|
---|
17 | import org.openstreetmap.josm.data.osm.Node;
|
---|
18 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
19 | import org.openstreetmap.josm.data.osm.Relation;
|
---|
20 | import org.openstreetmap.josm.data.osm.Way;
|
---|
21 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon;
|
---|
22 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon.JoinedWay;
|
---|
23 | import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
|
---|
24 | import org.openstreetmap.josm.data.validation.Severity;
|
---|
25 | import org.openstreetmap.josm.data.validation.Test;
|
---|
26 | import org.openstreetmap.josm.data.validation.TestError;
|
---|
27 | import org.openstreetmap.josm.gui.progress.ProgressMonitor;
|
---|
28 | import org.openstreetmap.josm.tools.Geometry;
|
---|
29 |
|
---|
30 | /**
|
---|
31 | * Checks for nodes in power lines/minor_lines that do not have a power=tower/pole tag.<br>
|
---|
32 | * See #7812 for discussions about this test.
|
---|
33 | */
|
---|
34 | public class PowerLines extends Test {
|
---|
35 |
|
---|
36 | protected static final int POWER_LINES = 2501;
|
---|
37 |
|
---|
38 | /** Values for {@code power} key interpreted as power lines */
|
---|
39 | protected static final Collection<String> POWER_LINE_TAGS = Arrays.asList("line", "minor_line");
|
---|
40 | /** Values for {@code power} key interpreted as power towers */
|
---|
41 | protected static final Collection<String> POWER_TOWER_TAGS = Arrays.asList("tower", "pole");
|
---|
42 | /** Values for {@code power} key interpreted as power stations */
|
---|
43 | protected static final Collection<String> POWER_STATION_TAGS = Arrays.asList("station", "sub_station", "substation", "plant", "generator");
|
---|
44 | /** Values for {@code building} key interpreted as power stations */
|
---|
45 | protected static final Collection<String> BUILDING_STATION_TAGS = Arrays.asList("transformer_tower");
|
---|
46 | /** Values for {@code power} key interpreted as allowed power items */
|
---|
47 | protected static final Collection<String> POWER_ALLOWED_TAGS = Arrays.asList("switch", "transformer", "busbar", "generator", "switchgear",
|
---|
48 | "portal", "terminal", "insulator");
|
---|
49 |
|
---|
50 | private final Map<Way, String> towerPoleTagMap = new HashMap<>();
|
---|
51 |
|
---|
52 | private final List<PowerLineError> potentialErrors = new ArrayList<>();
|
---|
53 |
|
---|
54 | private final List<OsmPrimitive> powerStations = new ArrayList<>();
|
---|
55 |
|
---|
56 | /**
|
---|
57 | * Constructs a new {@code PowerLines} test.
|
---|
58 | */
|
---|
59 | public PowerLines() {
|
---|
60 | super(tr("Power lines"), tr("Checks for nodes in power lines that do not have a power=tower/pole tag."));
|
---|
61 | }
|
---|
62 |
|
---|
63 | @Override
|
---|
64 | public void visit(Way w) {
|
---|
65 | if (w.isUsable()) {
|
---|
66 | if (isPowerLine(w) && !w.hasTag("location", "underground")) {
|
---|
67 | String fixValue = null;
|
---|
68 | boolean erroneous = false;
|
---|
69 | boolean canFix = false;
|
---|
70 | for (Node n : w.getNodes()) {
|
---|
71 | if (!isPowerTower(n)) {
|
---|
72 | if (!isPowerAllowed(n) && IN_DOWNLOADED_AREA.test(n)) {
|
---|
73 | if (!w.isFirstLastNode(n) || !isPowerStation(n)) {
|
---|
74 | potentialErrors.add(new PowerLineError(this, n, w));
|
---|
75 | erroneous = true;
|
---|
76 | }
|
---|
77 | }
|
---|
78 | } else if (fixValue == null) {
|
---|
79 | // First tower/pole tag found, remember it
|
---|
80 | fixValue = n.get("power");
|
---|
81 | canFix = true;
|
---|
82 | } else if (!fixValue.equals(n.get("power"))) {
|
---|
83 | // The power line contains both "tower" and "pole" -> cannot fix this error
|
---|
84 | canFix = false;
|
---|
85 | }
|
---|
86 | }
|
---|
87 | if (erroneous && canFix) {
|
---|
88 | towerPoleTagMap.put(w, fixValue);
|
---|
89 | }
|
---|
90 | } else if (w.isClosed() && isPowerStation(w)) {
|
---|
91 | powerStations.add(w);
|
---|
92 | }
|
---|
93 | }
|
---|
94 | }
|
---|
95 |
|
---|
96 | @Override
|
---|
97 | public void visit(Relation r) {
|
---|
98 | if (r.isMultipolygon() && isPowerStation(r)) {
|
---|
99 | powerStations.add(r);
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | @Override
|
---|
104 | public void startTest(ProgressMonitor progressMonitor) {
|
---|
105 | super.startTest(progressMonitor);
|
---|
106 | towerPoleTagMap.clear();
|
---|
107 | powerStations.clear();
|
---|
108 | potentialErrors.clear();
|
---|
109 | }
|
---|
110 |
|
---|
111 | @Override
|
---|
112 | public void endTest() {
|
---|
113 | for (PowerLineError e : potentialErrors) {
|
---|
114 | Node n = e.getNode();
|
---|
115 | if (n != null && !isInPowerStation(n)) {
|
---|
116 | errors.add(e);
|
---|
117 | }
|
---|
118 | }
|
---|
119 | potentialErrors.clear();
|
---|
120 | super.endTest();
|
---|
121 | }
|
---|
122 |
|
---|
123 | protected final boolean isInPowerStation(Node n) {
|
---|
124 | for (OsmPrimitive station : powerStations) {
|
---|
125 | List<List<Node>> nodesLists = new ArrayList<>();
|
---|
126 | if (station instanceof Way) {
|
---|
127 | nodesLists.add(((Way) station).getNodes());
|
---|
128 | } else if (station instanceof Relation) {
|
---|
129 | Multipolygon polygon = MultipolygonCache.getInstance().get(Main.map.mapView, (Relation) station);
|
---|
130 | if (polygon != null) {
|
---|
131 | for (JoinedWay outer : Multipolygon.joinWays(polygon.getOuterWays())) {
|
---|
132 | nodesLists.add(outer.getNodes());
|
---|
133 | }
|
---|
134 | }
|
---|
135 | }
|
---|
136 | for (List<Node> nodes : nodesLists) {
|
---|
137 | if (Geometry.nodeInsidePolygon(n, nodes)) {
|
---|
138 | return true;
|
---|
139 | }
|
---|
140 | }
|
---|
141 | }
|
---|
142 | return false;
|
---|
143 | }
|
---|
144 |
|
---|
145 | @Override
|
---|
146 | public Command fixError(TestError testError) {
|
---|
147 | if (testError instanceof PowerLineError && isFixable(testError)) {
|
---|
148 | // primitives list can be empty if all primitives have been purged
|
---|
149 | Iterator<? extends OsmPrimitive> it = testError.getPrimitives().iterator();
|
---|
150 | if (it.hasNext()) {
|
---|
151 | return new ChangePropertyCommand(it.next(),
|
---|
152 | "power", towerPoleTagMap.get(((PowerLineError) testError).line));
|
---|
153 | }
|
---|
154 | }
|
---|
155 | return null;
|
---|
156 | }
|
---|
157 |
|
---|
158 | @Override
|
---|
159 | public boolean isFixable(TestError testError) {
|
---|
160 | return testError instanceof PowerLineError && towerPoleTagMap.containsKey(((PowerLineError) testError).line);
|
---|
161 | }
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Determines if the specified way denotes a power line.
|
---|
165 | * @param w The way to be tested
|
---|
166 | * @return {@code true} if power key is set and equal to line/minor_line
|
---|
167 | */
|
---|
168 | protected static final boolean isPowerLine(Way w) {
|
---|
169 | return isPowerIn(w, POWER_LINE_TAGS);
|
---|
170 | }
|
---|
171 |
|
---|
172 | /**
|
---|
173 | * Determines if the specified primitive denotes a power station.
|
---|
174 | * @param p The primitive to be tested
|
---|
175 | * @return {@code true} if power key is set and equal to station/sub_station/plant
|
---|
176 | */
|
---|
177 | protected static final boolean isPowerStation(OsmPrimitive p) {
|
---|
178 | return isPowerIn(p, POWER_STATION_TAGS) || isBuildingIn(p, BUILDING_STATION_TAGS);
|
---|
179 | }
|
---|
180 |
|
---|
181 | /**
|
---|
182 | * Determines if the specified node denotes a power tower/pole.
|
---|
183 | * @param n The node to be tested
|
---|
184 | * @return {@code true} if power key is set and equal to tower/pole
|
---|
185 | */
|
---|
186 | protected static final boolean isPowerTower(Node n) {
|
---|
187 | return isPowerIn(n, POWER_TOWER_TAGS);
|
---|
188 | }
|
---|
189 |
|
---|
190 | /**
|
---|
191 | * Determines if the specified node denotes a power infrastructure allowed on a power line.
|
---|
192 | * @param n The node to be tested
|
---|
193 | * @return True if power key is set and equal to switch/tranformer/busbar/generator
|
---|
194 | */
|
---|
195 | protected static final boolean isPowerAllowed(Node n) {
|
---|
196 | return isPowerIn(n, POWER_ALLOWED_TAGS);
|
---|
197 | }
|
---|
198 |
|
---|
199 | /**
|
---|
200 | * Helper function to check if power tag is a certain value.
|
---|
201 | * @param p The primitive to be tested
|
---|
202 | * @param values List of possible values
|
---|
203 | * @return {@code true} if power key is set and equal to possible values
|
---|
204 | */
|
---|
205 | private static boolean isPowerIn(OsmPrimitive p, Collection<String> values) {
|
---|
206 | String v = p.get("power");
|
---|
207 | return v != null && values != null && values.contains(v);
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * Helper function to check if building tag is a certain value.
|
---|
212 | * @param p The primitive to be tested
|
---|
213 | * @param values List of possible values
|
---|
214 | * @return {@code true} if power key is set and equal to possible values
|
---|
215 | */
|
---|
216 | private static boolean isBuildingIn(OsmPrimitive p, Collection<String> values) {
|
---|
217 | String v = p.get("building");
|
---|
218 | return v != null && values != null && values.contains(v);
|
---|
219 | }
|
---|
220 |
|
---|
221 | protected static class PowerLineError extends TestError {
|
---|
222 | private final Way line;
|
---|
223 |
|
---|
224 | public PowerLineError(PowerLines tester, Node n, Way line) {
|
---|
225 | super(tester, Severity.WARNING,
|
---|
226 | tr("Missing power tower/pole within power line"), POWER_LINES, n);
|
---|
227 | this.line = line;
|
---|
228 | }
|
---|
229 |
|
---|
230 | public final Node getNode() {
|
---|
231 | // primitives list can be empty if all primitives have been purged
|
---|
232 | Iterator<? extends OsmPrimitive> it = getPrimitives().iterator();
|
---|
233 | return it.hasNext() ? (Node) it.next() : null;
|
---|
234 | }
|
---|
235 | }
|
---|
236 | }
|
---|