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