| | 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.Collection; |
| | 8 | import java.util.Collections; |
| | 9 | import java.util.Comparator; |
| | 10 | import java.util.LinkedHashMap; |
| | 11 | import java.util.List; |
| | 12 | import java.util.Map; |
| | 13 | import java.util.Map.Entry; |
| | 14 | import java.util.Optional; |
| | 15 | import java.util.TreeSet; |
| | 16 | |
| | 17 | import org.openstreetmap.josm.data.coor.EastNorth; |
| | 18 | import org.openstreetmap.josm.data.osm.Node; |
| | 19 | import org.openstreetmap.josm.data.osm.OsmPrimitive; |
| | 20 | import org.openstreetmap.josm.data.osm.Way; |
| | 21 | import org.openstreetmap.josm.data.osm.WaySegment; |
| | 22 | import org.openstreetmap.josm.data.validation.Severity; |
| | 23 | import org.openstreetmap.josm.data.validation.Test; |
| | 24 | import org.openstreetmap.josm.data.validation.TestError; |
| | 25 | import org.openstreetmap.josm.gui.progress.ProgressMonitor; |
| | 26 | import org.openstreetmap.josm.tools.Geometry; |
| | 27 | import org.openstreetmap.josm.tools.Logging; |
| | 28 | |
| | 29 | /** |
| | 30 | * Find highways that have sharp angles |
| | 31 | * @author Taylor Smock |
| | 32 | * |
| | 33 | */ |
| | 34 | public class SharpAngles extends Test { |
| | 35 | private static final int SHARPANGLESCODE = 3800; |
| | 36 | /** The code for a sharp angle */ |
| | 37 | protected static final int SHARP_ANGLES = SHARPANGLESCODE + 0; |
| | 38 | /** The maximum angle for sharp angles */ |
| | 39 | protected static double MAX_ANGLE = 45.0; // degrees |
| | 40 | /** The length that at least one way segment must be shorter than */ |
| | 41 | protected static double MAX_LENGTH = 10.0; // meters |
| | 42 | /** The stepping points for severity */ |
| | 43 | protected static Map<Double, Severity> severityBreakPoints = new LinkedHashMap<>(); |
| | 44 | /** Specific highway types to ignore */ |
| | 45 | protected static Collection<String> ignoreHighways = new TreeSet<>(); |
| | 46 | static { |
| | 47 | setBreakPoints(); |
| | 48 | addIgnoredHighway("rest_area"); |
| | 49 | addIgnoredHighway("platform"); |
| | 50 | addIgnoredHighway("services"); |
| | 51 | addIgnoredHighway("via_ferrata"); // mountainside paths |
| | 52 | } |
| | 53 | |
| | 54 | ArrayList<Way> allWays; |
| | 55 | /** |
| | 56 | * Construct a new {@code IntersectionIssues} object |
| | 57 | */ |
| | 58 | public SharpAngles() { |
| | 59 | super(tr("Sharp angles"), tr("Check for sharp angles on roads")); |
| | 60 | } |
| | 61 | |
| | 62 | @Override |
| | 63 | public void startTest(ProgressMonitor monitor) { |
| | 64 | super.startTest(monitor); |
| | 65 | allWays = new ArrayList<>(); |
| | 66 | } |
| | 67 | |
| | 68 | @Override |
| | 69 | public void endTest() { |
| | 70 | Way pWay = null; |
| | 71 | try { |
| | 72 | for (Way way : allWays) { |
| | 73 | pWay = way; |
| | 74 | checkWayForSharpAngles(way); |
| | 75 | } |
| | 76 | } catch (Exception e) { |
| | 77 | if (pWay != null) { |
| | 78 | Logging.debug("Way https://osm.org/way/{0} caused an error ({1})", pWay.getOsmId(), e); |
| | 79 | } |
| | 80 | Logging.warn(e); |
| | 81 | } |
| | 82 | allWays = null; |
| | 83 | super.endTest(); |
| | 84 | } |
| | 85 | |
| | 86 | @Override |
| | 87 | public void visit(Way way) { |
| | 88 | if (!way.isUsable()) return; |
| | 89 | if (way.hasKey("highway")) { |
| | 90 | if ((way.hasKey("area") && way.get("area").equalsIgnoreCase("yes")) || |
| | 91 | ignoreHighways.contains(way.get("highway"))) { |
| | 92 | return; |
| | 93 | } |
| | 94 | allWays.add(way); |
| | 95 | } |
| | 96 | } |
| | 97 | |
| | 98 | /** |
| | 99 | * Check nodes in a way for sharp angles |
| | 100 | * @param way A way to check for sharp angles |
| | 101 | */ |
| | 102 | public void checkWayForSharpAngles(Way way) { |
| | 103 | Node node1 = null; |
| | 104 | Node node2 = null; |
| | 105 | Node node3 = null; |
| | 106 | int i = -2; |
| | 107 | for (Node node : way.getNodes()) { |
| | 108 | node1 = node2; |
| | 109 | node2 = node3; |
| | 110 | node3 = node; |
| | 111 | checkAngle(node1, node2, node3, i, way, false); |
| | 112 | i++; |
| | 113 | } |
| | 114 | if (way.isClosed() && way.getNodesCount() > 2) { |
| | 115 | node1 = node2; |
| | 116 | node2 = node3; |
| | 117 | // Get the second node, not the first node, since a closed way has first node == second node |
| | 118 | node3 = way.getNode(1); |
| | 119 | checkAngle(node1, node2, node3, i, way, true); |
| | 120 | } |
| | 121 | } |
| | 122 | |
| | 123 | private void checkAngle(Node node1, Node node2, Node node3, Integer i, Way way, Boolean last) { |
| | 124 | if (node1 == null || node2 == null || node3 == null) return; |
| | 125 | EastNorth n1 = node1.getEastNorth(); |
| | 126 | EastNorth n2 = node2.getEastNorth(); |
| | 127 | EastNorth n3 = node3.getEastNorth(); |
| | 128 | Double angle = Math.toDegrees(Math.abs(Geometry.getCornerAngle(n1, n2, n3))); |
| | 129 | if (angle < MAX_ANGLE) { |
| | 130 | processSharpAngleForErrorCreation(angle, i, way, last); |
| | 131 | } |
| | 132 | } |
| | 133 | |
| | 134 | private void processSharpAngleForErrorCreation(Double angle, Integer i, Way way, Boolean last) { |
| | 135 | List<WaySegment> waysegmentList = new ArrayList<>(); |
| | 136 | waysegmentList.add(new WaySegment(way, i)); |
| | 137 | if (last) { |
| | 138 | waysegmentList.add(new WaySegment(way, 0)); |
| | 139 | } else { |
| | 140 | waysegmentList.add(new WaySegment(way, i+1)); |
| | 141 | } |
| | 142 | Optional<WaySegment> possibleShortSegment = waysegmentList.stream() |
| | 143 | .min(Comparator.comparing(segment -> segment.toWay().getLength())); |
| | 144 | if (possibleShortSegment.isPresent() && possibleShortSegment.get().toWay().getLength() < MAX_LENGTH) { |
| | 145 | createNearlyOverlappingError(angle, Collections.singleton(way), waysegmentList); |
| | 146 | } |
| | 147 | } |
| | 148 | |
| | 149 | private void createNearlyOverlappingError(Double angle, |
| | 150 | Collection<? extends OsmPrimitive> primitiveIssues, Collection<WaySegment> waysegments) { |
| | 151 | TestError.Builder testError = TestError.builder(this, getSeverity(angle), SHARP_ANGLES) |
| | 152 | .primitives(primitiveIssues) |
| | 153 | .highlightWaySegments(waysegments) |
| | 154 | .message(tr("Sharp angle")); |
| | 155 | errors.add(testError.build()); |
| | 156 | } |
| | 157 | |
| | 158 | private Severity getSeverity(Double angle) { |
| | 159 | Severity rSeverity = Severity.OTHER; |
| | 160 | for (Entry<Double, Severity> entry : severityBreakPoints.entrySet()) { |
| | 161 | if (angle < entry.getKey()) { |
| | 162 | rSeverity = entry.getValue(); |
| | 163 | } |
| | 164 | } |
| | 165 | return rSeverity; |
| | 166 | } |
| | 167 | |
| | 168 | /** |
| | 169 | * Set the maximum length for the shortest segment |
| | 170 | * @param length The max length in meters |
| | 171 | */ |
| | 172 | public static void setMaxLength(double length) { |
| | 173 | MAX_LENGTH = length; |
| | 174 | } |
| | 175 | |
| | 176 | /** |
| | 177 | * Add a highway to ignore |
| | 178 | * @param highway |
| | 179 | */ |
| | 180 | public static void addIgnoredHighway(String highway) { |
| | 181 | ignoreHighways.add(highway); |
| | 182 | } |
| | 183 | |
| | 184 | /** |
| | 185 | * Set the maximum angle |
| | 186 | * @param angle The maximum angle in degrees. |
| | 187 | */ |
| | 188 | public static void setMaxAngle(double angle) { |
| | 189 | MAX_ANGLE = angle; |
| | 190 | setBreakPoints(); |
| | 191 | } |
| | 192 | |
| | 193 | /** |
| | 194 | * Set the breakpoints for the test |
| | 195 | */ |
| | 196 | private static void setBreakPoints() { |
| | 197 | severityBreakPoints.put(MAX_ANGLE, Severity.OTHER); |
| | 198 | severityBreakPoints.put(MAX_ANGLE * 2 / 3, Severity.WARNING); |
| | 199 | severityBreakPoints.put(MAX_ANGLE / 3, Severity.ERROR); |
| | 200 | } |
| | 201 | |
| | 202 | @Override |
| | 203 | public boolean equals(Object other) { |
| | 204 | if (other instanceof SharpAngles) { |
| | 205 | SharpAngles otherReal = (SharpAngles) other; |
| | 206 | if (otherReal.allWays.equals(allWays)) { |
| | 207 | return true; |
| | 208 | } |
| | 209 | } |
| | 210 | return false; |
| | 211 | } |
| | 212 | |
| | 213 | @Override |
| | 214 | public int hashCode() { |
| | 215 | return super.hashCode(); |
| | 216 | } |
| | 217 | } |