| | 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.HashMap; |
| | 8 | import java.util.List; |
| | 9 | import java.util.Set; |
| | 10 | |
| | 11 | import org.openstreetmap.josm.data.coor.EastNorth; |
| | 12 | import org.openstreetmap.josm.data.coor.LatLon; |
| | 13 | import org.openstreetmap.josm.data.gpx.GpxDistance; |
| | 14 | import org.openstreetmap.josm.data.gpx.WayPoint; |
| | 15 | import org.openstreetmap.josm.data.osm.Node; |
| | 16 | import org.openstreetmap.josm.data.osm.Way; |
| | 17 | import org.openstreetmap.josm.data.osm.WaySegment; |
| | 18 | import org.openstreetmap.josm.data.validation.Severity; |
| | 19 | import org.openstreetmap.josm.data.validation.Test; |
| | 20 | import org.openstreetmap.josm.data.validation.TestError; |
| | 21 | import org.openstreetmap.josm.gui.progress.ProgressMonitor; |
| | 22 | import org.openstreetmap.josm.tools.Geometry; |
| | 23 | import org.openstreetmap.josm.tools.Logging; |
| | 24 | |
| | 25 | /** |
| | 26 | * Finds issues with highway intersections |
| | 27 | * @author Taylor Smock |
| | 28 | * @since xxx |
| | 29 | */ |
| | 30 | public class IntersectionIssues extends Test { |
| | 31 | private static final int INTERSECTIONISSUESCODE = 3800; |
| | 32 | /** The code for an intersection which briefly interrupts a road */ |
| | 33 | public static final int SHORT_DISCONNECT = INTERSECTIONISSUESCODE + 0; |
| | 34 | /** The code for a node that is almost on a way */ |
| | 35 | public static final int NEARBY_NODE = INTERSECTIONISSUESCODE + 1; |
| | 36 | /** The distance to consider for nearby nodes/short disconnects */ |
| | 37 | public static final double MAX_DISTANCE = 5.0; // meters |
| | 38 | /** The distance to consider for nearby nodes with tags */ |
| | 39 | public static final double MAX_DISTANCE_NODE_INFORMATION = MAX_DISTANCE / 5.0; // meters |
| | 40 | /** The maximum angle for almost overlapping ways */ |
| | 41 | public static final double MAX_ANGLE = 15.0; // degrees |
| | 42 | /** The maximum length to consider for almost overlapping ways */ |
| | 43 | public static final double MAX_LENGTH = 5.0; // meters |
| | 44 | |
| | 45 | private HashMap<String, ArrayList<Way>> ways; |
| | 46 | ArrayList<Way> allWays; |
| | 47 | |
| | 48 | /** |
| | 49 | * Construct a new {@code IntersectionIssues} object |
| | 50 | */ |
| | 51 | public IntersectionIssues() { |
| | 52 | super(tr("Intersection Issues"), tr("Check for issues at intersections"), OverlappingWays.class); |
| | 53 | } |
| | 54 | |
| | 55 | @Override |
| | 56 | public void startTest(ProgressMonitor monitor) { |
| | 57 | super.startTest(monitor); |
| | 58 | ways = new HashMap<>(); |
| | 59 | allWays = new ArrayList<>(); |
| | 60 | } |
| | 61 | |
| | 62 | @Override |
| | 63 | public void endTest() { |
| | 64 | Way pWay = null; |
| | 65 | try { |
| | 66 | for (String key : ways.keySet()) { |
| | 67 | ArrayList<Way> comparison = ways.get(key); |
| | 68 | pWay = comparison.get(0); |
| | 69 | checkNearbyEnds(comparison); |
| | 70 | } |
| | 71 | for (Way way : allWays) { |
| | 72 | pWay = way; |
| | 73 | for (Way way2 : allWays) { |
| | 74 | if (way2.equals(way)) continue; |
| | 75 | pWay = way2; |
| | 76 | if (way.getBBox().intersects(way2.getBBox())) { |
| | 77 | checkNearbyNodes(way, way2); |
| | 78 | } |
| | 79 | } |
| | 80 | } |
| | 81 | } catch (Exception e) { |
| | 82 | if (pWay != null) { |
| | 83 | Logging.debug("Way https://osm.org/way/{0} caused an error", pWay.getOsmId()); |
| | 84 | } |
| | 85 | Logging.warn(e); |
| | 86 | } |
| | 87 | ways = null; |
| | 88 | allWays = null; |
| | 89 | super.endTest(); |
| | 90 | } |
| | 91 | |
| | 92 | @Override |
| | 93 | public void visit(Way way) { |
| | 94 | if (!way.isUsable()) return; |
| | 95 | if (way.hasKey("highway") && !way.get("highway").contains("_link")) { |
| | 96 | String[] identityTags = new String[] {"name", "ref"}; |
| | 97 | for (String tag : identityTags) { |
| | 98 | if (way.hasKey(tag)) { |
| | 99 | ArrayList<Way> similar = ways.get(way.get(tag)) == null ? new ArrayList<>() : ways.get(way.get(tag)); |
| | 100 | if (!similar.contains(way)) similar.add(way); |
| | 101 | ways.put(way.get(tag), similar); |
| | 102 | } |
| | 103 | } |
| | 104 | if (!allWays.contains(way)) allWays.add(way); |
| | 105 | } |
| | 106 | } |
| | 107 | |
| | 108 | /** |
| | 109 | * Check for ends that are nearby but not directly connected |
| | 110 | * @param comparison Ways to look at |
| | 111 | */ |
| | 112 | public void checkNearbyEnds(ArrayList<Way> comparison) { |
| | 113 | ArrayList<Way> errored = new ArrayList<>(); |
| | 114 | for (Way one : comparison) { |
| | 115 | LatLon oneLast = one.lastNode().getCoor(); |
| | 116 | LatLon oneFirst = one.firstNode().getCoor(); |
| | 117 | for (Way two : comparison) { |
| | 118 | if (one.equals(two)) continue; |
| | 119 | if (one.isFirstLastNode(two.firstNode()) || one.isFirstLastNode(two.lastNode()) || |
| | 120 | (errored.contains(one) && errored.contains(two))) continue; |
| | 121 | LatLon twoLast = two.lastNode().getCoor(); |
| | 122 | LatLon twoFirst = two.firstNode().getCoor(); |
| | 123 | int nearCase = getNearCase(oneFirst, oneLast, twoFirst, twoLast); |
| | 124 | if (nearCase != 0) { |
| | 125 | for (Way way : two.lastNode().getParentWays()) { |
| | 126 | if (way.equals(two)) continue; |
| | 127 | if (one.hasKey("name") && way.hasKey("name") && way.get("name").equals(one.get("name")) || |
| | 128 | one.hasKey("ref") && way.hasKey("ref") && way.get("ref").equals(one.get("ref"))) { |
| | 129 | return; |
| | 130 | } |
| | 131 | } |
| | 132 | for (Way way : two.firstNode().getParentWays()) { |
| | 133 | if (way.equals(two)) continue; |
| | 134 | if (one.hasKey("name") && way.hasKey("name") && way.get("name").equals(one.get("name")) || |
| | 135 | one.hasKey("ref") && way.hasKey("ref") && way.get("ref").equals(one.get("ref"))) { |
| | 136 | return; |
| | 137 | } |
| | 138 | } |
| | 139 | } |
| | 140 | if (nearCase > 0) { |
| | 141 | List<Way> nearby = new ArrayList<>(); |
| | 142 | nearby.add(one); |
| | 143 | nearby.add(two); |
| | 144 | List<WaySegment> segments = new ArrayList<>(); |
| | 145 | if ((nearCase & 1) != 0) { |
| | 146 | segments.add(new WaySegment(two, two.getNodesCount() - 2)); |
| | 147 | segments.add(new WaySegment(one, one.getNodesCount() - 2)); |
| | 148 | } |
| | 149 | if ((nearCase & 2) != 0) { |
| | 150 | segments.add(new WaySegment(two, two.getNodesCount() - 2)); |
| | 151 | segments.add(new WaySegment(one, 0)); |
| | 152 | } |
| | 153 | if ((nearCase & 4) != 0) { |
| | 154 | segments.add(new WaySegment(two, 0)); |
| | 155 | segments.add(new WaySegment(one, one.getNodesCount() - 2)); |
| | 156 | } |
| | 157 | if ((nearCase & 8) != 0) { |
| | 158 | segments.add(new WaySegment(two, 0)); |
| | 159 | segments.add(new WaySegment(one, 0)); |
| | 160 | } |
| | 161 | errored.addAll(nearby); |
| | 162 | allWays.removeAll(errored); |
| | 163 | TestError.Builder testError = TestError.builder(this, Severity.WARNING, SHORT_DISCONNECT) |
| | 164 | .primitives(nearby) |
| | 165 | .highlightWaySegments(segments) |
| | 166 | .message(tr("Disconnected road")); |
| | 167 | errors.add(testError.build()); |
| | 168 | } |
| | 169 | } |
| | 170 | } |
| | 171 | } |
| | 172 | |
| | 173 | /** |
| | 174 | * Get nearby cases |
| | 175 | * @param oneFirst The {@code LatLon} of the the first node of the first way |
| | 176 | * @param oneLast The {@code LatLon} of the the last node of the first way |
| | 177 | * @param twoFirst The {@code LatLon} of the the first node of the second way |
| | 178 | * @param twoLast The {@code LatLon} of the the last node of the second way |
| | 179 | * @return A bitwise int (8421 -> twoFirst/oneFirst, twoFirst/oneLast, twoLast/oneFirst, twoLast/oneLast) |
| | 180 | * |
| | 181 | */ |
| | 182 | private int getNearCase(LatLon oneFirst, LatLon oneLast, LatLon twoFirst, LatLon twoLast) { |
| | 183 | int returnInt = 0; |
| | 184 | if (twoLast.greatCircleDistance(oneLast) <= MAX_DISTANCE) { |
| | 185 | returnInt = returnInt | 1; |
| | 186 | } |
| | 187 | if (twoLast.greatCircleDistance(oneFirst) <= MAX_DISTANCE) { |
| | 188 | returnInt = returnInt | 2; |
| | 189 | } |
| | 190 | if (twoFirst.greatCircleDistance(oneLast) <= MAX_DISTANCE) { |
| | 191 | returnInt = returnInt | 4; |
| | 192 | } |
| | 193 | if (twoFirst.greatCircleDistance(oneFirst) <= MAX_DISTANCE) { |
| | 194 | returnInt = returnInt | 8; |
| | 195 | } |
| | 196 | return returnInt; |
| | 197 | } |
| | 198 | |
| | 199 | /** |
| | 200 | * Check nearby nodes to an intersection of two ways |
| | 201 | * @param way1 A way to check an almost intersection with |
| | 202 | * @param way2 A way to check an almost intersection with |
| | 203 | */ |
| | 204 | public void checkNearbyNodes(Way way1, Way way2) { |
| | 205 | Node intersectingNode = getIntersectingNode(way1, way2); |
| | 206 | if (intersectingNode == null) return; |
| | 207 | checkNearbyNodes(way1, way2, intersectingNode); |
| | 208 | checkNearbyNodes(way2, way1, intersectingNode); |
| | 209 | } |
| | 210 | |
| | 211 | private void checkNearbyNodes(Way way1, Way way2, Node nearby) { |
| | 212 | for (Node node : way1.getNeighbours(nearby)) { |
| | 213 | if (node.equals(nearby) || way2.containsNode(node)) continue; |
| | 214 | WayPoint waypoint = new WayPoint(node.getCoor()); |
| | 215 | double distance = GpxDistance.getDistance(way2, waypoint); |
| | 216 | double angle = getSmallestAngle(way2, nearby, node); |
| | 217 | if (((distance < MAX_DISTANCE && !node.isTagged()) |
| | 218 | || (distance < MAX_DISTANCE_NODE_INFORMATION && node.isTagged())) |
| | 219 | && angle < MAX_ANGLE) { |
| | 220 | List<Way> primitiveIssues = new ArrayList<>(); |
| | 221 | primitiveIssues.add(way1); |
| | 222 | primitiveIssues.add(way2); |
| | 223 | List<TestError> tErrors = new ArrayList<>(); |
| | 224 | tErrors.addAll(previousErrors); |
| | 225 | tErrors.addAll(getErrors()); |
| | 226 | for (TestError error : tErrors) { |
| | 227 | int code = error.getCode(); |
| | 228 | if ((code == SHORT_DISCONNECT || code == NEARBY_NODE |
| | 229 | || code == OverlappingWays.OVERLAPPING_HIGHWAY |
| | 230 | || code == OverlappingWays.DUPLICATE_WAY_SEGMENT |
| | 231 | || code == OverlappingWays.OVERLAPPING_HIGHWAY_AREA |
| | 232 | || code == OverlappingWays.OVERLAPPING_WAY |
| | 233 | || code == OverlappingWays.OVERLAPPING_WAY_AREA |
| | 234 | || code == OverlappingWays.OVERLAPPING_RAILWAY |
| | 235 | || code == OverlappingWays.OVERLAPPING_RAILWAY_AREA) |
| | 236 | && primitiveIssues.containsAll(error.getPrimitives())) { |
| | 237 | return; |
| | 238 | } |
| | 239 | } |
| | 240 | List<WaySegment> waysegments = new ArrayList<>(); |
| | 241 | int index = way1.getNodes().indexOf(nearby); |
| | 242 | if (index >= way1.getNodesCount() - 1) index--; |
| | 243 | waysegments.add(new WaySegment(way1, index)); |
| | 244 | if (index > 0) waysegments.add(new WaySegment(way1, index - 1)); |
| | 245 | index = way2.getNodes().indexOf(nearby); |
| | 246 | if (index >= way2.getNodesCount() - 1) index--; |
| | 247 | waysegments.add(new WaySegment(way2, index)); |
| | 248 | if (index > 0) waysegments.add(new WaySegment(way2, index - 1)); |
| | 249 | |
| | 250 | TestError.Builder testError = TestError.builder(this, Severity.WARNING, NEARBY_NODE) |
| | 251 | .primitives(primitiveIssues) |
| | 252 | .highlightWaySegments(waysegments) |
| | 253 | .message(tr("Almost overlapping highways")); |
| | 254 | errors.add(testError.build()); |
| | 255 | } |
| | 256 | } |
| | 257 | } |
| | 258 | |
| | 259 | /** |
| | 260 | * Get the intersecting node of two ways |
| | 261 | * @param way1 A way that (hopefully) intersects with way2 |
| | 262 | * @param way2 A way to find an intersection with |
| | 263 | * @return {@code Node} if there is an intersecting node, {@code null} otherwise |
| | 264 | */ |
| | 265 | public Node getIntersectingNode(Way way1, Way way2) { |
| | 266 | for (Node node : way1.getNodes()) { |
| | 267 | if (way2.containsNode(node)) { |
| | 268 | return node; |
| | 269 | } |
| | 270 | } |
| | 271 | return null; |
| | 272 | } |
| | 273 | |
| | 274 | /** |
| | 275 | * Get the corner angle between nodes |
| | 276 | * @param way The way with additional nodes |
| | 277 | * @param intersection The node to get angles around |
| | 278 | * @param comparison The node to get angles from |
| | 279 | * @return The angle for comparison->intersection->(additional node) (normalized degrees) |
| | 280 | */ |
| | 281 | public double getSmallestAngle(Way way, Node intersection, Node comparison) { |
| | 282 | Set<Node> neighbours = way.getNeighbours(intersection); |
| | 283 | double angle = Double.MAX_VALUE; |
| | 284 | EastNorth eastNorthIntersection = intersection.getEastNorth(); |
| | 285 | EastNorth eastNorthComparison = comparison.getEastNorth(); |
| | 286 | for (Node node : neighbours) { |
| | 287 | EastNorth eastNorthNode = node.getEastNorth(); |
| | 288 | double tAngle = Geometry.getCornerAngle(eastNorthComparison, eastNorthIntersection, eastNorthNode); |
| | 289 | if (Math.abs(tAngle) < angle) angle = Math.abs(tAngle); |
| | 290 | } |
| | 291 | return Geometry.getNormalizedAngleInDegrees(angle); |
| | 292 | } |
| | 293 | } |