tmerc.hpp 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717
  1. // Boost.Geometry - gis-projections (based on PROJ4)
  2. // Copyright (c) 2008-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // Copyright (c) 2023 Adam Wulkiewicz, Lodz, Poland.
  4. // This file was modified by Oracle on 2017, 2018, 2019, 2022.
  5. // Modifications copyright (c) 2017-2022, Oracle and/or its affiliates.
  6. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle.
  7. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle.
  8. // Use, modification and distribution is subject to the Boost Software License,
  9. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  10. // http://www.boost.org/LICENSE_1_0.txt)
  11. // This file is converted from PROJ4, http://trac.osgeo.org/proj
  12. // PROJ4 is originally written by Gerald Evenden (then of the USGS)
  13. // PROJ4 is maintained by Frank Warmerdam
  14. // PROJ4 is converted to Boost.Geometry by Barend Gehrels
  15. // Last updated version of proj: 8.2.1
  16. // Original copyright notice:
  17. // Permission is hereby granted, free of charge, to any person obtaining a
  18. // copy of this software and associated documentation files (the "Software"),
  19. // to deal in the Software without restriction, including without limitation
  20. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  21. // and/or sell copies of the Software, and to permit persons to whom the
  22. // Software is furnished to do so, subject to the following conditions:
  23. // The above copyright notice and this permission notice shall be included
  24. // in all copies or substantial portions of the Software.
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  26. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  28. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  30. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  31. // DEALINGS IN THE SOFTWARE.
  32. #ifndef BOOST_GEOMETRY_PROJECTIONS_TMERC_HPP
  33. #define BOOST_GEOMETRY_PROJECTIONS_TMERC_HPP
  34. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  35. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  36. #include <boost/geometry/srs/projections/impl/projects.hpp>
  37. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  38. #include <boost/geometry/srs/projections/impl/function_overloads.hpp>
  39. #include <boost/geometry/srs/projections/impl/pj_mlfn.hpp>
  40. #include <boost/geometry/util/condition.hpp>
  41. #include <boost/geometry/util/math.hpp>
  42. namespace boost { namespace geometry
  43. {
  44. namespace projections
  45. {
  46. #ifndef DOXYGEN_NO_DETAIL
  47. namespace detail { namespace tmerc
  48. {
  49. static const double epsilon10 = 1.e-10;
  50. /* Constant for "exact" transverse mercator */
  51. static const int proj_etmerc_order = 6;
  52. template <typename T>
  53. inline T FC1() { return 1.; }
  54. template <typename T>
  55. inline T FC2() { return .5; }
  56. template <typename T>
  57. inline T FC3() { return .16666666666666666666666666666666666666; }
  58. template <typename T>
  59. inline T FC4() { return .08333333333333333333333333333333333333; }
  60. template <typename T>
  61. inline T FC5() { return .05; }
  62. template <typename T>
  63. inline T FC6() { return .03333333333333333333333333333333333333; }
  64. template <typename T>
  65. inline T FC7() { return .02380952380952380952380952380952380952; }
  66. template <typename T>
  67. inline T FC8() { return .01785714285714285714285714285714285714; }
  68. template <typename T>
  69. struct par_tmerc
  70. {
  71. T esp;
  72. T ml0;
  73. detail::en<T> en;
  74. };
  75. // More exact: Poder/Engsager
  76. template <typename T>
  77. struct par_tmerc_exact
  78. {
  79. T Qn; /* Merid. quad., scaled to the projection */
  80. T Zb; /* Radius vector in polar coord. systems */
  81. T cgb[6]; /* Constants for Gauss -> Geo lat */
  82. T cbg[6]; /* Constants for Geo lat -> Gauss */
  83. T utg[6]; /* Constants for transv. merc. -> geo */
  84. T gtu[6]; /* Constants for geo -> transv. merc. */
  85. };
  86. template <typename T, typename Parameters>
  87. struct base_tmerc_ellipsoid
  88. {
  89. par_tmerc<T> m_proj_parm;
  90. // FORWARD(e_forward) ellipse
  91. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  92. inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  93. {
  94. static const T half_pi = detail::half_pi<T>();
  95. static const T FC1 = tmerc::FC1<T>();
  96. static const T FC2 = tmerc::FC2<T>();
  97. static const T FC3 = tmerc::FC3<T>();
  98. static const T FC4 = tmerc::FC4<T>();
  99. static const T FC5 = tmerc::FC5<T>();
  100. static const T FC6 = tmerc::FC6<T>();
  101. static const T FC7 = tmerc::FC7<T>();
  102. static const T FC8 = tmerc::FC8<T>();
  103. T al, als, n, cosphi, sinphi, t;
  104. /*
  105. * Fail if our longitude is more than 90 degrees from the
  106. * central meridian since the results are essentially garbage.
  107. * Is error -20 really an appropriate return value?
  108. *
  109. * http://trac.osgeo.org/proj/ticket/5
  110. */
  111. if( lp_lon < -half_pi || lp_lon > half_pi )
  112. {
  113. xy_x = HUGE_VAL;
  114. xy_y = HUGE_VAL;
  115. BOOST_THROW_EXCEPTION( projection_exception(error_lat_or_lon_exceed_limit) );
  116. return;
  117. }
  118. sinphi = sin(lp_lat);
  119. cosphi = cos(lp_lat);
  120. t = fabs(cosphi) > 1e-10 ? sinphi/cosphi : 0.;
  121. t *= t;
  122. al = cosphi * lp_lon;
  123. als = al * al;
  124. al /= sqrt(1. - par.es * sinphi * sinphi);
  125. n = this->m_proj_parm.esp * cosphi * cosphi;
  126. xy_x = par.k0 * al * (FC1 +
  127. FC3 * als * (1. - t + n +
  128. FC5 * als * (5. + t * (t - 18.) + n * (14. - 58. * t)
  129. + FC7 * als * (61. + t * ( t * (179. - t) - 479. ) )
  130. )));
  131. xy_y = par.k0 * (pj_mlfn(lp_lat, sinphi, cosphi, this->m_proj_parm.en) - this->m_proj_parm.ml0 +
  132. sinphi * al * lp_lon * FC2 * ( 1. +
  133. FC4 * als * (5. - t + n * (9. + 4. * n) +
  134. FC6 * als * (61. + t * (t - 58.) + n * (270. - 330 * t)
  135. + FC8 * als * (1385. + t * ( t * (543. - t) - 3111.) )
  136. ))));
  137. }
  138. // INVERSE(e_inverse) ellipsoid
  139. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  140. inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  141. {
  142. static const T half_pi = detail::half_pi<T>();
  143. static const T FC1 = tmerc::FC1<T>();
  144. static const T FC2 = tmerc::FC2<T>();
  145. static const T FC3 = tmerc::FC3<T>();
  146. static const T FC4 = tmerc::FC4<T>();
  147. static const T FC5 = tmerc::FC5<T>();
  148. static const T FC6 = tmerc::FC6<T>();
  149. static const T FC7 = tmerc::FC7<T>();
  150. static const T FC8 = tmerc::FC8<T>();
  151. T n, con, cosphi, d, ds, sinphi, t;
  152. lp_lat = pj_inv_mlfn(this->m_proj_parm.ml0 + xy_y / par.k0, par.es, this->m_proj_parm.en);
  153. if (fabs(lp_lat) >= half_pi) {
  154. lp_lat = xy_y < 0. ? -half_pi : half_pi;
  155. lp_lon = 0.;
  156. } else {
  157. sinphi = sin(lp_lat);
  158. cosphi = cos(lp_lat);
  159. t = fabs(cosphi) > 1e-10 ? sinphi/cosphi : 0.;
  160. n = this->m_proj_parm.esp * cosphi * cosphi;
  161. d = xy_x * sqrt(con = 1. - par.es * sinphi * sinphi) / par.k0;
  162. con *= t;
  163. t *= t;
  164. ds = d * d;
  165. lp_lat -= (con * ds / (1.-par.es)) * FC2 * (1. -
  166. ds * FC4 * (5. + t * (3. - 9. * n) + n * (1. - 4 * n) -
  167. ds * FC6 * (61. + t * (90. - 252. * n +
  168. 45. * t) + 46. * n
  169. - ds * FC8 * (1385. + t * (3633. + t * (4095. + 1574. * t)) )
  170. )));
  171. lp_lon = d*(FC1 -
  172. ds*FC3*( 1. + 2.*t + n -
  173. ds*FC5*(5. + t*(28. + 24.*t + 8.*n) + 6.*n
  174. - ds * FC7 * (61. + t * (662. + t * (1320. + 720. * t)) )
  175. ))) / cosphi;
  176. }
  177. }
  178. static inline std::string get_name()
  179. {
  180. return "tmerc_ellipsoid";
  181. }
  182. };
  183. template <typename T, typename Parameters>
  184. struct base_tmerc_ellipsoid_exact
  185. {
  186. par_tmerc_exact<T> m_proj_parm;
  187. static inline std::string get_name()
  188. {
  189. return "tmerc_ellipsoid";
  190. }
  191. /* Helper functions for "exact" transverse mercator */
  192. inline
  193. static T gatg(const T *p1, int len_p1, T B, T cos_2B, T sin_2B)
  194. {
  195. T h = 0, h1, h2 = 0;
  196. const T two_cos_2B = 2*cos_2B;
  197. const T* p = p1 + len_p1;
  198. h1 = *--p;
  199. while (p - p1) {
  200. h = -h2 + two_cos_2B*h1 + *--p;
  201. h2 = h1;
  202. h1 = h;
  203. }
  204. return (B + h*sin_2B);
  205. }
  206. /* Complex Clenshaw summation */
  207. inline
  208. static T clenS(const T *a, int size,
  209. T sin_arg_r, T cos_arg_r,
  210. T sinh_arg_i, T cosh_arg_i,
  211. T *R, T *I)
  212. {
  213. T r, i, hr, hr1, hr2, hi, hi1, hi2;
  214. /* arguments */
  215. const T* p = a + size;
  216. r = 2*cos_arg_r*cosh_arg_i;
  217. i = -2*sin_arg_r*sinh_arg_i;
  218. /* summation loop */
  219. hi1 = hr1 = hi = 0;
  220. hr = *--p;
  221. for (; a - p;) {
  222. hr2 = hr1;
  223. hi2 = hi1;
  224. hr1 = hr;
  225. hi1 = hi;
  226. hr = -hr2 + r*hr1 - i*hi1 + *--p;
  227. hi = -hi2 + i*hr1 + r*hi1;
  228. }
  229. r = sin_arg_r*cosh_arg_i;
  230. i = cos_arg_r*sinh_arg_i;
  231. *R = r*hr - i*hi;
  232. *I = r*hi + i*hr;
  233. return *R;
  234. }
  235. /* Real Clenshaw summation */
  236. static T clens(const T *a, int size, T arg_r)
  237. {
  238. T r, hr, hr1, hr2, cos_arg_r;
  239. const T* p = a + size;
  240. cos_arg_r = cos(arg_r);
  241. r = 2*cos_arg_r;
  242. /* summation loop */
  243. hr1 = 0;
  244. hr = *--p;
  245. for (; a - p;) {
  246. hr2 = hr1;
  247. hr1 = hr;
  248. hr = -hr2 + r*hr1 + *--p;
  249. }
  250. return sin(arg_r)*hr;
  251. }
  252. /* Ellipsoidal, forward */
  253. //static PJ_XY exact_e_fwd (PJ_LP lp, PJ *P)
  254. inline void fwd(Parameters const& /*par*/,
  255. T const& lp_lon,
  256. T const& lp_lat,
  257. T& xy_x, T& xy_y) const
  258. {
  259. //PJ_XY xy = {0.0,0.0};
  260. //const auto *Q = &(static_cast<struct tmerc_data*>(par.opaque)->exact);
  261. /* ell. LAT, LNG -> Gaussian LAT, LNG */
  262. T Cn = gatg (this->m_proj_parm.cbg, proj_etmerc_order, lp_lat,
  263. cos(2*lp_lat), sin(2*lp_lat));
  264. /* Gaussian LAT, LNG -> compl. sph. LAT */
  265. const T sin_Cn = sin (Cn);
  266. const T cos_Cn = cos (Cn);
  267. const T sin_Ce = sin (lp_lon);
  268. const T cos_Ce = cos (lp_lon);
  269. const T cos_Cn_cos_Ce = cos_Cn*cos_Ce;
  270. Cn = atan2 (sin_Cn, cos_Cn_cos_Ce);
  271. const T inv_denom_tan_Ce = 1. / hypot (sin_Cn, cos_Cn_cos_Ce);
  272. const T tan_Ce = sin_Ce*cos_Cn * inv_denom_tan_Ce;
  273. #if 0
  274. // Variant of the above: found not to be measurably faster
  275. const T sin_Ce_cos_Cn = sin_Ce*cos_Cn;
  276. const T denom = sqrt(1 - sin_Ce_cos_Cn * sin_Ce_cos_Cn);
  277. const T tan_Ce = sin_Ce_cos_Cn / denom;
  278. #endif
  279. /* compl. sph. N, E -> ell. norm. N, E */
  280. T Ce = asinh ( tan_Ce ); /* Replaces: Ce = log(tan(FORTPI + Ce*0.5)); */
  281. /*
  282. * Non-optimized version:
  283. * const T sin_arg_r = sin(2*Cn);
  284. * const T cos_arg_r = cos(2*Cn);
  285. *
  286. * Given:
  287. * sin(2 * Cn) = 2 sin(Cn) cos(Cn)
  288. * sin(atan(y)) = y / sqrt(1 + y^2)
  289. * cos(atan(y)) = 1 / sqrt(1 + y^2)
  290. * ==> sin(2 * Cn) = 2 tan_Cn / (1 + tan_Cn^2)
  291. *
  292. * cos(2 * Cn) = 2cos^2(Cn) - 1
  293. * = 2 / (1 + tan_Cn^2) - 1
  294. */
  295. const T two_inv_denom_tan_Ce = 2 * inv_denom_tan_Ce;
  296. const T two_inv_denom_tan_Ce_square = two_inv_denom_tan_Ce * inv_denom_tan_Ce;
  297. const T tmp_r = cos_Cn_cos_Ce * two_inv_denom_tan_Ce_square;
  298. const T sin_arg_r = sin_Cn * tmp_r;
  299. const T cos_arg_r = cos_Cn_cos_Ce * tmp_r - 1;
  300. /*
  301. * Non-optimized version:
  302. * const T sinh_arg_i = sinh(2*Ce);
  303. * const T cosh_arg_i = cosh(2*Ce);
  304. *
  305. * Given
  306. * sinh(2 * Ce) = 2 sinh(Ce) cosh(Ce)
  307. * sinh(asinh(y)) = y
  308. * cosh(asinh(y)) = sqrt(1 + y^2)
  309. * ==> sinh(2 * Ce) = 2 tan_Ce sqrt(1 + tan_Ce^2)
  310. *
  311. * cosh(2 * Ce) = 2cosh^2(Ce) - 1
  312. * = 2 * (1 + tan_Ce^2) - 1
  313. *
  314. * and 1+tan_Ce^2 = 1 + sin_Ce^2 * cos_Cn^2 / (sin_Cn^2 + cos_Cn^2 * cos_Ce^2)
  315. * = (sin_Cn^2 + cos_Cn^2 * cos_Ce^2 + sin_Ce^2 * cos_Cn^2) / (sin_Cn^2 + cos_Cn^2 * cos_Ce^2)
  316. * = 1. / (sin_Cn^2 + cos_Cn^2 * cos_Ce^2)
  317. * = inv_denom_tan_Ce^2
  318. *
  319. */
  320. const T sinh_arg_i = tan_Ce * two_inv_denom_tan_Ce;
  321. const T cosh_arg_i = two_inv_denom_tan_Ce_square - 1;
  322. T dCn, dCe;
  323. Cn += clenS (this->m_proj_parm.gtu, proj_etmerc_order,
  324. sin_arg_r, cos_arg_r, sinh_arg_i, cosh_arg_i,
  325. &dCn, &dCe);
  326. Ce += dCe;
  327. if (fabs (Ce) <= 2.623395162778) {
  328. xy_y = this->m_proj_parm.Qn * Cn + this->m_proj_parm.Zb; /* Northing */
  329. xy_x = this->m_proj_parm.Qn * Ce; /* Easting */
  330. } else {
  331. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  332. xy_x = xy_y = HUGE_VAL;
  333. }
  334. }
  335. /* Ellipsoidal, inverse */
  336. inline void inv(Parameters const& /*par*/,
  337. T const& xy_x,
  338. T const& xy_y,
  339. T& lp_lon,
  340. T& lp_lat) const
  341. {
  342. //PJ_LP lp = {0.0,0.0};
  343. //const auto *Q = &(static_cast<struct tmerc_data*>(par.opaque)->exact);
  344. /* normalize N, E */
  345. T Cn = (xy_y - this->m_proj_parm.Zb)/this->m_proj_parm.Qn;
  346. T Ce = xy_x/this->m_proj_parm.Qn;
  347. if (fabs(Ce) <= 2.623395162778) { /* 150 degrees */
  348. /* norm. N, E -> compl. sph. LAT, LNG */
  349. const T sin_arg_r = sin(2*Cn);
  350. const T cos_arg_r = cos(2*Cn);
  351. //const T sinh_arg_i = sinh(2*Ce);
  352. //const T cosh_arg_i = cosh(2*Ce);
  353. const T exp_2_Ce = exp(2*Ce);
  354. const T half_inv_exp_2_Ce = 0.5 / exp_2_Ce;
  355. const T sinh_arg_i = 0.5 * exp_2_Ce - half_inv_exp_2_Ce;
  356. const T cosh_arg_i = 0.5 * exp_2_Ce + half_inv_exp_2_Ce;
  357. T dCn_ignored, dCe;
  358. Cn += clenS(this->m_proj_parm.utg, proj_etmerc_order,
  359. sin_arg_r, cos_arg_r, sinh_arg_i, cosh_arg_i,
  360. &dCn_ignored, &dCe);
  361. Ce += dCe;
  362. /* compl. sph. LAT -> Gaussian LAT, LNG */
  363. const T sin_Cn = sin (Cn);
  364. const T cos_Cn = cos (Cn);
  365. #if 0
  366. // Non-optimized version:
  367. T sin_Ce, cos_Ce;
  368. Ce = atan (sinh (Ce)); // Replaces: Ce = 2*(atan(exp(Ce)) - FORTPI);
  369. sin_Ce = sin (Ce);
  370. cos_Ce = cos (Ce);
  371. Ce = atan2 (sin_Ce, cos_Ce*cos_Cn);
  372. Cn = atan2 (sin_Cn*cos_Ce, hypot (sin_Ce, cos_Ce*cos_Cn));
  373. #else
  374. /*
  375. * One can divide both member of Ce = atan2(...) by cos_Ce, which gives:
  376. * Ce = atan2 (tan_Ce, cos_Cn) = atan2(sinh(Ce), cos_Cn)
  377. *
  378. * and the same for Cn = atan2(...)
  379. * Cn = atan2 (sin_Cn, hypot (sin_Ce, cos_Ce*cos_Cn)/cos_Ce)
  380. * = atan2 (sin_Cn, hypot (sin_Ce/cos_Ce, cos_Cn))
  381. * = atan2 (sin_Cn, hypot (tan_Ce, cos_Cn))
  382. * = atan2 (sin_Cn, hypot (sinhCe, cos_Cn))
  383. */
  384. const T sinhCe = sinh (Ce);
  385. Ce = atan2 (sinhCe, cos_Cn);
  386. const T modulus_Ce = hypot (sinhCe, cos_Cn);
  387. Cn = atan2 (sin_Cn, modulus_Ce);
  388. #endif
  389. /* Gaussian LAT, LNG -> ell. LAT, LNG */
  390. // Optimization of the computation of cos(2*Cn) and sin(2*Cn)
  391. const T tmp = 2 * modulus_Ce / (sinhCe * sinhCe + 1);
  392. const T sin_2_Cn = sin_Cn * tmp;
  393. const T cos_2_Cn = tmp * modulus_Ce - 1.;
  394. //const T cos_2_Cn = cos(2 * Cn);
  395. //const T sin_2_Cn = sin(2 * Cn);
  396. lp_lat = gatg (this->m_proj_parm.cgb, proj_etmerc_order, Cn, cos_2_Cn, sin_2_Cn);
  397. lp_lon = Ce;
  398. }
  399. else {
  400. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  401. lp_lat = lp_lon = HUGE_VAL;
  402. }
  403. }
  404. };
  405. template <typename T, typename Parameters>
  406. struct base_tmerc_spheroid
  407. {
  408. par_tmerc<T> m_proj_parm;
  409. // FORWARD(s_forward) sphere
  410. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  411. inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  412. {
  413. static const T half_pi = detail::half_pi<T>();
  414. T b, cosphi;
  415. /*
  416. * Fail if our longitude is more than 90 degrees from the
  417. * central meridian since the results are essentially garbage.
  418. * Is error -20 really an appropriate return value?
  419. *
  420. * http://trac.osgeo.org/proj/ticket/5
  421. */
  422. if( lp_lon < -half_pi || lp_lon > half_pi )
  423. {
  424. xy_x = HUGE_VAL;
  425. xy_y = HUGE_VAL;
  426. BOOST_THROW_EXCEPTION( projection_exception(error_lat_or_lon_exceed_limit) );
  427. return;
  428. }
  429. cosphi = cos(lp_lat);
  430. b = cosphi * sin(lp_lon);
  431. if (fabs(fabs(b) - 1.) <= epsilon10)
  432. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  433. xy_x = this->m_proj_parm.ml0 * log((1. + b) / (1. - b));
  434. xy_y = cosphi * cos(lp_lon) / sqrt(1. - b * b);
  435. b = fabs( xy_y );
  436. if (b >= 1.) {
  437. if ((b - 1.) > epsilon10)
  438. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  439. else xy_y = 0.;
  440. } else
  441. xy_y = acos(xy_y);
  442. if (lp_lat < 0.)
  443. xy_y = -xy_y;
  444. xy_y = this->m_proj_parm.esp * (xy_y - par.phi0);
  445. }
  446. // INVERSE(s_inverse) sphere
  447. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  448. inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  449. {
  450. T h, g;
  451. h = exp(xy_x / this->m_proj_parm.esp);
  452. g = .5 * (h - 1. / h);
  453. h = cos(par.phi0 + xy_y / this->m_proj_parm.esp);
  454. lp_lat = asin(sqrt((1. - h * h) / (1. + g * g)));
  455. /* Make sure that phi is on the correct hemisphere when false northing is used */
  456. if (xy_y < 0. && -lp_lat+par.phi0 < 0.0) lp_lat = -lp_lat;
  457. lp_lon = (g != 0.0 || h != 0.0) ? atan2(g, h) : 0.;
  458. }
  459. static inline std::string get_name()
  460. {
  461. return "tmerc_spheroid";
  462. }
  463. };
  464. template <typename Parameters, typename T>
  465. inline void setup(Parameters const& par, par_tmerc<T>& proj_parm)
  466. {
  467. if (par.es != 0.0) {
  468. proj_parm.en = pj_enfn<T>(par.es);
  469. proj_parm.ml0 = pj_mlfn(par.phi0, sin(par.phi0), cos(par.phi0), proj_parm.en);
  470. proj_parm.esp = par.es / (1. - par.es);
  471. } else {
  472. proj_parm.esp = par.k0;
  473. proj_parm.ml0 = .5 * proj_parm.esp;
  474. }
  475. }
  476. template <typename Parameters, typename T>
  477. inline void setup_exact(Parameters const& par, par_tmerc_exact<T>& proj_parm)
  478. {
  479. assert( par.es > 0 );
  480. /* third flattening n */
  481. //since we do not keep n in parameters we compute it here;
  482. const T n = pow(tan(asin(par.e)/2),2);
  483. T np = n;
  484. /* COEF. OF TRIG SERIES GEO <-> GAUSS */
  485. /* cgb := Gaussian -> Geodetic, KW p190 - 191 (61) - (62) */
  486. /* cbg := Geodetic -> Gaussian, KW p186 - 187 (51) - (52) */
  487. /* PROJ_ETMERC_ORDER = 6th degree : Engsager and Poder: ICC2007 */
  488. proj_parm.cgb[0] = n*( 2 + n*(-2/3.0 + n*(-2 + n*(116/45.0 + n*(26/45.0 +
  489. n*(-2854/675.0 ))))));
  490. proj_parm.cbg[0] = n*(-2 + n*( 2/3.0 + n*( 4/3.0 + n*(-82/45.0 + n*(32/45.0 +
  491. n*( 4642/4725.0))))));
  492. np *= n;
  493. proj_parm.cgb[1] = np*(7/3.0 + n*( -8/5.0 + n*(-227/45.0 + n*(2704/315.0 +
  494. n*( 2323/945.0)))));
  495. proj_parm.cbg[1] = np*(5/3.0 + n*(-16/15.0 + n*( -13/9.0 + n*( 904/315.0 +
  496. n*(-1522/945.0)))));
  497. np *= n;
  498. /* n^5 coeff corrected from 1262/105 -> -1262/105 */
  499. proj_parm.cgb[2] = np*( 56/15.0 + n*(-136/35.0 + n*(-1262/105.0 +
  500. n*( 73814/2835.0))));
  501. proj_parm.cbg[2] = np*(-26/15.0 + n*( 34/21.0 + n*( 8/5.0 +
  502. n*(-12686/2835.0))));
  503. np *= n;
  504. /* n^5 coeff corrected from 322/35 -> 332/35 */
  505. proj_parm.cgb[3] = np*(4279/630.0 + n*(-332/35.0 + n*(-399572/14175.0)));
  506. proj_parm.cbg[3] = np*(1237/630.0 + n*( -12/5.0 + n*( -24832/14175.0)));
  507. np *= n;
  508. proj_parm.cgb[4] = np*(4174/315.0 + n*(-144838/6237.0 ));
  509. proj_parm.cbg[4] = np*(-734/315.0 + n*( 109598/31185.0));
  510. np *= n;
  511. proj_parm.cgb[5] = np*(601676/22275.0 );
  512. proj_parm.cbg[5] = np*(444337/155925.0);
  513. /* Constants of the projections */
  514. /* Transverse Mercator (UTM, ITM, etc) */
  515. np = n*n;
  516. /* Norm. mer. quad, K&W p.50 (96), p.19 (38b), p.5 (2) */
  517. proj_parm.Qn = par.k0/(1 + n) * (1 + np*(1/4.0 + np*(1/64.0 + np/256.0)));
  518. /* coef of trig series */
  519. /* utg := ell. N, E -> sph. N, E, KW p194 (65) */
  520. /* gtu := sph. N, E -> ell. N, E, KW p196 (69) */
  521. proj_parm.utg[0] = n*(-0.5 + n*( 2/3.0 + n*(-37/96.0 + n*( 1/360.0 +
  522. n*( 81/512.0 + n*(-96199/604800.0))))));
  523. proj_parm.gtu[0] = n*( 0.5 + n*(-2/3.0 + n*( 5/16.0 + n*(41/180.0 +
  524. n*(-127/288.0 + n*( 7891/37800.0 ))))));
  525. proj_parm.utg[1] = np*(-1/48.0 + n*(-1/15.0 + n*(437/1440.0 + n*(-46/105.0 +
  526. n*( 1118711/3870720.0)))));
  527. proj_parm.gtu[1] = np*(13/48.0 + n*(-3/5.0 + n*(557/1440.0 + n*(281/630.0 +
  528. n*(-1983433/1935360.0)))));
  529. np *= n;
  530. proj_parm.utg[2] = np*(-17/480.0 + n*( 37/840.0 + n*( 209/4480.0 +
  531. n*( -5569/90720.0 ))));
  532. proj_parm.gtu[2] = np*( 61/240.0 + n*(-103/140.0 + n*(15061/26880.0 +
  533. n*(167603/181440.0))));
  534. np *= n;
  535. proj_parm.utg[3] = np*(-4397/161280.0 + n*( 11/504.0 + n*( 830251/7257600.0)));
  536. proj_parm.gtu[3] = np*(49561/161280.0 + n*(-179/168.0 + n*(6601661/7257600.0)));
  537. np *= n;
  538. proj_parm.utg[4] = np*(-4583/161280.0 + n*( 108847/3991680.0));
  539. proj_parm.gtu[4] = np*(34729/80640.0 + n*(-3418889/1995840.0));
  540. np *= n;
  541. proj_parm.utg[5] = np*(-20648693/638668800.0);
  542. proj_parm.gtu[5] = np*(212378941/319334400.0);
  543. /* Gaussian latitude value of the origin latitude */
  544. const T Z = base_tmerc_ellipsoid_exact<T, Parameters>::gatg (proj_parm.cbg, proj_etmerc_order, par.phi0, cos(2*par.phi0), sin(2*par.phi0));
  545. /* Origin northing minus true northing at the origin latitude */
  546. /* i.e. true northing = N - par.Zb */
  547. proj_parm.Zb = - proj_parm.Qn*(Z + base_tmerc_ellipsoid_exact<T, Parameters>::clens(proj_parm.gtu, proj_etmerc_order, 2*Z));
  548. }
  549. }} // namespace detail::tmerc
  550. #endif // doxygen
  551. /*!
  552. \brief Transverse Mercator projection
  553. \ingroup projections
  554. \tparam Geographic latlong point type
  555. \tparam Cartesian xy point type
  556. \tparam Parameters parameter type
  557. \par Projection characteristics
  558. - Cylindrical
  559. - Spheroid
  560. - Ellipsoid
  561. \par Example
  562. \image html ex_tmerc.gif
  563. */
  564. //approximate tmerc algorithm
  565. /*
  566. template <typename T, typename Parameters>
  567. struct tmerc_ellipsoid : public detail::tmerc::base_tmerc_ellipsoid<T, Parameters>
  568. {
  569. template <typename Params>
  570. inline tmerc_ellipsoid(Params const&, Parameters const& par)
  571. {
  572. detail::tmerc::setup(par, this->m_proj_parm);
  573. }
  574. };
  575. */
  576. template <typename T, typename Parameters>
  577. struct tmerc_ellipsoid : public detail::tmerc::base_tmerc_ellipsoid_exact<T, Parameters>
  578. {
  579. template <typename Params>
  580. inline tmerc_ellipsoid(Params const&, Parameters const& par)
  581. {
  582. detail::tmerc::setup_exact(par, this->m_proj_parm);
  583. }
  584. };
  585. /*!
  586. \brief Transverse Mercator projection
  587. \ingroup projections
  588. \tparam Geographic latlong point type
  589. \tparam Cartesian xy point type
  590. \tparam Parameters parameter type
  591. \par Projection characteristics
  592. - Cylindrical
  593. - Spheroid
  594. - Ellipsoid
  595. \par Example
  596. \image html ex_tmerc.gif
  597. */
  598. template <typename T, typename Parameters>
  599. struct tmerc_spheroid : public detail::tmerc::base_tmerc_spheroid<T, Parameters>
  600. {
  601. template <typename Params>
  602. inline tmerc_spheroid(Params const&, Parameters const& par)
  603. {
  604. detail::tmerc::setup(par, this->m_proj_parm);
  605. }
  606. };
  607. #ifndef DOXYGEN_NO_DETAIL
  608. namespace detail
  609. {
  610. // Static projection
  611. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_tmerc, tmerc_spheroid, tmerc_ellipsoid)
  612. // Factory entry(s) - dynamic projection
  613. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(tmerc_entry, tmerc_spheroid, tmerc_ellipsoid)
  614. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(tmerc_init)
  615. {
  616. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(tmerc, tmerc_entry)
  617. }
  618. } // namespace detail
  619. #endif // doxygen
  620. } // namespace projections
  621. }} // namespace boost::geometry
  622. #endif // BOOST_GEOMETRY_PROJECTIONS_TMERC_HPP