tmerc.hpp 14 KB

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