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