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