Ignore:
Timestamp:
2013-12-28T23:17:38+01:00 (10 years ago)
Author:
simon04
Message:

fix #6313 - validator, UnconnectedWays: handle intersections of t shapes and similar

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/data/osm/Node.java

    r6491 r6556  
    88import org.openstreetmap.josm.data.osm.visitor.Visitor;
    99import org.openstreetmap.josm.data.projection.Projections;
     10import org.openstreetmap.josm.tools.CheckParameterUtil;
     11import org.openstreetmap.josm.tools.Predicate;
     12import org.openstreetmap.josm.tools.Utils;
     13
     14import java.util.Collection;
     15import java.util.Set;
     16import java.util.TreeSet;
    1017
    1118/**
     
    337344        return false;
    338345    }
     346
     347    /**
     348     * Tests whether {@code this} node is connected to {@code otherNode} via at most {@code hops} nodes
     349     * matching the {@code predicate} (which may be {@code null} to consider all nodes).
     350     */
     351    public boolean isConnectedTo(final Collection<Node> otherNodes, final int hops, Predicate<Node> predicate) {
     352        CheckParameterUtil.ensureParameterNotNull(otherNodes);
     353        CheckParameterUtil.ensureThat(!otherNodes.isEmpty(), "otherNodes must not be empty!");
     354        CheckParameterUtil.ensureThat(hops >= 0, "hops must be non-negative!");
     355        return hops == 0
     356                ? isConnectedTo(otherNodes, hops, predicate, null)
     357                : isConnectedTo(otherNodes, hops, predicate, new TreeSet<Node>());
     358    }
     359
     360    private boolean isConnectedTo(final Collection<Node> otherNodes, final int hops, Predicate<Node> predicate, Set<Node> visited) {
     361        if (otherNodes.contains(this)) {
     362            return true;
     363        }
     364        if (hops > 0) {
     365            visited.add(this);
     366            for (final Way w : Utils.filteredCollection(this.getReferrers(), Way.class)) {
     367                for (final Node n : w.getNodes()) {
     368                    final boolean containsN = visited.contains(n);
     369                    visited.add(n);
     370                    if (!containsN && (predicate == null || predicate.evaluate(n)) && n.isConnectedTo(otherNodes, hops - 1, predicate, visited)) {
     371                        return true;
     372                    }
     373                }
     374            }
     375        }
     376        return false;
     377    }
    339378}
Note: See TracChangeset for help on using the changeset viewer.