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

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

fix #14572 - Don't index MultipolygonCache by NavigatableComponent

  • Property svn:eol-style set to native
File size: 6.9 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.validation.tests;
3
4import static org.openstreetmap.josm.tools.I18n.tr;
5
6import java.util.ArrayList;
7import java.util.Arrays;
8import java.util.Collection;
9import java.util.List;
10
11import org.openstreetmap.josm.data.osm.Node;
12import org.openstreetmap.josm.data.osm.OsmPrimitive;
13import org.openstreetmap.josm.data.osm.Relation;
14import org.openstreetmap.josm.data.osm.Way;
15import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon;
16import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon.JoinedWay;
17import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
18import org.openstreetmap.josm.data.validation.Severity;
19import org.openstreetmap.josm.data.validation.Test;
20import org.openstreetmap.josm.data.validation.TestError;
21import org.openstreetmap.josm.gui.progress.ProgressMonitor;
22import org.openstreetmap.josm.tools.Geometry;
23
24/**
25 * Checks for nodes in power lines/minor_lines that do not have a power=tower/pole tag.<br>
26 * See #7812 for discussions about this test.
27 */
28public class PowerLines extends Test {
29
30 /** Test identifier */
31 protected static final int POWER_LINES = 2501;
32
33 /** Values for {@code power} key interpreted as power lines */
34 static final Collection<String> POWER_LINE_TAGS = Arrays.asList("line", "minor_line");
35 /** Values for {@code power} key interpreted as power towers */
36 static final Collection<String> POWER_TOWER_TAGS = Arrays.asList("tower", "pole");
37 /** Values for {@code power} key interpreted as power stations */
38 static final Collection<String> POWER_STATION_TAGS = Arrays.asList("station", "sub_station", "substation", "plant", "generator");
39 /** Values for {@code building} key interpreted as power stations */
40 static final Collection<String> BUILDING_STATION_TAGS = Arrays.asList("transformer_tower");
41 /** Values for {@code power} key interpreted as allowed power items */
42 static final Collection<String> POWER_ALLOWED_TAGS = Arrays.asList("switch", "transformer", "busbar", "generator", "switchgear",
43 "portal", "terminal", "insulator");
44
45 private final List<TestError> potentialErrors = new ArrayList<>();
46
47 private final List<OsmPrimitive> powerStations = new ArrayList<>();
48
49 /**
50 * Constructs a new {@code PowerLines} test.
51 */
52 public PowerLines() {
53 super(tr("Power lines"), tr("Checks for nodes in power lines that do not have a power=tower/pole tag."));
54 }
55
56 @Override
57 public void visit(Way w) {
58 if (w.isUsable()) {
59 if (isPowerLine(w) && !w.hasTag("location", "underground")) {
60 for (Node n : w.getNodes()) {
61 if (!isPowerTower(n) && !isPowerAllowed(n) && IN_DOWNLOADED_AREA.test(n)
62 && (!w.isFirstLastNode(n) || !isPowerStation(n))) {
63 potentialErrors.add(TestError.builder(this, Severity.WARNING, POWER_LINES)
64 .message(tr("Missing power tower/pole within power line"))
65 .primitives(n)
66 .build());
67 }
68 }
69 } else if (w.isClosed() && isPowerStation(w)) {
70 powerStations.add(w);
71 }
72 }
73 }
74
75 @Override
76 public void visit(Relation r) {
77 if (r.isMultipolygon() && isPowerStation(r)) {
78 powerStations.add(r);
79 }
80 }
81
82 @Override
83 public void startTest(ProgressMonitor progressMonitor) {
84 super.startTest(progressMonitor);
85 powerStations.clear();
86 potentialErrors.clear();
87 }
88
89 @Override
90 public void endTest() {
91 for (TestError e : potentialErrors) {
92 e.getPrimitives().stream()
93 .map(Node.class::cast)
94 .filter(n -> !isInPowerStation(n))
95 .findAny()
96 .ifPresent(ignore -> errors.add(e));
97 }
98 potentialErrors.clear();
99 super.endTest();
100 }
101
102 protected final boolean isInPowerStation(Node n) {
103 for (OsmPrimitive station : powerStations) {
104 List<List<Node>> nodesLists = new ArrayList<>();
105 if (station instanceof Way) {
106 nodesLists.add(((Way) station).getNodes());
107 } else if (station instanceof Relation) {
108 Multipolygon polygon = MultipolygonCache.getInstance().get((Relation) station);
109 if (polygon != null) {
110 for (JoinedWay outer : Multipolygon.joinWays(polygon.getOuterWays())) {
111 nodesLists.add(outer.getNodes());
112 }
113 }
114 }
115 for (List<Node> nodes : nodesLists) {
116 if (Geometry.nodeInsidePolygon(n, nodes)) {
117 return true;
118 }
119 }
120 }
121 return false;
122 }
123
124 /**
125 * Determines if the specified way denotes a power line.
126 * @param w The way to be tested
127 * @return {@code true} if power key is set and equal to line/minor_line
128 */
129 protected static final boolean isPowerLine(Way w) {
130 return isPowerIn(w, POWER_LINE_TAGS);
131 }
132
133 /**
134 * Determines if the specified primitive denotes a power station.
135 * @param p The primitive to be tested
136 * @return {@code true} if power key is set and equal to station/sub_station/plant
137 */
138 protected static final boolean isPowerStation(OsmPrimitive p) {
139 return isPowerIn(p, POWER_STATION_TAGS) || isBuildingIn(p, BUILDING_STATION_TAGS);
140 }
141
142 /**
143 * Determines if the specified node denotes a power tower/pole.
144 * @param n The node to be tested
145 * @return {@code true} if power key is set and equal to tower/pole
146 */
147 protected static final boolean isPowerTower(Node n) {
148 return isPowerIn(n, POWER_TOWER_TAGS);
149 }
150
151 /**
152 * Determines if the specified node denotes a power infrastructure allowed on a power line.
153 * @param n The node to be tested
154 * @return True if power key is set and equal to switch/tranformer/busbar/generator
155 */
156 protected static final boolean isPowerAllowed(Node n) {
157 return isPowerIn(n, POWER_ALLOWED_TAGS);
158 }
159
160 /**
161 * Helper function to check if power tag is a certain value.
162 * @param p The primitive to be tested
163 * @param values List of possible values
164 * @return {@code true} if power key is set and equal to possible values
165 */
166 private static boolean isPowerIn(OsmPrimitive p, Collection<String> values) {
167 return p.hasTag("power", values);
168 }
169
170 /**
171 * Helper function to check if building tag is a certain value.
172 * @param p The primitive to be tested
173 * @param values List of possible values
174 * @return {@code true} if power key is set and equal to possible values
175 */
176 private static boolean isBuildingIn(OsmPrimitive p, Collection<String> values) {
177 return p.hasTag("building", values);
178 }
179}
Note: See TracBrowser for help on using the repository browser.