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

Last change on this file since 3634 was 3596, checked in by bastiK, 14 years ago

applied #5531 (patch by Martin Ždila) - There should be way to merge nodes by averaging their position

  • Property svn:eol-style set to native
File size: 12.4 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.conflict.tags.TagConflictResolutionUtil.combineTigerTags;
5import static org.openstreetmap.josm.gui.conflict.tags.TagConflictResolutionUtil.completeTagCollectionForEditing;
6import static org.openstreetmap.josm.gui.conflict.tags.TagConflictResolutionUtil.normalizeTagCollectionBeforeEditing;
7import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
8import static org.openstreetmap.josm.tools.I18n.tr;
9
10import java.awt.event.ActionEvent;
11import java.awt.event.KeyEvent;
12import java.util.ArrayList;
13import java.util.Collection;
14import java.util.HashSet;
15import java.util.LinkedHashSet;
16import java.util.LinkedList;
17import java.util.List;
18import java.util.Set;
19
20import javax.swing.JOptionPane;
21
22import org.openstreetmap.josm.Main;
23import org.openstreetmap.josm.command.ChangeCommand;
24import org.openstreetmap.josm.command.ChangeNodesCommand;
25import org.openstreetmap.josm.command.Command;
26import org.openstreetmap.josm.command.DeleteCommand;
27import org.openstreetmap.josm.command.SequenceCommand;
28import org.openstreetmap.josm.data.coor.LatLon;
29import org.openstreetmap.josm.data.osm.Node;
30import org.openstreetmap.josm.data.osm.OsmPrimitive;
31import org.openstreetmap.josm.data.osm.RelationToChildReference;
32import org.openstreetmap.josm.data.osm.TagCollection;
33import org.openstreetmap.josm.data.osm.Way;
34import org.openstreetmap.josm.gui.DefaultNameFormatter;
35import org.openstreetmap.josm.gui.HelpAwareOptionPane;
36import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
37import org.openstreetmap.josm.gui.conflict.tags.CombinePrimitiveResolverDialog;
38import org.openstreetmap.josm.gui.layer.OsmDataLayer;
39import org.openstreetmap.josm.tools.CheckParameterUtil;
40import org.openstreetmap.josm.tools.ImageProvider;
41import org.openstreetmap.josm.tools.Shortcut;
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 */
52public class MergeNodesAction extends JosmAction {
53
54 public MergeNodesAction() {
55 super(tr("Merge Nodes"), "mergenodes", tr("Merge nodes into the oldest one."),
56 Shortcut.registerShortcut("tools:mergenodes", tr("Tool: {0}", tr("Merge Nodes")), KeyEvent.VK_M, Shortcut.GROUP_EDIT), true);
57 putValue("help", ht("/Action/MergeNodesAction"));
58 }
59
60 public void actionPerformed(ActionEvent event) {
61 if (!isEnabled())
62 return;
63 Collection<OsmPrimitive> selection = getCurrentDataSet().getSelected();
64 LinkedHashSet<Node> selectedNodes = OsmPrimitive.getFilteredSet(selection, Node.class);
65 if (selectedNodes.size() < 2) {
66 JOptionPane.showMessageDialog(
67 Main.parent,
68 tr("Please select at least two nodes to merge."),
69 tr("Warning"),
70 JOptionPane.WARNING_MESSAGE
71 );
72 return;
73 }
74
75 Node targetNode = selectTargetNode(selectedNodes);
76 Node targetLocationNode = selectTargetLocationNode(selectedNodes);
77 Command cmd = mergeNodes(Main.main.getEditLayer(), selectedNodes, targetNode, targetLocationNode);
78 if (cmd != null) {
79 Main.main.undoRedo.add(cmd);
80 Main.main.getEditLayer().data.setSelected(targetNode);
81 }
82 }
83
84 /**
85 * Select the location of the target node after merge.
86 *
87 * @param candidates the collection of candidate nodes
88 * @return the coordinates of this node are later used for the target node
89 */
90 public static Node selectTargetLocationNode(LinkedHashSet<Node> candidates) {
91 if (! Main.pref.getBoolean("merge-nodes.average-location", false)) {
92 Node targetNode = null;
93 for (final Node n : candidates) { // pick last one
94 targetNode = n;
95 }
96 return targetNode;
97 }
98
99 double lat = 0, lon = 0;
100 for (final Node n : candidates) {
101 lat += n.getCoor().lat();
102 lon += n.getCoor().lon();
103 }
104
105 return new Node(new LatLon(lat / candidates.size(), lon / candidates.size()));
106 }
107
108 /**
109 * Find which node to merge into (i.e. which one will be left)
110 *
111 * @param candidates the collection of candidate nodes
112 * @return the selected target node
113 */
114 public static Node selectTargetNode(LinkedHashSet<Node> candidates) {
115 Node targetNode = null;
116 Node lastNode = null;
117 for (Node n : candidates) {
118 if (!n.isNew()) {
119 if (targetNode == null) {
120 targetNode = n;
121 } else if (n.getId() < targetNode.getId()) {
122 targetNode = n;
123 }
124 }
125 lastNode = n;
126 }
127 if (targetNode == null) {
128 targetNode = lastNode;
129 }
130 return targetNode;
131 }
132
133
134 /**
135 * Fixes the parent ways referring to one of the nodes.
136 *
137 * Replies null, if the ways could not be fixed, i.e. because a way would have to be deleted
138 * which is referred to by a relation.
139 *
140 * @param nodesToDelete the collection of nodes to be deleted
141 * @param targetNode the target node the other nodes are merged to
142 * @return a list of commands; null, if the ways could not be fixed
143 */
144 protected static List<Command> fixParentWays(Collection<Node> nodesToDelete, Node targetNode) {
145 List<Command> cmds = new ArrayList<Command>();
146 Set<Way> waysToDelete = new HashSet<Way>();
147
148 for (Way w: OsmPrimitive.getFilteredList(OsmPrimitive.getReferrer(nodesToDelete), Way.class)) {
149 ArrayList<Node> newNodes = new ArrayList<Node>(w.getNodesCount());
150 for (Node n: w.getNodes()) {
151 if (! nodesToDelete.contains(n) && n != targetNode) {
152 newNodes.add(n);
153 } else if (newNodes.isEmpty()) {
154 newNodes.add(targetNode);
155 } else if (newNodes.get(newNodes.size()-1) != targetNode) {
156 // make sure we collapse a sequence of deleted nodes
157 // to exactly one occurrence of the merged target node
158 //
159 newNodes.add(targetNode);
160 } else {
161 // drop the node
162 }
163 }
164 if (newNodes.size() < 2) {
165 if (w.getReferrers().isEmpty()) {
166 waysToDelete.add(w);
167 } else {
168 ButtonSpec[] options = new ButtonSpec[] {
169 new ButtonSpec(
170 tr("Abort Merging"),
171 ImageProvider.get("cancel"),
172 tr("Click to abort merging nodes"),
173 null /* no special help topic */
174 )
175 };
176 HelpAwareOptionPane.showOptionDialog(
177 Main.parent,
178 tr(
179 "Cannot merge nodes: Would have to delete way ''{0}'' which is still used.",
180 w.getDisplayName(DefaultNameFormatter.getInstance())
181 ),
182 tr("Warning"),
183 JOptionPane.WARNING_MESSAGE,
184 null, /* no icon */
185 options,
186 options[0],
187 ht("/Action/MergeNodes#WaysToDeleteStillInUse")
188 );
189 return null;
190 }
191 } else if(newNodes.size() < 2 && w.getReferrers().isEmpty()) {
192 waysToDelete.add(w);
193 } else {
194 cmds.add(new ChangeNodesCommand(w, newNodes));
195 }
196 }
197 if (!waysToDelete.isEmpty()) {
198 cmds.add(new DeleteCommand(waysToDelete));
199 }
200 return cmds;
201 }
202
203 public static Command mergeNodes(OsmDataLayer layer, Collection<Node> nodes, Node targetNode) {
204 return mergeNodes(layer, nodes, targetNode, targetNode);
205 }
206
207 /**
208 * Merges the nodes in <code>nodes</code> onto one of the nodes. Uses the dataset
209 * managed by <code>layer</code> as reference.
210 *
211 * @param layer layer the reference data layer. Must not be null.
212 * @param nodes the collection of nodes. Ignored if null.
213 * @param targetNode the target node the collection of nodes is merged to. Must not be null.
214 * @param targetLocationNode this node's location will be used for the targetNode.
215 * @throw IllegalArgumentException thrown if layer is null
216 */
217 public static Command mergeNodes(OsmDataLayer layer, Collection<Node> nodes, Node targetNode, Node targetLocationNode) {
218 CheckParameterUtil.ensureParameterNotNull(layer, "layer");
219 CheckParameterUtil.ensureParameterNotNull(targetNode, "targetNode");
220 if (nodes == null)
221 return null;
222
223 Set<RelationToChildReference> relationToNodeReferences = RelationToChildReference.getRelationToChildReferences(nodes);
224
225 // build the tag collection
226 //
227 TagCollection nodeTags = TagCollection.unionOfAllPrimitives(nodes);
228 combineTigerTags(nodeTags);
229 normalizeTagCollectionBeforeEditing(nodeTags, nodes);
230 TagCollection nodeTagsToEdit = new TagCollection(nodeTags);
231 completeTagCollectionForEditing(nodeTagsToEdit);
232
233 // launch a conflict resolution dialog, if necessary
234 //
235 CombinePrimitiveResolverDialog dialog = CombinePrimitiveResolverDialog.getInstance();
236 dialog.getTagConflictResolverModel().populate(nodeTagsToEdit, nodeTags.getKeysWithMultipleValues());
237 dialog.getRelationMemberConflictResolverModel().populate(relationToNodeReferences);
238 dialog.setTargetPrimitive(targetNode);
239 dialog.prepareDefaultDecisions();
240 // conflict resolution is necessary if there are conflicts in the merged tags
241 // or if at least one of the merged nodes is referred to by a relation
242 //
243 if (! nodeTags.isApplicableToPrimitive() || relationToNodeReferences.size() > 1) {
244 dialog.setVisible(true);
245 if (dialog.isCancelled())
246 return null;
247 }
248 LinkedList<Command> cmds = new LinkedList<Command>();
249
250 // the nodes we will have to delete
251 //
252 Collection<Node> nodesToDelete = new HashSet<Node>(nodes);
253 nodesToDelete.remove(targetNode);
254
255 // fix the ways referring to at least one of the merged nodes
256 //
257 Collection<Way> waysToDelete= new HashSet<Way>();
258 List<Command> wayFixCommands = fixParentWays(
259 nodesToDelete,
260 targetNode
261 );
262 if (wayFixCommands == null)
263 return null;
264 cmds.addAll(wayFixCommands);
265
266 // build the commands
267 //
268 if (targetNode != targetLocationNode) {
269 Node newTargetNode = new Node(targetNode);
270 newTargetNode.setCoor(targetLocationNode.getCoor());
271 cmds.add(new ChangeCommand(targetNode, newTargetNode));
272 }
273 cmds.addAll(dialog.buildResolutionCommands());
274 if (!nodesToDelete.isEmpty()) {
275 cmds.add(new DeleteCommand(nodesToDelete));
276 }
277 if (!waysToDelete.isEmpty()) {
278 cmds.add(new DeleteCommand(waysToDelete));
279 }
280 Command cmd = new SequenceCommand(tr("Merge {0} nodes", nodes.size()), cmds);
281 return cmd;
282 }
283
284 @Override
285 protected void updateEnabledState() {
286 if (getCurrentDataSet() == null) {
287 setEnabled(false);
288 } else {
289 updateEnabledState(getCurrentDataSet().getSelected());
290 }
291 }
292
293 @Override
294 protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
295 if (selection == null || selection.isEmpty()) {
296 setEnabled(false);
297 return;
298 }
299 boolean ok = true;
300 if (selection.size() < 2) {
301 setEnabled(false);
302 return;
303 }
304 for (OsmPrimitive osm : selection) {
305 if (!(osm instanceof Node)) {
306 ok = false;
307 break;
308 }
309 }
310 setEnabled(ok);
311 }
312}
Note: See TracBrowser for help on using the repository browser.