| 1 | // License: GPL. v2 and later. Copyright 2008-2009 by Pieren <pieren3@gmail.com> and others | 
|---|
| 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 |  | 
|---|
| 10 | /** | 
|---|
| 11 | * Imported from plugin UtilsPlugin | 
|---|
| 12 | * @author | 
|---|
| 13 | * | 
|---|
| 14 | */ | 
|---|
| 15 | public 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<Node>(); | 
|---|
| 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 | nodes.addAll(from+1, ns); | 
|---|
| 31 | wnew.setNodes(nodes); | 
|---|
| 32 | } | 
|---|
| 33 | } | 
|---|
| 34 |  | 
|---|
| 35 | /* | 
|---|
| 36 | * Takes an interval [from,to] and adds nodes from (from,to) to ns. | 
|---|
| 37 | * (from and to are indices of wnew.nodes.) | 
|---|
| 38 | */ | 
|---|
| 39 | public void simplifyWayRange(Way wnew, int from, int to, ArrayList<Node> ns, double thr) { | 
|---|
| 40 | Node fromN = wnew.getNode(from), toN = wnew.getNode(to); | 
|---|
| 41 |  | 
|---|
| 42 | int imax = -1; | 
|---|
| 43 | double xtemax = 0; | 
|---|
| 44 | for (int i = from + 1; i < to; i++) { | 
|---|
| 45 | Node n = wnew.getNode(i); | 
|---|
| 46 | double xte = Math.abs(EARTH_RAD | 
|---|
| 47 | * xtd(fromN.getCoor().lat() * Math.PI / 180, fromN.getCoor().lon() * Math.PI / 180, toN.getCoor().lat() * Math.PI | 
|---|
| 48 | / 180, toN.getCoor().lon() * Math.PI / 180, n.getCoor().lat() * Math.PI / 180, n.getCoor().lon() * Math.PI | 
|---|
| 49 | / 180)); | 
|---|
| 50 | if (xte > xtemax) { | 
|---|
| 51 | xtemax = xte; | 
|---|
| 52 | imax = i; | 
|---|
| 53 | } | 
|---|
| 54 | } | 
|---|
| 55 |  | 
|---|
| 56 | if (imax != -1 && xtemax >= thr) { | 
|---|
| 57 | simplifyWayRange(wnew, from, imax, ns, thr); | 
|---|
| 58 | ns.add(wnew.getNode(imax)); | 
|---|
| 59 | simplifyWayRange(wnew, imax, to, ns, thr); | 
|---|
| 60 | } | 
|---|
| 61 | } | 
|---|
| 62 | public static double EARTH_RAD = 6378137.0; | 
|---|
| 63 | /* From Aviaton Formulary v1.3 | 
|---|
| 64 | * http://williams.best.vwh.net/avform.htm | 
|---|
| 65 | */ | 
|---|
| 66 | public static double dist(double lat1, double lon1, double lat2, double lon2) { | 
|---|
| 67 | return 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2), 2) + Math.cos(lat1) * Math.cos(lat2) | 
|---|
| 68 | * Math.pow(Math.sin((lon1 - lon2) / 2), 2))); | 
|---|
| 69 | } | 
|---|
| 70 |  | 
|---|
| 71 | public static double course(double lat1, double lon1, double lat2, double lon2) { | 
|---|
| 72 | return Math.atan2(Math.sin(lon1 - lon2) * Math.cos(lat2), Math.cos(lat1) * Math.sin(lat2) - Math.sin(lat1) | 
|---|
| 73 | * Math.cos(lat2) * Math.cos(lon1 - lon2)) | 
|---|
| 74 | % (2 * Math.PI); | 
|---|
| 75 | } | 
|---|
| 76 | public static double xtd(double lat1, double lon1, double lat2, double lon2, double lat3, double lon3) { | 
|---|
| 77 | double dist_AD = dist(lat1, lon1, lat3, lon3); | 
|---|
| 78 | double crs_AD = course(lat1, lon1, lat3, lon3); | 
|---|
| 79 | double crs_AB = course(lat1, lon1, lat2, lon2); | 
|---|
| 80 | return Math.asin(Math.sin(dist_AD) * Math.sin(crs_AD - crs_AB)); | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | } | 
|---|