Skip to content

Commit

Permalink
Fix error highlight for self-intersecting ways
Browse files Browse the repository at this point in the history
  • Loading branch information
gabortim committed Apr 21, 2024
1 parent bf33e49 commit 5b3f5f4
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/org/openstreetmap/josm/data/osm/WaySegment.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
package org.openstreetmap.josm.data.osm;

/**
* A segment consisting of 2 consecutive nodes out of a way.
* A segment consisting of two consecutive nodes out of a way.
*/
public final class WaySegment extends IWaySegment<Node, Way> {

Expand Down Expand Up @@ -36,7 +36,7 @@ public static WaySegment forNodePair(Way way, Node first, Node second) {
}
endIndex--;
}
throw new IllegalArgumentException("Node pair is not part of way!");
throw new IllegalArgumentException("The node pair is not consecutive part of the way!");
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,7 @@ private static Collection<WaySegment> createSegments(Map<Node, List<Node>> graph
intersect.retainAll(mWays);

for (Way w : intersect) {
if (w.getNeighbours(n).contains(m) && getNodeIndex(w, n) + 1 == getNodeIndex(w, m)) {
if (isConsecutive(w, n, m)) {
segments.add(WaySegment.forNodePair(w, n, m));
}
}
Expand All @@ -166,20 +166,21 @@ private static Collection<WaySegment> createSegments(Map<Node, List<Node>> graph
}

/**
* Returns the way index of a node. Only the first occurrence is considered in case it's a closed way.
* Returns the way index of a node. Only the first occurrence is considered in case it's a closed or self-intersecting way.
*
* @param w parent way
* @param n the node to look up
* @return {@code >=0} if the node is found or<br>{@code -1} if node not part of the way
* @param n the first node to look up
* @param m the second, possibly consecutive node
* @return {@code true} if the nodes are consecutive order in the way direction
*/
private static int getNodeIndex(Way w, Node n) {
for (int i = 0; i < w.getNodesCount(); i++) {
if (w.getNode(i).equals(n)) {
return i;
private static boolean isConsecutive(Way w, Node n, Node m) {
for (int i = 0; i < w.getNodesCount() - 1; i++) {
if (w.getNode(i).equals(n) && w.getNode(i + 1).equals(m)) {
return true;
}
}

return -1;
return false;
}

/**
Expand Down Expand Up @@ -237,6 +238,12 @@ private Collection<Way> buildGraph(Way way) {
}
}
}

// case for single, non-connected waterways
if (graph.isEmpty()) {
graph.add(way);
}

return graph;
}
}

0 comments on commit 5b3f5f4

Please sign in to comment.