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

Last change on this file since 20214 was 19078, checked in by pieren, 15 years ago

modified URL's for request and images size due to WMS changes and new capabilities

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