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