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

Last change on this file since 13727 was 13545, checked in by pieren, 16 years ago

Add municipality boundary import from SVG data delivered by cadastre WMS.

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