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