You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

434 lines
13 KiB

7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
​
7 years ago
​
​
7 years ago
​
​
7 years ago
​
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
​
​
​
7 years ago
​
​
7 years ago
​
​
7 years ago
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
7 years ago
​
7 years ago
​
7 years ago
​
​
​
​
​
​
7 years ago
​
​
​
​
7 years ago
7 years ago
​
7 years ago
​
7 years ago
7 years ago
7 years ago
​
7 years ago
​
7 years ago
​
7 years ago
​
​
7 years ago
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. // UTM.c
  2. // Original Javascript by Chuck Taylor
  3. // Port to C++ by Alex Hajnal
  4. //
  5. // *** THIS CODE USES 32-BIT FLOATS BY DEFAULT ***
  6. // *** For 64-bit double-precision edit UTM.h: undefine FLOAT_32 and define FLOAT_64
  7. //
  8. // This is a simple port of the code on the Geographic/UTM Coordinate Converter (1) page from Javascript to C++.
  9. // Using this you can easily convert between UTM and WGS84 (latitude and longitude).
  10. // Accuracy seems to be around 50cm (I suspect rounding errors are limiting precision).
  11. // This code is provided as-is and has been minimally tested; enjoy but use at your own risk!
  12. // The license for UTM.cpp and UTM.h is the same as the original Javascript:
  13. // "The C++ source code in UTM.cpp and UTM.h may be copied and reused without restriction."
  14. //
  15. // 1) http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html
  16. //http://alephnull.net/software/gis/UTM_WGS84_C_plus_plus.shtml
  17. #include "utm.h"
  18. coordConverter::coordConverter(){
  19. };
  20. coordConverter::~coordConverter(){
  21. };
  22. // DegToRad
  23. // Converts degrees to radians.
  24. FLOAT_UTM coordConverter::DegToRad(FLOAT_UTM deg) {
  25. return (FLOAT_UTM)(deg / 180.0 * PI_UTM);
  26. };
  27. // RadToDeg
  28. // Converts radians to degrees.
  29. FLOAT_UTM coordConverter::RadToDeg(FLOAT_UTM rad) {
  30. return (FLOAT_UTM)(rad / PI_UTM * 180.0f);
  31. };
  32. // ArcLengthOfMeridian
  33. // Computes the ellipsoidal distance from the equator to a point at a
  34. // given latitude.
  35. //
  36. // Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
  37. // GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
  38. //
  39. // Inputs:
  40. // phi - Latitude of the point, in radians.
  41. //
  42. // Globals:
  43. // sm_a - Ellipsoid model major axis.
  44. // sm_b - Ellipsoid model minor axis.
  45. //
  46. // Returns:
  47. // The ellipsoidal distance of the point from the equator, in meters.
  48. FLOAT_UTM coordConverter::ArcLengthOfMeridian(FLOAT_UTM phi) {
  49. FLOAT_UTM alpha, beta, gamma, delta, epsilon, n;
  50. FLOAT_UTM result;
  51. /* Precalculate n */
  52. n = (FLOAT_UTM)(sm_a - sm_b) / (sm_a + sm_b);
  53. /* Precalculate alpha */
  54. alpha = (FLOAT_UTM)((sm_a + sm_b) / 2.0)
  55. * (1.0 + (POW_UTM(n, 2.0) / 4.0) + (POW_UTM(n, 4.0) / 64.0));
  56. /* Precalculate beta */
  57. beta = (FLOAT_UTM)(-3.0 * n / 2.0) + (9.0 * POW_UTM(n, 3.0) / 16.0)
  58. + (-3.0 * POW_UTM(n, 5.0) / 32.0);
  59. /* Precalculate gamma */
  60. gamma = (FLOAT_UTM)(15.0 * POW_UTM(n, 2.0) / 16.0)
  61. + (-15.0 * POW_UTM(n, 4.0) / 32.0);
  62. /* Precalculate delta */
  63. delta = (FLOAT_UTM)(-35.0 * POW_UTM(n, 3.0) / 48.0)
  64. + (105.0 * POW_UTM(n, 5.0) / 256.0);
  65. /* Precalculate epsilon */
  66. epsilon = (FLOAT_UTM)(315.0 * POW_UTM(n, 4.0) / 512.0);
  67. /* Now calculate the sum of the series and return */
  68. result = (FLOAT_UTM) alpha
  69. * (phi + (beta * SIN_UTM(2.0 * phi))
  70. + (gamma * SIN_UTM(4.0 * phi))
  71. + (delta * SIN_UTM(6.0 * phi))
  72. + (epsilon * SIN_UTM(8.0 * phi)));
  73. return result;
  74. };
  75. // UTMCentralMeridian
  76. // Determines the central meridian for the given UTM zone.
  77. //
  78. // Inputs:
  79. // zone - An integer value designating the UTM zone, range [1,60].
  80. //
  81. // Returns:
  82. // The central meridian for the given UTM zone, in radians
  83. // Range of the central meridian is the radian equivalent of [-177,+177].
  84. FLOAT_UTM coordConverter::UTMCentralMeridian(int zone) {
  85. FLOAT_UTM cmeridian;
  86. cmeridian = DegToRad(-183.0 + ((FLOAT_UTM)zone * 6.0));
  87. return cmeridian;
  88. };
  89. // FootpointLatitude
  90. //
  91. // Computes the footpoint latitude for use in converting transverse
  92. // Mercator coordinates to ellipsoidal coordinates.
  93. //
  94. // Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
  95. // GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
  96. //
  97. // Inputs:
  98. // y - The UTM northing coordinate, in meters.
  99. //
  100. // Returns:
  101. // The footpoint latitude, in radians.
  102. FLOAT_UTM coordConverter::FootpointLatitude(FLOAT_UTM y) {
  103. FLOAT_UTM y_, alpha_, beta_, gamma_, delta_, epsilon_, n;
  104. FLOAT_UTM result;
  105. /* Precalculate n (Eq. 10.18) */
  106. n = (FLOAT_UTM)(sm_a - sm_b) / (sm_a + sm_b);
  107. /* Precalculate alpha_ (Eq. 10.22) */
  108. /* (Same as alpha in Eq. 10.17) */
  109. alpha_ = (FLOAT_UTM)((sm_a + sm_b) / 2.0)
  110. * (1 + (POW_UTM(n, 2.0) / 4) + (POW_UTM(n, 4.0) / 64));
  111. /* Precalculate y_ (Eq. 10.23) */
  112. y_ = y / alpha_;
  113. /* Precalculate beta_ (Eq. 10.22) */
  114. beta_ = (FLOAT_UTM)(3.0 * n / 2.0) + (-27.0 * POW_UTM(n, 3.0) / 32.0)
  115. + (269.0 * POW_UTM(n, 5.0) / 512.0);
  116. /* Precalculate gamma_ (Eq. 10.22) */
  117. gamma_ = (FLOAT_UTM)(21.0 * POW_UTM(n, 2.0) / 16.0)
  118. + (-55.0 * POW_UTM(n, 4.0) / 32.0);
  119. /* Precalculate delta_ (Eq. 10.22) */
  120. delta_ = (FLOAT_UTM)(151.0 * POW_UTM(n, 3.0) / 96.0)
  121. + (-417.0 * POW_UTM(n, 5.0) / 128.0);
  122. /* Precalculate epsilon_ (Eq. 10.22) */
  123. epsilon_ = (FLOAT_UTM)(1097.0 * POW_UTM(n, 4.0) / 512.0);
  124. /* Now calculate the sum of the series (Eq. 10.21) */
  125. result = (FLOAT_UTM)(y_ + (beta_ * SIN_UTM(2.0 * y_))
  126. + (gamma_ * SIN_UTM(4.0 * y_))
  127. + (delta_ * SIN_UTM(6.0 * y_))
  128. + (epsilon_ * SIN_UTM(8.0 * y_)));
  129. return result;
  130. };
  131. // MapLatLonToXY
  132. // Converts a latitude/longitude pair to x and y coordinates in the
  133. // Transverse Mercator projection. Note that Transverse Mercator is not
  134. // the same as UTM; a scale factor is required to convert between them.
  135. //
  136. // Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
  137. // GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
  138. //
  139. // Inputs:
  140. // phi - Latitude of the point, in radians.
  141. // lambda - Longitude of the point, in radians.
  142. // lambda0 - Longitude of the central meridian to be used, in radians.
  143. //
  144. // Outputs:
  145. // x - The x coordinate of the computed point.
  146. // y - The y coordinate of the computed point.
  147. //
  148. // Returns:
  149. // The function does not return a value.
  150. void coordConverter::MapLatLonToXY(FLOAT_UTM phi, FLOAT_UTM lambda, FLOAT_UTM lambda0, FLOAT_UTM &x, FLOAT_UTM &y) {
  151. FLOAT_UTM N, nu2, ep2, t, t2, l;
  152. FLOAT_UTM l3coef, l4coef, l5coef, l6coef, l7coef, l8coef;
  153. //FLOAT tmp; // Unused
  154. /* Precalculate ep2 */
  155. ep2 = (FLOAT_UTM)(POW_UTM(sm_a, 2.0) - POW_UTM(sm_b, 2.0)) / POW_UTM(sm_b, 2.0);
  156. /* Precalculate nu2 */
  157. nu2 = (FLOAT_UTM)ep2 * POW_UTM(COS_UTM(phi), 2.0);
  158. /* Precalculate N */
  159. N = (FLOAT_UTM)POW_UTM(sm_a, 2.0) / (sm_b * SQRT_UTM(1 + nu2));
  160. /* Precalculate t */
  161. t = TAN_UTM(phi);
  162. t2 = t * t;
  163. //tmp = (t2 * t2 * t2) - POW(t, 6.0); // Unused
  164. /* Precalculate l */
  165. l = (FLOAT_UTM) lambda - lambda0;
  166. /* Precalculate coefficients for l**n in the equations below
  167. so a normal human being can read the expressions for easting
  168. and northing
  169. -- l**1 and l**2 have coefficients of 1.0 */
  170. l3coef = (FLOAT_UTM)(1.0 - t2 + nu2);
  171. l4coef = (FLOAT_UTM)(5.0 - t2 + 9 * nu2 + 4.0 * (nu2 * nu2));
  172. l5coef = (FLOAT_UTM)(5.0 - 18.0 * t2 + (t2 * t2) + 14.0 * nu2
  173. - 58.0 * t2 * nu2);
  174. l6coef = (FLOAT_UTM)(61.0 - 58.0 * t2 + (t2 * t2) + 270.0 * nu2
  175. - 330.0 * t2 * nu2);
  176. l7coef = (FLOAT_UTM)(61.0 - 479.0 * t2 + 179.0 * (t2 * t2) - (t2 * t2 * t2));
  177. l8coef = (FLOAT_UTM)(1385.0 - 3111.0 * t2 + 543.0 * (t2 * t2) - (t2 * t2 * t2));
  178. /* Calculate easting (x) */
  179. x = (FLOAT_UTM)( N * COS_UTM(phi) * l
  180. + (N / 6.0 * POW_UTM(COS_UTM(phi), 3.0) * l3coef * POW_UTM(l, 3.0))
  181. + (N / 120.0 * POW_UTM(COS_UTM(phi), 5.0) * l5coef * POW_UTM(l, 5.0))
  182. + (N / 5040.0 * POW_UTM(COS_UTM(phi), 7.0) * l7coef * POW_UTM(l, 7.0)));
  183. /* Calculate northing (y) */
  184. y = (FLOAT_UTM) ( ArcLengthOfMeridian (phi)
  185. + (t / 2.0 * N * POW_UTM(COS_UTM(phi), 2.0) * POW_UTM(l, 2.0))
  186. + (t / 24.0 * N * POW_UTM(COS_UTM(phi), 4.0) * l4coef * POW_UTM(l, 4.0))
  187. + (t / 720.0 * N * POW_UTM(COS_UTM(phi), 6.0) * l6coef * POW_UTM(l, 6.0))
  188. + (t / 40320.0 * N * POW_UTM(COS_UTM(phi), 8.0) * l8coef * POW_UTM(l, 8.0)));
  189. return;
  190. };
  191. // MapXYToLatLon
  192. // Converts x and y coordinates in the Transverse Mercator projection to
  193. // a latitude/longitude pair. Note that Transverse Mercator is not
  194. // the same as UTM; a scale factor is required to convert between them.
  195. //
  196. // Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
  197. // GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
  198. //
  199. // Inputs:
  200. // x - The easting of the point, in meters.
  201. // y - The northing of the point, in meters.
  202. // lambda0 - Longitude of the central meridian to be used, in radians.
  203. //
  204. // Outputs:
  205. // phi - Latitude in radians.
  206. // lambda - Longitude in radians.
  207. //
  208. // Returns:
  209. // The function does not return a value.
  210. //
  211. // Remarks:
  212. // The local variables Nf, nuf2, tf, and tf2 serve the same purpose as
  213. // N, nu2, t, and t2 in MapLatLonToXY, but they are computed with respect
  214. // to the footpoint latitude phif.
  215. //
  216. // x1frac, x2frac, x2poly, x3poly, etc. are to enhance readability and
  217. // to optimize computations.
  218. void coordConverter::MapXYToLatLon(FLOAT_UTM x, FLOAT_UTM y, FLOAT_UTM lambda0, FLOAT_UTM& phi, FLOAT_UTM& lambda){
  219. //FLOAT_UTM
  220. double phif, Nf, Nfpow, nuf2, ep2, tf, tf2, tf4, cf;
  221. //FLOAT_UTM
  222. double x1frac, x2frac, x3frac, x4frac, x5frac, x6frac, x7frac, x8frac;
  223. //FLOAT_UTM
  224. double x2poly, x3poly, x4poly, x5poly, x6poly, x7poly, x8poly;
  225. /* Get the value of phif, the footpoint latitude. */
  226. phif = FootpointLatitude (y);
  227. /* Precalculate ep2 */
  228. ep2 = (POW_UTM(sm_a, 2.0) - POW_UTM(sm_b, 2.0))
  229. / POW_UTM(sm_b, 2.0);
  230. /* Precalculate cos (phif) */
  231. cf = COS_UTM(phif);
  232. /* Precalculate nuf2 */
  233. nuf2 = ep2 * POW_UTM(cf, 2.0);
  234. /* Precalculate Nf and initialize Nfpow */
  235. Nf = POW_UTM(sm_a, 2.0) / (sm_b * SQRT_UTM(1 + nuf2));
  236. Nfpow = Nf;
  237. /* Precalculate tf */
  238. tf = TAN_UTM(phif);
  239. tf2 = tf * tf;
  240. tf4 = tf2 * tf2;
  241. /* Precalculate fractional coefficients for x**n in the equations
  242. below to simplify the expressions for latitude and longitude. */
  243. x1frac = 1.0 / (Nfpow * cf);
  244. Nfpow *= Nf; /* now equals Nf**2) */
  245. x2frac = tf / (2.0 * Nfpow);
  246. Nfpow *= Nf; /* now equals Nf**3) */
  247. x3frac = 1.0 / (6.0 * Nfpow * cf);
  248. Nfpow *= Nf; /* now equals Nf**4) */
  249. x4frac = tf / (24.0 * Nfpow);
  250. Nfpow *= Nf; /* now equals Nf**5) */
  251. x5frac = 1.0 / (120.0 * Nfpow * cf);
  252. Nfpow *= Nf; /* now equals Nf**6) */
  253. x6frac = tf / (720.0 * Nfpow);
  254. Nfpow *= Nf; /* now equals Nf**7) */
  255. x7frac = 1.0 / (5040.0 * Nfpow * cf);
  256. Nfpow *= Nf; /* now equals Nf**8) */
  257. x8frac = tf / (40320.0 * Nfpow);
  258. /* Precalculate polynomial coefficients for x**n.
  259. -- x**1 does not have a polynomial coefficient. */
  260. x2poly = -1.0 - nuf2;
  261. x3poly = -1.0 - 2 * tf2 - nuf2;
  262. x4poly = 5.0 + 3.0 * tf2 + 6.0 * nuf2 - 6.0 * tf2 * nuf2
  263. - 3.0 * (nuf2 *nuf2) - 9.0 * tf2 * (nuf2 * nuf2);
  264. x5poly = 5.0 + 28.0 * tf2 + 24.0 * tf4 + 6.0 * nuf2 + 8.0 * tf2 * nuf2;
  265. x6poly = -61.0 - 90.0 * tf2 - 45.0 * tf4 - 107.0 * nuf2
  266. + 162.0 * tf2 * nuf2;
  267. x7poly = -61.0 - 662.0 * tf2 - 1320.0 * tf4 - 720.0 * (tf4 * tf2);
  268. x8poly = 1385.0 + 3633.0 * tf2 + 4095.0 * tf4 + 1575 * (tf4 * tf2);
  269. /* Calculate latitude */
  270. double tVal;
  271. tVal = phif + x2frac * x2poly * (x * x);
  272. tVal = tVal + x4frac * x4poly * POW_UTM(x, 4.0);
  273. tVal = tVal + x6frac * x6poly * POW_UTM(x, 6.0);
  274. tVal = tVal + x8frac * x8poly * pow((double)x, 8.0);
  275. phi = RadToDeg(tVal);
  276. /* Calculate longitude */
  277. tVal = lambda0 + x1frac * x;
  278. tVal = tVal + x3frac * x3poly * POW_UTM(x, 3.0);
  279. tVal = tVal + x5frac * x5poly * POW_UTM(x, 5.0);
  280. tVal = tVal + x7frac * x7poly * pow((double)x, 7.0);
  281. lambda = RadToDeg(tVal);
  282. return;
  283. };
  284. // LatLonToUTMXY
  285. // Converts a latitude/longitude pair to x and y coordinates in the
  286. // Universal Transverse Mercator projection.
  287. //
  288. // Inputs:
  289. // lat - Latitude of the point, in radians.
  290. // lon - Longitude of the point, in radians.
  291. // zone - UTM zone to be used for calculating values for x and y.
  292. // If zone is less than 1 or greater than 60, the routine
  293. // will determine the appropriate zone from the value of lon.
  294. //
  295. // Outputs:
  296. // x - The x coordinate (easting) of the computed point. (in meters)
  297. // y - The y coordinate (northing) of the computed point. (in meters)
  298. //
  299. // Returns:
  300. // The UTM zone used for calculating the values of x and y.
  301. int coordConverter::LatLonToUTMXY(FLOAT_UTM lat, FLOAT_UTM lon, int zone, FLOAT_UTM& x, FLOAT_UTM& y) {
  302. if ( (zone < 1) || (zone > 60) )
  303. zone = FLOOR_UTM((lon + 180.0) / 6) + 1;
  304. MapLatLonToXY (DegToRad(lat), DegToRad(lon), UTMCentralMeridian(zone), x, y);
  305. /* Adjust easting and northing for UTM system. */
  306. x = x * UTMScaleFactor + 500000.0;
  307. y = y * UTMScaleFactor;
  308. if (y < 0.0)
  309. y = y + 10000000.0;
  310. return zone;
  311. };
  312. // UTMXYToLatLon
  313. //
  314. // Converts x and y coordinates in the Universal Transverse Mercator
  315. // projection to a latitude/longitude pair.
  316. //
  317. // Inputs:
  318. // x - The easting of the point, in meters.
  319. // y - The northing of the point, in meters.
  320. // zone - The UTM zone in which the point lies.
  321. // southhemi - True if the point is in the southern hemisphere;
  322. // false otherwise.
  323. //
  324. // Outputs:
  325. // lat - The latitude of the point, in radians.
  326. // lon - The longitude of the point, in radians.
  327. //
  328. // Returns:
  329. // The function does not return a value.
  330. void coordConverter::UTMXYToLatLon(FLOAT_UTM x, FLOAT_UTM y, int zone, bool southhemi, FLOAT_UTM& lat, FLOAT_UTM& lon){
  331. FLOAT_UTM cmeridian;
  332. x -= 500000.0;
  333. x /= UTMScaleFactor;
  334. /* If in southern hemisphere, adjust y accordingly. */
  335. if (southhemi)
  336. y -= 10000000.0;
  337. y /= UTMScaleFactor;
  338. cmeridian = UTMCentralMeridian (zone);
  339. MapXYToLatLon (x, y, cmeridian, lat, lon);
  340. return;
  341. };