[8378] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[466] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[7400] | 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;
|
---|
[8308] | 9 | import java.io.Serializable;
|
---|
[7400] | 10 | import java.util.Collection;
|
---|
| 11 | import java.util.Collections;
|
---|
| 12 | import java.util.Comparator;
|
---|
| 13 | import java.util.HashMap;
|
---|
| 14 | import java.util.LinkedList;
|
---|
| 15 | import java.util.List;
|
---|
| 16 | import java.util.Map;
|
---|
| 17 | import java.util.Set;
|
---|
| 18 | import java.util.SortedSet;
|
---|
| 19 | import java.util.TreeSet;
|
---|
| 20 |
|
---|
[466] | 21 | import org.openstreetmap.josm.Main;
|
---|
[582] | 22 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
[466] | 23 | import org.openstreetmap.josm.command.Command;
|
---|
[7131] | 24 | import org.openstreetmap.josm.command.MoveCommand;
|
---|
[466] | 25 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
[7131] | 26 | import org.openstreetmap.josm.data.coor.EastNorth;
|
---|
[582] | 27 | import org.openstreetmap.josm.data.osm.Node;
|
---|
[466] | 28 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
[582] | 29 | import org.openstreetmap.josm.data.osm.Way;
|
---|
| 30 | import org.openstreetmap.josm.data.osm.WaySegment;
|
---|
[7131] | 31 | import org.openstreetmap.josm.data.projection.Projections;
|
---|
| 32 | import org.openstreetmap.josm.tools.Geometry;
|
---|
| 33 | import org.openstreetmap.josm.tools.MultiMap;
|
---|
[1084] | 34 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
[466] | 35 |
|
---|
[7400] | 36 | /**
|
---|
| 37 | * Action allowing to join a node to a nearby way, operating on two modes:<ul>
|
---|
| 38 | * <li><b>Join Node to Way</b>: Include a node into the nearest way segments. The node does not move</li>
|
---|
| 39 | * <li><b>Move Node onto Way</b>: Move the node onto the nearest way segments and include it</li>
|
---|
| 40 | * </ul>
|
---|
[7859] | 41 | * @since 466
|
---|
[7400] | 42 | */
|
---|
[466] | 43 | public class JoinNodeWayAction extends JosmAction {
|
---|
[6814] | 44 |
|
---|
[7131] | 45 | protected final boolean joinWayToNode;
|
---|
| 46 |
|
---|
[7859] | 47 | protected JoinNodeWayAction(boolean joinWayToNode, String name, String iconName, String tooltip,
|
---|
| 48 | Shortcut shortcut, boolean registerInToolbar) {
|
---|
[7131] | 49 | super(name, iconName, tooltip, shortcut, registerInToolbar);
|
---|
| 50 | this.joinWayToNode = joinWayToNode;
|
---|
| 51 | }
|
---|
| 52 |
|
---|
[6814] | 53 | /**
|
---|
[7131] | 54 | * Constructs a Join Node to Way action.
|
---|
[7400] | 55 | * @return the Join Node to Way action
|
---|
[6814] | 56 | */
|
---|
[7131] | 57 | public static JoinNodeWayAction createJoinNodeToWayAction() {
|
---|
| 58 | JoinNodeWayAction action = new JoinNodeWayAction(false,
|
---|
[7859] | 59 | tr("Join Node to Way"), /* ICON */ "joinnodeway",
|
---|
| 60 | tr("Include a node into the nearest way segments"),
|
---|
| 61 | Shortcut.registerShortcut("tools:joinnodeway", tr("Tool: {0}", tr("Join Node to Way")),
|
---|
| 62 | KeyEvent.VK_J, Shortcut.DIRECT), true);
|
---|
[7131] | 63 | action.putValue("help", ht("/Action/JoinNodeWay"));
|
---|
| 64 | return action;
|
---|
[1169] | 65 | }
|
---|
[466] | 66 |
|
---|
[7131] | 67 | /**
|
---|
| 68 | * Constructs a Move Node onto Way action.
|
---|
[7400] | 69 | * @return the Move Node onto Way action
|
---|
[7131] | 70 | */
|
---|
| 71 | public static JoinNodeWayAction createMoveNodeOntoWayAction() {
|
---|
| 72 | JoinNodeWayAction action = new JoinNodeWayAction(true,
|
---|
[7859] | 73 | tr("Move Node onto Way"), /* ICON*/ "movenodeontoway",
|
---|
| 74 | tr("Move the node onto the nearest way segments and include it"),
|
---|
| 75 | Shortcut.registerShortcut("tools:movenodeontoway", tr("Tool: {0}", tr("Move Node onto Way")),
|
---|
| 76 | KeyEvent.VK_N, Shortcut.DIRECT), true);
|
---|
| 77 | action.putValue("help", ht("/Action/MoveNodeWay"));
|
---|
[7131] | 78 | return action;
|
---|
| 79 | }
|
---|
| 80 |
|
---|
[6084] | 81 | @Override
|
---|
[1169] | 82 | public void actionPerformed(ActionEvent e) {
|
---|
[1820] | 83 | if (!isEnabled())
|
---|
| 84 | return;
|
---|
[10382] | 85 | Collection<Node> selectedNodes = getLayerManager().getEditDataSet().getSelectedNodes();
|
---|
[7005] | 86 | Collection<Command> cmds = new LinkedList<>();
|
---|
[7302] | 87 | Map<Way, MultiMap<Integer, Node>> data = new HashMap<>();
|
---|
[466] | 88 |
|
---|
[4557] | 89 | // If the user has selected some ways, only join the node to these.
|
---|
| 90 | boolean restrictToSelectedWays =
|
---|
[10382] | 91 | !getLayerManager().getEditDataSet().getSelectedWays().isEmpty();
|
---|
[3643] | 92 |
|
---|
[7302] | 93 | // Planning phase: decide where we'll insert the nodes and put it all in "data"
|
---|
| 94 | for (Node node : selectedNodes) {
|
---|
| 95 | List<WaySegment> wss = Main.map.mapView.getNearestWaySegments(
|
---|
| 96 | Main.map.mapView.getPoint(node), OsmPrimitive.isSelectablePredicate);
|
---|
[4557] | 97 |
|
---|
[7302] | 98 | MultiMap<Way, Integer> insertPoints = new MultiMap<>();
|
---|
| 99 | for (WaySegment ws : wss) {
|
---|
| 100 | // Maybe cleaner to pass a "isSelected" predicate to getNearestWaySegments, but this is less invasive.
|
---|
| 101 | if (restrictToSelectedWays && !ws.way.isSelected()) {
|
---|
| 102 | continue;
|
---|
| 103 | }
|
---|
| 104 |
|
---|
[7859] | 105 | if (!ws.getFirstNode().equals(node) && !ws.getSecondNode().equals(node)) {
|
---|
[7302] | 106 | insertPoints.put(ws.way, ws.lowerIndex);
|
---|
| 107 | }
|
---|
[7131] | 108 | }
|
---|
[7302] | 109 | for (Map.Entry<Way, Set<Integer>> entry : insertPoints.entrySet()) {
|
---|
| 110 | final Way w = entry.getKey();
|
---|
| 111 | final Set<Integer> insertPointsForWay = entry.getValue();
|
---|
| 112 | for (int i : pruneSuccs(insertPointsForWay)) {
|
---|
| 113 | MultiMap<Integer, Node> innerMap;
|
---|
| 114 | if (!data.containsKey(w)) {
|
---|
| 115 | innerMap = new MultiMap<>();
|
---|
| 116 | } else {
|
---|
| 117 | innerMap = data.get(w);
|
---|
| 118 | }
|
---|
| 119 | innerMap.put(i, node);
|
---|
| 120 | data.put(w, innerMap);
|
---|
| 121 | }
|
---|
| 122 | }
|
---|
[7131] | 123 | }
|
---|
[3643] | 124 |
|
---|
[7302] | 125 | // Execute phase: traverse the structure "data" and finally put the nodes into place
|
---|
| 126 | for (Map.Entry<Way, MultiMap<Integer, Node>> entry : data.entrySet()) {
|
---|
[7131] | 127 | final Way w = entry.getKey();
|
---|
[7302] | 128 | final MultiMap<Integer, Node> innerEntry = entry.getValue();
|
---|
[466] | 129 |
|
---|
[7302] | 130 | List<Integer> segmentIndexes = new LinkedList<>();
|
---|
| 131 | segmentIndexes.addAll(innerEntry.keySet());
|
---|
| 132 | Collections.sort(segmentIndexes, Collections.reverseOrder());
|
---|
| 133 |
|
---|
| 134 | List<Node> wayNodes = w.getNodes();
|
---|
| 135 | for (Integer segmentIndex : segmentIndexes) {
|
---|
| 136 | final Set<Node> nodesInSegment = innerEntry.get(segmentIndex);
|
---|
[7131] | 137 | if (joinWayToNode) {
|
---|
[7302] | 138 | for (Node node : nodesInSegment) {
|
---|
| 139 | EastNorth newPosition = Geometry.closestPointToSegment(w.getNode(segmentIndex).getEastNorth(),
|
---|
| 140 | w.getNode(segmentIndex+1).getEastNorth(),
|
---|
| 141 | node.getEastNorth());
|
---|
[8447] | 142 | MoveCommand c = new MoveCommand(node, Projections.inverseProject(newPosition));
|
---|
| 143 | // Avoid moving a given node several times at the same position in case of overlapping ways
|
---|
| 144 | if (!cmds.contains(c)) {
|
---|
| 145 | cmds.add(c);
|
---|
| 146 | }
|
---|
[7302] | 147 | }
|
---|
[3713] | 148 | }
|
---|
[7302] | 149 | List<Node> nodesToAdd = new LinkedList<>();
|
---|
| 150 | nodesToAdd.addAll(nodesInSegment);
|
---|
[7859] | 151 | Collections.sort(nodesToAdd, new NodeDistanceToRefNodeComparator(
|
---|
| 152 | w.getNode(segmentIndex), w.getNode(segmentIndex+1), !joinWayToNode));
|
---|
[7302] | 153 | wayNodes.addAll(segmentIndex + 1, nodesToAdd);
|
---|
[1814] | 154 | }
|
---|
[7131] | 155 | Way wnew = new Way(w);
|
---|
[7302] | 156 | wnew.setNodes(wayNodes);
|
---|
[7131] | 157 | cmds.add(new ChangeCommand(w, wnew));
|
---|
| 158 | }
|
---|
[7302] | 159 |
|
---|
[7131] | 160 | if (cmds.isEmpty()) return;
|
---|
| 161 | Main.main.undoRedo.add(new SequenceCommand(getValue(NAME).toString(), cmds));
|
---|
| 162 | Main.map.repaint();
|
---|
[1169] | 163 | }
|
---|
[466] | 164 |
|
---|
[7302] | 165 | private static SortedSet<Integer> pruneSuccs(Collection<Integer> is) {
|
---|
| 166 | SortedSet<Integer> is2 = new TreeSet<>();
|
---|
[1169] | 167 | for (int i : is) {
|
---|
| 168 | if (!is2.contains(i - 1) && !is2.contains(i + 1)) {
|
---|
| 169 | is2.add(i);
|
---|
| 170 | }
|
---|
| 171 | }
|
---|
[7131] | 172 | return is2;
|
---|
[1169] | 173 | }
|
---|
[1820] | 174 |
|
---|
[7302] | 175 | /**
|
---|
| 176 | * Sorts collinear nodes by their distance to a common reference node.
|
---|
| 177 | */
|
---|
[8308] | 178 | private static class NodeDistanceToRefNodeComparator implements Comparator<Node>, Serializable {
|
---|
| 179 |
|
---|
| 180 | private static final long serialVersionUID = 1L;
|
---|
| 181 |
|
---|
[7302] | 182 | private final EastNorth refPoint;
|
---|
[9067] | 183 | private final EastNorth refPoint2;
|
---|
[7302] | 184 | private final boolean projectToSegment;
|
---|
[8510] | 185 |
|
---|
[7302] | 186 | NodeDistanceToRefNodeComparator(Node referenceNode, Node referenceNode2, boolean projectFirst) {
|
---|
| 187 | refPoint = referenceNode.getEastNorth();
|
---|
| 188 | refPoint2 = referenceNode2.getEastNorth();
|
---|
| 189 | projectToSegment = projectFirst;
|
---|
| 190 | }
|
---|
[8510] | 191 |
|
---|
[7302] | 192 | @Override
|
---|
| 193 | public int compare(Node first, Node second) {
|
---|
| 194 | EastNorth firstPosition = first.getEastNorth();
|
---|
| 195 | EastNorth secondPosition = second.getEastNorth();
|
---|
| 196 |
|
---|
| 197 | if (projectToSegment) {
|
---|
| 198 | firstPosition = Geometry.closestPointToSegment(refPoint, refPoint2, firstPosition);
|
---|
| 199 | secondPosition = Geometry.closestPointToSegment(refPoint, refPoint2, secondPosition);
|
---|
| 200 | }
|
---|
| 201 |
|
---|
| 202 | double distanceFirst = firstPosition.distance(refPoint);
|
---|
| 203 | double distanceSecond = secondPosition.distance(refPoint);
|
---|
[9880] | 204 | return Double.compare(distanceFirst, distanceSecond);
|
---|
[7302] | 205 | }
|
---|
| 206 | }
|
---|
| 207 |
|
---|
[1820] | 208 | @Override
|
---|
| 209 | protected void updateEnabledState() {
|
---|
[10409] | 210 | updateEnabledStateOnCurrentSelection();
|
---|
[1820] | 211 | }
|
---|
[2256] | 212 |
|
---|
| 213 | @Override
|
---|
| 214 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 215 | setEnabled(selection != null && !selection.isEmpty());
|
---|
| 216 | }
|
---|
[466] | 217 | }
|
---|