1 | package UtilsPlugin;
|
---|
2 |
|
---|
3 | import static org.openstreetmap.josm.tools.I18n.tr;
|
---|
4 | import static org.openstreetmap.josm.tools.I18n.trn;
|
---|
5 |
|
---|
6 | import java.awt.event.ActionEvent;
|
---|
7 | import java.awt.event.KeyEvent;
|
---|
8 | import java.util.ArrayList;
|
---|
9 | import java.util.Collection;
|
---|
10 | import java.util.Collections;
|
---|
11 | import java.util.HashSet;
|
---|
12 | import java.util.LinkedList;
|
---|
13 | import java.util.List;
|
---|
14 |
|
---|
15 | import javax.swing.JOptionPane;
|
---|
16 |
|
---|
17 | import org.openstreetmap.josm.Main;
|
---|
18 | import org.openstreetmap.josm.actions.JosmAction;
|
---|
19 | import org.openstreetmap.josm.command.ChangeCommand;
|
---|
20 | import org.openstreetmap.josm.command.Command;
|
---|
21 | import org.openstreetmap.josm.command.DeleteCommand;
|
---|
22 | import org.openstreetmap.josm.command.SequenceCommand;
|
---|
23 | import org.openstreetmap.josm.data.Bounds;
|
---|
24 | import org.openstreetmap.josm.data.osm.DataSource;
|
---|
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.data.osm.visitor.CollectBackReferencesVisitor;
|
---|
29 | import org.openstreetmap.josm.gui.layer.OsmDataLayer;
|
---|
30 | import org.openstreetmap.josm.tools.Shortcut;
|
---|
31 |
|
---|
32 | public class SimplifyWayAction extends JosmAction {
|
---|
33 | public SimplifyWayAction() {
|
---|
34 | super(tr("Simplify Way"), "simplify", tr("Delete unnecessary nodes from a way."), Shortcut.registerShortcut("tools:simplify", tr("Tool: {0}", tr("Simplify Way")),
|
---|
35 | KeyEvent.VK_Y, Shortcut.GROUP_EDIT, Shortcut.SHIFT_DEFAULT), true);
|
---|
36 | }
|
---|
37 |
|
---|
38 | public void actionPerformed(ActionEvent e) {
|
---|
39 | Collection<OsmPrimitive> selection = Main.main.getCurrentDataSet().getSelected();
|
---|
40 |
|
---|
41 | int ways = 0;
|
---|
42 | LinkedList<Bounds> bounds = new LinkedList<Bounds>();
|
---|
43 | OsmDataLayer dataLayer = Main.map.mapView.getEditLayer();
|
---|
44 | for (DataSource ds : dataLayer.data.dataSources) {
|
---|
45 | if (ds.bounds != null)
|
---|
46 | bounds.add(ds.bounds);
|
---|
47 | }
|
---|
48 | for (OsmPrimitive prim : selection) {
|
---|
49 | if (prim instanceof Way) {
|
---|
50 | if (bounds.size() > 0) {
|
---|
51 | Way way = (Way) prim;
|
---|
52 | // We check if each node of each way is at least in one download
|
---|
53 | // bounding box. Otherwise nodes may get deleted that are necessary by
|
---|
54 | // unloaded ways (see Ticket #1594)
|
---|
55 | for (Node node : way.getNodes()) {
|
---|
56 | boolean isInsideOneBoundingBox = false;
|
---|
57 | for (Bounds b : bounds) {
|
---|
58 | if (b.contains(node.getCoor())) {
|
---|
59 | isInsideOneBoundingBox = true;
|
---|
60 | break;
|
---|
61 | }
|
---|
62 | }
|
---|
63 | if (!isInsideOneBoundingBox) {
|
---|
64 | int option = JOptionPane.showConfirmDialog(Main.parent,
|
---|
65 | tr("The selected way(s) have nodes outside of the downloaded data region.\n"
|
---|
66 | + "This can lead to nodes being deleted accidentally.\n"
|
---|
67 | + "Are you really sure to continue?"),
|
---|
68 | tr("Please abort if you are not sure"), JOptionPane.YES_NO_CANCEL_OPTION,
|
---|
69 | JOptionPane.WARNING_MESSAGE);
|
---|
70 |
|
---|
71 | if (option != JOptionPane.YES_OPTION)
|
---|
72 | return;
|
---|
73 | break;
|
---|
74 | }
|
---|
75 | }
|
---|
76 | }
|
---|
77 |
|
---|
78 | ways++;
|
---|
79 | }
|
---|
80 | }
|
---|
81 |
|
---|
82 | if (ways == 0) {
|
---|
83 | JOptionPane.showMessageDialog(Main.parent, tr("Please select at least one way to simplify."));
|
---|
84 | return;
|
---|
85 | } else if (ways > 10) {
|
---|
86 | //TRANSLATION: Although for English the use of trn is needless it is important for other languages
|
---|
87 | int option = JOptionPane.showConfirmDialog(Main.parent, trn(
|
---|
88 | "The selection contains {0} way. Are you sure you want to simplify it?",
|
---|
89 | "The selection contains {0} ways. Are you sure you want to simplify them all?",
|
---|
90 | ways,ways),
|
---|
91 | tr("Are you sure?"), JOptionPane.YES_NO_OPTION);
|
---|
92 | if (option != JOptionPane.YES_OPTION)
|
---|
93 | return;
|
---|
94 | }
|
---|
95 |
|
---|
96 | for (OsmPrimitive prim : selection) {
|
---|
97 | if (prim instanceof Way) {
|
---|
98 | simplifyWay((Way) prim);
|
---|
99 | }
|
---|
100 | }
|
---|
101 | }
|
---|
102 |
|
---|
103 | public void simplifyWay(Way w) {
|
---|
104 | double threshold = Double.parseDouble(Main.pref.get("simplify-way.max-error", "3"));
|
---|
105 |
|
---|
106 | Way wnew = new Way(w);
|
---|
107 |
|
---|
108 | int toI = wnew.getNodesCount() - 1;
|
---|
109 | for (int i = wnew.getNodesCount() - 1; i >= 0; i--) {
|
---|
110 | CollectBackReferencesVisitor backRefsV = new CollectBackReferencesVisitor(Main.main.getCurrentDataSet(), false);
|
---|
111 | backRefsV.visit(wnew.getNode(i));
|
---|
112 | boolean used = false;
|
---|
113 | if (backRefsV.getData().size() == 1) {
|
---|
114 | used = Collections.frequency(w.getNodes(), wnew.getNode(i)) > 1;
|
---|
115 | } else {
|
---|
116 | backRefsV.getData().remove(w);
|
---|
117 | used = !backRefsV.getData().isEmpty();
|
---|
118 | }
|
---|
119 | if (!used)
|
---|
120 | used = wnew.getNode(i).isTagged();
|
---|
121 |
|
---|
122 | if (used) {
|
---|
123 | simplifyWayRange(wnew, i, toI, threshold);
|
---|
124 | toI = i;
|
---|
125 | }
|
---|
126 | }
|
---|
127 | simplifyWayRange(wnew, 0, toI, threshold);
|
---|
128 |
|
---|
129 | HashSet<Node> delNodes = new HashSet<Node>();
|
---|
130 | delNodes.addAll(w.getNodes());
|
---|
131 | delNodes.removeAll(wnew.getNodes());
|
---|
132 |
|
---|
133 | if (wnew.getNodesCount() != w.getNodesCount()) {
|
---|
134 | Collection<Command> cmds = new LinkedList<Command>();
|
---|
135 | cmds.add(new ChangeCommand(w, wnew));
|
---|
136 | cmds.add(new DeleteCommand(delNodes));
|
---|
137 | Main.main.undoRedo.add(new SequenceCommand(trn("Simplify Way (remove {0} node)", "Simplify Way (remove {0} nodes)", delNodes.size(), delNodes.size()), cmds));
|
---|
138 | Main.map.repaint();
|
---|
139 | }
|
---|
140 | }
|
---|
141 |
|
---|
142 | public void simplifyWayRange(Way wnew, int from, int to, double thr) {
|
---|
143 | if (to - from >= 2) {
|
---|
144 | ArrayList<Node> ns = new ArrayList<Node>();
|
---|
145 | simplifyWayRange(wnew, from, to, ns, thr);
|
---|
146 | List<Node> nodes = wnew.getNodes();
|
---|
147 | for (int j = to - 1; j > from; j--) {
|
---|
148 | nodes.remove(j);
|
---|
149 | }
|
---|
150 | nodes.addAll(from + 1, ns);
|
---|
151 | wnew.setNodes(nodes);
|
---|
152 | }
|
---|
153 | }
|
---|
154 |
|
---|
155 | /*
|
---|
156 | * Takes an interval [from,to] and adds nodes from (from,to) to ns.
|
---|
157 | * (from and to are indices of wnew.nodes.)
|
---|
158 | */
|
---|
159 | public void simplifyWayRange(Way wnew, int from, int to, ArrayList<Node> ns, double thr) {
|
---|
160 | Node fromN = wnew.getNode(from), toN = wnew.getNode(to);
|
---|
161 |
|
---|
162 | int imax = -1;
|
---|
163 | double xtemax = 0;
|
---|
164 | for (int i = from + 1; i < to; i++) {
|
---|
165 | Node n = wnew.getNode(i);
|
---|
166 | double xte = Math.abs(EARTH_RAD
|
---|
167 | * xtd(fromN.getCoor().lat() * Math.PI / 180, fromN.getCoor().lon() * Math.PI / 180, toN.getCoor().lat() * Math.PI
|
---|
168 | / 180, toN.getCoor().lon() * Math.PI / 180, n.getCoor().lat() * Math.PI / 180, n.getCoor().lon() * Math.PI
|
---|
169 | / 180));
|
---|
170 | if (xte > xtemax) {
|
---|
171 | xtemax = xte;
|
---|
172 | imax = i;
|
---|
173 | }
|
---|
174 | }
|
---|
175 |
|
---|
176 | if (imax != -1 && xtemax >= thr) {
|
---|
177 | simplifyWayRange(wnew, from, imax, ns, thr);
|
---|
178 | ns.add(wnew.getNode(imax));
|
---|
179 | simplifyWayRange(wnew, imax, to, ns, thr);
|
---|
180 | }
|
---|
181 | }
|
---|
182 |
|
---|
183 | public static double EARTH_RAD = 6378137.0;
|
---|
184 |
|
---|
185 | /* From Aviaton Formulary v1.3
|
---|
186 | * http://williams.best.vwh.net/avform.htm
|
---|
187 | */
|
---|
188 | public static double dist(double lat1, double lon1, double lat2, double lon2) {
|
---|
189 | return 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2), 2) + Math.cos(lat1) * Math.cos(lat2)
|
---|
190 | * Math.pow(Math.sin((lon1 - lon2) / 2), 2)));
|
---|
191 | }
|
---|
192 |
|
---|
193 | public static double course(double lat1, double lon1, double lat2, double lon2) {
|
---|
194 | return Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
|
---|
195 | * Math.cos(lat2) * Math.cos(lon1 - lon2))
|
---|
196 | % (2 * Math.PI);
|
---|
197 | }
|
---|
198 |
|
---|
199 | public static double xtd(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3) {
|
---|
200 | double dist_AD = dist(lat1, lon1, lat3, lon3);
|
---|
201 | double crs_AD = course(lat1, lon1, lat3, lon3);
|
---|
202 | double crs_AB = course(lat1, lon1, lat2, lon2);
|
---|
203 | return Math.asin(Math.sin(dist_AD) * Math.sin(crs_AD - crs_AB));
|
---|
204 | }
|
---|
205 | }
|
---|