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

Last change on this file since 32556 was 32556, checked in by donvip, 8 years ago

checkstyle

File size: 3.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package cadastre_fr;
3
4import java.util.ArrayList;
5import java.util.List;
6
7import org.openstreetmap.josm.data.osm.Node;
8import org.openstreetmap.josm.data.osm.Way;
9
10/**
11 * Imported from plugin UtilsPlugin
12 * @author
13 *
14 */
15public class SimplifyWay {
16 public void simplifyWay(Way w/*, DataSet dataSet*/, double threshold) {
17 Way wnew = new Way(w);
18
19 simplifyWayRange(wnew, 0, wnew.getNodesCount() - 1, threshold);
20 w.setNodes(wnew.getNodes());
21 }
22
23 public void simplifyWayRange(Way wnew, int from, int to, double thr) {
24 if (to - from >= 2) {
25 ArrayList<Node> ns = new ArrayList<>();
26 simplifyWayRange(wnew, from, to, ns, thr);
27 List<Node> nodes = wnew.getNodes();
28 for (int j = to - 1; j > from; j--) {
29 nodes.remove(j);
30 }
31 nodes.addAll(from+1, ns);
32 wnew.setNodes(nodes);
33 }
34 }
35
36 /*
37 * Takes an interval [from,to] and adds nodes from (from,to) to ns.
38 * (from and to are indices of wnew.nodes.)
39 */
40 public void simplifyWayRange(Way wnew, int from, int to, ArrayList<Node> ns, double thr) {
41 Node fromN = wnew.getNode(from), toN = wnew.getNode(to);
42
43 int imax = -1;
44 double xtemax = 0;
45 for (int i = from + 1; i < to; i++) {
46 Node n = wnew.getNode(i);
47 double xte = Math.abs(EARTH_RAD
48 * xtd(fromN.getCoor().lat() * Math.PI / 180, fromN.getCoor().lon() * Math.PI / 180, toN.getCoor().lat() * Math.PI
49 / 180, toN.getCoor().lon() * Math.PI / 180, n.getCoor().lat() * Math.PI / 180, n.getCoor().lon() * Math.PI
50 / 180));
51 if (xte > xtemax) {
52 xtemax = xte;
53 imax = i;
54 }
55 }
56
57 if (imax != -1 && xtemax >= thr) {
58 simplifyWayRange(wnew, from, imax, ns, thr);
59 ns.add(wnew.getNode(imax));
60 simplifyWayRange(wnew, imax, to, ns, thr);
61 }
62 }
63
64 public static double EARTH_RAD = 6378137.0;
65
66 /* From Aviaton Formulary v1.3
67 * http://williams.best.vwh.net/avform.htm
68 */
69 public static double dist(double lat1, double lon1, double lat2, double lon2) {
70 return 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2), 2) + Math.cos(lat1) * Math.cos(lat2)
71 * Math.pow(Math.sin((lon1 - lon2) / 2), 2)));
72 }
73
74 public static double course(double lat1, double lon1, double lat2, double lon2) {
75 return Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1)
76 * Math.cos(lat2) * Math.cos(lon1 - lon2))
77 % (2 * Math.PI);
78 }
79
80 public static double xtd(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3) {
81 double dist_AD = dist(lat1, lon1, lat3, lon3);
82 double crs_AD = course(lat1, lon1, lat3, lon3);
83 double crs_AB = course(lat1, lon1, lat2, lon2);
84 return Math.asin(Math.sin(dist_AD) * Math.sin(crs_AD - crs_AB));
85 }
86}
Note: See TracBrowser for help on using the repository browser.