Changeset 5312 in josm for trunk/src/org/openstreetmap
- Timestamp:
- 2012-07-01T14:27:30+02:00 (12 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/org/openstreetmap/josm/data/validation/tests/PowerLines.java
r5300 r5312 4 4 import static org.openstreetmap.josm.tools.I18n.tr; 5 5 6 import java.util.ArrayList; 7 import java.util.Arrays; 8 import java.util.Collection; 6 9 import java.util.HashMap; 10 import java.util.List; 7 11 import java.util.Map; 8 12 13 import org.openstreetmap.josm.Main; 9 14 import org.openstreetmap.josm.command.ChangePropertyCommand; 10 15 import org.openstreetmap.josm.command.Command; 11 16 import org.openstreetmap.josm.data.osm.Node; 17 import org.openstreetmap.josm.data.osm.OsmPrimitive; 18 import org.openstreetmap.josm.data.osm.Relation; 12 19 import org.openstreetmap.josm.data.osm.Way; 20 import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon; 21 import org.openstreetmap.josm.data.osm.visitor.paint.relations.Multipolygon.JoinedWay; 22 import org.openstreetmap.josm.data.osm.visitor.paint.relations.MultipolygonCache; 13 23 import org.openstreetmap.josm.data.validation.Severity; 14 24 import org.openstreetmap.josm.data.validation.Test; 15 25 import org.openstreetmap.josm.data.validation.TestError; 26 import org.openstreetmap.josm.tools.Geometry; 16 27 17 28 public class PowerLines extends Test { … … 19 30 protected static final int POWER_LINES = 2501; 20 31 21 protected Map<Way, String> towerPoleTagMap = new HashMap<Way, String>(); 32 public static final Collection<String> POWER_LINE_TAGS = Arrays.asList("line", "minor_line"); 33 public static final Collection<String> POWER_TOWER_TAGS = Arrays.asList("tower", "pole"); 34 public static final Collection<String> POWER_STATION_TAGS = Arrays.asList("station", "sub_station", "plant", "generator"); 35 public static final Collection<String> POWER_ALLOWED_TAGS = Arrays.asList("switch", "transformer", "busbar", "generator"); 22 36 37 protected final Map<Way, String> towerPoleTagMap = new HashMap<Way, String>(); 38 39 protected final List<PowerLineError> potentialErrors = new ArrayList<PowerLineError>(); 40 41 protected final List<OsmPrimitive> powerStations = new ArrayList<OsmPrimitive>(); 42 23 43 public PowerLines() { 24 44 super(tr("Power lines"), tr("Checks for nodes in power lines that do not have a power=tower/pole tag.")); … … 27 47 @Override 28 48 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; 49 if (w.isUsable()) { 50 if (isPowerLine(w)) { 51 String fixValue = null; 52 boolean erroneous = false; 53 boolean canFix = false; 54 for (Node n : w.getNodes()) { 55 if (!isPowerTower(n)) { 56 if (!w.isFirstLastNode(n) && !isPowerAllowed(n)) { 57 potentialErrors.add(new PowerLineError(n, w)); 58 erroneous = true; 59 } 60 } else if (fixValue == null) { 61 // First tower/pole tag found, remember it 62 fixValue = n.get("power"); 63 canFix = true; 64 } else if (!fixValue.equals(n.get("power"))) { 65 // The power line contains both "tower" and "pole" -> cannot fix this error 66 canFix = false; 67 } 68 } 69 if (erroneous && canFix) { 70 towerPoleTagMap.put(w, fixValue); 71 } 72 } else if (w.isClosed() && isPowerStation(w)) { 73 powerStations.add(w); 74 } 75 } 76 } 77 78 @Override 79 public void visit(Relation r) { 80 if (r.isMultipolygon() && isPowerStation(r)) { 81 powerStations.add(r); 82 } 83 } 84 85 @Override 86 public void endTest() { 87 for (PowerLineError e : potentialErrors) { 88 if (!isInPowerStation(e.getNode())) { 89 errors.add(e); 90 } 91 } 92 super.endTest(); 93 } 94 95 protected final boolean isInPowerStation(Node n) { 96 for (OsmPrimitive station : powerStations) { 97 List<List<Node>> nodesLists = new ArrayList<List<Node>>(); 98 if (station instanceof Way) { 99 nodesLists.add(((Way)station).getNodes()); 100 } else if (station instanceof Relation) { 101 Multipolygon polygon = MultipolygonCache.getInstance().get(Main.map.mapView, (Relation) station); 102 if (polygon != null) { 103 for (JoinedWay outer : Multipolygon.joinWays(polygon.getOuterWays())) { 104 nodesLists.add(outer.getNodes()); 105 } 44 106 } 45 107 } 46 if (erroneous && canFix) { 47 towerPoleTagMap.put(w, fixValue); 108 for (List<Node> nodes : nodesLists) { 109 if (Geometry.nodeInsidePolygon(n, nodes)) { 110 return true; 111 } 48 112 } 49 113 } 114 return false; 50 115 } 51 116 … … 71 136 */ 72 137 protected static final boolean isPowerLine(Way w) { 73 String v = w.get("power"); 74 return v != null && (v.equals("line") || v.equals("minor_line")); 138 return isPowerIn(w, POWER_LINE_TAGS); 139 } 140 141 /** 142 * Determines if the specified primitive denotes a power station. 143 * @param w The way to be tested 144 * @return True if power key is set and equal to station/sub_station/plant 145 */ 146 protected static final boolean isPowerStation(OsmPrimitive p) { 147 return isPowerIn(p, POWER_STATION_TAGS); 75 148 } 76 149 … … 81 154 */ 82 155 protected static final boolean isPowerTower(Node n) { 83 String v = n.get("power"); 84 return v != null && (v.equals("tower") || v.equals("pole")); 156 return isPowerIn(n, POWER_TOWER_TAGS); 157 } 158 159 /** 160 * Determines if the specified node denotes a power infrastructure allowed on a power line. 161 * @param w The node to be tested 162 * @return True if power key is set and equal to switch/tranformer/busbar/generator 163 */ 164 protected static final boolean isPowerAllowed(Node n) { 165 return isPowerIn(n, POWER_ALLOWED_TAGS); 166 } 167 168 private static final boolean isPowerIn(OsmPrimitive p, Collection<String> values) { 169 String v = p.get("power"); 170 return v != null && values != null && values.contains(v); 85 171 } 86 172 … … 92 178 this.line = line; 93 179 } 180 public final Node getNode() { 181 return (Node) getPrimitives().iterator().next(); 182 } 94 183 } 95 184 }
Note:
See TracChangeset
for help on using the changeset viewer.