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

Last change on this file since 6227 was 6131, checked in by bastiK, 11 years ago

see #6963 - optional help button for notifications; use notifications in Simplify Way action

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