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.

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