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