source: josm/trunk/src/org/openstreetmap/josm/actions/MergeNodesAction.java@ 6202

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

fix #2447 - Unnessary changes of a target node by dragging another node onto it

  • Property svn:eol-style set to native
File size: 15.3 KB
Line 
1//License: GPL. Copyright 2007 by Immanuel Scholz and others. See LICENSE file for details.
2package org.openstreetmap.josm.actions;
3
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;
9import java.util.ArrayList;
10import java.util.Collection;
11import java.util.Collections;
12import java.util.HashSet;
13import java.util.LinkedList;
14import java.util.List;
15import java.util.Set;
16
17import javax.swing.JOptionPane;
18
19import org.openstreetmap.josm.Main;
20import org.openstreetmap.josm.command.ChangeCommand;
21import org.openstreetmap.josm.command.ChangeNodesCommand;
22import org.openstreetmap.josm.command.Command;
23import org.openstreetmap.josm.command.DeleteCommand;
24import org.openstreetmap.josm.command.SequenceCommand;
25import org.openstreetmap.josm.corrector.UserCancelException;
26import org.openstreetmap.josm.data.coor.EastNorth;
27import org.openstreetmap.josm.data.coor.LatLon;
28import org.openstreetmap.josm.data.osm.Node;
29import org.openstreetmap.josm.data.osm.OsmPrimitive;
30import org.openstreetmap.josm.data.osm.TagCollection;
31import org.openstreetmap.josm.data.osm.Way;
32import org.openstreetmap.josm.gui.DefaultNameFormatter;
33import org.openstreetmap.josm.gui.HelpAwareOptionPane;
34import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
35import org.openstreetmap.josm.gui.Notification;
36import org.openstreetmap.josm.gui.conflict.tags.CombinePrimitiveResolverDialog;
37import org.openstreetmap.josm.gui.layer.OsmDataLayer;
38import org.openstreetmap.josm.tools.CheckParameterUtil;
39import org.openstreetmap.josm.tools.ImageProvider;
40import org.openstreetmap.josm.tools.Shortcut;
41
42/**
43 * Merges a collection of nodes into one node.
44 *
45 * The "surviving" node will be the one with the lowest positive id.
46 * (I.e. it was uploaded to the server and is the oldest one.)
47 *
48 * However we use the location of the node that was selected *last*.
49 * The "surviving" node will be moved to that location if it is
50 * different from the last selected node.
51 *
52 * @since 422
53 */
54public class MergeNodesAction extends JosmAction {
55
56 /**
57 * Constructs a new {@code MergeNodesAction}.
58 */
59 public MergeNodesAction() {
60 super(tr("Merge Nodes"), "mergenodes", tr("Merge nodes into the oldest one."),
61 Shortcut.registerShortcut("tools:mergenodes", tr("Tool: {0}", tr("Merge Nodes")), KeyEvent.VK_M, Shortcut.DIRECT), true);
62 putValue("help", ht("/Action/MergeNodes"));
63 }
64
65 @Override
66 public void actionPerformed(ActionEvent event) {
67 if (!isEnabled())
68 return;
69 Collection<OsmPrimitive> selection = getCurrentDataSet().getAllSelected();
70 List<Node> selectedNodes = OsmPrimitive.getFilteredList(selection, Node.class);
71
72 if (selectedNodes.size() == 1) {
73 List<Node> nearestNodes = Main.map.mapView.getNearestNodes(Main.map.mapView.getPoint(selectedNodes.get(0)), selectedNodes, OsmPrimitive.isUsablePredicate);
74 if (nearestNodes.isEmpty()) {
75 new Notification(
76 tr("Please select at least two nodes to merge or one node that is close to another node."))
77 .setIcon(JOptionPane.WARNING_MESSAGE)
78 .show();
79 return;
80 }
81 selectedNodes.addAll(nearestNodes);
82 }
83
84 Node targetNode = selectTargetNode(selectedNodes);
85 Node targetLocationNode = selectTargetLocationNode(selectedNodes);
86 Command cmd = mergeNodes(Main.main.getEditLayer(), selectedNodes, targetNode, targetLocationNode);
87 if (cmd != null) {
88 Main.main.undoRedo.add(cmd);
89 Main.main.getEditLayer().data.setSelected(targetNode);
90 }
91 }
92
93 /**
94 * Select the location of the target node after merge.
95 *
96 * @param candidates the collection of candidate nodes
97 * @return the coordinates of this node are later used for the target node
98 */
99 public static Node selectTargetLocationNode(List<Node> candidates) {
100 int size = candidates.size();
101 if (size == 0)
102 throw new IllegalArgumentException("empty list");
103
104 switch (Main.pref.getInteger("merge-nodes.mode", 0)) {
105 case 0: {
106 Node targetNode = candidates.get(size - 1);
107 for (final Node n : candidates) { // pick last one
108 targetNode = n;
109 }
110 return targetNode;
111 }
112 case 1: {
113 double east = 0, north = 0;
114 for (final Node n : candidates) {
115 east += n.getEastNorth().east();
116 north += n.getEastNorth().north();
117 }
118
119 return new Node(new EastNorth(east / size, north / size));
120 }
121 case 2: {
122 final double[] weights = new double[size];
123
124 for (int i = 0; i < size; i++) {
125 final LatLon c1 = candidates.get(i).getCoor();
126 for (int j = i + 1; j < size; j++) {
127 final LatLon c2 = candidates.get(j).getCoor();
128 final double d = c1.distance(c2);
129 weights[i] += d;
130 weights[j] += d;
131 }
132 }
133
134 double east = 0, north = 0, weight = 0;
135 for (int i = 0; i < size; i++) {
136 final EastNorth en = candidates.get(i).getEastNorth();
137 final double w = weights[i];
138 east += en.east() * w;
139 north += en.north() * w;
140 weight += w;
141 }
142
143 return new Node(new EastNorth(east / weight, north / weight));
144 }
145 default:
146 throw new RuntimeException("unacceptable merge-nodes.mode");
147 }
148
149 }
150
151 /**
152 * Find which node to merge into (i.e. which one will be left)
153 *
154 * @param candidates the collection of candidate nodes
155 * @return the selected target node
156 */
157 public static Node selectTargetNode(Collection<Node> candidates) {
158 Node oldestNode = null;
159 Node targetNode = null;
160 Node lastNode = null;
161 for (Node n : candidates) {
162 if (!n.isNew()) {
163 // Among existing nodes, try to keep the oldest used one
164 if (!n.getReferrers().isEmpty()) {
165 if (targetNode == null) {
166 targetNode = n;
167 } else if (n.getId() < targetNode.getId()) {
168 targetNode = n;
169 }
170 } else if (oldestNode == null) {
171 oldestNode = n;
172 } else if (n.getId() < oldestNode.getId()) {
173 oldestNode = n;
174 }
175 }
176 lastNode = n;
177 }
178 if (targetNode == null) {
179 targetNode = (oldestNode != null ? oldestNode : lastNode);
180 }
181 return targetNode;
182 }
183
184
185 /**
186 * Fixes the parent ways referring to one of the nodes.
187 *
188 * Replies null, if the ways could not be fixed, i.e. because a way would have to be deleted
189 * which is referred to by a relation.
190 *
191 * @param nodesToDelete the collection of nodes to be deleted
192 * @param targetNode the target node the other nodes are merged to
193 * @return a list of commands; null, if the ways could not be fixed
194 */
195 protected static List<Command> fixParentWays(Collection<Node> nodesToDelete, Node targetNode) {
196 List<Command> cmds = new ArrayList<Command>();
197 Set<Way> waysToDelete = new HashSet<Way>();
198
199 for (Way w: OsmPrimitive.getFilteredList(OsmPrimitive.getReferrer(nodesToDelete), Way.class)) {
200 ArrayList<Node> newNodes = new ArrayList<Node>(w.getNodesCount());
201 for (Node n: w.getNodes()) {
202 if (! nodesToDelete.contains(n) && n != targetNode) {
203 newNodes.add(n);
204 } else if (newNodes.isEmpty()) {
205 newNodes.add(targetNode);
206 } else if (newNodes.get(newNodes.size()-1) != targetNode) {
207 // make sure we collapse a sequence of deleted nodes
208 // to exactly one occurrence of the merged target node
209 //
210 newNodes.add(targetNode);
211 } else {
212 // drop the node
213 }
214 }
215 if (newNodes.size() < 2) {
216 if (w.getReferrers().isEmpty()) {
217 waysToDelete.add(w);
218 } else {
219 ButtonSpec[] options = new ButtonSpec[] {
220 new ButtonSpec(
221 tr("Abort Merging"),
222 ImageProvider.get("cancel"),
223 tr("Click to abort merging nodes"),
224 null /* no special help topic */
225 )
226 };
227 HelpAwareOptionPane.showOptionDialog(
228 Main.parent,
229 tr("Cannot merge nodes: Would have to delete way {0} which is still used by {1}",
230 DefaultNameFormatter.getInstance().formatAsHtmlUnorderedList(w),
231 DefaultNameFormatter.getInstance().formatAsHtmlUnorderedList(w.getReferrers())),
232 tr("Warning"),
233 JOptionPane.WARNING_MESSAGE,
234 null, /* no icon */
235 options,
236 options[0],
237 ht("/Action/MergeNodes#WaysToDeleteStillInUse")
238 );
239 return null;
240 }
241 } else if(newNodes.size() < 2 && w.getReferrers().isEmpty()) {
242 waysToDelete.add(w);
243 } else {
244 cmds.add(new ChangeNodesCommand(w, newNodes));
245 }
246 }
247 if (!waysToDelete.isEmpty()) {
248 cmds.add(new DeleteCommand(waysToDelete));
249 }
250 return cmds;
251 }
252
253 /**
254 * Merges the nodes in {@code nodes} at the specified node's location. Uses the dataset
255 * managed by {@code layer} as reference.
256 * @param layer layer the reference data layer. Must not be null
257 * @param nodes the collection of nodes. Ignored if null
258 * @param targetLocationNode this node's location will be used for the target node
259 * @throws IllegalArgumentException thrown if {@code layer} is null
260 */
261 public static void doMergeNodes(OsmDataLayer layer, Collection<Node> nodes, Node targetLocationNode) {
262 if (nodes == null) {
263 return;
264 }
265 Set<Node> allNodes = new HashSet<Node>(nodes);
266 allNodes.add(targetLocationNode);
267 Node target;
268 if (nodes.contains(targetLocationNode) && !targetLocationNode.isNew()) {
269 target = targetLocationNode; // keep existing targetLocationNode as target to avoid unnecessary changes (see #2447)
270 } else {
271 target = selectTargetNode(allNodes);
272 }
273
274 Command cmd = mergeNodes(layer, nodes, target, targetLocationNode);
275 if (cmd != null) {
276 Main.main.undoRedo.add(cmd);
277 getCurrentDataSet().setSelected(target);
278 }
279 }
280
281 /**
282 * Merges the nodes in {@code nodes} at the specified node's location. Uses the dataset
283 * managed by {@code layer} as reference.
284 *
285 * @param layer layer the reference data layer. Must not be null.
286 * @param nodes the collection of nodes. Ignored if null.
287 * @param targetLocationNode this node's location will be used for the targetNode.
288 * @return The command necessary to run in order to perform action, or {@code null} if there is nothing to do
289 * @throws IllegalArgumentException thrown if {@code layer} is null
290 */
291 public static Command mergeNodes(OsmDataLayer layer, Collection<Node> nodes, Node targetLocationNode) {
292 if (nodes == null) {
293 return null;
294 }
295 Set<Node> allNodes = new HashSet<Node>(nodes);
296 allNodes.add(targetLocationNode);
297 return mergeNodes(layer, nodes, selectTargetNode(allNodes), targetLocationNode);
298 }
299
300 /**
301 * Merges the nodes in <code>nodes</code> onto one of the nodes. Uses the dataset
302 * managed by <code>layer</code> as reference.
303 *
304 * @param layer layer the reference data layer. Must not be null.
305 * @param nodes the collection of nodes. Ignored if null.
306 * @param targetNode the target node the collection of nodes is merged to. Must not be null.
307 * @param targetLocationNode this node's location will be used for the targetNode.
308 * @return The command necessary to run in order to perform action, or {@code null} if there is nothing to do
309 * @throws IllegalArgumentException thrown if layer is null
310 */
311 public static Command mergeNodes(OsmDataLayer layer, Collection<Node> nodes, Node targetNode, Node targetLocationNode) {
312 CheckParameterUtil.ensureParameterNotNull(layer, "layer");
313 CheckParameterUtil.ensureParameterNotNull(targetNode, "targetNode");
314 if (nodes == null) {
315 return null;
316 }
317
318 try {
319 TagCollection nodeTags = TagCollection.unionOfAllPrimitives(nodes);
320 List<Command> resultion = CombinePrimitiveResolverDialog.launchIfNecessary(nodeTags, nodes, Collections.singleton(targetNode));
321 LinkedList<Command> cmds = new LinkedList<Command>();
322
323 // the nodes we will have to delete
324 //
325 Collection<Node> nodesToDelete = new HashSet<Node>(nodes);
326 nodesToDelete.remove(targetNode);
327
328 // fix the ways referring to at least one of the merged nodes
329 //
330 Collection<Way> waysToDelete = new HashSet<Way>();
331 List<Command> wayFixCommands = fixParentWays(
332 nodesToDelete,
333 targetNode);
334 if (wayFixCommands == null) {
335 return null;
336 }
337 cmds.addAll(wayFixCommands);
338
339 // build the commands
340 //
341 if (targetNode != targetLocationNode) {
342 Node newTargetNode = new Node(targetNode);
343 newTargetNode.setCoor(targetLocationNode.getCoor());
344 cmds.add(new ChangeCommand(targetNode, newTargetNode));
345 }
346 cmds.addAll(resultion);
347 if (!nodesToDelete.isEmpty()) {
348 cmds.add(new DeleteCommand(nodesToDelete));
349 }
350 if (!waysToDelete.isEmpty()) {
351 cmds.add(new DeleteCommand(waysToDelete));
352 }
353 Command cmd = new SequenceCommand(tr("Merge {0} nodes", nodes.size()), cmds);
354 return cmd;
355 } catch (UserCancelException ex) {
356 return null;
357 }
358 }
359
360 @Override
361 protected void updateEnabledState() {
362 if (getCurrentDataSet() == null) {
363 setEnabled(false);
364 } else {
365 updateEnabledState(getCurrentDataSet().getAllSelected());
366 }
367 }
368
369 @Override
370 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
371 if (selection == null || selection.isEmpty()) {
372 setEnabled(false);
373 return;
374 }
375 boolean ok = true;
376 for (OsmPrimitive osm : selection) {
377 if (!(osm instanceof Node)) {
378 ok = false;
379 break;
380 }
381 }
382 setEnabled(ok);
383 }
384}
Note: See TracBrowser for help on using the repository browser.