| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.actions;
|
|---|
| 3 |
|
|---|
| 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
|---|
| 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
|---|
| 6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
|---|
| 7 |
|
|---|
| 8 | import java.awt.GridBagLayout;
|
|---|
| 9 | import java.awt.event.ActionEvent;
|
|---|
| 10 | import java.awt.event.KeyEvent;
|
|---|
| 11 | import java.util.ArrayList;
|
|---|
| 12 | import java.util.Arrays;
|
|---|
| 13 | import java.util.Collection;
|
|---|
| 14 | import java.util.Collections;
|
|---|
| 15 | import java.util.HashSet;
|
|---|
| 16 | import java.util.LinkedList;
|
|---|
| 17 | import java.util.List;
|
|---|
| 18 | import java.util.Objects;
|
|---|
| 19 | import java.util.Set;
|
|---|
| 20 | import java.util.function.Consumer;
|
|---|
| 21 | import java.util.function.Supplier;
|
|---|
| 22 | import java.util.stream.Collectors;
|
|---|
| 23 |
|
|---|
| 24 | import javax.swing.BorderFactory;
|
|---|
| 25 | import javax.swing.JCheckBox;
|
|---|
| 26 | import javax.swing.JLabel;
|
|---|
| 27 | import javax.swing.JOptionPane;
|
|---|
| 28 | import javax.swing.JPanel;
|
|---|
| 29 | import javax.swing.JSpinner;
|
|---|
| 30 | import javax.swing.SpinnerNumberModel;
|
|---|
| 31 | import javax.swing.SwingUtilities;
|
|---|
| 32 | import javax.swing.event.ChangeEvent;
|
|---|
| 33 | import javax.swing.event.ChangeListener;
|
|---|
| 34 |
|
|---|
| 35 | import org.openstreetmap.josm.command.ChangeNodesCommand;
|
|---|
| 36 | import org.openstreetmap.josm.command.Command;
|
|---|
| 37 | import org.openstreetmap.josm.command.DeleteCommand;
|
|---|
| 38 | import org.openstreetmap.josm.command.SequenceCommand;
|
|---|
| 39 | import org.openstreetmap.josm.data.SystemOfMeasurement;
|
|---|
| 40 | import org.openstreetmap.josm.data.UndoRedoHandler;
|
|---|
| 41 | import org.openstreetmap.josm.data.coor.EastNorth;
|
|---|
| 42 | import org.openstreetmap.josm.data.osm.DataSelectionListener;
|
|---|
| 43 | import org.openstreetmap.josm.data.osm.DataSet;
|
|---|
| 44 | import org.openstreetmap.josm.data.osm.Node;
|
|---|
| 45 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
|---|
| 46 | import org.openstreetmap.josm.data.osm.Way;
|
|---|
| 47 | import org.openstreetmap.josm.data.projection.Ellipsoid;
|
|---|
| 48 | import org.openstreetmap.josm.gui.ExtendedDialog;
|
|---|
| 49 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
|---|
| 50 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
|---|
| 51 | import org.openstreetmap.josm.gui.MainApplication;
|
|---|
| 52 | import org.openstreetmap.josm.gui.Notification;
|
|---|
| 53 | import org.openstreetmap.josm.gui.util.GuiHelper;
|
|---|
| 54 | import org.openstreetmap.josm.spi.preferences.Config;
|
|---|
| 55 | import org.openstreetmap.josm.spi.preferences.IPreferences;
|
|---|
| 56 | import org.openstreetmap.josm.tools.GBC;
|
|---|
| 57 | import org.openstreetmap.josm.tools.ImageProvider;
|
|---|
| 58 | import org.openstreetmap.josm.tools.Shortcut;
|
|---|
| 59 | import org.openstreetmap.josm.tools.StreamUtils;
|
|---|
| 60 |
|
|---|
| 61 | /**
|
|---|
| 62 | * Delete unnecessary nodes from a way
|
|---|
| 63 | * @since 2575
|
|---|
| 64 | */
|
|---|
| 65 | public class SimplifyWayAction extends JosmAction {
|
|---|
| 66 |
|
|---|
| 67 | /**
|
|---|
| 68 | * Constructs a new {@code SimplifyWayAction}.
|
|---|
| 69 | */
|
|---|
| 70 | public SimplifyWayAction() {
|
|---|
| 71 | super(tr("Simplify Way"), "simplify", tr("Delete unnecessary nodes from a way."),
|
|---|
| 72 | Shortcut.registerShortcut("tools:simplify", tr("Tools: {0}", tr("Simplify Way")), KeyEvent.VK_Y, Shortcut.SHIFT), true);
|
|---|
| 73 | setHelpId(ht("/Action/SimplifyWay"));
|
|---|
| 74 | }
|
|---|
| 75 |
|
|---|
| 76 | protected boolean confirmWayWithNodesOutsideBoundingBox(List<? extends OsmPrimitive> primitives) {
|
|---|
| 77 | return DeleteAction.checkAndConfirmOutlyingDelete(primitives, null);
|
|---|
| 78 | }
|
|---|
| 79 |
|
|---|
| 80 | protected void alertSelectAtLeastOneWay() {
|
|---|
| 81 | SwingUtilities.invokeLater(() ->
|
|---|
| 82 | new Notification(
|
|---|
| 83 | tr("Please select at least one way to simplify."))
|
|---|
| 84 | .setIcon(JOptionPane.WARNING_MESSAGE)
|
|---|
| 85 | .setDuration(Notification.TIME_SHORT)
|
|---|
| 86 | .setHelpTopic(ht("/Action/SimplifyWay#SelectAWayToSimplify"))
|
|---|
| 87 | .show()
|
|---|
| 88 | );
|
|---|
| 89 | }
|
|---|
| 90 |
|
|---|
| 91 | protected boolean confirmSimplifyManyWays(int numWays) {
|
|---|
| 92 | ButtonSpec[] options = {
|
|---|
| 93 | new ButtonSpec(
|
|---|
| 94 | tr("Yes"),
|
|---|
| 95 | new ImageProvider("ok"),
|
|---|
| 96 | tr("Simplify all selected ways"),
|
|---|
| 97 | null),
|
|---|
| 98 | new ButtonSpec(
|
|---|
| 99 | tr("Cancel"),
|
|---|
| 100 | new ImageProvider("cancel"),
|
|---|
| 101 | tr("Cancel operation"),
|
|---|
| 102 | null)
|
|---|
| 103 | };
|
|---|
| 104 | return 0 == HelpAwareOptionPane.showOptionDialog(
|
|---|
| 105 | MainApplication.getMainFrame(),
|
|---|
| 106 | tr("The selection contains {0} ways. Are you sure you want to simplify them all?", numWays),
|
|---|
| 107 | tr("Simplify ways?"),
|
|---|
| 108 | JOptionPane.WARNING_MESSAGE,
|
|---|
| 109 | null, // no special icon
|
|---|
| 110 | options,
|
|---|
| 111 | options[0],
|
|---|
| 112 | ht("/Action/SimplifyWay#ConfirmSimplifyAll")
|
|---|
| 113 | );
|
|---|
| 114 | }
|
|---|
| 115 |
|
|---|
| 116 | /**
|
|---|
| 117 | * Asks the user for max-err value used to simplify ways, if not remembered before
|
|---|
| 118 | * @param text the text being shown
|
|---|
| 119 | * @param auto whether it's called automatically (conversion) or by the user
|
|---|
| 120 | * @return the max-err value or -1 if canceled
|
|---|
| 121 | * @since 15419
|
|---|
| 122 | */
|
|---|
| 123 | public static double askSimplifyWays(String text, boolean auto) {
|
|---|
| 124 | return askSimplifyWays(Collections.emptyList(), text, auto);
|
|---|
| 125 | }
|
|---|
| 126 |
|
|---|
| 127 | /**
|
|---|
| 128 | * Asks the user for max-err value used to simplify ways, if not remembered before
|
|---|
| 129 | * @param ways the ways that are being simplified (to show estimated number of nodes to be removed)
|
|---|
| 130 | * @param text the text being shown
|
|---|
| 131 | * @param auto whether it's called automatically (conversion) or by the user
|
|---|
| 132 | * @return the max-err value or -1 if canceled
|
|---|
| 133 | * @since 16566
|
|---|
| 134 | */
|
|---|
| 135 | public static double askSimplifyWays(List<Way> ways, String text, boolean auto) {
|
|---|
| 136 | return askSimplifyWays(ways, () -> text, null, auto);
|
|---|
| 137 | }
|
|---|
| 138 |
|
|---|
| 139 | /**
|
|---|
| 140 | * Asks the user for max-err value used to simplify ways, if not remembered before
|
|---|
| 141 | * @param ways the ways that are being simplified (to show estimated number of nodes to be removed)
|
|---|
| 142 | * @param textSupplier the text being shown (called when the DataSet selection changes)
|
|---|
| 143 | * @param auto whether it's called automatically (conversion) or by the user
|
|---|
| 144 | * @param listener The dataset selection update listener
|
|---|
| 145 | * @return the max-err value or -1 if canceled
|
|---|
| 146 | */
|
|---|
| 147 | private static double askSimplifyWays(List<Way> ways, Supplier<String> textSupplier, SimplifyWayDataSelectionListener listener,
|
|---|
| 148 | boolean auto) {
|
|---|
| 149 | final IPreferences s = Config.getPref();
|
|---|
| 150 | final String key = "simplify-way." + (auto ? "auto." : "");
|
|---|
| 151 | final String keyRemember = key + "remember";
|
|---|
| 152 | final String keyError = key + "max-error";
|
|---|
| 153 |
|
|---|
| 154 | final String r = s.get(keyRemember, "ask");
|
|---|
| 155 | if (auto && "no".equals(r)) {
|
|---|
| 156 | return -1;
|
|---|
| 157 | } else if ("yes".equals(r)) {
|
|---|
| 158 | return s.getDouble(keyError, 3.0);
|
|---|
| 159 | }
|
|---|
| 160 |
|
|---|
| 161 | final JPanel p = new JPanel(new GridBagLayout());
|
|---|
| 162 | final Supplier<String> actualTextSupplier = () -> "<html><body style=\"width: 375px;\">" + textSupplier.get() + "<br><br>" +
|
|---|
| 163 | tr("This reduces unnecessary nodes along the way and is especially recommended if GPS tracks were recorded by time "
|
|---|
| 164 | + "(e.g. one point per second) or when the accuracy was low (reduces \"zigzag\" tracks).")
|
|---|
| 165 | + "</body></html>";
|
|---|
| 166 | final JLabel textLabel = new JLabel(actualTextSupplier.get());
|
|---|
| 167 | p.add(textLabel, GBC.eol());
|
|---|
| 168 | p.setBorder(BorderFactory.createEmptyBorder(5, 10, 10, 5));
|
|---|
| 169 | JPanel q = new JPanel(new GridBagLayout());
|
|---|
| 170 | q.add(new JLabel(tr("Maximum error (meters): ")));
|
|---|
| 171 | SpinnerNumberModel errorModel = new SpinnerNumberModel(
|
|---|
| 172 | s.getDouble(keyError, 3.0), 0.01, null, 0.5);
|
|---|
| 173 | JSpinner n = new JSpinner(errorModel);
|
|---|
| 174 | ((JSpinner.DefaultEditor) n.getEditor()).getTextField().setColumns(4);
|
|---|
| 175 | q.add(n);
|
|---|
| 176 |
|
|---|
| 177 | JLabel nodesToRemove = new JLabel();
|
|---|
| 178 | SimplifyChangeListener l = new SimplifyChangeListener(nodesToRemove, errorModel, ways);
|
|---|
| 179 | final Runnable changeCleanup = () -> {
|
|---|
| 180 | if (l.lastCommand != null && l.lastCommand.equals(UndoRedoHandler.getInstance().getLastCommand())) {
|
|---|
| 181 | UndoRedoHandler.getInstance().undo();
|
|---|
| 182 | l.lastCommand = null;
|
|---|
| 183 | }
|
|---|
| 184 | };
|
|---|
| 185 | if (listener != null) {
|
|---|
| 186 | listener.addConsumer(ignored -> {
|
|---|
| 187 | textLabel.setText(actualTextSupplier.get());
|
|---|
| 188 | changeCleanup.run();
|
|---|
| 189 | l.stateChanged(null);
|
|---|
| 190 | });
|
|---|
| 191 | }
|
|---|
| 192 | if (!ways.isEmpty()) {
|
|---|
| 193 | errorModel.addChangeListener(l);
|
|---|
| 194 | l.stateChanged(null);
|
|---|
| 195 | q.add(nodesToRemove, GBC.std().insets(5, 0, 0, 0));
|
|---|
| 196 | errorModel.getChangeListeners();
|
|---|
| 197 | }
|
|---|
| 198 |
|
|---|
| 199 | q.setBorder(BorderFactory.createEmptyBorder(14, 0, 10, 0));
|
|---|
| 200 | p.add(q, GBC.eol());
|
|---|
| 201 | JCheckBox c = new JCheckBox(tr("Do not ask again"));
|
|---|
| 202 | p.add(c, GBC.eol());
|
|---|
| 203 |
|
|---|
| 204 | ExtendedDialog ed = new ExtendedDialog(MainApplication.getMainFrame(),
|
|---|
| 205 | tr("Simplify way"), tr("Simplify"),
|
|---|
| 206 | auto ? tr("Proceed without simplifying") : tr("Cancel"))
|
|---|
| 207 | .setContent(p)
|
|---|
| 208 | .configureContextsensitiveHelp("Action/SimplifyWay", true);
|
|---|
| 209 | if (auto) {
|
|---|
| 210 | ed.setButtonIcons("simplify", "ok");
|
|---|
| 211 | } else {
|
|---|
| 212 | ed.setButtonIcons("ok", "cancel");
|
|---|
| 213 | }
|
|---|
| 214 |
|
|---|
| 215 | int ret = ed.showDialog().getValue();
|
|---|
| 216 | double val = (double) n.getValue();
|
|---|
| 217 | changeCleanup.run();
|
|---|
| 218 | if (ret == 1) {
|
|---|
| 219 | s.putDouble(keyError, val);
|
|---|
| 220 | if (c.isSelected()) {
|
|---|
| 221 | s.put(keyRemember, "yes");
|
|---|
| 222 | }
|
|---|
| 223 | return val;
|
|---|
| 224 | } else {
|
|---|
| 225 | if (auto && c.isSelected()) { //do not remember cancel for manual simplify, otherwise nothing would happen
|
|---|
| 226 | s.put(keyRemember, "no");
|
|---|
| 227 | }
|
|---|
| 228 | return -1;
|
|---|
| 229 | }
|
|---|
| 230 | }
|
|---|
| 231 |
|
|---|
| 232 | @Override
|
|---|
| 233 | public void actionPerformed(ActionEvent e) {
|
|---|
| 234 | final DataSet ds = getLayerManager().getEditDataSet();
|
|---|
| 235 | final List<Way> ways = new ArrayList<>();
|
|---|
| 236 | final SimplifyWayDataSelectionListener listener = new SimplifyWayDataSelectionListener(ways);
|
|---|
| 237 | ds.addSelectionListener(listener);
|
|---|
| 238 | try {
|
|---|
| 239 | listener.updateWayList(ds);
|
|---|
| 240 | if (ways.isEmpty()) {
|
|---|
| 241 | alertSelectAtLeastOneWay();
|
|---|
| 242 | return;
|
|---|
| 243 | } else if (!confirmWayWithNodesOutsideBoundingBox(ways) || (ways.size() > 10 && !confirmSimplifyManyWays(ways.size()))) {
|
|---|
| 244 | return;
|
|---|
| 245 | }
|
|---|
| 246 |
|
|---|
| 247 | final Supplier<String> lengthStr = () -> SystemOfMeasurement.getSystemOfMeasurement().getDistText(
|
|---|
| 248 | ways.stream().mapToDouble(Way::getLength).sum());
|
|---|
| 249 |
|
|---|
| 250 | final double err = askSimplifyWays(ways, () -> trn(
|
|---|
| 251 | "You are about to simplify {0} way with a total length of {1}.",
|
|---|
| 252 | "You are about to simplify {0} ways with a total length of {1}.",
|
|---|
| 253 | ways.size(), ways.size(), lengthStr.get()), listener, false);
|
|---|
| 254 |
|
|---|
| 255 | if (err > 0) {
|
|---|
| 256 | simplifyWays(ways, err);
|
|---|
| 257 | }
|
|---|
| 258 | } finally {
|
|---|
| 259 | ds.removeSelectionListener(listener);
|
|---|
| 260 | }
|
|---|
| 261 | }
|
|---|
| 262 |
|
|---|
| 263 | /**
|
|---|
| 264 | * Replies true if <code>node</code> is a required node which can't be removed
|
|---|
| 265 | * in order to simplify the way.
|
|---|
| 266 | *
|
|---|
| 267 | * @param way the way to be simplified
|
|---|
| 268 | * @param node the node to check
|
|---|
| 269 | * @param multipleUseNodes set of nodes which is used more than once in the way
|
|---|
| 270 | * @return true if <code>node</code> is a required node which can't be removed
|
|---|
| 271 | * in order to simplify the way.
|
|---|
| 272 | */
|
|---|
| 273 | protected static boolean isRequiredNode(Way way, Node node, Set<Node> multipleUseNodes) {
|
|---|
| 274 | boolean isRequired = node.isTagged();
|
|---|
| 275 | if (!isRequired && multipleUseNodes.contains(node)) {
|
|---|
| 276 | int frequency = Collections.frequency(way.getNodes(), node);
|
|---|
| 277 | if ((way.getNode(0) == node) && (way.getNode(way.getNodesCount()-1) == node)) {
|
|---|
| 278 | frequency = frequency - 1; // closed way closing node counted only once
|
|---|
| 279 | }
|
|---|
| 280 | isRequired = frequency > 1;
|
|---|
| 281 | }
|
|---|
| 282 | if (!isRequired) {
|
|---|
| 283 | List<OsmPrimitive> parents = new LinkedList<>(node.getReferrers());
|
|---|
| 284 | parents.remove(way);
|
|---|
| 285 | isRequired = !parents.isEmpty();
|
|---|
| 286 | }
|
|---|
| 287 | return isRequired;
|
|---|
| 288 | }
|
|---|
| 289 |
|
|---|
| 290 | /**
|
|---|
| 291 | * Calculate a set of nodes which occurs more than once in the way
|
|---|
| 292 | * @param w the way
|
|---|
| 293 | * @return a set of nodes which occurs more than once in the way
|
|---|
| 294 | */
|
|---|
| 295 | private static Set<Node> getMultiUseNodes(Way w) {
|
|---|
| 296 | Set<Node> allNodes = new HashSet<>();
|
|---|
| 297 | return w.getNodes().stream()
|
|---|
| 298 | .filter(n -> !allNodes.add(n))
|
|---|
| 299 | .collect(Collectors.toSet());
|
|---|
| 300 | }
|
|---|
| 301 |
|
|---|
| 302 | /**
|
|---|
| 303 | * Runs the commands to simplify the ways with the given threshold
|
|---|
| 304 | *
|
|---|
| 305 | * @param ways the ways to simplify
|
|---|
| 306 | * @param threshold the max error threshold
|
|---|
| 307 | * @return The number of nodes removed from the ways (does not double-count)
|
|---|
| 308 | * @since 16566
|
|---|
| 309 | */
|
|---|
| 310 | public static int simplifyWaysCountNodesRemoved(List<Way> ways, double threshold) {
|
|---|
| 311 | Command command = buildSimplifyWaysCommand(ways, threshold);
|
|---|
| 312 | if (command == null) {
|
|---|
| 313 | return 0;
|
|---|
| 314 | }
|
|---|
| 315 | return (int) command.getParticipatingPrimitives().stream()
|
|---|
| 316 | .filter(Node.class::isInstance)
|
|---|
| 317 | .count();
|
|---|
| 318 | }
|
|---|
| 319 |
|
|---|
| 320 | /**
|
|---|
| 321 | * Runs the commands to simplify the ways with the given threshold
|
|---|
| 322 | *
|
|---|
| 323 | * @param ways the ways to simplify
|
|---|
| 324 | * @param threshold the max error threshold
|
|---|
| 325 | * @since 15419
|
|---|
| 326 | */
|
|---|
| 327 | public static void simplifyWays(List<Way> ways, double threshold) {
|
|---|
| 328 | Command command = buildSimplifyWaysCommand(ways, threshold);
|
|---|
| 329 | if (command != null) {
|
|---|
| 330 | UndoRedoHandler.getInstance().add(command);
|
|---|
| 331 | }
|
|---|
| 332 | }
|
|---|
| 333 |
|
|---|
| 334 | /**
|
|---|
| 335 | * Creates the commands to simplify the ways with the given threshold
|
|---|
| 336 | *
|
|---|
| 337 | * @param ways the ways to simplify
|
|---|
| 338 | * @param threshold the max error threshold
|
|---|
| 339 | * @return The command to simplify ways
|
|---|
| 340 | * @since 16566 (private)
|
|---|
| 341 | */
|
|---|
| 342 | private static SequenceCommand buildSimplifyWaysCommand(List<Way> ways, double threshold) {
|
|---|
| 343 | List<Command> allCommands = ways.stream()
|
|---|
| 344 | .map(way -> createSimplifyCommand(way, threshold, false))
|
|---|
| 345 | .filter(Objects::nonNull)
|
|---|
| 346 | .collect(StreamUtils.toUnmodifiableList());
|
|---|
| 347 | if (allCommands.isEmpty())
|
|---|
| 348 | return null;
|
|---|
| 349 | final List<OsmPrimitive> deletedPrimitives = allCommands.stream()
|
|---|
| 350 | .map(Command::getChildren)
|
|---|
| 351 | .flatMap(Collection::stream)
|
|---|
| 352 | .filter(DeleteCommand.class::isInstance)
|
|---|
| 353 | .map(DeleteCommand.class::cast)
|
|---|
| 354 | .map(DeleteCommand::getParticipatingPrimitives)
|
|---|
| 355 | .flatMap(Collection::stream)
|
|---|
| 356 | .collect(Collectors.toList());
|
|---|
| 357 | allCommands.get(0).getAffectedDataSet().clearSelection(deletedPrimitives);
|
|---|
| 358 | return new SequenceCommand(
|
|---|
| 359 | trn("Simplify {0} way", "Simplify {0} ways", allCommands.size(), allCommands.size()),
|
|---|
| 360 | allCommands);
|
|---|
| 361 | }
|
|---|
| 362 |
|
|---|
| 363 | /**
|
|---|
| 364 | * Creates the SequenceCommand to simplify a way with default threshold.
|
|---|
| 365 | *
|
|---|
| 366 | * @param w the way to simplify
|
|---|
| 367 | * @return The sequence of commands to run
|
|---|
| 368 | * @since 15419
|
|---|
| 369 | */
|
|---|
| 370 | public static SequenceCommand createSimplifyCommand(Way w) {
|
|---|
| 371 | return createSimplifyCommand(w, Config.getPref().getDouble("simplify-way.max-error", 3.0));
|
|---|
| 372 | }
|
|---|
| 373 |
|
|---|
| 374 | /**
|
|---|
| 375 | * Creates the SequenceCommand to simplify a way with a given threshold.
|
|---|
| 376 | *
|
|---|
| 377 | * @param w the way to simplify
|
|---|
| 378 | * @param threshold the max error threshold
|
|---|
| 379 | * @return The sequence of commands to run
|
|---|
| 380 | * @since 15419
|
|---|
| 381 | */
|
|---|
| 382 | public static SequenceCommand createSimplifyCommand(Way w, double threshold) {
|
|---|
| 383 | return createSimplifyCommand(w, threshold, true);
|
|---|
| 384 | }
|
|---|
| 385 |
|
|---|
| 386 | /**
|
|---|
| 387 | * Creates the SequenceCommand to simplify a way with a given threshold.
|
|---|
| 388 | *
|
|---|
| 389 | * @param w the way to simplify
|
|---|
| 390 | * @param threshold the max error threshold
|
|---|
| 391 | * @param deselect {@code true} if we want to deselect the deleted nodes
|
|---|
| 392 | * @return The sequence of commands to run
|
|---|
| 393 | */
|
|---|
| 394 | private static SequenceCommand createSimplifyCommand(Way w, double threshold, boolean deselect) {
|
|---|
| 395 | int lower = 0;
|
|---|
| 396 | int i = 0;
|
|---|
| 397 |
|
|---|
| 398 | Set<Node> multipleUseNodes = getMultiUseNodes(w);
|
|---|
| 399 | List<Node> newNodes = new ArrayList<>(w.getNodesCount());
|
|---|
| 400 | while (i < w.getNodesCount()) {
|
|---|
| 401 | if (isRequiredNode(w, w.getNode(i), multipleUseNodes)) {
|
|---|
| 402 | // copy a required node to the list of new nodes. Simplify not possible
|
|---|
| 403 | newNodes.add(w.getNode(i));
|
|---|
| 404 | i++;
|
|---|
| 405 | lower++;
|
|---|
| 406 | continue;
|
|---|
| 407 | }
|
|---|
| 408 | i++;
|
|---|
| 409 | // find the longest sequence of not required nodes ...
|
|---|
| 410 | while (i < w.getNodesCount() && !isRequiredNode(w, w.getNode(i), multipleUseNodes)) {
|
|---|
| 411 | i++;
|
|---|
| 412 | }
|
|---|
| 413 | // ... and simplify them
|
|---|
| 414 | buildSimplifiedNodeList(w.getNodes(), lower, Math.min(w.getNodesCount()-1, i), threshold, newNodes);
|
|---|
| 415 | lower = i;
|
|---|
| 416 | i++;
|
|---|
| 417 | }
|
|---|
| 418 |
|
|---|
| 419 | // Closed way, check if the first node could also be simplified ...
|
|---|
| 420 | if (newNodes.size() > 3 && newNodes.get(0) == newNodes.get(newNodes.size() - 1)
|
|---|
| 421 | && !isRequiredNode(w, newNodes.get(0), multipleUseNodes)) {
|
|---|
| 422 | final List<Node> l1 = Arrays.asList(newNodes.get(newNodes.size() - 2), newNodes.get(0), newNodes.get(1));
|
|---|
| 423 | final List<Node> l2 = new ArrayList<>(3);
|
|---|
| 424 | buildSimplifiedNodeList(l1, 0, 2, threshold, l2);
|
|---|
| 425 | if (!l2.contains(newNodes.get(0))) {
|
|---|
| 426 | newNodes.remove(0);
|
|---|
| 427 | newNodes.set(newNodes.size() - 1, newNodes.get(0)); // close the way
|
|---|
| 428 | }
|
|---|
| 429 | }
|
|---|
| 430 |
|
|---|
| 431 | if (newNodes.size() == w.getNodesCount()) return null;
|
|---|
| 432 |
|
|---|
| 433 | Set<Node> delNodes = new HashSet<>(w.getNodes());
|
|---|
| 434 | delNodes.removeAll(newNodes);
|
|---|
| 435 |
|
|---|
| 436 | if (delNodes.isEmpty()) return null;
|
|---|
| 437 |
|
|---|
| 438 | Collection<Command> cmds = new LinkedList<>();
|
|---|
| 439 | cmds.add(new ChangeNodesCommand(w, newNodes));
|
|---|
| 440 | cmds.add(new DeleteCommand(w.getDataSet(), delNodes));
|
|---|
| 441 | if (deselect) {
|
|---|
| 442 | w.getDataSet().clearSelection(delNodes);
|
|---|
| 443 | }
|
|---|
| 444 | return new SequenceCommand(
|
|---|
| 445 | trn("Simplify Way (remove {0} node)", "Simplify Way (remove {0} nodes)", delNodes.size(), delNodes.size()), cmds);
|
|---|
| 446 | }
|
|---|
| 447 |
|
|---|
| 448 | /**
|
|---|
| 449 | * Builds the simplified list of nodes for a way segment given by a lower index <code>from</code>
|
|---|
| 450 | * and an upper index <code>to</code>. Uses the Douglas-Peucker-Algorithm.
|
|---|
| 451 | *
|
|---|
| 452 | * @param wnew the way to simplify
|
|---|
| 453 | * @param from the lower index
|
|---|
| 454 | * @param to the upper index
|
|---|
| 455 | * @param threshold the max error threshold
|
|---|
| 456 | * @param simplifiedNodes list that will contain resulting nodes
|
|---|
| 457 | */
|
|---|
| 458 | protected static void buildSimplifiedNodeList(List<Node> wnew, int from, int to, double threshold, List<Node> simplifiedNodes) {
|
|---|
| 459 |
|
|---|
| 460 | Node fromN = wnew.get(from);
|
|---|
| 461 | Node toN = wnew.get(to);
|
|---|
| 462 | EastNorth p1 = fromN.getEastNorth();
|
|---|
| 463 | EastNorth p2 = toN.getEastNorth();
|
|---|
| 464 | // Get max xte
|
|---|
| 465 | int imax = -1;
|
|---|
| 466 | double xtemax = 0;
|
|---|
| 467 | for (int i = from + 1; i < to; i++) {
|
|---|
| 468 | Node n = wnew.get(i);
|
|---|
| 469 | EastNorth p = n.getEastNorth();
|
|---|
| 470 | double ldx = p2.getX() - p1.getX();
|
|---|
| 471 | double ldy = p2.getY() - p1.getY();
|
|---|
| 472 | double offset;
|
|---|
| 473 | //segment zero length
|
|---|
| 474 | if (ldx == 0 && ldy == 0)
|
|---|
| 475 | offset = 0;
|
|---|
| 476 | else {
|
|---|
| 477 | double pdx = p.getX() - p1.getX();
|
|---|
| 478 | double pdy = p.getY() - p1.getY();
|
|---|
| 479 | offset = (pdx * ldx + pdy * ldy) / (ldx * ldx + ldy * ldy);
|
|---|
| 480 | }
|
|---|
| 481 | final double distRad;
|
|---|
| 482 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
|---|
| 483 | if (offset <= 0) {
|
|---|
| 484 | distRad = dist(fromN.lat() * Math.PI / 180, fromN.lon() * Math.PI / 180,
|
|---|
| 485 | n.lat() * Math.PI / 180, n.lon() * Math.PI / 180);
|
|---|
| 486 | } else if (offset >= 1) {
|
|---|
| 487 | distRad = dist(toN.lat() * Math.PI / 180, toN.lon() * Math.PI / 180,
|
|---|
| 488 | n.lat() * Math.PI / 180, n.lon() * Math.PI / 180);
|
|---|
| 489 | } else {
|
|---|
| 490 | distRad = xtd(fromN.lat() * Math.PI / 180, fromN.lon() * Math.PI / 180,
|
|---|
| 491 | toN.lat() * Math.PI / 180, toN.lon() * Math.PI / 180,
|
|---|
| 492 | n.lat() * Math.PI / 180, n.lon() * Math.PI / 180);
|
|---|
| 493 | }
|
|---|
| 494 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
|---|
| 495 | double xte = Math.abs(distRad);
|
|---|
| 496 | if (xte > xtemax) {
|
|---|
| 497 | xtemax = xte;
|
|---|
| 498 | imax = i;
|
|---|
| 499 | }
|
|---|
| 500 | }
|
|---|
| 501 | if (imax != -1 && Ellipsoid.WGS84.a * xtemax >= threshold) {
|
|---|
| 502 | // Segment cannot be simplified - try shorter segments
|
|---|
| 503 | buildSimplifiedNodeList(wnew, from, imax, threshold, simplifiedNodes);
|
|---|
| 504 | buildSimplifiedNodeList(wnew, imax, to, threshold, simplifiedNodes);
|
|---|
| 505 | } else {
|
|---|
| 506 | // Simplify segment
|
|---|
| 507 | if (simplifiedNodes.isEmpty() || simplifiedNodes.get(simplifiedNodes.size()-1) != fromN) {
|
|---|
| 508 | simplifiedNodes.add(fromN);
|
|---|
| 509 | }
|
|---|
| 510 | if (fromN != toN) {
|
|---|
| 511 | simplifiedNodes.add(toN);
|
|---|
| 512 | }
|
|---|
| 513 | }
|
|---|
| 514 | }
|
|---|
| 515 |
|
|---|
| 516 | /* From Aviaton Formulary v1.3
|
|---|
| 517 | * http://williams.best.vwh.net/avform.htm
|
|---|
| 518 | */
|
|---|
| 519 | private static double dist(double lat1, double lon1, double lat2, double lon2) {
|
|---|
| 520 | return 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2), 2) + Math.cos(lat1) * Math.cos(lat2)
|
|---|
| 521 | * Math.pow(Math.sin((lon1 - lon2) / 2), 2)));
|
|---|
| 522 | }
|
|---|
| 523 |
|
|---|
| 524 | private static double course(double lat1, double lon1, double lat2, double lon2) {
|
|---|
| 525 | return Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
|
|---|
| 526 | * Math.cos(lat2) * Math.cos(lon1 - lon2))
|
|---|
| 527 | % (2 * Math.PI);
|
|---|
| 528 | }
|
|---|
| 529 |
|
|---|
| 530 | private static double xtd(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3) {
|
|---|
| 531 | double distAD = dist(lat1, lon1, lat3, lon3);
|
|---|
| 532 | double crsAD = course(lat1, lon1, lat3, lon3);
|
|---|
| 533 | double crsAB = course(lat1, lon1, lat2, lon2);
|
|---|
| 534 | return Math.asin(Math.sin(distAD) * Math.sin(crsAD - crsAB));
|
|---|
| 535 | }
|
|---|
| 536 |
|
|---|
| 537 | @Override
|
|---|
| 538 | protected void updateEnabledState() {
|
|---|
| 539 | updateEnabledStateOnCurrentSelection();
|
|---|
| 540 | }
|
|---|
| 541 |
|
|---|
| 542 | @Override
|
|---|
| 543 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
|---|
| 544 | updateEnabledStateOnModifiableSelection(selection);
|
|---|
| 545 | }
|
|---|
| 546 |
|
|---|
| 547 | private static class SimplifyChangeListener implements ChangeListener {
|
|---|
| 548 | Command lastCommand;
|
|---|
| 549 | private final JLabel nodesToRemove;
|
|---|
| 550 | private final SpinnerNumberModel errorModel;
|
|---|
| 551 | private final List<Way> ways;
|
|---|
| 552 |
|
|---|
| 553 | SimplifyChangeListener(JLabel nodesToRemove, SpinnerNumberModel errorModel, List<Way> ways) {
|
|---|
| 554 | this.nodesToRemove = nodesToRemove;
|
|---|
| 555 | this.errorModel = errorModel;
|
|---|
| 556 | this.ways = ways;
|
|---|
| 557 | }
|
|---|
| 558 |
|
|---|
| 559 | @Override
|
|---|
| 560 | public void stateChanged(ChangeEvent e) {
|
|---|
| 561 | if (Objects.equals(UndoRedoHandler.getInstance().getLastCommand(), lastCommand)) {
|
|---|
| 562 | UndoRedoHandler.getInstance().undo();
|
|---|
| 563 | }
|
|---|
| 564 | double threshold = errorModel.getNumber().doubleValue();
|
|---|
| 565 | int removeNodes = simplifyWaysCountNodesRemoved(ways, threshold);
|
|---|
| 566 | nodesToRemove.setText(trn(
|
|---|
| 567 | "(about {0} node to remove)",
|
|---|
| 568 | "(about {0} nodes to remove)", removeNodes, removeNodes));
|
|---|
| 569 | lastCommand = SimplifyWayAction.buildSimplifyWaysCommand(ways, threshold);
|
|---|
| 570 | if (lastCommand != null) {
|
|---|
| 571 | UndoRedoHandler.getInstance().add(lastCommand);
|
|---|
| 572 | }
|
|---|
| 573 | }
|
|---|
| 574 | }
|
|---|
| 575 |
|
|---|
| 576 | private static final class SimplifyWayDataSelectionListener implements DataSelectionListener {
|
|---|
| 577 | private final List<Way> wayList;
|
|---|
| 578 | private Consumer<List<Way>> consumer;
|
|---|
| 579 |
|
|---|
| 580 | /**
|
|---|
| 581 | * Create a new selection listener for {@link SimplifyWayAction}
|
|---|
| 582 | * @param wayList The <i>modifiable</i> list to update on selection changes
|
|---|
| 583 | */
|
|---|
| 584 | SimplifyWayDataSelectionListener(List<Way> wayList) {
|
|---|
| 585 | this.wayList = wayList;
|
|---|
| 586 | }
|
|---|
| 587 |
|
|---|
| 588 | @Override
|
|---|
| 589 | public void selectionChanged(SelectionChangeEvent event) {
|
|---|
| 590 | updateWayList(event.getSource());
|
|---|
| 591 | }
|
|---|
| 592 |
|
|---|
| 593 | void updateWayList(DataSet dataSet) {
|
|---|
| 594 | final List<Way> newWays = dataSet.getSelectedWays().stream()
|
|---|
| 595 | .filter(p -> !p.isIncomplete())
|
|---|
| 596 | .collect(Collectors.toList());
|
|---|
| 597 | this.wayList.clear();
|
|---|
| 598 | this.wayList.addAll(newWays);
|
|---|
| 599 | if (this.consumer != null) {
|
|---|
| 600 | GuiHelper.runInEDT(() -> this.consumer.accept(this.wayList));
|
|---|
| 601 | }
|
|---|
| 602 | }
|
|---|
| 603 |
|
|---|
| 604 | void addConsumer(Consumer<List<Way>> wayConsumer) {
|
|---|
| 605 | this.consumer = wayConsumer;
|
|---|
| 606 | }
|
|---|
| 607 | }
|
|---|
| 608 | }
|
|---|