source: osm/applications/editors/josm/plugins/cadastre-fr/src/cadastre_fr/SimplifyWay.java@ 17707

Last change on this file since 17707 was 17707, checked in by stoecker, 15 years ago

some fixes for josm 2166

File size: 4.9 KB
Line 
1package cadastre_fr;
2
3import java.util.ArrayList;
4
5import java.util.Collection;
6import java.util.Collections;
7import java.util.HashSet;
8import java.util.LinkedList;
9import java.util.List;
10
11import org.openstreetmap.josm.Main;
12import org.openstreetmap.josm.command.ChangeCommand;
13import org.openstreetmap.josm.command.Command;
14import org.openstreetmap.josm.command.DeleteCommand;
15import org.openstreetmap.josm.command.SequenceCommand;
16import org.openstreetmap.josm.data.osm.DataSet;
17import org.openstreetmap.josm.data.osm.Node;
18import org.openstreetmap.josm.data.osm.Way;
19import org.openstreetmap.josm.data.osm.visitor.CollectBackReferencesVisitor;
20import static org.openstreetmap.josm.tools.I18n.trn;
21
22
23
24/**
25 * Imported from plugin UtilsPlugin
26 * @author
27 *
28 */
29public class SimplifyWay {
30 public void simplifyWay(Way w, DataSet dataSet, double threshold) {
31 Way wnew = new Way(w);
32
33 int toI = wnew.getNodesCount() - 1;
34 for (int i = wnew.getNodesCount() - 1; i >= 0; i--) {
35 CollectBackReferencesVisitor backRefsV = new CollectBackReferencesVisitor(dataSet, false);
36 backRefsV.visit(wnew.getNode(i));
37 boolean used = false;
38 if (backRefsV.getData().size() == 1) {
39 used = Collections.frequency(w.getNodes(), wnew.getNode(i)) > 1;
40 } else {
41 backRefsV.getData().remove(w);
42 used = !backRefsV.getData().isEmpty();
43 }
44 if (!used)
45 used = wnew.getNode(i).isTagged();
46
47 if (used) {
48 simplifyWayRange(wnew, i, toI, threshold);
49 toI = i;
50 }
51 }
52 simplifyWayRange(wnew, 0, toI, threshold);
53
54 HashSet<Node> delNodes = new HashSet<Node>();
55 delNodes.addAll(w.getNodes());
56 delNodes.removeAll(wnew.getNodes());
57
58 if (wnew.getNodesCount() != w.getNodesCount()) {
59 Collection<Command> cmds = new LinkedList<Command>();
60 cmds.add(new ChangeCommand(w, wnew));
61 cmds.add(new DeleteCommand(delNodes));
62 Main.main.undoRedo.add(new SequenceCommand(trn("Simplify Way (remove {0} node)", "Simplify Way (remove {0} nodes)", delNodes.size(), delNodes.size()), cmds));
63 Main.map.repaint();
64 }
65 }
66
67 public void simplifyWayRange(Way wnew, int from, int to, double thr) {
68 if (to - from >= 2) {
69 ArrayList<Node> ns = new ArrayList<Node>();
70 simplifyWayRange(wnew, from, to, ns, thr);
71 List<Node> nodes = wnew.getNodes();
72 for (int j = to - 1; j > from; j--)
73 nodes.remove(j);
74 nodes.addAll(from+1, ns);
75 wnew.setNodes(nodes);
76 }
77 }
78
79 /*
80 * Takes an interval [from,to] and adds nodes from (from,to) to ns.
81 * (from and to are indices of wnew.nodes.)
82 */
83 public void simplifyWayRange(Way wnew, int from, int to, ArrayList<Node> ns, double thr) {
84 Node fromN = wnew.getNode(from), toN = wnew.getNode(to);
85
86 int imax = -1;
87 double xtemax = 0;
88 for (int i = from + 1; i < to; i++) {
89 Node n = wnew.getNode(i);
90 double xte = Math.abs(EARTH_RAD
91 * xtd(fromN.getCoor().lat() * Math.PI / 180, fromN.getCoor().lon() * Math.PI / 180, toN.getCoor().lat() * Math.PI
92 / 180, toN.getCoor().lon() * Math.PI / 180, n.getCoor().lat() * Math.PI / 180, n.getCoor().lon() * Math.PI
93 / 180));
94 if (xte > xtemax) {
95 xtemax = xte;
96 imax = i;
97 }
98 }
99
100 if (imax != -1 && xtemax >= thr) {
101 simplifyWayRange(wnew, from, imax, ns, thr);
102 ns.add(wnew.getNode(imax));
103 simplifyWayRange(wnew, imax, to, ns, thr);
104 }
105 }
106 public static double EARTH_RAD = 6378137.0;
107 /* From Aviaton Formulary v1.3
108 * http://williams.best.vwh.net/avform.htm
109 */
110 public static double dist(double lat1, double lon1, double lat2, double lon2) {
111 return 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2), 2) + Math.cos(lat1) * Math.cos(lat2)
112 * Math.pow(Math.sin((lon1 - lon2) / 2), 2)));
113 }
114
115 public static double course(double lat1, double lon1, double lat2, double lon2) {
116 return Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
117 * Math.cos(lat2) * Math.cos(lon1 - lon2))
118 % (2 * Math.PI);
119 }
120 public static double xtd(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3) {
121 double dist_AD = dist(lat1, lon1, lat3, lon3);
122 double crs_AD = course(lat1, lon1, lat3, lon3);
123 double crs_AB = course(lat1, lon1, lat2, lon2);
124 return Math.asin(Math.sin(dist_AD) * Math.sin(crs_AD - crs_AB));
125 }
126
127}
Note: See TracBrowser for help on using the repository browser.