1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.actions;
|
---|
3 |
|
---|
4 | import static org.openstreetmap.josm.gui.help.HelpUtil.ht;
|
---|
5 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
6 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
7 |
|
---|
8 | import java.awt.event.ActionEvent;
|
---|
9 | import java.awt.event.KeyEvent;
|
---|
10 | import java.util.ArrayList;
|
---|
11 | import java.util.Collection;
|
---|
12 | import java.util.Collections;
|
---|
13 | import java.util.HashSet;
|
---|
14 | import java.util.LinkedList;
|
---|
15 | import java.util.List;
|
---|
16 | import java.util.Set;
|
---|
17 |
|
---|
18 | import javax.swing.JOptionPane;
|
---|
19 |
|
---|
20 | import org.openstreetmap.josm.Main;
|
---|
21 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
22 | import org.openstreetmap.josm.command.Command;
|
---|
23 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
24 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
25 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
26 | import org.openstreetmap.josm.data.osm.Node;
|
---|
27 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
28 | import org.openstreetmap.josm.data.osm.Way;
|
---|
29 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
30 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
31 | import org.openstreetmap.josm.gui.Notification;
|
---|
32 | import org.openstreetmap.josm.gui.help.HelpUtil;
|
---|
33 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
34 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
35 |
|
---|
36 | /**
|
---|
37 | * Delete unnecessary nodes from a way
|
---|
38 | * @since 2575
|
---|
39 | */
|
---|
40 | public 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(HelpUtil.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 | int ret = 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 | HelpUtil.ht("/Action/SimplifyWay#ConfirmSimplifyAll")
|
---|
91 | );
|
---|
92 | return ret == 0;
|
---|
93 | }
|
---|
94 |
|
---|
95 | @Override
|
---|
96 | public void actionPerformed(ActionEvent e) {
|
---|
97 | DataSet ds = getCurrentDataSet();
|
---|
98 | ds.beginUpdate();
|
---|
99 | try {
|
---|
100 | List<Way> ways = OsmPrimitive.getFilteredList(ds.getSelected(), Way.class);
|
---|
101 | if (ways.isEmpty()) {
|
---|
102 | alertSelectAtLeastOneWay();
|
---|
103 | return;
|
---|
104 | } else if (!confirmWayWithNodesOutsideBoundingBox(ways)) {
|
---|
105 | return;
|
---|
106 | } else if (ways.size() > 10 && !confirmSimplifyManyWays(ways.size())) {
|
---|
107 | return;
|
---|
108 | }
|
---|
109 |
|
---|
110 | Collection<Command> allCommands = new LinkedList<>();
|
---|
111 | for (Way way: ways) {
|
---|
112 | SequenceCommand simplifyCommand = simplifyWay(way);
|
---|
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<>();
|
---|
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 with default threshold (read from preferences).
|
---|
155 | *
|
---|
156 | * @param w the way to simplify
|
---|
157 | * @return The sequence of commands to run
|
---|
158 | * @since 6411
|
---|
159 | */
|
---|
160 | public final SequenceCommand simplifyWay(Way w) {
|
---|
161 | return simplifyWay(w, Main.pref.getDouble("simplify-way.max-error", 3.0));
|
---|
162 | }
|
---|
163 |
|
---|
164 | /**
|
---|
165 | * Simplifies a way with a given threshold.
|
---|
166 | *
|
---|
167 | * @param w the way to simplify
|
---|
168 | * @param threshold the max error threshold
|
---|
169 | * @return The sequence of commands to run
|
---|
170 | * @since 6411
|
---|
171 | */
|
---|
172 | public SequenceCommand simplifyWay(Way w, double threshold) {
|
---|
173 | int lower = 0;
|
---|
174 | int i = 0;
|
---|
175 | List<Node> newNodes = new ArrayList<>(w.getNodesCount());
|
---|
176 | while (i < w.getNodesCount()) {
|
---|
177 | if (isRequiredNode(w, w.getNode(i))) {
|
---|
178 | // copy a required node to the list of new nodes. Simplify not possible
|
---|
179 | newNodes.add(w.getNode(i));
|
---|
180 | i++;
|
---|
181 | lower++;
|
---|
182 | continue;
|
---|
183 | }
|
---|
184 | i++;
|
---|
185 | // find the longest sequence of not required nodes ...
|
---|
186 | while (i < w.getNodesCount() && !isRequiredNode(w, w.getNode(i))) {
|
---|
187 | i++;
|
---|
188 | }
|
---|
189 | // ... and simplify them
|
---|
190 | buildSimplifiedNodeList(w.getNodes(), lower, Math.min(w.getNodesCount()-1, i), threshold, newNodes);
|
---|
191 | lower = i;
|
---|
192 | i++;
|
---|
193 | }
|
---|
194 |
|
---|
195 | Set<Node> delNodes = new HashSet<>();
|
---|
196 | delNodes.addAll(w.getNodes());
|
---|
197 | delNodes.removeAll(newNodes);
|
---|
198 |
|
---|
199 | if (delNodes.isEmpty()) return null;
|
---|
200 |
|
---|
201 | Collection<Command> cmds = new LinkedList<>();
|
---|
202 | Way newWay = new Way(w);
|
---|
203 | newWay.setNodes(newNodes);
|
---|
204 | cmds.add(new ChangeCommand(w, newWay));
|
---|
205 | cmds.add(new DeleteCommand(delNodes));
|
---|
206 | w.getDataSet().clearSelection(delNodes);
|
---|
207 | return new SequenceCommand(
|
---|
208 | trn("Simplify Way (remove {0} node)", "Simplify Way (remove {0} nodes)", delNodes.size(), delNodes.size()), cmds);
|
---|
209 | }
|
---|
210 |
|
---|
211 | /**
|
---|
212 | * Builds the simplified list of nodes for a way segment given by a lower index <code>from</code>
|
---|
213 | * and an upper index <code>to</code>
|
---|
214 | *
|
---|
215 | * @param wnew the way to simplify
|
---|
216 | * @param from the lower index
|
---|
217 | * @param to the upper index
|
---|
218 | * @param threshold the max error threshold
|
---|
219 | */
|
---|
220 | protected void buildSimplifiedNodeList(List<Node> wnew, int from, int to, double threshold, List<Node> simplifiedNodes) {
|
---|
221 |
|
---|
222 | Node fromN = wnew.get(from);
|
---|
223 | Node toN = wnew.get(to);
|
---|
224 |
|
---|
225 | // Get max xte
|
---|
226 | int imax = -1;
|
---|
227 | double xtemax = 0;
|
---|
228 | for (int i = from + 1; i < to; i++) {
|
---|
229 | Node n = wnew.get(i);
|
---|
230 | double xte = Math.abs(EARTH_RAD
|
---|
231 | * xtd(fromN.getCoor().lat() * Math.PI / 180, fromN.getCoor().lon() * Math.PI / 180, toN.getCoor().lat() * Math.PI
|
---|
232 | / 180, toN.getCoor().lon() * Math.PI / 180, n.getCoor().lat() * Math.PI / 180, n.getCoor().lon() * Math.PI
|
---|
233 | / 180));
|
---|
234 | if (xte > xtemax) {
|
---|
235 | xtemax = xte;
|
---|
236 | imax = i;
|
---|
237 | }
|
---|
238 | }
|
---|
239 |
|
---|
240 | if (imax != -1 && xtemax >= threshold) {
|
---|
241 | // Segment cannot be simplified - try shorter segments
|
---|
242 | buildSimplifiedNodeList(wnew, from, imax, threshold, simplifiedNodes);
|
---|
243 | buildSimplifiedNodeList(wnew, imax, to, threshold, simplifiedNodes);
|
---|
244 | } else {
|
---|
245 | // Simplify segment
|
---|
246 | if (simplifiedNodes.isEmpty() || simplifiedNodes.get(simplifiedNodes.size()-1) != fromN) {
|
---|
247 | simplifiedNodes.add(fromN);
|
---|
248 | }
|
---|
249 | if (fromN != toN) {
|
---|
250 | simplifiedNodes.add(toN);
|
---|
251 | }
|
---|
252 | }
|
---|
253 | }
|
---|
254 |
|
---|
255 | public static final double EARTH_RAD = 6378137.0;
|
---|
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 | }
|
---|