if (ws.getFirstNode() != node && ws.getSecondNode() != node) {
insertPoints.put(ws.way, ws.lowerIndex);
}
}
for (Map.Entry<Way, Set<Integer>> entry : insertPoints.entrySet()) {
final Way w = entry.getKey();
final Set<Integer> insertPointsForWay = entry.getValue();
for (int i : pruneSuccs(insertPointsForWay)) {
MultiMap<Integer, Node> innerMap;
if (!data.containsKey(w)) {
innerMap = new MultiMap<>();
} else {
innerMap = data.get(w);
}
innerMap.put(i, node);
data.put(w, innerMap);
}
}
}
// Execute phase: traverse the structure "data" and finally put the nodes into place
for (Map.Entry<Way, MultiMap<Integer, Node>> entry : data.entrySet()) {
final Way w = entry.getKey();
final MultiMap<Integer, Node> innerEntry = entry.getValue();
List<Integer> segmentIndexes = new LinkedList<>();
segmentIndexes.addAll(innerEntry.keySet());
Collections.sort(segmentIndexes, Collections.reverseOrder());
List<Node> wayNodes = w.getNodes();
for (Integer segmentIndex : segmentIndexes) {
final Set<Node> nodesInSegment = innerEntry.get(segmentIndex);
if (joinWayToNode) {
for (Node node : nodesInSegment) {
EastNorth newPosition = Geometry.closestPointToSegment(w.getNode(segmentIndex).getEastNorth(),
w.getNode(segmentIndex+1).getEastNorth(),
node.getEastNorth());
cmds.add(new MoveCommand(node, Projections.inverseProject(newPosition)));
}
}
List<Node> nodesToAdd = new LinkedList<>();
nodesToAdd.addAll(nodesInSegment);
Collections.sort(nodesToAdd, new NodeDistanceToRefNodeComparator(w.getNode(segmentIndex), w.getNode(segmentIndex+1), !joinWayToNode));
wayNodes.addAll(segmentIndex + 1, nodesToAdd);
}
Way wnew = new Way(w);
wnew.setNodes(wayNodes);
cmds.add(new ChangeCommand(w, wnew));
}
if (cmds.isEmpty()) return;
Main.main.undoRedo.add(new SequenceCommand(getValue(NAME).toString(), cmds));