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.

216 lines
7.3 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
​
  1. #ifndef __UTM_WGS84_CONVERTER_HEADER__
  2. #define __UTM_WGS84_CONVERTER_HEADER__
  3. // From: http://alephnull.net/software/gis/UTM_WGS84_C_plus_plus.shtml
  4. // UTM.h
  5. // Original Javascript by Chuck Taylor
  6. // Port to C++ by Alex Hajnal
  7. //
  8. // *** THIS CODE USES 32-BIT FLOATS BY DEFAULT ***
  9. // *** For 64-bit double-precision edit this file: undefine FLOAT_32 and define FLOAT_64 (see below)
  10. //
  11. // This is a simple port of the code on the Geographic/UTM Coordinate Converter (1) page from Javascript to C++.
  12. // Using this you can easily convert between UTM and WGS84 (latitude and longitude).
  13. // Accuracy seems to be around 50cm (I suspect rounding errors are limiting precision).
  14. // This code is provided as-is and has been minimally tested; enjoy but use at your own risk!
  15. // The license for UTM.cpp and UTM.h is the same as the original Javascript:
  16. // "The C++ source code in UTM.cpp and UTM.h may be copied and reused without restriction."
  17. //
  18. // 1) http://home.hiwaay.net/~taylorc/toolbox/geography/geoutm.html
  19. // Choose floating point precision:
  20. // 32-bit (for Teensy 3.5/3.6 ARM boards, etc.)
  21. #define FLOAT_32
  22. // 64-bit (for desktop/server use)
  23. // #define FLOAT_64
  24. #ifdef FLOAT_64
  25. #define FLOAT_UTM double
  26. #define SIN_UTM sin
  27. #define COS_UTM cos
  28. #define TAN_UTM tan
  29. #define POW_UTM pow
  30. #define SQRT_UTM sqrt
  31. #define FLOOR_UTM floor
  32. #define PI_UTM 3.14159265358979
  33. /* Ellipsoid model constants (actual values here are for WGS84) */
  34. #define sm_a 6378137.0
  35. #define sm_b 6356752.314
  36. #define sm_EccSquared 6.69437999013e-03
  37. #define UTMScaleFactor 0.9996
  38. #else
  39. #ifdef FLOAT_32
  40. #define FLOAT_UTM float
  41. #define SIN_UTM sinf
  42. #define COS_UTM cosf
  43. #define TAN_UTM tanf
  44. #define POW_UTM powf
  45. #define SQRT_UTM sqrtf
  46. #define FLOOR_UTM floorf
  47. #define PI_UTM 3.14159265358979f
  48. /* Ellipsoid model constants (actual values here are for WGS84) */
  49. #define sm_a 6378137.0f
  50. #define sm_b 6356752.314f
  51. #define sm_EccSquared 0.00669437999013f
  52. #define UTMScaleFactor 0.9996f
  53. #endif
  54. #endif
  55. #include <math.h>
  56. class coordConverter
  57. {
  58. private:
  59. // DegToRad
  60. // Converts degrees to radians.
  61. FLOAT_UTM DegToRad(FLOAT_UTM deg);
  62. // RadToDeg
  63. // Converts radians to degrees.
  64. FLOAT_UTM RadToDeg(FLOAT_UTM rad);
  65. // ArcLengthOfMeridian
  66. // Computes the ellipsoidal distance from the equator to a point at a
  67. // given latitude.
  68. //
  69. // Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
  70. // GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
  71. //
  72. // Inputs:
  73. // phi - Latitude of the point, in radians.
  74. //
  75. // Globals:
  76. // sm_a - Ellipsoid model major axis.
  77. // sm_b - Ellipsoid model minor axis.
  78. //
  79. // Returns:
  80. // The ellipsoidal distance of the point from the equator, in meters.
  81. FLOAT_UTM ArcLengthOfMeridian(FLOAT_UTM phi);
  82. // UTMCentralMeridian
  83. // Determines the central meridian for the given UTM zone.
  84. //
  85. // Inputs:
  86. // zone - An integer value designating the UTM zone, range [1,60].
  87. //
  88. // Returns:
  89. // The central meridian for the given UTM zone, in radians
  90. // Range of the central meridian is the radian equivalent of [-177,+177].
  91. FLOAT_UTM UTMCentralMeridian(int zone);
  92. // FootpointLatitude
  93. //
  94. // Computes the footpoint latitude for use in converting transverse
  95. // Mercator coordinates to ellipsoidal coordinates.
  96. //
  97. // Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
  98. // GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
  99. //
  100. // Inputs:
  101. // y - The UTM northing coordinate, in meters.
  102. //
  103. // Returns:
  104. // The footpoint latitude, in radians.
  105. FLOAT_UTM FootpointLatitude(FLOAT_UTM y);
  106. // MapLatLonToXY
  107. // Converts a latitude/longitude pair to x and y coordinates in the
  108. // Transverse Mercator projection. Note that Transverse Mercator is not
  109. // the same as UTM; a scale factor is required to convert between them.
  110. //
  111. // Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
  112. // GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
  113. //
  114. // Inputs:
  115. // phi - Latitude of the point, in radians.
  116. // lambda - Longitude of the point, in radians.
  117. // lambda0 - Longitude of the central meridian to be used, in radians.
  118. //
  119. // Outputs:
  120. // x - The x coordinate of the computed point.
  121. // y - The y coordinate of the computed point.
  122. //
  123. // Returns:
  124. // The function does not return a value.
  125. void MapLatLonToXY(FLOAT_UTM phi, FLOAT_UTM lambda, FLOAT_UTM lambda0, FLOAT_UTM & x, FLOAT_UTM & y);
  126. // MapXYToLatLon
  127. // Converts x and y coordinates in the Transverse Mercator projection to
  128. // a latitude/longitude pair. Note that Transverse Mercator is not
  129. // the same as UTM; a scale factor is required to convert between them.
  130. //
  131. // Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
  132. // GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
  133. //
  134. // Inputs:
  135. // x - The easting of the point, in meters.
  136. // y - The northing of the point, in meters.
  137. // lambda0 - Longitude of the central meridian to be used, in radians.
  138. //
  139. // Outputs:
  140. // phi - Latitude in radians.
  141. // lambda - Longitude in radians.
  142. //
  143. // Returns:
  144. // The function does not return a value.
  145. //
  146. // Remarks:
  147. // The local variables Nf, nuf2, tf, and tf2 serve the same purpose as
  148. // N, nu2, t, and t2 in MapLatLonToXY, but they are computed with respect
  149. // to the footpoint latitude phif.
  150. //
  151. // x1frac, x2frac, x2poly, x3poly, etc. are to enhance readability and
  152. // to optimize computations.
  153. void MapXYToLatLon(FLOAT_UTM x, FLOAT_UTM y, FLOAT_UTM lambda0, FLOAT_UTM & phi, FLOAT_UTM & lambda);
  154. public:
  155. coordConverter();
  156. ~coordConverter();
  157. // LatLonToUTMXY
  158. // Converts a latitude/longitude pair to x and y coordinates in the
  159. // Universal Transverse Mercator projection.
  160. //
  161. // Inputs:
  162. // lat - Latitude of the point, in radians.
  163. // lon - Longitude of the point, in radians.
  164. // zone - UTM zone to be used for calculating values for x and y.
  165. // If zone is less than 1 or greater than 60, the routine
  166. // will determine the appropriate zone from the value of lon.
  167. //
  168. // Outputs:
  169. // x - The x coordinate (easting) of the computed point. (in meters)
  170. // y - The y coordinate (northing) of the computed point. (in meters)
  171. //
  172. // Returns:
  173. // The UTM zone used for calculating the values of x and y.
  174. int LatLonToUTMXY(FLOAT_UTM lat, FLOAT_UTM lon, int zone, FLOAT_UTM & x, FLOAT_UTM & y);
  175. // UTMXYToLatLon
  176. //
  177. // Converts x and y coordinates in the Universal Transverse Mercator// The UTM zone parameter should be in the range [1,60].
  178. // projection to a latitude/longitude pair.
  179. //
  180. // Inputs:
  181. // x - The easting of the point, in meters.
  182. // y - The northing of the point, in meters.
  183. // zone - The UTM zone in which the point lies.
  184. // southhemi - True if the point is in the southern hemisphere;
  185. // false otherwise.
  186. //
  187. // Outputs:
  188. // lat - The latitude of the point, in radians.
  189. // lon - The longitude of the point, in radians.
  190. //
  191. // Returns:
  192. // The function does not return a value.
  193. void UTMXYToLatLon(FLOAT_UTM x, FLOAT_UTM y, int zone, bool southhemi, FLOAT_UTM & lat, FLOAT_UTM & lon);
  194. };
  195. #endif