source: osm/applications/editors/josm/plugins/utilsplugin/src/UtilsPlugin/SimplifyWayAction.java@ 5114

Last change on this file since 5114 was 5084, checked in by gabriel, 18 years ago

utilsplugin: Simplify ways without junctions.

File size: 8.4 KB
Line 
1package UtilsPlugin;
2
3import static org.openstreetmap.josm.tools.I18n.tr;
4
5import java.awt.event.ActionEvent;
6import java.util.ArrayList;
7import java.util.Collection;
8import java.util.Collections;
9import java.util.HashSet;
10import java.util.LinkedList;
11import java.util.List;
12
13import org.openstreetmap.josm.Main;
14import org.openstreetmap.josm.command.ChangeCommand;
15import org.openstreetmap.josm.command.Command;
16import org.openstreetmap.josm.command.DeleteCommand;
17import org.openstreetmap.josm.command.SequenceCommand;
18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.data.osm.Node;
20import org.openstreetmap.josm.data.osm.OsmPrimitive;
21import org.openstreetmap.josm.data.osm.Way;
22import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
23
24import org.openstreetmap.josm.data.osm.DataSet;
25import org.openstreetmap.josm.actions.JosmAction;
26
27public class SimplifyWayAction extends JosmAction {
28 public SimplifyWayAction() {
29 super(tr("Simplify Way"), "simplify",
30 tr("Delete unnecessary nodes from a way."), 0, 0, true);
31 }
32
33 public void actionPerformed(ActionEvent e) {
34 Collection<OsmPrimitive> selection = Main.ds.getSelected();
35
36 if (selection.size() == 1 && selection.iterator().next() instanceof Way) {
37 simplifyWay((Way) selection.iterator().next());
38 }
39 }
40
41 public void simplifyWay(Way w) {
42 double threshold = Double.parseDouble(
43 Main.pref.get("simplify-way.max-error", "50"));
44
45 Way wnew = new Way(w);
46
47 int toI = wnew.nodes.size() - 1;
48 for (int i = wnew.nodes.size() - 1; i >= 0; i--) {
49 CollectBackReferencesVisitor backRefsV =
50 new CollectBackReferencesVisitor(Main.ds, false);
51 backRefsV.visit(wnew.nodes.get(i));
52 boolean used = false;
53 if (backRefsV.data.size() == 1) {
54 used = Collections.frequency(
55 w.nodes, wnew.nodes.get(i)) > 1;
56 } else {
57 backRefsV.data.remove(w);
58 used = !backRefsV.data.isEmpty();
59 }
60
61 if (used) {
62 simplifyWayRange(wnew, i, toI, threshold);
63 toI = i;
64 }
65 }
66 simplifyWayRange(wnew, 0, toI, threshold);
67
68 HashSet<Node> delNodes = new HashSet<Node>();
69 delNodes.addAll(w.nodes);
70 delNodes.removeAll(wnew.nodes);
71
72 if (wnew.nodes.size() != w.nodes.size()) {
73 Collection<Command> cmds = new LinkedList<Command>();
74 cmds.add(new ChangeCommand(w, wnew));
75 cmds.add(new DeleteCommand(delNodes));
76 Main.main.undoRedo.add(
77 new SequenceCommand(tr("Simplify Way (remove {0} nodes)",
78 delNodes.size()),
79 cmds));
80 Main.map.repaint();
81 }
82 }
83
84 public void simplifyWayRange(Way wnew, int from, int to, double thr) {
85 if (to - from >= 2) {
86 ArrayList<Node> ns = new ArrayList<Node>();
87 simplifyWayRange(wnew, from, to, ns, thr);
88 for (int j = to-1; j > from; j--) wnew.nodes.remove(j);
89 wnew.nodes.addAll(from+1, ns);
90 }
91 }
92
93 /*
94 * Takes an interval [from,to] and adds nodes from the set (from,to) to
95 * ns.
96 */
97 public void simplifyWayRange(Way wnew, int from, int to, ArrayList<Node> ns, double thr) {
98 Node fromN = wnew.nodes.get(from), toN = wnew.nodes.get(to);
99
100 int imax = -1;
101 double xtemax = 0;
102 for (int i = from+1; i < to; i++) {
103 Node n = wnew.nodes.get(i);
104 double xte = radtometers(linedist(
105 fromN.coor.lat(), fromN.coor.lon(),
106 n.coor.lat(), n.coor.lon(),
107 toN.coor.lat(), toN.coor.lon()));
108 if (xte > xtemax) {
109 xtemax = xte;
110 imax = i;
111 }
112 }
113
114 if (imax != -1 && xtemax >= thr) {
115 simplifyWayRange(wnew, from, imax, ns, thr);
116 ns.add(wnew.nodes.get(imax));
117 simplifyWayRange(wnew, imax, to, ns, thr);
118 }
119 }
120
121 /* ----------------------------------------------------------------------
122 * Everything below this comment was converted from C to Java by Frederik
123 * Ramm. The original sources are the files grtcirc.c and smplrout.c from
124 * the gpsbabel source code (www.gpsbabel.org), which is under GPL. The
125 * relevant code portions have been written by Robert Lipe.
126 *
127 * Method names have been left unchanged where possible.
128 */
129
130 public static double EARTH_RAD = 6378137.0;
131 public static double radmiles = EARTH_RAD*100.0/2.54/12.0/5280.0;
132
133 public static double[] crossproduct(double[] v1, double[] v2) {
134 double[] rv = new double[3];
135 rv[0] = v1[1]*v2[2]-v2[1]*v1[2];
136 rv[1] = v1[2]*v2[0]-v2[2]*v1[0];
137 rv[2] = v1[0]*v2[1]-v1[1]*v2[0];
138 return rv;
139 }
140
141 public static double dotproduct(double[] v1, double[] v2) {
142 return v1[0]*v2[0]+v1[1]*v2[1]+v1[2]*v2[2];
143 }
144
145 public static double radtomiles(double rads) {
146 return (rads*radmiles);
147 }
148
149 public static double radtometers(double rads) {
150 return (rads * EARTH_RAD);
151 }
152
153 public static double veclen(double[] vec) {
154 return Math.sqrt(vec[0]*vec[0]+vec[1]*vec[1]+vec[2]*vec[2]);
155 }
156
157 public static double gcdist(double lat1, double lon1, double lat2, double lon2)
158 {
159 double res;
160 double sdlat, sdlon;
161
162 sdlat = Math.sin((lat1 - lat2) / 2.0);
163 sdlon = Math.sin((lon1 - lon2) / 2.0);
164
165 res = Math.sqrt(sdlat * sdlat + Math.cos(lat1) * Math.cos(lat2) * sdlon * sdlon);
166
167 if (res > 1.0) {
168 res = 1.0;
169 } else if (res < -1.0) {
170 res = -1.0;
171 }
172
173 res = Math.asin(res);
174 return 2.0 * res;
175 }
176
177 static double linedist(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3) {
178
179 double dot;
180
181 /* degrees to radians */
182 lat1 = Math.toRadians(lat1); lon1 = Math.toRadians(lon1);
183 lat2 = Math.toRadians(lat2); lon2 = Math.toRadians(lon2);
184 lat3 = Math.toRadians(lat3); lon3 = Math.toRadians(lon3);
185
186 /* polar to ECEF rectangular */
187 double[] v1 = new double[3];
188 double[] v2 = new double[3];
189 double[] v3 = new double[3];
190 v1[0] = Math.cos(lon1)*Math.cos(lat1); v1[1] = Math.sin(lat1); v1[2] = Math.sin(lon1)*Math.cos(lat1);
191 v2[0] = Math.cos(lon2)*Math.cos(lat2); v2[1] = Math.sin(lat2); v2[2] = Math.sin(lon2)*Math.cos(lat2);
192 v3[0] = Math.cos(lon3)*Math.cos(lat3); v3[1] = Math.sin(lat3); v3[2] = Math.sin(lon3)*Math.cos(lat3);
193
194 /* 'va' is the axis; the line that passes through the center of the earth
195 * and is perpendicular to the great circle through point 1 and point 2
196 * It is computed by taking the cross product of the '1' and '2' vectors.*/
197 double[] va = crossproduct(v1, v2);
198 double la = veclen(va);
199
200 if (la != 0) {
201 va[0] /= la;
202 va[1] /= la;
203 va[2] /= la;
204
205 /* dot is the component of the length of '3' that is along the axis.
206 * What's left is a non-normalized vector that lies in the plane of
207 * 1 and 2. */
208
209 dot = dotproduct(v3, va);
210
211 double[] vp = new double[3];
212 vp[0]=v3[0]-dot*va[0];
213 vp[1]=v3[1]-dot*va[1];
214 vp[2]=v3[2]-dot*va[2];
215
216 double lp = veclen(vp);
217
218 if (lp != 0) {
219
220 /* After this, 'p' is normalized */
221 vp[0] /= lp;
222 vp[1] /= lp;
223 vp[2] /= lp;
224
225 double[] cp1 = crossproduct(v1, vp);
226 double dp1 = dotproduct(cp1, va);
227
228 double[] cp2 = crossproduct(v2, vp);
229 double dp2 = dotproduct(cp2, va);
230
231 if ( dp1 >= 0 && dp2 >= 0 ) {
232 /* rather than call gcdist and all its sines and cosines and
233 * worse, we can get the angle directly. It's the arctangent
234 * of the length of the component of vector 3 along the axis
235 * divided by the length of the component of vector 3 in the
236 * plane. We already have both of those numbers.
237 *
238 * atan2 would be overkill because lp and Math.abs are both
239 * known to be positive. */
240 return Math.atan(Math.abs(dot)/lp);
241 }
242
243 /* otherwise, get the distance from the closest endpoint */
244 double c1 = dotproduct(v1, vp);
245 double c2 = dotproduct(v2, vp);
246 dp1 = Math.abs(dp1);
247 dp2 = Math.abs(dp2);
248
249 /* This is a hack. d$n$ is proportional to the sine of the angle
250 * between point $n$ and point p. That preserves orderedness up
251 * to an angle of 90 degrees. c$n$ is proportional to the cosine
252 * of the same angle; if the angle is over 90 degrees, c$n$ is
253 * negative. In that case, we flop the sine across the y=1 axis
254 * so that the resulting value increases as the angle increases.
255 *
256 * This only works because all of the points are on a unit sphere. */
257
258 if (c1 < 0) {
259 dp1 = 2 - dp1;
260 }
261 if (c2 < 0) {
262 dp2 = 2 - dp2;
263 }
264
265 if (Math.abs(dp1) < Math.abs(dp2)) {
266 return gcdist(lat1,lon1,lat3,lon3);
267 } else {
268 return gcdist(lat2,lon2,lat3,lon3);
269 }
270 } else {
271 /* lp is 0 when 3 is 90 degrees from the great circle */
272 return Math.PI/2;
273 }
274 } else {
275 /* la is 0 when 1 and 2 are either the same point or 180 degrees apart */
276 dot = dotproduct(v1, v2);
277 if (dot >= 0) {
278 return gcdist(lat1,lon1,lat3,lon3);
279 } else {
280 return 0;
281 }
282 }
283 }
284}
Note: See TracBrowser for help on using the repository browser.