Ticket #17528: intersectionissues_v3.patch

File intersectionissues_v3.patch, 18.0 KB (added by taylor.smock, 7 years ago)

Filter out items that are not relevant (_links, ways that connect to another short section of with the same ref/name)

  • src/org/openstreetmap/josm/actions/ValidateAction.java

     
    116116        private boolean canceled;
    117117        private List<TestError> errors;
    118118
     119        private List<Class<? extends Test>> runTests;
     120
    119121        /**
    120122         * Constructs a new {@code ValidationTask}
    121123         * @param tests  the tests to run
     
    153155        @Override
    154156        protected void realRun() throws SAXException, IOException,
    155157        OsmTransferException {
     158            runTests = new ArrayList<>();
    156159            if (tests == null || tests.isEmpty())
    157160                return;
    158161            errors = new ArrayList<>(200);
    159162            getProgressMonitor().setTicksCount(tests.size() * validatedPrimitives.size());
    160             int testCounter = 0;
     163            runTests(tests, 0);
     164            tests = null;
     165            if (ValidatorPrefHelper.PREF_USE_IGNORE.get()) {
     166                getProgressMonitor().setCustomText("");
     167                getProgressMonitor().subTask(tr("Updating ignored errors ..."));
     168                for (TestError error : errors) {
     169                    if (canceled) return;
     170                    error.updateIgnored();
     171                }
     172            }
     173        }
     174
     175        protected int runTests(Collection<Test> tests, int testCounter) {
     176            ArrayList<Test> remaining = new ArrayList<>();
    161177            for (Test test : tests) {
    162178                if (canceled)
    163                     return;
     179                    return testCounter;
     180                if (test.getAfterClass() != null && !runTests.contains(test.getAfterClass())) {
     181                    remaining.add(test);
     182                    continue;
     183                }
    164184                testCounter++;
    165                 getProgressMonitor().setCustomText(tr("Test {0}/{1}: Starting {2}", testCounter, tests.size(), test.getName()));
     185                getProgressMonitor().setCustomText(tr("Test {0}/{1}: Starting {2}", testCounter, this.tests.size(), test.getName()));
    166186                test.setPartialSelection(formerValidatedPrimitives != null);
     187                test.setPreviousErrors(errors);
    167188                test.startTest(getProgressMonitor().createSubTaskMonitor(validatedPrimitives.size(), false));
    168189                test.visit(validatedPrimitives);
    169190                test.endTest();
    170191                errors.addAll(test.getErrors());
    171192                test.clear();
     193                runTests.add(test.getClass());
    172194            }
    173             tests = null;
    174             if (ValidatorPrefHelper.PREF_USE_IGNORE.get()) {
    175                 getProgressMonitor().setCustomText("");
    176                 getProgressMonitor().subTask(tr("Updating ignored errors ..."));
    177                 for (TestError error : errors) {
    178                     if (canceled) return;
    179                     error.updateIgnored();
    180                 }
     195            if (!remaining.isEmpty()) {
     196                testCounter = runTests(remaining, testCounter);
    181197            }
     198            return testCounter;
    182199        }
    183200    }
    184201}
  • src/org/openstreetmap/josm/data/validation/OsmValidator.java

     
    4949import org.openstreetmap.josm.data.validation.tests.DuplicatedWayNodes;
    5050import org.openstreetmap.josm.data.validation.tests.Highways;
    5151import org.openstreetmap.josm.data.validation.tests.InternetTags;
     52import org.openstreetmap.josm.data.validation.tests.IntersectionIssues;
    5253import org.openstreetmap.josm.data.validation.tests.Lanes;
    5354import org.openstreetmap.josm.data.validation.tests.LongSegment;
    5455import org.openstreetmap.josm.data.validation.tests.MapCSSTagChecker;
     
    148149        LongSegment.class, // 3500 .. 3599
    149150        PublicTransportRouteTest.class, // 3600 .. 3699
    150151        RightAngleBuildingTest.class, // 3700 .. 3799
     152        IntersectionIssues.class, // 3800 .. 3899
    151153    };
    152154
    153155    /**
  • src/org/openstreetmap/josm/data/validation/Test.java

     
    4646    /** Name of the test */
    4747    protected final String name;
    4848
     49    /** Test to run after */
     50    protected Class<? extends Test> afterTest;
     51
    4952    /** Description of the test */
    5053    protected final String description;
    5154
     
    6770    /** The list of errors */
    6871    protected List<TestError> errors = new ArrayList<>(30);
    6972
     73    /** The list of previously found errors */
     74    protected List<TestError> previousErrors;
     75
    7076    /** Whether the test is run on a partial selection data */
    7177    protected boolean partialSelection;
    7278
     
    8490     * @param description Description of the test
    8591     */
    8692    public Test(String name, String description) {
     93        this(name, description, null);
     94    }
     95
     96    /**
     97     * Constructor
     98     * @param name Name of the test
     99     * @param description Description of the test
     100     * @param afterTest Ensure the test is run after a test with this name
     101     *
     102     * @since xxx
     103     */
     104    public Test(String name, String description, Class<? extends Test> afterTest) {
    87105        this.name = name;
    88106        this.description = description;
     107        this.afterTest = afterTest;
    89108    }
    90109
    91110    /**
     
    178197    }
    179198
    180199    /**
     200     * Set the validation errors accumulated by other tests until this moment
     201     * For validation errors accumulated by this test, use {@code getErrors()}
     202     * @param errors The errors from previous tests
     203     */
     204    public void setPreviousErrors(List<TestError> errors) {
     205        previousErrors = errors;
     206    }
     207
     208    /**
    181209     * Notification of the end of the test. The tester may perform additional
    182210     * actions and destroy the used structures.
    183211     * <p>
     
    319347    }
    320348
    321349    /**
     350     * Get the class that the test must run after
     351     * @return A class that extends {@code Test}
     352     *
     353     * @since xxx
     354     */
     355    public Class<? extends Test> getAfterClass() {
     356        return afterTest;
     357    }
     358
     359    /**
    322360     * Determines if the test has been canceled.
    323361     * @return {@code true} if the test has been canceled, {@code false} otherwise
    324362     */
  • src/org/openstreetmap/josm/data/validation/tests/IntersectionIssues.java

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