[6380] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[2575] | 2 | package org.openstreetmap.josm.actions;
|
---|
| 3 |
|
---|
[2976] | 4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
[2575] | 5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
| 6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
| 7 |
|
---|
| 8 | import java.awt.event.ActionEvent;
|
---|
| 9 | import java.awt.event.KeyEvent;
|
---|
[2999] | 10 | import java.util.ArrayList;
|
---|
[2575] | 11 | import java.util.Collection;
|
---|
| 12 | import java.util.Collections;
|
---|
| 13 | import java.util.HashSet;
|
---|
| 14 | import java.util.LinkedList;
|
---|
| 15 | import java.util.List;
|
---|
[8338] | 16 | import java.util.Set;
|
---|
[2575] | 17 |
|
---|
| 18 | import javax.swing.JOptionPane;
|
---|
| 19 |
|
---|
| 20 | import org.openstreetmap.josm.Main;
|
---|
| 21 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
| 22 | import org.openstreetmap.josm.command.Command;
|
---|
| 23 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
| 24 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
[3952] | 25 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
[2575] | 26 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 27 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
| 28 | import org.openstreetmap.josm.data.osm.Way;
|
---|
[9230] | 29 | import org.openstreetmap.josm.data.projection.Ellipsoid;
|
---|
[2976] | 30 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
| 31 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
[6131] | 32 | import org.openstreetmap.josm.gui.Notification;
|
---|
[2976] | 33 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
[2575] | 34 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
| 35 |
|
---|
[6248] | 36 | /**
|
---|
| 37 | * Delete unnecessary nodes from a way
|
---|
[6411] | 38 | * @since 2575
|
---|
[6248] | 39 | */
|
---|
[2575] | 40 | public class SimplifyWayAction extends JosmAction {
|
---|
[7509] | 41 |
|
---|
[6248] | 42 | /**
|
---|
| 43 | * Constructs a new {@code SimplifyWayAction}.
|
---|
| 44 | */
|
---|
[2575] | 45 | public SimplifyWayAction() {
|
---|
[8509] | 46 | super(tr("Simplify Way"), "simplify", tr("Delete unnecessary nodes from a way."),
|
---|
| 47 | Shortcut.registerShortcut("tools:simplify", tr("Tool: {0}", tr("Simplify Way")), KeyEvent.VK_Y, Shortcut.SHIFT), true);
|
---|
[2976] | 48 | putValue("help", ht("/Action/SimplifyWay"));
|
---|
[2575] | 49 | }
|
---|
| 50 |
|
---|
[4461] | 51 | protected boolean confirmWayWithNodesOutsideBoundingBox(List<? extends OsmPrimitive> primitives) {
|
---|
[6639] | 52 | return DeleteCommand.checkAndConfirmOutlyingDelete(primitives, null);
|
---|
[2976] | 53 | }
|
---|
| 54 |
|
---|
| 55 | protected void alertSelectAtLeastOneWay() {
|
---|
[6131] | 56 | new Notification(
|
---|
| 57 | tr("Please select at least one way to simplify."))
|
---|
| 58 | .setIcon(JOptionPane.WARNING_MESSAGE)
|
---|
| 59 | .setDuration(Notification.TIME_SHORT)
|
---|
[9972] | 60 | .setHelpTopic(ht("/Action/SimplifyWay#SelectAWayToSimplify"))
|
---|
[6131] | 61 | .show();
|
---|
[2976] | 62 | }
|
---|
| 63 |
|
---|
| 64 | protected boolean confirmSimplifyManyWays(int numWays) {
|
---|
| 65 | ButtonSpec[] options = new ButtonSpec[] {
|
---|
| 66 | new ButtonSpec(
|
---|
| 67 | tr("Yes"),
|
---|
| 68 | ImageProvider.get("ok"),
|
---|
| 69 | tr("Simplify all selected ways"),
|
---|
| 70 | null
|
---|
[4869] | 71 | ),
|
---|
| 72 | new ButtonSpec(
|
---|
| 73 | tr("Cancel"),
|
---|
| 74 | ImageProvider.get("cancel"),
|
---|
| 75 | tr("Cancel operation"),
|
---|
| 76 | null
|
---|
| 77 | )
|
---|
[2976] | 78 | };
|
---|
[9917] | 79 | return 0 == HelpAwareOptionPane.showOptionDialog(
|
---|
[2976] | 80 | Main.parent,
|
---|
| 81 | tr(
|
---|
| 82 | "The selection contains {0} ways. Are you sure you want to simplify them all?",
|
---|
| 83 | numWays
|
---|
[4869] | 84 | ),
|
---|
| 85 | tr("Simplify ways?"),
|
---|
| 86 | JOptionPane.WARNING_MESSAGE,
|
---|
| 87 | null, // no special icon
|
---|
| 88 | options,
|
---|
| 89 | options[0],
|
---|
[9972] | 90 | ht("/Action/SimplifyWay#ConfirmSimplifyAll")
|
---|
[4869] | 91 | );
|
---|
[2976] | 92 | }
|
---|
| 93 |
|
---|
[6084] | 94 | @Override
|
---|
[2976] | 95 | public void actionPerformed(ActionEvent e) {
|
---|
[3952] | 96 | DataSet ds = getCurrentDataSet();
|
---|
| 97 | ds.beginUpdate();
|
---|
[6981] | 98 | try {
|
---|
[4461] | 99 | List<Way> ways = OsmPrimitive.getFilteredList(ds.getSelected(), Way.class);
|
---|
[3952] | 100 | if (ways.isEmpty()) {
|
---|
| 101 | alertSelectAtLeastOneWay();
|
---|
[2575] | 102 | return;
|
---|
[6981] | 103 | } else if (!confirmWayWithNodesOutsideBoundingBox(ways)) {
|
---|
[4461] | 104 | return;
|
---|
[6981] | 105 | } else if (ways.size() > 10 && !confirmSimplifyManyWays(ways.size())) {
|
---|
| 106 | return;
|
---|
[3952] | 107 | }
|
---|
[2575] | 108 |
|
---|
[7005] | 109 | Collection<Command> allCommands = new LinkedList<>();
|
---|
[3952] | 110 | for (Way way: ways) {
|
---|
[6411] | 111 | SequenceCommand simplifyCommand = simplifyWay(way);
|
---|
[3952] | 112 | if (simplifyCommand == null) {
|
---|
| 113 | continue;
|
---|
| 114 | }
|
---|
| 115 | allCommands.add(simplifyCommand);
|
---|
[2575] | 116 | }
|
---|
[3952] | 117 | if (allCommands.isEmpty()) return;
|
---|
| 118 | SequenceCommand rootCommand = new SequenceCommand(
|
---|
| 119 | trn("Simplify {0} way", "Simplify {0} ways", allCommands.size(), allCommands.size()),
|
---|
| 120 | allCommands
|
---|
[4869] | 121 | );
|
---|
[3952] | 122 | Main.main.undoRedo.add(rootCommand);
|
---|
| 123 | } finally {
|
---|
| 124 | ds.endUpdate();
|
---|
[2575] | 125 | }
|
---|
[2976] | 126 | Main.map.repaint();
|
---|
[2575] | 127 | }
|
---|
| 128 |
|
---|
[2976] | 129 | /**
|
---|
| 130 | * Replies true if <code>node</code> is a required node which can't be removed
|
---|
| 131 | * in order to simplify the way.
|
---|
[3530] | 132 | *
|
---|
[2976] | 133 | * @param way the way to be simplified
|
---|
| 134 | * @param node the node to check
|
---|
| 135 | * @return true if <code>node</code> is a required node which can't be removed
|
---|
| 136 | * in order to simplify the way.
|
---|
| 137 | */
|
---|
| 138 | protected boolean isRequiredNode(Way way, Node node) {
|
---|
| 139 | boolean isRequired = Collections.frequency(way.getNodes(), node) > 1;
|
---|
[8443] | 140 | if (!isRequired) {
|
---|
[7005] | 141 | List<OsmPrimitive> parents = new LinkedList<>();
|
---|
[2976] | 142 | parents.addAll(node.getReferrers());
|
---|
| 143 | parents.remove(way);
|
---|
| 144 | isRequired = !parents.isEmpty();
|
---|
| 145 | }
|
---|
| 146 | if (!isRequired) {
|
---|
| 147 | isRequired = node.isTagged();
|
---|
| 148 | }
|
---|
| 149 | return isRequired;
|
---|
| 150 | }
|
---|
| 151 |
|
---|
| 152 | /**
|
---|
[6411] | 153 | * Simplifies a way with default threshold (read from preferences).
|
---|
[3530] | 154 | *
|
---|
[2976] | 155 | * @param w the way to simplify
|
---|
[6411] | 156 | * @return The sequence of commands to run
|
---|
| 157 | * @since 6411
|
---|
[2976] | 158 | */
|
---|
[6411] | 159 | public final SequenceCommand simplifyWay(Way w) {
|
---|
| 160 | return simplifyWay(w, Main.pref.getDouble("simplify-way.max-error", 3.0));
|
---|
| 161 | }
|
---|
| 162 |
|
---|
| 163 | /**
|
---|
| 164 | * Simplifies a way with a given threshold.
|
---|
| 165 | *
|
---|
| 166 | * @param w the way to simplify
|
---|
[8459] | 167 | * @param threshold the max error threshold
|
---|
[6411] | 168 | * @return The sequence of commands to run
|
---|
| 169 | * @since 6411
|
---|
| 170 | */
|
---|
| 171 | public SequenceCommand simplifyWay(Way w, double threshold) {
|
---|
[2976] | 172 | int lower = 0;
|
---|
| 173 | int i = 0;
|
---|
[7005] | 174 | List<Node> newNodes = new ArrayList<>(w.getNodesCount());
|
---|
[8510] | 175 | while (i < w.getNodesCount()) {
|
---|
| 176 | if (isRequiredNode(w, w.getNode(i))) {
|
---|
[6411] | 177 | // copy a required node to the list of new nodes. Simplify not possible
|
---|
[2976] | 178 | newNodes.add(w.getNode(i));
|
---|
| 179 | i++;
|
---|
| 180 | lower++;
|
---|
| 181 | continue;
|
---|
[2575] | 182 | }
|
---|
[2976] | 183 | i++;
|
---|
| 184 | // find the longest sequence of not required nodes ...
|
---|
[8510] | 185 | while (i < w.getNodesCount() && !isRequiredNode(w, w.getNode(i))) {
|
---|
[2976] | 186 | i++;
|
---|
[2575] | 187 | }
|
---|
[2976] | 188 | // ... and simplify them
|
---|
[8459] | 189 | buildSimplifiedNodeList(w.getNodes(), lower, Math.min(w.getNodesCount()-1, i), threshold, newNodes);
|
---|
[8510] | 190 | lower = i;
|
---|
[2976] | 191 | i++;
|
---|
[2575] | 192 | }
|
---|
| 193 |
|
---|
[8338] | 194 | Set<Node> delNodes = new HashSet<>();
|
---|
[2575] | 195 | delNodes.addAll(w.getNodes());
|
---|
[2976] | 196 | delNodes.removeAll(newNodes);
|
---|
[2575] | 197 |
|
---|
[2976] | 198 | if (delNodes.isEmpty()) return null;
|
---|
[2575] | 199 |
|
---|
[7005] | 200 | Collection<Command> cmds = new LinkedList<>();
|
---|
[2976] | 201 | Way newWay = new Way(w);
|
---|
| 202 | newWay.setNodes(newNodes);
|
---|
| 203 | cmds.add(new ChangeCommand(w, newWay));
|
---|
| 204 | cmds.add(new DeleteCommand(delNodes));
|
---|
[6411] | 205 | w.getDataSet().clearSelection(delNodes);
|
---|
[8540] | 206 | return new SequenceCommand(
|
---|
| 207 | trn("Simplify Way (remove {0} node)", "Simplify Way (remove {0} nodes)", delNodes.size(), delNodes.size()), cmds);
|
---|
[2575] | 208 | }
|
---|
| 209 |
|
---|
[2976] | 210 | /**
|
---|
| 211 | * Builds the simplified list of nodes for a way segment given by a lower index <code>from</code>
|
---|
| 212 | * and an upper index <code>to</code>
|
---|
[3530] | 213 | *
|
---|
[2976] | 214 | * @param wnew the way to simplify
|
---|
| 215 | * @param from the lower index
|
---|
| 216 | * @param to the upper index
|
---|
[8459] | 217 | * @param threshold the max error threshold
|
---|
[9230] | 218 | * @param simplifiedNodes list that will contain resulting nodes
|
---|
[2575] | 219 | */
|
---|
[2999] | 220 | protected void buildSimplifiedNodeList(List<Node> wnew, int from, int to, double threshold, List<Node> simplifiedNodes) {
|
---|
[2575] | 221 |
|
---|
[2999] | 222 | Node fromN = wnew.get(from);
|
---|
| 223 | Node toN = wnew.get(to);
|
---|
[2976] | 224 |
|
---|
[2999] | 225 | // Get max xte
|
---|
[2575] | 226 | int imax = -1;
|
---|
| 227 | double xtemax = 0;
|
---|
| 228 | for (int i = from + 1; i < to; i++) {
|
---|
[2999] | 229 | Node n = wnew.get(i);
|
---|
[9230] | 230 | double xte = Math.abs(Ellipsoid.WGS84.a
|
---|
[2575] | 231 | * xtd(fromN.getCoor().lat() * Math.PI / 180, fromN.getCoor().lon() * Math.PI / 180, toN.getCoor().lat() * Math.PI
|
---|
| 232 | / 180, toN.getCoor().lon() * Math.PI / 180, n.getCoor().lat() * Math.PI / 180, n.getCoor().lon() * Math.PI
|
---|
| 233 | / 180));
|
---|
| 234 | if (xte > xtemax) {
|
---|
| 235 | xtemax = xte;
|
---|
| 236 | imax = i;
|
---|
| 237 | }
|
---|
| 238 | }
|
---|
| 239 |
|
---|
[2976] | 240 | if (imax != -1 && xtemax >= threshold) {
|
---|
[2999] | 241 | // Segment cannot be simplified - try shorter segments
|
---|
[8510] | 242 | buildSimplifiedNodeList(wnew, from, imax, threshold, simplifiedNodes);
|
---|
| 243 | buildSimplifiedNodeList(wnew, imax, to, threshold, simplifiedNodes);
|
---|
[2976] | 244 | } else {
|
---|
[2999] | 245 | // Simplify segment
|
---|
[2976] | 246 | if (simplifiedNodes.isEmpty() || simplifiedNodes.get(simplifiedNodes.size()-1) != fromN) {
|
---|
| 247 | simplifiedNodes.add(fromN);
|
---|
| 248 | }
|
---|
[2999] | 249 | if (fromN != toN) {
|
---|
[2976] | 250 | simplifiedNodes.add(toN);
|
---|
| 251 | }
|
---|
[2575] | 252 | }
|
---|
| 253 | }
|
---|
| 254 |
|
---|
| 255 | /* From Aviaton Formulary v1.3
|
---|
| 256 | * http://williams.best.vwh.net/avform.htm
|
---|
| 257 | */
|
---|
| 258 | public static double dist(double lat1, double lon1, double lat2, double lon2) {
|
---|
| 259 | return 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2), 2) + Math.cos(lat1) * Math.cos(lat2)
|
---|
| 260 | * Math.pow(Math.sin((lon1 - lon2) / 2), 2)));
|
---|
| 261 | }
|
---|
| 262 |
|
---|
| 263 | public static double course(double lat1, double lon1, double lat2, double lon2) {
|
---|
| 264 | return Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
|
---|
| 265 | * Math.cos(lat2) * Math.cos(lon1 - lon2))
|
---|
| 266 | % (2 * Math.PI);
|
---|
| 267 | }
|
---|
| 268 |
|
---|
| 269 | public static double xtd(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3) {
|
---|
[6981] | 270 | double distAD = dist(lat1, lon1, lat3, lon3);
|
---|
| 271 | double crsAD = course(lat1, lon1, lat3, lon3);
|
---|
| 272 | double crsAB = course(lat1, lon1, lat2, lon2);
|
---|
| 273 | return Math.asin(Math.sin(distAD) * Math.sin(crsAD - crsAB));
|
---|
[2575] | 274 | }
|
---|
| 275 |
|
---|
| 276 | @Override
|
---|
| 277 | protected void updateEnabledState() {
|
---|
| 278 | if (getCurrentDataSet() == null) {
|
---|
| 279 | setEnabled(false);
|
---|
| 280 | } else {
|
---|
| 281 | updateEnabledState(getCurrentDataSet().getSelected());
|
---|
| 282 | }
|
---|
| 283 | }
|
---|
| 284 |
|
---|
| 285 | @Override
|
---|
| 286 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
| 287 | setEnabled(selection != null && !selection.isEmpty());
|
---|
| 288 | }
|
---|
| 289 | }
|
---|