8558f8e14b2ae410c62d5654d12face0c27f6746
[aaf/cadi.git] / client / src / main / java / com / att / cadi / routing / GreatCircle.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi.routing;\r
24 \r
25 import com.att.inno.env.util.Split;\r
26 \r
27 public class GreatCircle {\r
28         // Note: multiplying by this constant is faster than calling Math equivalent function \r
29         private static final double DEGREES_2_RADIANS = Math.PI/180.0;\r
30         \r
31         public static final double DEGREES_2_NM = 60;\r
32         public static final double DEGREES_2_KM = DEGREES_2_NM * 1.852; // 1.852 is exact ratio per 1929 Standard Treaty, adopted US 1954\r
33         public static final double DEGREES_2_MI = DEGREES_2_NM * 1.1507795; \r
34         \r
35         /**\r
36          * \r
37          * Calculate the length of an arc on a perfect sphere based on Latitude and Longitudes of two points\r
38          *    Parameters are in Degrees (i.e. the coordinate system you get from GPS, Mapping WebSites, Phones, etc)\r
39          *    \r
40          *              L1 = Latitude of point A\r
41          *      G1 = Longitude of point A\r
42          *          L2 = Latitude of point B\r
43          *      G2 = Longitude of point B\r
44          *      \r
45          *      d  = acos (sin(L1)*sin(L2) + cos(L1)*cos(L2)*cos(G1 - G2))\r
46          * \r
47          * Returns answer in Degrees\r
48          * \r
49          * Since there are 60 degrees per nautical miles, you can convert to NM by multiplying by 60\r
50          * \r
51          * Essential formula from a Princeton website, the "Law of Cosines" method.  \r
52          * \r
53          * Refactored cleaned up for speed  3/8/2013\r
54          * \r
55          * @param latA\r
56          * @param lonA\r
57          * @param latB\r
58          * @param lonB\r
59          * @return\r
60          */\r
61         public static double calc(double latA, double lonA, double latB, double lonB) {\r
62                 // Formula requires Radians.  Expect Params to be Coordinates (Degrees)\r
63                 // Simple ratio, quicker than calling Math.toRadians()\r
64                 latA *= DEGREES_2_RADIANS;\r
65                 lonA *= DEGREES_2_RADIANS;\r
66                 latB *= DEGREES_2_RADIANS;\r
67                 lonB *= DEGREES_2_RADIANS;\r
68 \r
69                 return Math.acos(\r
70                                 Math.sin(latA) * Math.sin(latB) + \r
71                                 Math.cos(latA) * Math.cos(latB) * Math.cos(lonA-lonB)\r
72                         )\r
73                         / DEGREES_2_RADIANS;\r
74         }\r
75         \r
76         /** \r
77          * Convert from "Lat,Long Lat,Long" String format\r
78          *              "Lat,Long,Lat,Long" Format\r
79          *           or all four entries "Lat Long Lat Long"\r
80          * \r
81          * (Convenience function)\r
82          * \r
83          * Since Distance is positive, a "-1" indicates an error in String formatting\r
84          */\r
85         public static double calc(String ... coords) {\r
86                 try {\r
87                         String [] array;\r
88                         switch(coords.length) {\r
89                         case 1:\r
90                                 array = Split.split(',',coords[0]);\r
91                                 if(array.length!=4)return -1;\r
92                                 return calc(\r
93                                         Double.parseDouble(array[0]),\r
94                                         Double.parseDouble(array[1]),\r
95                                         Double.parseDouble(array[2]),\r
96                                         Double.parseDouble(array[3])\r
97                                         );\r
98                         case 2:\r
99                                 array = Split.split(',',coords[0]);\r
100                                 String [] array2 = Split.split(',',coords[1]);\r
101                                 if(array.length!=2 || array2.length!=2)return -1;\r
102                                 return calc(\r
103                                         Double.parseDouble(array[0]),\r
104                                         Double.parseDouble(array[1]),\r
105                                         Double.parseDouble(array2[0]),\r
106                                         Double.parseDouble(array2[1])\r
107                                         );\r
108                         case 4:\r
109                                 return calc(\r
110                                         Double.parseDouble(coords[0]),\r
111                                         Double.parseDouble(coords[1]),\r
112                                         Double.parseDouble(coords[2]),\r
113                                         Double.parseDouble(coords[3])\r
114                                         );\r
115                                 \r
116                         default:\r
117                                 return -1;\r
118                         }\r
119                 } catch (NumberFormatException e) {\r
120                         return -1;\r
121                 }\r
122         }\r
123 \r
124 }\r
125 \r
126 ///**\r
127 //* Haverside method, from Princeton\r
128 //* \r
129 //* @param alat\r
130 //* @param alon\r
131 //* @param blat\r
132 //* @param blon\r
133 //* @return\r
134 //*/\r
135 //public static double calc3(double alat, double alon, double blat, double blon) {\r
136 //      alat *= DEGREES_2_RADIANS;\r
137 //      alon *= DEGREES_2_RADIANS;\r
138 //      blat *= DEGREES_2_RADIANS;\r
139 //      blon *= DEGREES_2_RADIANS;\r
140 //      return 2 * Math.asin(\r
141 //                      Math.min(1, Math.sqrt(\r
142 //                              Math.pow(Math.sin((blat-alat)/2), 2) +\r
143 //                              (Math.cos(alat)*Math.cos(blat)*\r
144 //                                      Math.pow(\r
145 //                                              Math.sin((blon-alon)/2),2)\r
146 //                                      )\r
147 //                              )\r
148 //                      )\r
149 //              )\r
150 //      / DEGREES_2_RADIANS;\r
151 //}\r
152 //\r
153 \r
154 \r
155 \r
156 //This is a MEAN radius.  The Earth is not perfectly spherical\r
157 //      public static final double EARTH_RADIUS_KM = 6371.0;\r
158 //      public static final double EARTH_RADIUS_NM = 3440.07;\r
159 //      public static final double KM_2_MILES_RATIO = 0.621371192;\r
160 ///**\r
161 //* Code on Internet based on Unknown book.  Lat/Long is in Degrees\r
162 //* @param alat\r
163 //* @param alon\r
164 //* @param blat\r
165 //* @param blon\r
166 //* @return\r
167 //*/\r
168 //public static double calc1(double alat, double alon, double blat, double blon) {\r
169 //      alat *= DEGREES_2_RADIANS;\r
170 //      alon *= DEGREES_2_RADIANS;\r
171 //      blat *= DEGREES_2_RADIANS;\r
172 //      blon *= DEGREES_2_RADIANS;\r
173 //      \r
174 //      // Reused values\r
175 //      double cosAlat,cosBlat;\r
176 //      \r
177 //      return Math.acos(\r
178 //              ((cosAlat=Math.cos(alat))*Math.cos(alon)*(cosBlat=Math.cos(blat))*Math.cos(blon)) +\r
179 //              (cosAlat*Math.sin(alon)*cosBlat*Math.sin(blon)) +\r
180 //              (Math.sin(alat)*Math.sin(blat))\r
181 //              )/DEGREES_2_RADIANS;\r
182 //      \r
183 //}\r
184 \r
185 /*\r
186 *  This method was 50% faster than calculation 1, and 75% than the Haverside method\r
187 *  Also, since it's based off of Agree standard Degrees of the Earth, etc, the calculations are more exact,\r
188 *    at least for Nautical Miles and Kilometers\r
189 */\r