tmerc.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. // Boost.Geometry - gis-projections (based on PROJ4)
  2. // Copyright (c) 2008-2015 Barend Gehrels, Amsterdam, the Netherlands.
  3. // This file was modified by Oracle on 2017, 2018.
  4. // Modifications copyright (c) 2017-2018, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle.
  6. // Use, modification and distribution is subject to the Boost Software License,
  7. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  8. // http://www.boost.org/LICENSE_1_0.txt)
  9. // This file is converted from PROJ4, http://trac.osgeo.org/proj
  10. // PROJ4 is originally written by Gerald Evenden (then of the USGS)
  11. // PROJ4 is maintained by Frank Warmerdam
  12. // PROJ4 is converted to Boost.Geometry by Barend Gehrels
  13. // Last updated version of proj: 5.0.0
  14. // Original copyright notice:
  15. // Permission is hereby granted, free of charge, to any person obtaining a
  16. // copy of this software and associated documentation files (the "Software"),
  17. // to deal in the Software without restriction, including without limitation
  18. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  19. // and/or sell copies of the Software, and to permit persons to whom the
  20. // Software is furnished to do so, subject to the following conditions:
  21. // The above copyright notice and this permission notice shall be included
  22. // in all copies or substantial portions of the Software.
  23. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  24. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  25. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  26. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  27. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  28. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  29. // DEALINGS IN THE SOFTWARE.
  30. #ifndef BOOST_GEOMETRY_PROJECTIONS_TMERC_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_TMERC_HPP
  32. #include <boost/geometry/util/math.hpp>
  33. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  34. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  35. #include <boost/geometry/srs/projections/impl/projects.hpp>
  36. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  37. #include <boost/geometry/srs/projections/impl/function_overloads.hpp>
  38. #include <boost/geometry/srs/projections/impl/pj_mlfn.hpp>
  39. namespace boost { namespace geometry
  40. {
  41. namespace srs { namespace par4
  42. {
  43. struct tmerc {}; // Transverse Mercator
  44. }} //namespace srs::par4
  45. namespace projections
  46. {
  47. #ifndef DOXYGEN_NO_DETAIL
  48. namespace detail { namespace tmerc
  49. {
  50. static const double epsilon10 = 1.e-10;
  51. template <typename T>
  52. inline T FC1() { return 1.; }
  53. template <typename T>
  54. inline T FC2() { return .5; }
  55. template <typename T>
  56. inline T FC3() { return .16666666666666666666666666666666666666; }
  57. template <typename T>
  58. inline T FC4() { return .08333333333333333333333333333333333333; }
  59. template <typename T>
  60. inline T FC5() { return .05; }
  61. template <typename T>
  62. inline T FC6() { return .03333333333333333333333333333333333333; }
  63. template <typename T>
  64. inline T FC7() { return .02380952380952380952380952380952380952; }
  65. template <typename T>
  66. inline T FC8() { return .01785714285714285714285714285714285714; }
  67. template <typename T>
  68. struct par_tmerc
  69. {
  70. T esp;
  71. T ml0;
  72. detail::en<T> en;
  73. };
  74. // template class, using CRTP to implement forward/inverse
  75. template <typename T, typename Parameters>
  76. struct base_tmerc_ellipsoid
  77. : public base_t_fi<base_tmerc_ellipsoid<T, Parameters>, T, Parameters>
  78. {
  79. par_tmerc<T> m_proj_parm;
  80. inline base_tmerc_ellipsoid(const Parameters& par)
  81. : base_t_fi<base_tmerc_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  82. {}
  83. // FORWARD(e_forward) ellipse
  84. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  85. inline void fwd(T& lp_lon, T& lp_lat, T& xy_x, T& xy_y) const
  86. {
  87. static const T half_pi = detail::half_pi<T>();
  88. static const T FC1 = tmerc::FC1<T>();
  89. static const T FC2 = tmerc::FC2<T>();
  90. static const T FC3 = tmerc::FC3<T>();
  91. static const T FC4 = tmerc::FC4<T>();
  92. static const T FC5 = tmerc::FC5<T>();
  93. static const T FC6 = tmerc::FC6<T>();
  94. static const T FC7 = tmerc::FC7<T>();
  95. static const T FC8 = tmerc::FC8<T>();
  96. T al, als, n, cosphi, sinphi, t;
  97. /*
  98. * Fail if our longitude is more than 90 degrees from the
  99. * central meridian since the results are essentially garbage.
  100. * Is error -20 really an appropriate return value?
  101. *
  102. * http://trac.osgeo.org/proj/ticket/5
  103. */
  104. if( lp_lon < -half_pi || lp_lon > half_pi )
  105. {
  106. xy_x = HUGE_VAL;
  107. xy_y = HUGE_VAL;
  108. BOOST_THROW_EXCEPTION( projection_exception(error_lat_or_lon_exceed_limit) );
  109. return;
  110. }
  111. sinphi = sin(lp_lat);
  112. cosphi = cos(lp_lat);
  113. t = fabs(cosphi) > 1e-10 ? sinphi/cosphi : 0.;
  114. t *= t;
  115. al = cosphi * lp_lon;
  116. als = al * al;
  117. al /= sqrt(1. - this->m_par.es * sinphi * sinphi);
  118. n = this->m_proj_parm.esp * cosphi * cosphi;
  119. xy_x = this->m_par.k0 * al * (FC1 +
  120. FC3 * als * (1. - t + n +
  121. FC5 * als * (5. + t * (t - 18.) + n * (14. - 58. * t)
  122. + FC7 * als * (61. + t * ( t * (179. - t) - 479. ) )
  123. )));
  124. xy_y = this->m_par.k0 * (pj_mlfn(lp_lat, sinphi, cosphi, this->m_proj_parm.en) - this->m_proj_parm.ml0 +
  125. sinphi * al * lp_lon * FC2 * ( 1. +
  126. FC4 * als * (5. - t + n * (9. + 4. * n) +
  127. FC6 * als * (61. + t * (t - 58.) + n * (270. - 330 * t)
  128. + FC8 * als * (1385. + t * ( t * (543. - t) - 3111.) )
  129. ))));
  130. }
  131. // INVERSE(e_inverse) ellipsoid
  132. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  133. inline void inv(T& xy_x, T& xy_y, T& lp_lon, T& lp_lat) const
  134. {
  135. static const T half_pi = detail::half_pi<T>();
  136. static const T FC1 = tmerc::FC1<T>();
  137. static const T FC2 = tmerc::FC2<T>();
  138. static const T FC3 = tmerc::FC3<T>();
  139. static const T FC4 = tmerc::FC4<T>();
  140. static const T FC5 = tmerc::FC5<T>();
  141. static const T FC6 = tmerc::FC6<T>();
  142. static const T FC7 = tmerc::FC7<T>();
  143. static const T FC8 = tmerc::FC8<T>();
  144. T n, con, cosphi, d, ds, sinphi, t;
  145. lp_lat = pj_inv_mlfn(this->m_proj_parm.ml0 + xy_y / this->m_par.k0, this->m_par.es, this->m_proj_parm.en);
  146. if (fabs(lp_lat) >= half_pi) {
  147. lp_lat = xy_y < 0. ? -half_pi : half_pi;
  148. lp_lon = 0.;
  149. } else {
  150. sinphi = sin(lp_lat);
  151. cosphi = cos(lp_lat);
  152. t = fabs(cosphi) > 1e-10 ? sinphi/cosphi : 0.;
  153. n = this->m_proj_parm.esp * cosphi * cosphi;
  154. d = xy_x * sqrt(con = 1. - this->m_par.es * sinphi * sinphi) / this->m_par.k0;
  155. con *= t;
  156. t *= t;
  157. ds = d * d;
  158. lp_lat -= (con * ds / (1.-this->m_par.es)) * FC2 * (1. -
  159. ds * FC4 * (5. + t * (3. - 9. * n) + n * (1. - 4 * n) -
  160. ds * FC6 * (61. + t * (90. - 252. * n +
  161. 45. * t) + 46. * n
  162. - ds * FC8 * (1385. + t * (3633. + t * (4095. + 1574. * t)) )
  163. )));
  164. lp_lon = d*(FC1 -
  165. ds*FC3*( 1. + 2.*t + n -
  166. ds*FC5*(5. + t*(28. + 24.*t + 8.*n) + 6.*n
  167. - ds * FC7 * (61. + t * (662. + t * (1320. + 720. * t)) )
  168. ))) / cosphi;
  169. }
  170. }
  171. static inline std::string get_name()
  172. {
  173. return "tmerc_ellipsoid";
  174. }
  175. };
  176. // template class, using CRTP to implement forward/inverse
  177. template <typename T, typename Parameters>
  178. struct base_tmerc_spheroid
  179. : public base_t_fi<base_tmerc_spheroid<T, Parameters>, T, Parameters>
  180. {
  181. par_tmerc<T> m_proj_parm;
  182. inline base_tmerc_spheroid(const Parameters& par)
  183. : base_t_fi<base_tmerc_spheroid<T, Parameters>, T, Parameters>(*this, par)
  184. {}
  185. // FORWARD(s_forward) sphere
  186. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  187. inline void fwd(T& lp_lon, T& lp_lat, T& xy_x, T& xy_y) const
  188. {
  189. static const T half_pi = detail::half_pi<T>();
  190. T b, cosphi;
  191. /*
  192. * Fail if our longitude is more than 90 degrees from the
  193. * central meridian since the results are essentially garbage.
  194. * Is error -20 really an appropriate return value?
  195. *
  196. * http://trac.osgeo.org/proj/ticket/5
  197. */
  198. if( lp_lon < -half_pi || lp_lon > half_pi )
  199. {
  200. xy_x = HUGE_VAL;
  201. xy_y = HUGE_VAL;
  202. BOOST_THROW_EXCEPTION( projection_exception(error_lat_or_lon_exceed_limit) );
  203. return;
  204. }
  205. cosphi = cos(lp_lat);
  206. b = cosphi * sin(lp_lon);
  207. if (fabs(fabs(b) - 1.) <= epsilon10)
  208. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  209. xy_x = this->m_proj_parm.ml0 * log((1. + b) / (1. - b));
  210. xy_y = cosphi * cos(lp_lon) / sqrt(1. - b * b);
  211. b = fabs( xy_y );
  212. if (b >= 1.) {
  213. if ((b - 1.) > epsilon10)
  214. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  215. else xy_y = 0.;
  216. } else
  217. xy_y = acos(xy_y);
  218. if (lp_lat < 0.)
  219. xy_y = -xy_y;
  220. xy_y = this->m_proj_parm.esp * (xy_y - this->m_par.phi0);
  221. }
  222. // INVERSE(s_inverse) sphere
  223. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  224. inline void inv(T& xy_x, T& xy_y, T& lp_lon, T& lp_lat) const
  225. {
  226. T h, g;
  227. h = exp(xy_x / this->m_proj_parm.esp);
  228. g = .5 * (h - 1. / h);
  229. h = cos(this->m_par.phi0 + xy_y / this->m_proj_parm.esp);
  230. lp_lat = asin(sqrt((1. - h * h) / (1. + g * g)));
  231. /* Make sure that phi is on the correct hemisphere when false northing is used */
  232. if (xy_y < 0. && -lp_lat+this->m_par.phi0 < 0.0) lp_lat = -lp_lat;
  233. lp_lon = (g != 0.0 || h != 0.0) ? atan2(g, h) : 0.;
  234. }
  235. static inline std::string get_name()
  236. {
  237. return "tmerc_spheroid";
  238. }
  239. };
  240. template <typename Parameters, typename T>
  241. inline void setup(Parameters& par, par_tmerc<T>& proj_parm)
  242. {
  243. if (par.es != 0.0) {
  244. proj_parm.en = pj_enfn<T>(par.es);
  245. proj_parm.ml0 = pj_mlfn(par.phi0, sin(par.phi0), cos(par.phi0), proj_parm.en);
  246. proj_parm.esp = par.es / (1. - par.es);
  247. } else {
  248. proj_parm.esp = par.k0;
  249. proj_parm.ml0 = .5 * proj_parm.esp;
  250. }
  251. }
  252. }} // namespace detail::tmerc
  253. #endif // doxygen
  254. /*!
  255. \brief Transverse Mercator projection
  256. \ingroup projections
  257. \tparam Geographic latlong point type
  258. \tparam Cartesian xy point type
  259. \tparam Parameters parameter type
  260. \par Projection characteristics
  261. - Cylindrical
  262. - Spheroid
  263. - Ellipsoid
  264. \par Example
  265. \image html ex_tmerc.gif
  266. */
  267. template <typename T, typename Parameters>
  268. struct tmerc_ellipsoid : public detail::tmerc::base_tmerc_ellipsoid<T, Parameters>
  269. {
  270. inline tmerc_ellipsoid(const Parameters& par) : detail::tmerc::base_tmerc_ellipsoid<T, Parameters>(par)
  271. {
  272. detail::tmerc::setup(this->m_par, this->m_proj_parm);
  273. }
  274. };
  275. /*!
  276. \brief Transverse Mercator projection
  277. \ingroup projections
  278. \tparam Geographic latlong point type
  279. \tparam Cartesian xy point type
  280. \tparam Parameters parameter type
  281. \par Projection characteristics
  282. - Cylindrical
  283. - Spheroid
  284. - Ellipsoid
  285. \par Example
  286. \image html ex_tmerc.gif
  287. */
  288. template <typename T, typename Parameters>
  289. struct tmerc_spheroid : public detail::tmerc::base_tmerc_spheroid<T, Parameters>
  290. {
  291. inline tmerc_spheroid(const Parameters& par) : detail::tmerc::base_tmerc_spheroid<T, Parameters>(par)
  292. {
  293. detail::tmerc::setup(this->m_par, this->m_proj_parm);
  294. }
  295. };
  296. #ifndef DOXYGEN_NO_DETAIL
  297. namespace detail
  298. {
  299. // Static projection
  300. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::tmerc, tmerc_spheroid, tmerc_ellipsoid)
  301. // Factory entry(s) - dynamic projection
  302. template <typename T, typename Parameters>
  303. class tmerc_entry : public detail::factory_entry<T, Parameters>
  304. {
  305. public :
  306. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  307. {
  308. if (par.es)
  309. return new base_v_fi<tmerc_ellipsoid<T, Parameters>, T, Parameters>(par);
  310. else
  311. return new base_v_fi<tmerc_spheroid<T, Parameters>, T, Parameters>(par);
  312. }
  313. };
  314. template <typename T, typename Parameters>
  315. inline void tmerc_init(detail::base_factory<T, Parameters>& factory)
  316. {
  317. factory.add_to_factory("tmerc", new tmerc_entry<T, Parameters>);
  318. }
  319. } // namespace detail
  320. #endif // doxygen
  321. } // namespace projections
  322. }} // namespace boost::geometry
  323. #endif // BOOST_GEOMETRY_PROJECTIONS_TMERC_HPP