source: josm/trunk/src/org/openstreetmap/josm/actions/JoinNodeWayAction.java@ 12726

Last change on this file since 12726 was 12726, checked in by Don-vip, 7 years ago

see #13036 - deprecate Command() default constructor, fix unit tests and java warnings

  • Property svn:eol-style set to native
File size: 9.1 KB
RevLine 
[8378]1// License: GPL. For details, see LICENSE file.
[466]2package org.openstreetmap.josm.actions;
3
[7400]4import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
5import static org.openstreetmap.josm.tools.I18n.tr;
6
7import java.awt.event.ActionEvent;
8import java.awt.event.KeyEvent;
[8308]9import java.io.Serializable;
[7400]10import java.util.Collection;
11import java.util.Collections;
12import java.util.Comparator;
13import java.util.HashMap;
14import java.util.LinkedList;
15import java.util.List;
16import java.util.Map;
17import java.util.Set;
18import java.util.SortedSet;
19import java.util.TreeSet;
20
[582]21import org.openstreetmap.josm.command.ChangeCommand;
[466]22import org.openstreetmap.josm.command.Command;
[7131]23import org.openstreetmap.josm.command.MoveCommand;
[466]24import org.openstreetmap.josm.command.SequenceCommand;
[7131]25import org.openstreetmap.josm.data.coor.EastNorth;
[12726]26import org.openstreetmap.josm.data.osm.DataSet;
[582]27import org.openstreetmap.josm.data.osm.Node;
[466]28import org.openstreetmap.josm.data.osm.OsmPrimitive;
[582]29import org.openstreetmap.josm.data.osm.Way;
30import org.openstreetmap.josm.data.osm.WaySegment;
[7131]31import org.openstreetmap.josm.data.projection.Projections;
[12630]32import org.openstreetmap.josm.gui.MainApplication;
33import org.openstreetmap.josm.gui.MapView;
[7131]34import org.openstreetmap.josm.tools.Geometry;
35import org.openstreetmap.josm.tools.MultiMap;
[1084]36import 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]45public 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) {
140 EastNorth newPosition = Geometry.closestPointToSegment(w.getNode(segmentIndex).getEastNorth(),
141 w.getNode(segmentIndex+1).getEastNorth(),
142 node.getEastNorth());
[8447]143 MoveCommand c = new MoveCommand(node, Projections.inverseProject(newPosition));
144 // Avoid moving a given node several times at the same position in case of overlapping ways
145 if (!cmds.contains(c)) {
146 cmds.add(c);
147 }
[7302]148 }
[3713]149 }
[7302]150 List<Node> nodesToAdd = new LinkedList<>();
151 nodesToAdd.addAll(nodesInSegment);
[10619]152 nodesToAdd.sort(new NodeDistanceToRefNodeComparator(
[7859]153 w.getNode(segmentIndex), w.getNode(segmentIndex+1), !joinWayToNode));
[7302]154 wayNodes.addAll(segmentIndex + 1, nodesToAdd);
[1814]155 }
[7131]156 Way wnew = new Way(w);
[7302]157 wnew.setNodes(wayNodes);
[12726]158 cmds.add(new ChangeCommand(ds, w, wnew));
[7131]159 }
[7302]160
[7131]161 if (cmds.isEmpty()) return;
[12641]162 MainApplication.undoRedo.add(new SequenceCommand(getValue(NAME).toString(), cmds));
[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}
Note: See TracBrowser for help on using the repository browser.