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