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.

220 lines
7.2 KiB

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