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.Arrays;
|
---|
12 | import java.util.Collection;
|
---|
13 | import java.util.Collections;
|
---|
14 | import java.util.HashSet;
|
---|
15 | import java.util.LinkedList;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.Set;
|
---|
18 |
|
---|
19 | import javax.swing.JOptionPane;
|
---|
20 | import javax.swing.SwingUtilities;
|
---|
21 |
|
---|
22 | import org.openstreetmap.josm.Main;
|
---|
23 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
24 | import org.openstreetmap.josm.command.Command;
|
---|
25 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
26 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
27 | import org.openstreetmap.josm.data.osm.DataSet;
|
---|
28 | import org.openstreetmap.josm.data.osm.Node;
|
---|
29 | import org.openstreetmap.josm.data.osm.OsmPrimitive;
|
---|
30 | import org.openstreetmap.josm.data.osm.Way;
|
---|
31 | import org.openstreetmap.josm.data.projection.Ellipsoid;
|
---|
32 | import org.openstreetmap.josm.gui.HelpAwareOptionPane;
|
---|
33 | import org.openstreetmap.josm.gui.HelpAwareOptionPane.ButtonSpec;
|
---|
34 | import org.openstreetmap.josm.gui.Notification;
|
---|
35 | import org.openstreetmap.josm.tools.ImageProvider;
|
---|
36 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
37 |
|
---|
38 | /**
|
---|
39 | * Delete unnecessary nodes from a way
|
---|
40 | * @since 2575
|
---|
41 | */
|
---|
42 | public class SimplifyWayAction extends JosmAction {
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Constructs a new {@code SimplifyWayAction}.
|
---|
46 | */
|
---|
47 | public SimplifyWayAction() {
|
---|
48 | super(tr("Simplify Way"), "simplify", tr("Delete unnecessary nodes from a way."),
|
---|
49 | Shortcut.registerShortcut("tools:simplify", tr("Tool: {0}", tr("Simplify Way")), KeyEvent.VK_Y, Shortcut.SHIFT), true);
|
---|
50 | putValue("help", ht("/Action/SimplifyWay"));
|
---|
51 | }
|
---|
52 |
|
---|
53 | protected boolean confirmWayWithNodesOutsideBoundingBox(List<? extends OsmPrimitive> primitives) {
|
---|
54 | return DeleteCommand.checkAndConfirmOutlyingDelete(primitives, null);
|
---|
55 | }
|
---|
56 |
|
---|
57 | protected void alertSelectAtLeastOneWay() {
|
---|
58 | SwingUtilities.invokeLater(() ->
|
---|
59 | new Notification(
|
---|
60 | tr("Please select at least one way to simplify."))
|
---|
61 | .setIcon(JOptionPane.WARNING_MESSAGE)
|
---|
62 | .setDuration(Notification.TIME_SHORT)
|
---|
63 | .setHelpTopic(ht("/Action/SimplifyWay#SelectAWayToSimplify"))
|
---|
64 | .show()
|
---|
65 | );
|
---|
66 | }
|
---|
67 |
|
---|
68 | protected boolean confirmSimplifyManyWays(int numWays) {
|
---|
69 | ButtonSpec[] options = new ButtonSpec[] {
|
---|
70 | new ButtonSpec(
|
---|
71 | tr("Yes"),
|
---|
72 | ImageProvider.get("ok"),
|
---|
73 | tr("Simplify all selected ways"),
|
---|
74 | null
|
---|
75 | ),
|
---|
76 | new ButtonSpec(
|
---|
77 | tr("Cancel"),
|
---|
78 | ImageProvider.get("cancel"),
|
---|
79 | tr("Cancel operation"),
|
---|
80 | null
|
---|
81 | )
|
---|
82 | };
|
---|
83 | return 0 == HelpAwareOptionPane.showOptionDialog(
|
---|
84 | Main.parent,
|
---|
85 | tr(
|
---|
86 | "The selection contains {0} ways. Are you sure you want to simplify them all?",
|
---|
87 | numWays
|
---|
88 | ),
|
---|
89 | tr("Simplify ways?"),
|
---|
90 | JOptionPane.WARNING_MESSAGE,
|
---|
91 | null, // no special icon
|
---|
92 | options,
|
---|
93 | options[0],
|
---|
94 | ht("/Action/SimplifyWay#ConfirmSimplifyAll")
|
---|
95 | );
|
---|
96 | }
|
---|
97 |
|
---|
98 | @Override
|
---|
99 | public void actionPerformed(ActionEvent e) {
|
---|
100 | DataSet ds = getLayerManager().getEditDataSet();
|
---|
101 | ds.beginUpdate();
|
---|
102 | try {
|
---|
103 | List<Way> ways = OsmPrimitive.getFilteredList(ds.getSelected(), Way.class);
|
---|
104 | if (ways.isEmpty()) {
|
---|
105 | alertSelectAtLeastOneWay();
|
---|
106 | return;
|
---|
107 | } else if (!confirmWayWithNodesOutsideBoundingBox(ways) || (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 | }
|
---|
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 | int frequency = Collections.frequency(way.getNodes(), node);
|
---|
141 | if ((way.getNode(0) == node) && (way.getNode(way.getNodesCount()-1) == node)) {
|
---|
142 | frequency = frequency - 1; // closed way closing node counted only once
|
---|
143 | }
|
---|
144 | boolean isRequired = frequency > 1;
|
---|
145 | if (!isRequired) {
|
---|
146 | List<OsmPrimitive> parents = new LinkedList<>();
|
---|
147 | parents.addAll(node.getReferrers());
|
---|
148 | parents.remove(way);
|
---|
149 | isRequired = !parents.isEmpty();
|
---|
150 | }
|
---|
151 | if (!isRequired) {
|
---|
152 | isRequired = node.isTagged();
|
---|
153 | }
|
---|
154 | return isRequired;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /**
|
---|
158 | * Simplifies a way with default threshold (read from preferences).
|
---|
159 | *
|
---|
160 | * @param w the way to simplify
|
---|
161 | * @return The sequence of commands to run
|
---|
162 | * @since 6411
|
---|
163 | */
|
---|
164 | public final SequenceCommand simplifyWay(Way w) {
|
---|
165 | return simplifyWay(w, Main.pref.getDouble("simplify-way.max-error", 3.0));
|
---|
166 | }
|
---|
167 |
|
---|
168 | /**
|
---|
169 | * Simplifies a way with a given threshold.
|
---|
170 | *
|
---|
171 | * @param w the way to simplify
|
---|
172 | * @param threshold the max error threshold
|
---|
173 | * @return The sequence of commands to run
|
---|
174 | * @since 6411
|
---|
175 | */
|
---|
176 | public SequenceCommand simplifyWay(Way w, double threshold) {
|
---|
177 | int lower = 0;
|
---|
178 | int i = 0;
|
---|
179 | List<Node> newNodes = new ArrayList<>(w.getNodesCount());
|
---|
180 | while (i < w.getNodesCount()) {
|
---|
181 | if (isRequiredNode(w, w.getNode(i))) {
|
---|
182 | // copy a required node to the list of new nodes. Simplify not possible
|
---|
183 | newNodes.add(w.getNode(i));
|
---|
184 | i++;
|
---|
185 | lower++;
|
---|
186 | continue;
|
---|
187 | }
|
---|
188 | i++;
|
---|
189 | // find the longest sequence of not required nodes ...
|
---|
190 | while (i < w.getNodesCount() && !isRequiredNode(w, w.getNode(i))) {
|
---|
191 | i++;
|
---|
192 | }
|
---|
193 | // ... and simplify them
|
---|
194 | buildSimplifiedNodeList(w.getNodes(), lower, Math.min(w.getNodesCount()-1, i), threshold, newNodes);
|
---|
195 | lower = i;
|
---|
196 | i++;
|
---|
197 | }
|
---|
198 |
|
---|
199 | // Closed way, check if the first node could also be simplified ...
|
---|
200 | if (newNodes.size() > 3 && newNodes.get(0) == newNodes.get(newNodes.size() - 1) && !isRequiredNode(w, newNodes.get(0))) {
|
---|
201 | final List<Node> l1 = Arrays.asList(newNodes.get(newNodes.size() - 2), newNodes.get(0), newNodes.get(1));
|
---|
202 | final List<Node> l2 = new ArrayList<>(3);
|
---|
203 | buildSimplifiedNodeList(l1, 0, 2, threshold, l2);
|
---|
204 | if (!l2.contains(newNodes.get(0))) {
|
---|
205 | newNodes.remove(0);
|
---|
206 | newNodes.set(newNodes.size() - 1, newNodes.get(0)); // close the way
|
---|
207 | }
|
---|
208 | }
|
---|
209 |
|
---|
210 | Set<Node> delNodes = new HashSet<>();
|
---|
211 | delNodes.addAll(w.getNodes());
|
---|
212 | delNodes.removeAll(newNodes);
|
---|
213 |
|
---|
214 | if (delNodes.isEmpty()) return null;
|
---|
215 |
|
---|
216 | Collection<Command> cmds = new LinkedList<>();
|
---|
217 | Way newWay = new Way(w);
|
---|
218 | newWay.setNodes(newNodes);
|
---|
219 | cmds.add(new ChangeCommand(w, newWay));
|
---|
220 | cmds.add(new DeleteCommand(delNodes));
|
---|
221 | w.getDataSet().clearSelection(delNodes);
|
---|
222 | return new SequenceCommand(
|
---|
223 | trn("Simplify Way (remove {0} node)", "Simplify Way (remove {0} nodes)", delNodes.size(), delNodes.size()), cmds);
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Builds the simplified list of nodes for a way segment given by a lower index <code>from</code>
|
---|
228 | * and an upper index <code>to</code>
|
---|
229 | *
|
---|
230 | * @param wnew the way to simplify
|
---|
231 | * @param from the lower index
|
---|
232 | * @param to the upper index
|
---|
233 | * @param threshold the max error threshold
|
---|
234 | * @param simplifiedNodes list that will contain resulting nodes
|
---|
235 | */
|
---|
236 | protected void buildSimplifiedNodeList(List<Node> wnew, int from, int to, double threshold, List<Node> simplifiedNodes) {
|
---|
237 |
|
---|
238 | Node fromN = wnew.get(from);
|
---|
239 | Node toN = wnew.get(to);
|
---|
240 |
|
---|
241 | // Get max xte
|
---|
242 | int imax = -1;
|
---|
243 | double xtemax = 0;
|
---|
244 | for (int i = from + 1; i < to; i++) {
|
---|
245 | Node n = wnew.get(i);
|
---|
246 | // CHECKSTYLE.OFF: SingleSpaceSeparator
|
---|
247 | double xte = Math.abs(Ellipsoid.WGS84.a
|
---|
248 | * xtd(fromN.lat() * Math.PI / 180, fromN.lon() * Math.PI / 180, toN.lat() * Math.PI / 180,
|
---|
249 | toN.lon() * Math.PI / 180, n.lat() * Math.PI / 180, n.lon() * Math.PI / 180));
|
---|
250 | // CHECKSTYLE.ON: SingleSpaceSeparator
|
---|
251 | if (xte > xtemax) {
|
---|
252 | xtemax = xte;
|
---|
253 | imax = i;
|
---|
254 | }
|
---|
255 | }
|
---|
256 |
|
---|
257 | if (imax != -1 && xtemax >= threshold) {
|
---|
258 | // Segment cannot be simplified - try shorter segments
|
---|
259 | buildSimplifiedNodeList(wnew, from, imax, threshold, simplifiedNodes);
|
---|
260 | buildSimplifiedNodeList(wnew, imax, to, threshold, simplifiedNodes);
|
---|
261 | } else {
|
---|
262 | // Simplify segment
|
---|
263 | if (simplifiedNodes.isEmpty() || simplifiedNodes.get(simplifiedNodes.size()-1) != fromN) {
|
---|
264 | simplifiedNodes.add(fromN);
|
---|
265 | }
|
---|
266 | if (fromN != toN) {
|
---|
267 | simplifiedNodes.add(toN);
|
---|
268 | }
|
---|
269 | }
|
---|
270 | }
|
---|
271 |
|
---|
272 | /* From Aviaton Formulary v1.3
|
---|
273 | * http://williams.best.vwh.net/avform.htm
|
---|
274 | */
|
---|
275 | private static double dist(double lat1, double lon1, double lat2, double lon2) {
|
---|
276 | return 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2), 2) + Math.cos(lat1) * Math.cos(lat2)
|
---|
277 | * Math.pow(Math.sin((lon1 - lon2) / 2), 2)));
|
---|
278 | }
|
---|
279 |
|
---|
280 | private static double course(double lat1, double lon1, double lat2, double lon2) {
|
---|
281 | return Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
|
---|
282 | * Math.cos(lat2) * Math.cos(lon1 - lon2))
|
---|
283 | % (2 * Math.PI);
|
---|
284 | }
|
---|
285 |
|
---|
286 | private static double xtd(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3) {
|
---|
287 | double distAD = dist(lat1, lon1, lat3, lon3);
|
---|
288 | double crsAD = course(lat1, lon1, lat3, lon3);
|
---|
289 | double crsAB = course(lat1, lon1, lat2, lon2);
|
---|
290 | return Math.asin(Math.sin(distAD) * Math.sin(crsAD - crsAB));
|
---|
291 | }
|
---|
292 |
|
---|
293 | @Override
|
---|
294 | protected void updateEnabledState() {
|
---|
295 | updateEnabledStateOnCurrentSelection();
|
---|
296 | }
|
---|
297 |
|
---|
298 | @Override
|
---|
299 | protected void updateEnabledState(Collection<? extends OsmPrimitive> selection) {
|
---|
300 | setEnabled(selection != null && !selection.isEmpty());
|
---|
301 | }
|
---|
302 | }
|
---|