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.

214 lines
7.0 KiB

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. #else
  34. #ifdef FLOAT_32
  35. #define FLOAT_UTM float
  36. #define SIN_UTM sinf
  37. #define COS_UTM cosf
  38. #define TAN_UTM tanf
  39. #define POW_UTM powf
  40. #define SQRT_UTM sqrtf
  41. #define FLOOR_UTM floorf
  42. #define PI_UTM 3.14159265358979f
  43. #endif
  44. #endif
  45. #include <math.h>
  46. /* Ellipsoid model constants (actual values here are for WGS84) */
  47. #define sm_a 6378137.0
  48. #define sm_b 6356752.314
  49. #define sm_EccSquared 6.69437999013e-03
  50. #define UTMScaleFactor 0.9996
  51. class coordConverter{
  52. private:
  53. // DegToRad
  54. // Converts degrees to radians.
  55. FLOAT_UTM DegToRad(FLOAT_UTM deg);
  56. // RadToDeg
  57. // Converts radians to degrees.
  58. FLOAT_UTM RadToDeg(FLOAT_UTM rad);
  59. // ArcLengthOfMeridian
  60. // Computes the ellipsoidal distance from the equator to a point at a
  61. // given latitude.
  62. //
  63. // Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
  64. // GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
  65. //
  66. // Inputs:
  67. // phi - Latitude of the point, in radians.
  68. //
  69. // Globals:
  70. // sm_a - Ellipsoid model major axis.
  71. // sm_b - Ellipsoid model minor axis.
  72. //
  73. // Returns:
  74. // The ellipsoidal distance of the point from the equator, in meters.
  75. FLOAT_UTM ArcLengthOfMeridian(FLOAT_UTM phi);
  76. // UTMCentralMeridian
  77. // Determines the central meridian for the given UTM zone.
  78. //
  79. // Inputs:
  80. // zone - An integer value designating the UTM zone, range [1,60].
  81. //
  82. // Returns:
  83. // The central meridian for the given UTM zone, in radians
  84. // Range of the central meridian is the radian equivalent of [-177,+177].
  85. FLOAT_UTM UTMCentralMeridian(int zone);
  86. // FootpointLatitude
  87. //
  88. // Computes the footpoint latitude for use in converting transverse
  89. // Mercator coordinates to ellipsoidal coordinates.
  90. //
  91. // Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
  92. // GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
  93. //
  94. // Inputs:
  95. // y - The UTM northing coordinate, in meters.
  96. //
  97. // Returns:
  98. // The footpoint latitude, in radians.
  99. FLOAT_UTM FootpointLatitude(FLOAT_UTM y);
  100. // MapLatLonToXY
  101. // Converts a latitude/longitude pair to x and y coordinates in the
  102. // Transverse Mercator projection. Note that Transverse Mercator is not
  103. // the same as UTM; a scale factor is required to convert between them.
  104. //
  105. // Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
  106. // GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
  107. //
  108. // Inputs:
  109. // phi - Latitude of the point, in radians.
  110. // lambda - Longitude of the point, in radians.
  111. // lambda0 - Longitude of the central meridian to be used, in radians.
  112. //
  113. // Outputs:
  114. // x - The x coordinate of the computed point.
  115. // y - The y coordinate of the computed point.
  116. //
  117. // Returns:
  118. // The function does not return a value.
  119. void MapLatLonToXY(FLOAT_UTM phi, FLOAT_UTM lambda, FLOAT_UTM lambda0, FLOAT_UTM &x, FLOAT_UTM &y);
  120. // MapXYToLatLon
  121. // Converts x and y coordinates in the Transverse Mercator projection to
  122. // a latitude/longitude pair. Note that Transverse Mercator is not
  123. // the same as UTM; a scale factor is required to convert between them.
  124. //
  125. // Reference: Hoffmann-Wellenhof, B., Lichtenegger, H., and Collins, J.,
  126. // GPS: Theory and Practice, 3rd ed. New York: Springer-Verlag Wien, 1994.
  127. //
  128. // Inputs:
  129. // x - The easting of the point, in meters.
  130. // y - The northing of the point, in meters.
  131. // lambda0 - Longitude of the central meridian to be used, in radians.
  132. //
  133. // Outputs:
  134. // phi - Latitude in radians.
  135. // lambda - Longitude in radians.
  136. //
  137. // Returns:
  138. // The function does not return a value.
  139. //
  140. // Remarks:
  141. // The local variables Nf, nuf2, tf, and tf2 serve the same purpose as
  142. // N, nu2, t, and t2 in MapLatLonToXY, but they are computed with respect
  143. // to the footpoint latitude phif.
  144. //
  145. // x1frac, x2frac, x2poly, x3poly, etc. are to enhance readability and
  146. // to optimize computations.
  147. void MapXYToLatLon(FLOAT_UTM x, FLOAT_UTM y, FLOAT_UTM lambda0, FLOAT_UTM& phi, FLOAT_UTM& lambda);
  148. public:
  149. coordConverter();
  150. ~coordConverter();
  151. // LatLonToUTMXY
  152. // Converts a latitude/longitude pair to x and y coordinates in the
  153. // Universal Transverse Mercator projection.
  154. //
  155. // Inputs:
  156. // lat - Latitude of the point, in radians.
  157. // lon - Longitude of the point, in radians.
  158. // zone - UTM zone to be used for calculating values for x and y.
  159. // If zone is less than 1 or greater than 60, the routine
  160. // will determine the appropriate zone from the value of lon.
  161. //
  162. // Outputs:
  163. // x - The x coordinate (easting) of the computed point. (in meters)
  164. // y - The y coordinate (northing) of the computed point. (in meters)
  165. //
  166. // Returns:
  167. // The UTM zone used for calculating the values of x and y.
  168. int LatLonToUTMXY(FLOAT_UTM lat, FLOAT_UTM lon, int zone, FLOAT_UTM& x, FLOAT_UTM& y);
  169. // UTMXYToLatLon
  170. //
  171. // Converts x and y coordinates in the Universal Transverse Mercator// The UTM zone parameter should be in the range [1,60].
  172. // projection to a latitude/longitude pair.
  173. //
  174. // Inputs:
  175. // x - The easting of the point, in meters.
  176. // y - The northing of the point, in meters.
  177. // zone - The UTM zone in which the point lies.
  178. // southhemi - True if the point is in the southern hemisphere;
  179. // false otherwise.
  180. //
  181. // Outputs:
  182. // lat - The latitude of the point, in radians.
  183. // lon - The longitude of the point, in radians.
  184. //
  185. // Returns:
  186. // The function does not return a value.
  187. void UTMXYToLatLon(FLOAT_UTM x, FLOAT_UTM y, int zone, bool southhemi, FLOAT_UTM& lat, FLOAT_UTM& lon);
  188. };
  189. #endif