001// License: GPL. For details, see LICENSE file.
002package org.openstreetmap.josm.data.validation.tests;
003
004import static org.openstreetmap.josm.tools.I18n.tr;
005
006import java.util.ArrayList;
007import java.util.Arrays;
008import java.util.Collection;
009import java.util.List;
010
011import org.openstreetmap.josm.Main;
012import org.openstreetmap.josm.command.ChangePropertyCommand;
013import org.openstreetmap.josm.data.osm.Node;
014import org.openstreetmap.josm.data.osm.OsmPrimitive;
015import org.openstreetmap.josm.data.osm.Relation;
016import org.openstreetmap.josm.data.osm.Way;
017import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon;
018import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon.JoinedWay;
019import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache;
020import org.openstreetmap.josm.data.validation.Severity;
021import org.openstreetmap.josm.data.validation.Test;
022import org.openstreetmap.josm.data.validation.TestError;
023import org.openstreetmap.josm.gui.progress.ProgressMonitor;
024import org.openstreetmap.josm.tools.Geometry;
025
026/**
027 * Checks for nodes in power lines/minor_lines that do not have a power=tower/pole tag.<br>
028 * See #7812 for discussions about this test.
029 */
030public class PowerLines extends Test {
031
032    protected static final int POWER_LINES = 2501;
033
034    /** Values for {@code power} key interpreted as power lines */
035    protected static final Collection<String> POWER_LINE_TAGS = Arrays.asList("line", "minor_line");
036    /** Values for {@code power} key interpreted as power towers */
037    protected static final Collection<String> POWER_TOWER_TAGS = Arrays.asList("tower", "pole");
038    /** Values for {@code power} key interpreted as power stations */
039    protected static final Collection<String> POWER_STATION_TAGS = Arrays.asList("station", "sub_station", "substation", "plant", "generator");
040    /** Values for {@code building} key interpreted as power stations */
041    protected static final Collection<String> BUILDING_STATION_TAGS = Arrays.asList("transformer_tower");
042    /** Values for {@code power} key interpreted as allowed power items */
043    protected static final Collection<String> POWER_ALLOWED_TAGS = Arrays.asList("switch", "transformer", "busbar", "generator", "switchgear",
044            "portal", "terminal", "insulator");
045
046    private final List<TestError> potentialErrors = new ArrayList<>();
047
048    private final List<OsmPrimitive> powerStations = new ArrayList<>();
049
050    /**
051     * Constructs a new {@code PowerLines} test.
052     */
053    public PowerLines() {
054        super(tr("Power lines"), tr("Checks for nodes in power lines that do not have a power=tower/pole tag."));
055    }
056
057    @Override
058    public void visit(Way w) {
059        if (w.isUsable()) {
060            if (isPowerLine(w) && !w.hasTag("location", "underground")) {
061                String fixValue = null;
062                TestError.Builder error = null;
063                Node errorNode = null;
064                boolean canFix = false;
065                for (Node n : w.getNodes()) {
066                    if (!isPowerTower(n)) {
067                        if (!isPowerAllowed(n) && IN_DOWNLOADED_AREA.test(n)) {
068                            if (!w.isFirstLastNode(n) || !isPowerStation(n)) {
069                                error = TestError.builder(this, Severity.WARNING, POWER_LINES)
070                                        .message(tr("Missing power tower/pole within power line"))
071                                        .primitives(n);
072                                errorNode = n;
073                            }
074                        }
075                    } else if (fixValue == null) {
076                        // First tower/pole tag found, remember it
077                        fixValue = n.get("power");
078                        canFix = true;
079                    } else if (!fixValue.equals(n.get("power"))) {
080                        // The power line contains both "tower" and "pole" -> cannot fix this error
081                        canFix = false;
082                    }
083                }
084                if (error != null && canFix) {
085                    final ChangePropertyCommand fix = new ChangePropertyCommand(errorNode, "power", fixValue);
086                    potentialErrors.add(error.fix(() -> fix).build());
087                } else if (error != null) {
088                    potentialErrors.add(error.build());
089                }
090            } else if (w.isClosed() && isPowerStation(w)) {
091                powerStations.add(w);
092            }
093        }
094    }
095
096    @Override
097    public void visit(Relation r) {
098        if (r.isMultipolygon() && isPowerStation(r)) {
099            powerStations.add(r);
100        }
101    }
102
103    @Override
104    public void startTest(ProgressMonitor progressMonitor) {
105        super.startTest(progressMonitor);
106        powerStations.clear();
107        potentialErrors.clear();
108    }
109
110    @Override
111    public void endTest() {
112        for (TestError e : potentialErrors) {
113            e.getPrimitives().stream()
114                    .map(Node.class::cast)
115                    .filter(n -> !isInPowerStation(n))
116                    .findAny()
117                    .ifPresent(ignore -> errors.add(e));
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    /**
146     * Determines if the specified way denotes a power line.
147     * @param w The way to be tested
148     * @return {@code true} if power key is set and equal to line/minor_line
149     */
150    protected static final boolean isPowerLine(Way w) {
151        return isPowerIn(w, POWER_LINE_TAGS);
152    }
153
154    /**
155     * Determines if the specified primitive denotes a power station.
156     * @param p The primitive to be tested
157     * @return {@code true} if power key is set and equal to station/sub_station/plant
158     */
159    protected static final boolean isPowerStation(OsmPrimitive p) {
160        return isPowerIn(p, POWER_STATION_TAGS) || isBuildingIn(p, BUILDING_STATION_TAGS);
161    }
162
163    /**
164     * Determines if the specified node denotes a power tower/pole.
165     * @param n The node to be tested
166     * @return {@code true} if power key is set and equal to tower/pole
167     */
168    protected static final boolean isPowerTower(Node n) {
169        return isPowerIn(n, POWER_TOWER_TAGS);
170    }
171
172    /**
173     * Determines if the specified node denotes a power infrastructure allowed on a power line.
174     * @param n The node to be tested
175     * @return True if power key is set and equal to switch/tranformer/busbar/generator
176     */
177    protected static final boolean isPowerAllowed(Node n) {
178        return isPowerIn(n, POWER_ALLOWED_TAGS);
179    }
180
181    /**
182     * Helper function to check if power tag is a certain value.
183     * @param p The primitive to be tested
184     * @param values List of possible values
185     * @return {@code true} if power key is set and equal to possible values
186     */
187    private static boolean isPowerIn(OsmPrimitive p, Collection<String> values) {
188        String v = p.get("power");
189        return v != null && values != null && values.contains(v);
190    }
191
192    /**
193     * Helper function to check if building tag is a certain value.
194     * @param p The primitive to be tested
195     * @param values List of possible values
196     * @return {@code true} if power key is set and equal to possible values
197     */
198    private static boolean isBuildingIn(OsmPrimitive p, Collection<String> values) {
199        String v = p.get("building");
200        return v != null && values != null && values.contains(v);
201    }
202}