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

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

see #12472, fix #13230, fix #13225, fix #13228 - disable ReferenceEquality warning + partial revert of r10656 + r10659, causes too much problems

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