Ticket #17528: intersectionissues_v2.patch

File intersectionissues_v2.patch, 16.1 KB (added by taylor.smock, 6 years ago)

Ensure that tests have information on previously run tests

  • 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")) {
     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                if (twoLast.greatCircleDistance(oneLast) <= maxDistance ||
     121                        twoLast.greatCircleDistance(oneFirst) <= maxDistance ||
     122                        twoFirst.greatCircleDistance(oneLast) <= maxDistance ||
     123                        twoFirst.greatCircleDistance(oneFirst) <= maxDistance) {
     124                    List<Way> nearby = new ArrayList<>();
     125                    nearby.add(one);
     126                    nearby.add(two);
     127                    errored.addAll(nearby);
     128                    allWays.removeAll(errored);
     129                    TestError.Builder testError = TestError.builder(this, Severity.WARNING, SHORT_DISCONNECT)
     130                            .primitives(nearby)
     131                            .message(tr("Disconnected road"));
     132                    errors.add(testError.build());
     133                }
     134            }
     135        }
     136    }
     137
     138    /**
     139     * Check nearby nodes to an intersection of two ways
     140     * @param way1 A way to check an almost intersection with
     141     * @param way2 A way to check an almost intersection with
     142     */
     143    public void checkNearbyNodes(Way way1, Way way2) {
     144        Node intersectingNode = getIntersectingNode(way1, way2);
     145        if (intersectingNode == null) return;
     146        checkNearbyNodes(way1, way2, intersectingNode);
     147        checkNearbyNodes(way2, way1, intersectingNode);
     148    }
     149
     150    private void checkNearbyNodes(Way way1, Way way2, Node nearby) {
     151        for (Node node : way1.getNeighbours(nearby)) {
     152            if (node.equals(nearby)) continue;
     153            WayPoint waypoint = new WayPoint(node.getCoor());
     154            double distance = GpxDistance.getDistance(way2, waypoint);
     155            if (((distance < maxDistance && !node.isTagged())
     156                    || (distance < maxDistanceNodeInformation && node.isTagged()))
     157                    && getSmallestAngle(way2, nearby, node) < maxAngle) {
     158                List<Way> primitiveIssues = new ArrayList<>();
     159                primitiveIssues.add(way1);
     160                primitiveIssues.add(way2);
     161                List<TestError> tErrors = new ArrayList<>();
     162                tErrors.addAll(previousErrors);
     163                tErrors.addAll(getErrors());
     164                for (TestError error : tErrors) {
     165                    int code = error.getCode();
     166                    if ((code == SHORT_DISCONNECT || code == NEARBY_NODE
     167                            || code == OverlappingWays.OVERLAPPING_HIGHWAY
     168                            || code == OverlappingWays.DUPLICATE_WAY_SEGMENT
     169                            || code == OverlappingWays.OVERLAPPING_HIGHWAY_AREA
     170                            || code == OverlappingWays.OVERLAPPING_WAY
     171                            || code == OverlappingWays.OVERLAPPING_WAY_AREA
     172                            || code == OverlappingWays.OVERLAPPING_RAILWAY
     173                            || code == OverlappingWays.OVERLAPPING_RAILWAY_AREA)
     174                            && primitiveIssues.containsAll(error.getPrimitives())) {
     175                        return;
     176                    }
     177                }
     178                TestError.Builder testError = TestError.builder(this, Severity.WARNING, NEARBY_NODE)
     179                        .primitives(primitiveIssues)
     180                        .message(tr("Almost overlapping highways"));
     181                errors.add(testError.build());
     182            }
     183        }
     184    }
     185
     186    /**
     187     * Get the intersecting node of two ways
     188     * @param way1 A way that (hopefully) intersects with way2
     189     * @param way2 A way to find an intersection with
     190     * @return {@code Node} if there is an intersecting node, {@code null} otherwise
     191     */
     192    public Node getIntersectingNode(Way way1, Way way2) {
     193        for (Node node : way1.getNodes()) {
     194            if (way2.containsNode(node)) {
     195                return node;
     196            }
     197        }
     198        return null;
     199    }
     200
     201    /**
     202     * Get the corner angle between nodes
     203     * @param way The way with additional nodes
     204     * @param intersection The node to get angles around
     205     * @param comparison The node to get angles from
     206     * @return The angle for comparison->intersection->(additional node) (normalized degrees)
     207     */
     208    public double getSmallestAngle(Way way, Node intersection, Node comparison) {
     209        Set<Node> neighbours = way.getNeighbours(intersection);
     210        double angle = Double.MAX_VALUE;
     211        EastNorth eastNorthIntersection = intersection.getEastNorth();
     212        EastNorth eastNorthComparison = comparison.getEastNorth();
     213        for (Node node : neighbours) {
     214            EastNorth eastNorthNode = node.getEastNorth();
     215            double tAngle = Geometry.getCornerAngle(eastNorthComparison, eastNorthIntersection, eastNorthNode);
     216            if (Math.abs(tAngle) < angle) angle = Math.abs(tAngle);
     217        }
     218        return Geometry.getNormalizedAngleInDegrees(angle);
     219    }
     220}