Changeset 16968 in josm


Ignore:
Timestamp:
2020-08-29T17:44:57+02:00 (4 years ago)
Author:
simon04
Message:

fix #14200 - Warn when moving elements by a large distance

Location:
trunk
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/org/openstreetmap/josm/actions/mapmode/SelectAction.java

    r16626 r16968  
    1818import java.util.LinkedList;
    1919import java.util.Optional;
    20 import java.util.Set;
    2120
    2221import javax.swing.JOptionPane;
     
    3029import org.openstreetmap.josm.command.ScaleCommand;
    3130import org.openstreetmap.josm.command.SequenceCommand;
     31import org.openstreetmap.josm.data.SystemOfMeasurement;
    3232import org.openstreetmap.josm.data.UndoRedoHandler;
    3333import org.openstreetmap.josm.data.coor.EastNorth;
     
    827827            showConfirmMoveDialog(ed);
    828828        }
    829         Set<Node> nodes = new HashSet<>();
     829        final int moveCount = UndoRedoHandler.getInstance().getLastCommand().getParticipatingPrimitives().size();
     830        if (UndoRedoHandler.getInstance().getLastCommand() instanceof MoveCommand) {
     831            final double moveDistance = ((MoveCommand) UndoRedoHandler.getInstance().getLastCommand()).getDistance(n -> !n.isNew());
     832            if (Double.isFinite(moveDistance) && moveDistance > Config.getPref().getInt("warn.move.maxdistance", 200)) {
     833                final ConfirmMoveDialog ed = new ConfirmMoveDialog();
     834                ed.setContent(trn(
     835                        "You moved {0} element by a distance of {1}. "
     836                                + "Moving elements by a large distance is often an error.\n" + "Really move them?",
     837                        "You moved {0} elements by a distance of {1}. "
     838                                + "Moving elements by a large distance is often an error.\n" + "Really move them?",
     839                        moveCount, moveCount, SystemOfMeasurement.getSystemOfMeasurement().getDistText(moveDistance)));
     840                ed.toggleEnable("movedLargeDistance");
     841                showConfirmMoveDialog(ed);
     842            }
     843        }
    830844        int max = Config.getPref().getInt("warn.move.maxelements", 20);
    831         for (OsmPrimitive osm : getLayerManager().getEditDataSet().getSelected()) {
    832             if (osm instanceof Way) {
    833                 nodes.addAll(((Way) osm).getNodes());
    834             } else if (osm instanceof Node) {
    835                 nodes.add((Node) osm);
    836             }
    837             if (nodes.size() > max) {
    838                 break;
    839             }
    840         }
    841         if (nodes.size() > max) {
     845        if (moveCount > max) {
    842846            final ConfirmMoveDialog ed = new ConfirmMoveDialog();
    843847            ed.setContent(
  • trunk/src/org/openstreetmap/josm/command/MoveCommand.java

    r15000 r16968  
    1111import java.util.NoSuchElementException;
    1212import java.util.Objects;
     13import java.util.function.Predicate;
    1314
    1415import javax.swing.Icon;
     
    300301
    301302    /**
    302      * Gets the offset.
    303      * @return The current offset.
     303     * Gets the current move offset.
     304     * @return The current move offset.
    304305     */
    305306    protected EastNorth getOffset() {
    306307        return new EastNorth(x, y);
     308    }
     309
     310    /**
     311     * Computes the move distance for one node matching the specified predicate
     312     * @param predicate predicate to match
     313     * @return distance in metres
     314     */
     315    public double getDistance(Predicate<Node> predicate) {
     316        return nodes.stream()
     317                .filter(predicate)
     318                .filter(node -> node.getCoor() != null && node.getEastNorth() != null)
     319                .findFirst()
     320                .map(node -> {
     321                    final Node old = new Node(node);
     322                    old.setEastNorth(old.getEastNorth().add(-x, -y));
     323                    return node.getCoor().greatCircleDistance(old.getCoor());
     324                }).orElse(Double.NaN);
    307325    }
    308326
  • trunk/test/unit/org/openstreetmap/josm/command/MoveCommandTest.java

    r14120 r16968  
    7777        assertEquals("east", 1, moveCommand.getOffset().east(), 0.0001);
    7878        assertEquals("north", 2, moveCommand.getOffset().north(), 0.0001);
     79        assertEquals("distance", 2.236068, moveCommand.getDistance(n -> true), 0.0001);
    7980    }
    8081
     
    8990        assertEquals("east", 4, testData.existingNode.getEastNorth().east(), 0.0001);
    9091        assertEquals("north", 9, testData.existingNode.getEastNorth().north(), 0.0001);
     92        assertEquals("distance", 2.236068, command.getDistance(n -> true), 0.0001);
    9193    }
    9294
Note: See TracChangeset for help on using the changeset viewer.