tmerc.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369
  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 projections
  42. {
  43. #ifndef DOXYGEN_NO_DETAIL
  44. namespace detail { namespace tmerc
  45. {
  46. static const double epsilon10 = 1.e-10;
  47. template <typename T>
  48. inline T FC1() { return 1.; }
  49. template <typename T>
  50. inline T FC2() { return .5; }
  51. template <typename T>
  52. inline T FC3() { return .16666666666666666666666666666666666666; }
  53. template <typename T>
  54. inline T FC4() { return .08333333333333333333333333333333333333; }
  55. template <typename T>
  56. inline T FC5() { return .05; }
  57. template <typename T>
  58. inline T FC6() { return .03333333333333333333333333333333333333; }
  59. template <typename T>
  60. inline T FC7() { return .02380952380952380952380952380952380952; }
  61. template <typename T>
  62. inline T FC8() { return .01785714285714285714285714285714285714; }
  63. template <typename T>
  64. struct par_tmerc
  65. {
  66. T esp;
  67. T ml0;
  68. detail::en<T> en;
  69. };
  70. // template class, using CRTP to implement forward/inverse
  71. template <typename T, typename Parameters>
  72. struct base_tmerc_ellipsoid
  73. : public base_t_fi<base_tmerc_ellipsoid<T, Parameters>, T, Parameters>
  74. {
  75. par_tmerc<T> m_proj_parm;
  76. inline base_tmerc_ellipsoid(const Parameters& par)
  77. : base_t_fi<base_tmerc_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  78. {}
  79. // FORWARD(e_forward) ellipse
  80. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  81. inline void fwd(T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  82. {
  83. static const T half_pi = detail::half_pi<T>();
  84. static const T FC1 = tmerc::FC1<T>();
  85. static const T FC2 = tmerc::FC2<T>();
  86. static const T FC3 = tmerc::FC3<T>();
  87. static const T FC4 = tmerc::FC4<T>();
  88. static const T FC5 = tmerc::FC5<T>();
  89. static const T FC6 = tmerc::FC6<T>();
  90. static const T FC7 = tmerc::FC7<T>();
  91. static const T FC8 = tmerc::FC8<T>();
  92. T al, als, n, cosphi, sinphi, t;
  93. /*
  94. * Fail if our longitude is more than 90 degrees from the
  95. * central meridian since the results are essentially garbage.
  96. * Is error -20 really an appropriate return value?
  97. *
  98. * http://trac.osgeo.org/proj/ticket/5
  99. */
  100. if( lp_lon < -half_pi || lp_lon > half_pi )
  101. {
  102. xy_x = HUGE_VAL;
  103. xy_y = HUGE_VAL;
  104. BOOST_THROW_EXCEPTION( projection_exception(error_lat_or_lon_exceed_limit) );
  105. return;
  106. }
  107. sinphi = sin(lp_lat);
  108. cosphi = cos(lp_lat);
  109. t = fabs(cosphi) > 1e-10 ? sinphi/cosphi : 0.;
  110. t *= t;
  111. al = cosphi * lp_lon;
  112. als = al * al;
  113. al /= sqrt(1. - this->m_par.es * sinphi * sinphi);
  114. n = this->m_proj_parm.esp * cosphi * cosphi;
  115. xy_x = this->m_par.k0 * al * (FC1 +
  116. FC3 * als * (1. - t + n +
  117. FC5 * als * (5. + t * (t - 18.) + n * (14. - 58. * t)
  118. + FC7 * als * (61. + t * ( t * (179. - t) - 479. ) )
  119. )));
  120. xy_y = this->m_par.k0 * (pj_mlfn(lp_lat, sinphi, cosphi, this->m_proj_parm.en) - this->m_proj_parm.ml0 +
  121. sinphi * al * lp_lon * FC2 * ( 1. +
  122. FC4 * als * (5. - t + n * (9. + 4. * n) +
  123. FC6 * als * (61. + t * (t - 58.) + n * (270. - 330 * t)
  124. + FC8 * als * (1385. + t * ( t * (543. - t) - 3111.) )
  125. ))));
  126. }
  127. // INVERSE(e_inverse) ellipsoid
  128. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  129. inline void inv(T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  130. {
  131. static const T half_pi = detail::half_pi<T>();
  132. static const T FC1 = tmerc::FC1<T>();
  133. static const T FC2 = tmerc::FC2<T>();
  134. static const T FC3 = tmerc::FC3<T>();
  135. static const T FC4 = tmerc::FC4<T>();
  136. static const T FC5 = tmerc::FC5<T>();
  137. static const T FC6 = tmerc::FC6<T>();
  138. static const T FC7 = tmerc::FC7<T>();
  139. static const T FC8 = tmerc::FC8<T>();
  140. T n, con, cosphi, d, ds, sinphi, t;
  141. 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);
  142. if (fabs(lp_lat) >= half_pi) {
  143. lp_lat = xy_y < 0. ? -half_pi : half_pi;
  144. lp_lon = 0.;
  145. } else {
  146. sinphi = sin(lp_lat);
  147. cosphi = cos(lp_lat);
  148. t = fabs(cosphi) > 1e-10 ? sinphi/cosphi : 0.;
  149. n = this->m_proj_parm.esp * cosphi * cosphi;
  150. d = xy_x * sqrt(con = 1. - this->m_par.es * sinphi * sinphi) / this->m_par.k0;
  151. con *= t;
  152. t *= t;
  153. ds = d * d;
  154. lp_lat -= (con * ds / (1.-this->m_par.es)) * FC2 * (1. -
  155. ds * FC4 * (5. + t * (3. - 9. * n) + n * (1. - 4 * n) -
  156. ds * FC6 * (61. + t * (90. - 252. * n +
  157. 45. * t) + 46. * n
  158. - ds * FC8 * (1385. + t * (3633. + t * (4095. + 1574. * t)) )
  159. )));
  160. lp_lon = d*(FC1 -
  161. ds*FC3*( 1. + 2.*t + n -
  162. ds*FC5*(5. + t*(28. + 24.*t + 8.*n) + 6.*n
  163. - ds * FC7 * (61. + t * (662. + t * (1320. + 720. * t)) )
  164. ))) / cosphi;
  165. }
  166. }
  167. static inline std::string get_name()
  168. {
  169. return "tmerc_ellipsoid";
  170. }
  171. };
  172. // template class, using CRTP to implement forward/inverse
  173. template <typename T, typename Parameters>
  174. struct base_tmerc_spheroid
  175. : public base_t_fi<base_tmerc_spheroid<T, Parameters>, T, Parameters>
  176. {
  177. par_tmerc<T> m_proj_parm;
  178. inline base_tmerc_spheroid(const Parameters& par)
  179. : base_t_fi<base_tmerc_spheroid<T, Parameters>, T, Parameters>(*this, par)
  180. {}
  181. // FORWARD(s_forward) sphere
  182. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  183. inline void fwd(T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  184. {
  185. static const T half_pi = detail::half_pi<T>();
  186. T b, cosphi;
  187. /*
  188. * Fail if our longitude is more than 90 degrees from the
  189. * central meridian since the results are essentially garbage.
  190. * Is error -20 really an appropriate return value?
  191. *
  192. * http://trac.osgeo.org/proj/ticket/5
  193. */
  194. if( lp_lon < -half_pi || lp_lon > half_pi )
  195. {
  196. xy_x = HUGE_VAL;
  197. xy_y = HUGE_VAL;
  198. BOOST_THROW_EXCEPTION( projection_exception(error_lat_or_lon_exceed_limit) );
  199. return;
  200. }
  201. cosphi = cos(lp_lat);
  202. b = cosphi * sin(lp_lon);
  203. if (fabs(fabs(b) - 1.) <= epsilon10)
  204. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  205. xy_x = this->m_proj_parm.ml0 * log((1. + b) / (1. - b));
  206. xy_y = cosphi * cos(lp_lon) / sqrt(1. - b * b);
  207. b = fabs( xy_y );
  208. if (b >= 1.) {
  209. if ((b - 1.) > epsilon10)
  210. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  211. else xy_y = 0.;
  212. } else
  213. xy_y = acos(xy_y);
  214. if (lp_lat < 0.)
  215. xy_y = -xy_y;
  216. xy_y = this->m_proj_parm.esp * (xy_y - this->m_par.phi0);
  217. }
  218. // INVERSE(s_inverse) sphere
  219. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  220. inline void inv(T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  221. {
  222. T h, g;
  223. h = exp(xy_x / this->m_proj_parm.esp);
  224. g = .5 * (h - 1. / h);
  225. h = cos(this->m_par.phi0 + xy_y / this->m_proj_parm.esp);
  226. lp_lat = asin(sqrt((1. - h * h) / (1. + g * g)));
  227. /* Make sure that phi is on the correct hemisphere when false northing is used */
  228. if (xy_y < 0. && -lp_lat+this->m_par.phi0 < 0.0) lp_lat = -lp_lat;
  229. lp_lon = (g != 0.0 || h != 0.0) ? atan2(g, h) : 0.;
  230. }
  231. static inline std::string get_name()
  232. {
  233. return "tmerc_spheroid";
  234. }
  235. };
  236. template <typename Parameters, typename T>
  237. inline void setup(Parameters& par, par_tmerc<T>& proj_parm)
  238. {
  239. if (par.es != 0.0) {
  240. proj_parm.en = pj_enfn<T>(par.es);
  241. proj_parm.ml0 = pj_mlfn(par.phi0, sin(par.phi0), cos(par.phi0), proj_parm.en);
  242. proj_parm.esp = par.es / (1. - par.es);
  243. } else {
  244. proj_parm.esp = par.k0;
  245. proj_parm.ml0 = .5 * proj_parm.esp;
  246. }
  247. }
  248. }} // namespace detail::tmerc
  249. #endif // doxygen
  250. /*!
  251. \brief Transverse Mercator projection
  252. \ingroup projections
  253. \tparam Geographic latlong point type
  254. \tparam Cartesian xy point type
  255. \tparam Parameters parameter type
  256. \par Projection characteristics
  257. - Cylindrical
  258. - Spheroid
  259. - Ellipsoid
  260. \par Example
  261. \image html ex_tmerc.gif
  262. */
  263. template <typename T, typename Parameters>
  264. struct tmerc_ellipsoid : public detail::tmerc::base_tmerc_ellipsoid<T, Parameters>
  265. {
  266. template <typename Params>
  267. inline tmerc_ellipsoid(Params const&, Parameters const& par)
  268. : detail::tmerc::base_tmerc_ellipsoid<T, Parameters>(par)
  269. {
  270. detail::tmerc::setup(this->m_par, this->m_proj_parm);
  271. }
  272. };
  273. /*!
  274. \brief Transverse Mercator projection
  275. \ingroup projections
  276. \tparam Geographic latlong point type
  277. \tparam Cartesian xy point type
  278. \tparam Parameters parameter type
  279. \par Projection characteristics
  280. - Cylindrical
  281. - Spheroid
  282. - Ellipsoid
  283. \par Example
  284. \image html ex_tmerc.gif
  285. */
  286. template <typename T, typename Parameters>
  287. struct tmerc_spheroid : public detail::tmerc::base_tmerc_spheroid<T, Parameters>
  288. {
  289. template <typename Params>
  290. inline tmerc_spheroid(Params const&, Parameters const& par)
  291. : 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::spar::proj_tmerc, tmerc_spheroid, tmerc_ellipsoid)
  301. // Factory entry(s) - dynamic projection
  302. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(tmerc_entry, tmerc_spheroid, tmerc_ellipsoid)
  303. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(tmerc_init)
  304. {
  305. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(tmerc, tmerc_entry)
  306. }
  307. } // namespace detail
  308. #endif // doxygen
  309. } // namespace projections
  310. }} // namespace boost::geometry
  311. #endif // BOOST_GEOMETRY_PROJECTIONS_TMERC_HPP