source: josm/trunk/src/org/openstreetmap/josm/actions/SimplifyWayAction.java@ 11464

Last change on this file since 11464 was 11385, checked in by Don-vip, 7 years ago

sonar - squid:S1066 - Collapsible "if" statements should be merged

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