[32556] | 1 | // License: GPL. For details, see LICENSE file.
|
---|
[13545] | 2 | package cadastre_fr;
|
---|
| 3 |
|
---|
| 4 | import java.util.ArrayList;
|
---|
[17365] | 5 | import java.util.List;
|
---|
[13545] | 6 |
|
---|
| 7 | import org.openstreetmap.josm.data.osm.Node;
|
---|
| 8 | import org.openstreetmap.josm.data.osm.Way;
|
---|
| 9 |
|
---|
| 10 | /**
|
---|
| 11 | * Imported from plugin UtilsPlugin
|
---|
[32556] | 12 | * @author
|
---|
[13545] | 13 | *
|
---|
| 14 | */
|
---|
| 15 | public class SimplifyWay {
|
---|
[20390] | 16 | public void simplifyWay(Way w/*, DataSet dataSet*/, double threshold) {
|
---|
[13545] | 17 | Way wnew = new Way(w);
|
---|
| 18 |
|
---|
[20390] | 19 | simplifyWayRange(wnew, 0, wnew.getNodesCount() - 1, threshold);
|
---|
| 20 | w.setNodes(wnew.getNodes());
|
---|
[13545] | 21 | }
|
---|
| 22 |
|
---|
| 23 | public void simplifyWayRange(Way wnew, int from, int to, double thr) {
|
---|
| 24 | if (to - from >= 2) {
|
---|
[30737] | 25 | ArrayList<Node> ns = new ArrayList<>();
|
---|
[13545] | 26 | simplifyWayRange(wnew, from, to, ns, thr);
|
---|
[17365] | 27 | List<Node> nodes = wnew.getNodes();
|
---|
[32556] | 28 | for (int j = to - 1; j > from; j--) {
|
---|
[17365] | 29 | nodes.remove(j);
|
---|
[32556] | 30 | }
|
---|
[17365] | 31 | nodes.addAll(from+1, ns);
|
---|
| 32 | wnew.setNodes(nodes);
|
---|
[13545] | 33 | }
|
---|
| 34 | }
|
---|
[32556] | 35 |
|
---|
[13545] | 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) {
|
---|
[17365] | 41 | Node fromN = wnew.getNode(from), toN = wnew.getNode(to);
|
---|
[13545] | 42 |
|
---|
| 43 | int imax = -1;
|
---|
| 44 | double xtemax = 0;
|
---|
| 45 | for (int i = from + 1; i < to; i++) {
|
---|
[17365] | 46 | Node n = wnew.getNode(i);
|
---|
[13545] | 47 | double xte = Math.abs(EARTH_RAD
|
---|
[16160] | 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
|
---|
[13545] | 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);
|
---|
[17365] | 59 | ns.add(wnew.getNode(imax));
|
---|
[13545] | 60 | simplifyWayRange(wnew, imax, to, ns, thr);
|
---|
| 61 | }
|
---|
| 62 | }
|
---|
[32556] | 63 |
|
---|
[13545] | 64 | public static double EARTH_RAD = 6378137.0;
|
---|
[32556] | 65 |
|
---|
[13545] | 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 | }
|
---|
[32556] | 79 |
|
---|
[13545] | 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 | }
|
---|