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