| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 |
|
|---|
| 7 | import java.awt.event.ActionEvent;
|
|---|
| 8 | import java.awt.event.KeyEvent;
|
|---|
| 9 | import java.io.Serializable;
|
|---|
| 10 | import java.util.Collection;
|
|---|
| 11 | import java.util.Collections;
|
|---|
| 12 | import java.util.Comparator;
|
|---|
| 13 | import java.util.HashMap;
|
|---|
| 14 | import java.util.HashSet;
|
|---|
| 15 | import java.util.LinkedHashMap;
|
|---|
| 16 | import java.util.LinkedList;
|
|---|
| 17 | import java.util.List;
|
|---|
| 18 | import java.util.Map;
|
|---|
| 19 | import java.util.Map.Entry;
|
|---|
| 20 | import java.util.Set;
|
|---|
| 21 | import java.util.TreeMap;
|
|---|
| 22 |
|
|---|
| 23 | import javax.swing.JOptionPane;
|
|---|
| 24 |
|
|---|
| 25 | import org.openstreetmap.josm.command.ChangeNodesCommand;
|
|---|
| 26 | import org.openstreetmap.josm.command.Command;
|
|---|
| 27 | import org.openstreetmap.josm.command.MoveCommand;
|
|---|
| 28 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| 29 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
|---|
| 30 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 31 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 32 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 33 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 34 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 35 | import org.openstreetmap.josm.data.osm.WaySegment;
|
|---|
| 36 | import org.openstreetmap.josm.data.projection.ProjectionRegistry;
|
|---|
| 37 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 38 | import org.openstreetmap.josm.gui.MapView;
|
|---|
| 39 | import org.openstreetmap.josm.gui.Notification;
|
|---|
| 40 | import org.openstreetmap.josm.tools.Geometry;
|
|---|
| 41 | import org.openstreetmap.josm.tools.MultiMap;
|
|---|
| 42 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 43 |
|
|---|
| 44 | /**
|
|---|
| 45 | * Action allowing to join a node to a nearby way, operating on two modes:<ul>
|
|---|
| 46 | * <li><b>Join Node to Way</b>: Include a node into the nearest way segments. The node does not move</li>
|
|---|
| 47 | * <li><b>Move Node onto Way</b>: Move the node onto the nearest way segments and include it</li>
|
|---|
| 48 | * </ul>
|
|---|
| 49 | * @since 466
|
|---|
| 50 | */
|
|---|
| 51 | public class JoinNodeWayAction extends JosmAction {
|
|---|
| 52 |
|
|---|
| 53 | protected final boolean joinWayToNode;
|
|---|
| 54 |
|
|---|
| 55 | protected JoinNodeWayAction(boolean joinWayToNode, String name, String iconName, String tooltip,
|
|---|
| 56 | Shortcut shortcut, boolean registerInToolbar) {
|
|---|
| 57 | super(name, iconName, tooltip, shortcut, registerInToolbar);
|
|---|
| 58 | this.joinWayToNode = joinWayToNode;
|
|---|
| 59 | }
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Constructs a Join Node to Way action.
|
|---|
| 63 | * @return the Join Node to Way action
|
|---|
| 64 | */
|
|---|
| 65 | public static JoinNodeWayAction createJoinNodeToWayAction() {
|
|---|
| 66 | JoinNodeWayAction action = new JoinNodeWayAction(false,
|
|---|
| 67 | tr("Join Node to Way"), /* ICON */ "joinnodeway",
|
|---|
| 68 | tr("Include a node into the nearest way segments"),
|
|---|
| 69 | Shortcut.registerShortcut("tools:joinnodeway", tr("Tools: {0}", tr("Join Node to Way")),
|
|---|
| 70 | KeyEvent.VK_J, Shortcut.DIRECT), true);
|
|---|
| 71 | action.setHelpId(ht("/Action/JoinNodeWay"));
|
|---|
| 72 | return action;
|
|---|
| 73 | }
|
|---|
| 74 |
|
|---|
| 75 | /**
|
|---|
| 76 | * Constructs a Move Node onto Way action.
|
|---|
| 77 | * @return the Move Node onto Way action
|
|---|
| 78 | */
|
|---|
| 79 | public static JoinNodeWayAction createMoveNodeOntoWayAction() {
|
|---|
| 80 | JoinNodeWayAction action = new JoinNodeWayAction(true,
|
|---|
| 81 | tr("Move Node onto Way"), /* ICON*/ "movenodeontoway",
|
|---|
| 82 | tr("Move the node onto the nearest way segments and include it"),
|
|---|
| 83 | Shortcut.registerShortcut("tools:movenodeontoway", tr("Tools: {0}", tr("Move Node onto Way")),
|
|---|
| 84 | KeyEvent.VK_N, Shortcut.DIRECT), true);
|
|---|
| 85 | action.setHelpId(ht("/Action/MoveNodeWay"));
|
|---|
| 86 | return action;
|
|---|
| 87 | }
|
|---|
| 88 |
|
|---|
| 89 | @Override
|
|---|
| 90 | public void actionPerformed(ActionEvent e) {
|
|---|
| 91 | if (!isEnabled())
|
|---|
| 92 | return;
|
|---|
| 93 | DataSet ds = getLayerManager().getEditDataSet();
|
|---|
| 94 | Collection<Node> selectedNodes = ds.getSelectedNodes();
|
|---|
| 95 | Collection<Command> cmds = new LinkedList<>();
|
|---|
| 96 | Map<Way, MultiMap<Integer, Node>> data = new LinkedHashMap<>();
|
|---|
| 97 |
|
|---|
| 98 | // If the user has selected some ways, only join the node to these.
|
|---|
| 99 | boolean restrictToSelectedWays = !ds.getSelectedWays().isEmpty();
|
|---|
| 100 |
|
|---|
| 101 | // Planning phase: decide where we'll insert the nodes and put it all in "data"
|
|---|
| 102 | MapView mapView = MainApplication.getMap().mapView;
|
|---|
| 103 | for (Node node : selectedNodes) {
|
|---|
| 104 | List<WaySegment> wss = mapView.getNearestWaySegments(mapView.getPoint(node), OsmPrimitive::isSelectable);
|
|---|
| 105 | // we cannot trust the order of elements in wss because it was calculated based on rounded position value of node
|
|---|
| 106 | TreeMap<Double, List<WaySegment>> nearestMap = new TreeMap<>();
|
|---|
| 107 | EastNorth en = node.getEastNorth();
|
|---|
| 108 | for (WaySegment ws : wss) {
|
|---|
| 109 | // Maybe cleaner to pass a "isSelected" predicate to getNearestWaySegments, but this is less invasive.
|
|---|
| 110 | if (restrictToSelectedWays && !ws.getWay().isSelected()) {
|
|---|
| 111 | continue;
|
|---|
| 112 | }
|
|---|
| 113 | /* perpendicular distance squared
|
|---|
| 114 | * loose some precision to account for possible deviations in the calculation above
|
|---|
| 115 | * e.g. if identical (A and B) come about reversed in another way, values may differ
|
|---|
| 116 | * -- zero out least significant 32 dual digits of mantissa..
|
|---|
| 117 | */
|
|---|
| 118 | double distSq = en.distanceSq(Geometry.closestPointToSegment(ws.getFirstNode().getEastNorth(),
|
|---|
| 119 | ws.getSecondNode().getEastNorth(), en));
|
|---|
| 120 | // resolution in numbers with large exponent not needed here..
|
|---|
| 121 | distSq = Double.longBitsToDouble(Double.doubleToLongBits(distSq) >> 32 << 32);
|
|---|
| 122 | List<WaySegment> wslist = nearestMap.computeIfAbsent(distSq, k -> new LinkedList<>());
|
|---|
| 123 | wslist.add(ws);
|
|---|
| 124 | }
|
|---|
| 125 | Set<Way> seenWays = new HashSet<>();
|
|---|
| 126 | Double usedDist = null;
|
|---|
| 127 | while (!nearestMap.isEmpty()) {
|
|---|
| 128 | Entry<Double, List<WaySegment>> entry = nearestMap.pollFirstEntry();
|
|---|
| 129 | if (usedDist != null) {
|
|---|
| 130 | double delta = entry.getKey() - usedDist;
|
|---|
| 131 | if (delta > 1e-4)
|
|---|
| 132 | break;
|
|---|
| 133 | }
|
|---|
| 134 | for (WaySegment ws : entry.getValue()) {
|
|---|
| 135 | // only use the closest WaySegment of each way and ignore those that already contain the node
|
|---|
| 136 | if (!ws.getFirstNode().equals(node) && !ws.getSecondNode().equals(node)
|
|---|
| 137 | && !seenWays.contains(ws.getWay())) {
|
|---|
| 138 | if (usedDist == null)
|
|---|
| 139 | usedDist = entry.getKey();
|
|---|
| 140 | MultiMap<Integer, Node> innerMap = data.get(ws.getWay());
|
|---|
| 141 | if (innerMap == null) {
|
|---|
| 142 | innerMap = new MultiMap<>();
|
|---|
| 143 | data.put(ws.getWay(), innerMap);
|
|---|
| 144 | }
|
|---|
| 145 | innerMap.put(ws.getLowerIndex(), node);
|
|---|
| 146 | seenWays.add(ws.getWay());
|
|---|
| 147 | }
|
|---|
| 148 | }
|
|---|
| 149 | }
|
|---|
| 150 | }
|
|---|
| 151 |
|
|---|
| 152 | // Execute phase: traverse the structure "data" and finally put the nodes into place
|
|---|
| 153 | Map<Node, EastNorth> movedNodes = new HashMap<>();
|
|---|
| 154 | for (Map.Entry<Way, MultiMap<Integer, Node>> entry : data.entrySet()) {
|
|---|
| 155 | final Way w = entry.getKey();
|
|---|
| 156 | final MultiMap<Integer, Node> innerEntry = entry.getValue();
|
|---|
| 157 |
|
|---|
| 158 | List<Integer> segmentIndexes = new LinkedList<>();
|
|---|
| 159 | segmentIndexes.addAll(innerEntry.keySet());
|
|---|
| 160 | segmentIndexes.sort(Collections.reverseOrder());
|
|---|
| 161 |
|
|---|
| 162 | List<Node> wayNodes = w.getNodes();
|
|---|
| 163 | for (Integer segmentIndex : segmentIndexes) {
|
|---|
| 164 | final Set<Node> nodesInSegment = innerEntry.get(segmentIndex);
|
|---|
| 165 | if (joinWayToNode) {
|
|---|
| 166 | for (Node node : nodesInSegment) {
|
|---|
| 167 | EastNorth newPosition = Geometry.closestPointToSegment(
|
|---|
| 168 | w.getNode(segmentIndex).getEastNorth(),
|
|---|
| 169 | w.getNode(segmentIndex+1).getEastNorth(),
|
|---|
| 170 | node.getEastNorth());
|
|---|
| 171 | EastNorth prevMove = movedNodes.get(node);
|
|---|
| 172 | if (prevMove != null) {
|
|---|
| 173 | if (!prevMove.equalsEpsilon(newPosition, 1e-4)) {
|
|---|
| 174 | // very unlikely: node has same distance to multiple ways which are not nearly overlapping
|
|---|
| 175 | new Notification(tr("Multiple target ways, no common point found. Nothing was changed."))
|
|---|
| 176 | .setIcon(JOptionPane.INFORMATION_MESSAGE)
|
|---|
| 177 | .show();
|
|---|
| 178 | return;
|
|---|
| 179 | }
|
|---|
| 180 | continue;
|
|---|
| 181 | }
|
|---|
| 182 | MoveCommand c = new MoveCommand(node,
|
|---|
| 183 | ProjectionRegistry.getProjection().eastNorth2latlon(newPosition));
|
|---|
| 184 | cmds.add(c);
|
|---|
| 185 | movedNodes.put(node, newPosition);
|
|---|
| 186 | }
|
|---|
| 187 | }
|
|---|
| 188 | List<Node> nodesToAdd = new LinkedList<>();
|
|---|
| 189 | nodesToAdd.addAll(nodesInSegment);
|
|---|
| 190 | nodesToAdd.sort(new NodeDistanceToRefNodeComparator(
|
|---|
| 191 | w.getNode(segmentIndex), w.getNode(segmentIndex+1), !joinWayToNode));
|
|---|
| 192 | wayNodes.addAll(segmentIndex + 1, nodesToAdd);
|
|---|
| 193 | }
|
|---|
| 194 | cmds.add(new ChangeNodesCommand(ds, w, wayNodes));
|
|---|
| 195 | }
|
|---|
| 196 |
|
|---|
| 197 | if (cmds.isEmpty()) return;
|
|---|
| 198 | UndoRedoHandler.getInstance().add(new SequenceCommand(getValue(NAME).toString(), cmds));
|
|---|
| 199 | }
|
|---|
| 200 |
|
|---|
| 201 | /**
|
|---|
| 202 | * Sorts collinear nodes by their distance to a common reference node.
|
|---|
| 203 | */
|
|---|
| 204 | private static class NodeDistanceToRefNodeComparator implements Comparator<Node>, Serializable {
|
|---|
| 205 |
|
|---|
| 206 | private static final long serialVersionUID = 1L;
|
|---|
| 207 |
|
|---|
| 208 | private final EastNorth refPoint;
|
|---|
| 209 | private final EastNorth refPoint2;
|
|---|
| 210 | private final boolean projectToSegment;
|
|---|
| 211 |
|
|---|
| 212 | NodeDistanceToRefNodeComparator(Node referenceNode, Node referenceNode2, boolean projectFirst) {
|
|---|
| 213 | refPoint = referenceNode.getEastNorth();
|
|---|
| 214 | refPoint2 = referenceNode2.getEastNorth();
|
|---|
| 215 | projectToSegment = projectFirst;
|
|---|
| 216 | }
|
|---|
| 217 |
|
|---|
| 218 | @Override
|
|---|
| 219 | public int compare(Node first, Node second) {
|
|---|
| 220 | EastNorth firstPosition = first.getEastNorth();
|
|---|
| 221 | EastNorth secondPosition = second.getEastNorth();
|
|---|
| 222 |
|
|---|
| 223 | if (projectToSegment) {
|
|---|
| 224 | firstPosition = Geometry.closestPointToSegment(refPoint, refPoint2, firstPosition);
|
|---|
| 225 | secondPosition = Geometry.closestPointToSegment(refPoint, refPoint2, secondPosition);
|
|---|
| 226 | }
|
|---|
| 227 |
|
|---|
| 228 | double distanceFirst = firstPosition.distance(refPoint);
|
|---|
| 229 | double distanceSecond = secondPosition.distance(refPoint);
|
|---|
| 230 | return Double.compare(distanceFirst, distanceSecond);
|
|---|
| 231 | }
|
|---|
| 232 | }
|
|---|
| 233 |
|
|---|
| 234 | @Override
|
|---|
| 235 | protected void updateEnabledState() {
|
|---|
| 236 | updateEnabledStateOnCurrentSelection();
|
|---|
| 237 | }
|
|---|
| 238 |
|
|---|
| 239 | @Override
|
|---|
| 240 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
|---|
| 241 | updateEnabledStateOnModifiableSelection(selection);
|
|---|
| 242 | }
|
|---|
| 243 | }
|
|---|