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

Last change on this file since 6296 was 6296, checked in by Don-vip, 11 years ago

Sonar/Findbugs - Avoid commented-out lines of code, javadoc

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