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

Last change on this file since 9291 was 9230, checked in by Don-vip, 8 years ago

fix javadoc errors/warnings

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