| | 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.validation.Severity; |
| | 18 | import org.openstreetmap.josm.data.validation.Test; |
| | 19 | import org.openstreetmap.josm.data.validation.TestError; |
| | 20 | import org.openstreetmap.josm.gui.progress.ProgressMonitor; |
| | 21 | import org.openstreetmap.josm.tools.Geometry; |
| | 22 | import org.openstreetmap.josm.tools.Logging; |
| | 23 | |
| | 24 | /** |
| | 25 | * Finds issues with highway intersections |
| | 26 | * @author Taylor Smock |
| | 27 | * @since xxx |
| | 28 | */ |
| | 29 | public class IntersectionIssues extends Test { |
| | 30 | private static final int INTERSECTIONISSUESCODE = 3800; |
| | 31 | /** The code for an intersection which briefly interrupts a road */ |
| | 32 | public static final int SHORT_DISCONNECT = INTERSECTIONISSUESCODE + 0; |
| | 33 | /** The code for a node that is almost on a way */ |
| | 34 | public static final int NEARBY_NODE = INTERSECTIONISSUESCODE + 1; |
| | 35 | /** The distance to consider for nearby nodes/short disconnects */ |
| | 36 | public static final double maxDistance = 5.0; // meters |
| | 37 | /** The distance to consider for nearby nodes with tags */ |
| | 38 | public static final double maxDistanceNodeInformation = maxDistance / 5.0; // meters |
| | 39 | /** The maximum angle for almost overlapping ways */ |
| | 40 | public static final double maxAngle = 15.0; |
| | 41 | |
| | 42 | private HashMap<String, ArrayList<Way>> ways; |
| | 43 | ArrayList<Way> allWays; |
| | 44 | |
| | 45 | /** |
| | 46 | * Construct a new {@code IntersectionIssues} object |
| | 47 | */ |
| | 48 | public IntersectionIssues() { |
| | 49 | super(tr("Intersection Issues"), tr("Check for issues at intersections"), OverlappingWays.class); |
| | 50 | } |
| | 51 | |
| | 52 | @Override |
| | 53 | public void startTest(ProgressMonitor monitor) { |
| | 54 | super.startTest(monitor); |
| | 55 | ways = new HashMap<>(); |
| | 56 | allWays = new ArrayList<>(); |
| | 57 | } |
| | 58 | |
| | 59 | @Override |
| | 60 | public void endTest() { |
| | 61 | Way pWay = null; |
| | 62 | try { |
| | 63 | for (String key : ways.keySet()) { |
| | 64 | ArrayList<Way> comparison = ways.get(key); |
| | 65 | pWay = comparison.get(0); |
| | 66 | checkNearbyEnds(comparison); |
| | 67 | } |
| | 68 | for (Way way : allWays) { |
| | 69 | pWay = way; |
| | 70 | for (Way way2 : allWays) { |
| | 71 | if (way2.equals(way)) continue; |
| | 72 | pWay = way2; |
| | 73 | if (way.getBBox().intersects(way2.getBBox())) { |
| | 74 | checkNearbyNodes(way, way2); |
| | 75 | } |
| | 76 | } |
| | 77 | } |
| | 78 | } catch (Exception e) { |
| | 79 | if (pWay != null) { |
| | 80 | System.out.printf("Way https://osm.org/way/%d caused an error".concat(System.lineSeparator()), pWay.getOsmId()); |
| | 81 | } |
| | 82 | e.printStackTrace(); |
| | 83 | } |
| | 84 | ways = null; |
| | 85 | allWays = null; |
| | 86 | super.endTest(); |
| | 87 | } |
| | 88 | |
| | 89 | @Override |
| | 90 | public void visit(Way way) { |
| | 91 | if (!way.isUsable()) return; |
| | 92 | if (way.hasKey("highway")) { |
| | 93 | String[] identityTags = new String[] {"name", "ref"}; |
| | 94 | for (String tag : identityTags) { |
| | 95 | if (way.hasKey(tag)) { |
| | 96 | ArrayList<Way> similar = new ArrayList<>(); |
| | 97 | if (ways.containsKey(way.get(tag))) similar = ways.get(way.get(tag)); |
| | 98 | |
| | 99 | if (!similar.contains(way)) similar.add(way); |
| | 100 | ways.put(way.get(tag), similar); |
| | 101 | } |
| | 102 | } |
| | 103 | if (!allWays.contains(way)) allWays.add(way); |
| | 104 | } |
| | 105 | } |
| | 106 | |
| | 107 | /** |
| | 108 | * Check for ends that are nearby but not directly connected |
| | 109 | * @param comparison Ways to look at |
| | 110 | */ |
| | 111 | public void checkNearbyEnds(ArrayList<Way> comparison) { |
| | 112 | ArrayList<Way> errored = new ArrayList<>(); |
| | 113 | for (Way one : comparison) { |
| | 114 | LatLon oneLast = one.lastNode().getCoor(); |
| | 115 | LatLon oneFirst = one.firstNode().getCoor(); |
| | 116 | for (Way two : comparison) { |
| | 117 | if (one.isFirstLastNode(two.firstNode()) || one.isFirstLastNode(two.lastNode()) || |
| | 118 | (errored.contains(one) && errored.contains(two))) continue; |
| | 119 | LatLon twoLast = two.lastNode().getCoor(); |
| | 120 | LatLon twoFirst = two.firstNode().getCoor(); |
| | 121 | if (twoLast.greatCircleDistance(oneLast) <= maxDistance || |
| | 122 | twoLast.greatCircleDistance(oneFirst) <= maxDistance || |
| | 123 | twoFirst.greatCircleDistance(oneLast) <= maxDistance || |
| | 124 | twoFirst.greatCircleDistance(oneFirst) <= maxDistance) { |
| | 125 | List<Way> nearby = new ArrayList<>(); |
| | 126 | nearby.add(one); |
| | 127 | nearby.add(two); |
| | 128 | errored.addAll(nearby); |
| | 129 | allWays.removeAll(errored); |
| | 130 | TestError.Builder testError = TestError.builder(this, Severity.WARNING, SHORT_DISCONNECT) |
| | 131 | .primitives(nearby) |
| | 132 | .message(tr("Disconnected road")); |
| | 133 | errors.add(testError.build()); |
| | 134 | } |
| | 135 | } |
| | 136 | } |
| | 137 | } |
| | 138 | |
| | 139 | /** |
| | 140 | * Check nearby nodes to an intersection of two ways |
| | 141 | * @param way1 A way to check an almost intersection with |
| | 142 | * @param way2 A way to check an almost intersection with |
| | 143 | */ |
| | 144 | public void checkNearbyNodes(Way way1, Way way2) { |
| | 145 | Node intersectingNode = getIntersectingNode(way1, way2); |
| | 146 | if (intersectingNode == null) return; |
| | 147 | checkNearbyNodes(way1, way2, intersectingNode); |
| | 148 | checkNearbyNodes(way2, way1, intersectingNode); |
| | 149 | } |
| | 150 | |
| | 151 | private void checkNearbyNodes(Way way1, Way way2, Node nearby) { |
| | 152 | for (Node node : way1.getNeighbours(nearby)) { |
| | 153 | if (node.equals(nearby)) continue; |
| | 154 | WayPoint waypoint = new WayPoint(node.getCoor()); |
| | 155 | double distance = GpxDistance.getDistance(way2, waypoint); |
| | 156 | if (((distance < maxDistance && !node.isTagged()) |
| | 157 | || (distance < maxDistanceNodeInformation && node.isTagged())) |
| | 158 | && getSmallestAngle(way2, nearby, node) < maxAngle) { |
| | 159 | List<Way> primitiveIssues = new ArrayList<>(); |
| | 160 | primitiveIssues.add(way1); |
| | 161 | primitiveIssues.add(way2); |
| | 162 | for (TestError error : getErrors()) { |
| | 163 | int code = error.getCode(); |
| | 164 | if ((code == SHORT_DISCONNECT || code == NEARBY_NODE |
| | 165 | || code == OverlappingWays.OVERLAPPING_HIGHWAY |
| | 166 | || code == OverlappingWays.DUPLICATE_WAY_SEGMENT |
| | 167 | || code == OverlappingWays.OVERLAPPING_HIGHWAY_AREA |
| | 168 | || code == OverlappingWays.OVERLAPPING_WAY |
| | 169 | || code == OverlappingWays.OVERLAPPING_WAY_AREA |
| | 170 | || code == OverlappingWays.OVERLAPPING_RAILWAY |
| | 171 | || code == OverlappingWays.OVERLAPPING_RAILWAY_AREA) |
| | 172 | && primitiveIssues.containsAll(error.getPrimitives())) { |
| | 173 | Logging.info("{0}: Found duplicate error", getName()); |
| | 174 | return; |
| | 175 | } |
| | 176 | } |
| | 177 | TestError.Builder testError = TestError.builder(this, Severity.WARNING, NEARBY_NODE) |
| | 178 | .primitives(primitiveIssues) |
| | 179 | .message(tr("Almost overlapping highways")); |
| | 180 | errors.add(testError.build()); |
| | 181 | } |
| | 182 | } |
| | 183 | } |
| | 184 | |
| | 185 | /** |
| | 186 | * Get the intersecting node of two ways |
| | 187 | * @param way1 A way that (hopefully) intersects with way2 |
| | 188 | * @param way2 A way to find an intersection with |
| | 189 | * @return {@code Node} if there is an intersecting node, {@code null} otherwise |
| | 190 | */ |
| | 191 | public Node getIntersectingNode(Way way1, Way way2) { |
| | 192 | for (Node node : way1.getNodes()) { |
| | 193 | if (way2.containsNode(node)) { |
| | 194 | return node; |
| | 195 | } |
| | 196 | } |
| | 197 | return null; |
| | 198 | } |
| | 199 | |
| | 200 | /** |
| | 201 | * Get the corner angle between nodes |
| | 202 | * @param way The way with additional nodes |
| | 203 | * @param intersection The node to get angles around |
| | 204 | * @param comparison The node to get angles from |
| | 205 | * @return The angle for comparison->intersection->(additional node) (normalized degrees) |
| | 206 | */ |
| | 207 | public double getSmallestAngle(Way way, Node intersection, Node comparison) { |
| | 208 | Set<Node> neighbours = way.getNeighbours(intersection); |
| | 209 | double angle = Double.MAX_VALUE; |
| | 210 | EastNorth eastNorthIntersection = intersection.getEastNorth(); |
| | 211 | EastNorth eastNorthComparison = comparison.getEastNorth(); |
| | 212 | for (Node node : neighbours) { |
| | 213 | EastNorth eastNorthNode = node.getEastNorth(); |
| | 214 | double tAngle = Geometry.getCornerAngle(eastNorthComparison, eastNorthIntersection, eastNorthNode); |
| | 215 | if (Math.abs(tAngle) < angle) angle = Math.abs(tAngle); |
| | 216 | } |
| | 217 | return Geometry.getNormalizedAngleInDegrees(angle); |
| | 218 | } |
| | 219 | } |