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