aitoff.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. // Purpose: Implementation of the aitoff (Aitoff) and wintri (Winkel Tripel)
  16. // projections.
  17. // Author: Gerald Evenden (1995)
  18. // Drazen Tutic, Lovro Gradiser (2015) - add inverse
  19. // Thomas Knudsen (2016) - revise/add regression tests
  20. // Copyright (c) 1995, Gerald Evenden
  21. // Permission is hereby granted, free of charge, to any person obtaining a
  22. // copy of this software and associated documentation files (the "Software"),
  23. // to deal in the Software without restriction, including without limitation
  24. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  25. // and/or sell copies of the Software, and to permit persons to whom the
  26. // Software is furnished to do so, subject to the following conditions:
  27. // The above copyright notice and this permission notice shall be included
  28. // in all copies or substantial portions of the Software.
  29. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  30. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  31. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  32. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  33. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  34. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  35. // DEALINGS IN THE SOFTWARE.
  36. #ifndef BOOST_GEOMETRY_PROJECTIONS_AITOFF_HPP
  37. #define BOOST_GEOMETRY_PROJECTIONS_AITOFF_HPP
  38. #include <boost/core/ignore_unused.hpp>
  39. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  40. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  41. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  42. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  43. #include <boost/geometry/srs/projections/impl/projects.hpp>
  44. #include <boost/geometry/util/math.hpp>
  45. namespace boost { namespace geometry
  46. {
  47. namespace projections
  48. {
  49. #ifndef DOXYGEN_NO_DETAIL
  50. namespace detail { namespace aitoff
  51. {
  52. enum mode_type {
  53. mode_aitoff = 0,
  54. mode_winkel_tripel = 1
  55. };
  56. template <typename T>
  57. struct par_aitoff
  58. {
  59. T cosphi1;
  60. mode_type mode;
  61. };
  62. // template class, using CRTP to implement forward/inverse
  63. template <typename T, typename Parameters>
  64. struct base_aitoff_spheroid
  65. : public base_t_fi<base_aitoff_spheroid<T, Parameters>, T, Parameters>
  66. {
  67. par_aitoff<T> m_proj_parm;
  68. inline base_aitoff_spheroid(const Parameters& par)
  69. : base_t_fi<base_aitoff_spheroid<T, Parameters>, T, Parameters>(*this, par)
  70. {}
  71. // FORWARD(s_forward) spheroid
  72. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  73. inline void fwd(T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  74. {
  75. T c, d;
  76. if((d = acos(cos(lp_lat) * cos(c = 0.5 * lp_lon)))) {/* basic Aitoff */
  77. xy_x = 2. * d * cos(lp_lat) * sin(c) * (xy_y = 1. / sin(d));
  78. xy_y *= d * sin(lp_lat);
  79. } else
  80. xy_x = xy_y = 0.;
  81. if (this->m_proj_parm.mode == mode_winkel_tripel) { /* Winkel Tripel */
  82. xy_x = (xy_x + lp_lon * this->m_proj_parm.cosphi1) * 0.5;
  83. xy_y = (xy_y + lp_lat) * 0.5;
  84. }
  85. }
  86. /***********************************************************************************
  87. *
  88. * Inverse functions added by Drazen Tutic and Lovro Gradiser based on paper:
  89. *
  90. * I.Özbug Biklirici and Cengizhan Ipbüker. A General Algorithm for the Inverse
  91. * Transformation of Map Projections Using Jacobian Matrices. In Proceedings of the
  92. * Third International Symposium Mathematical & Computational Applications,
  93. * pages 175{182, Turkey, September 2002.
  94. *
  95. * Expected accuracy is defined by epsilon = 1e-12. Should be appropriate for
  96. * most applications of Aitoff and Winkel Tripel projections.
  97. *
  98. * Longitudes of 180W and 180E can be mixed in solution obtained.
  99. *
  100. * Inverse for Aitoff projection in poles is undefined, longitude value of 0 is assumed.
  101. *
  102. * Contact : dtutic@geof.hr
  103. * Date: 2015-02-16
  104. *
  105. ************************************************************************************/
  106. // INVERSE(s_inverse) sphere
  107. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  108. inline void inv(T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  109. {
  110. static const T pi = detail::pi<T>();
  111. static const T two_pi = detail::two_pi<T>();
  112. static const T epsilon = 1e-12;
  113. int iter, max_iter = 10, round = 0, max_round = 20;
  114. T D, C, f1, f2, f1p, f1l, f2p, f2l, dp, dl, sl, sp, cp, cl, x, y;
  115. if ((fabs(xy_x) < epsilon) && (fabs(xy_y) < epsilon )) {
  116. lp_lat = 0.; lp_lon = 0.;
  117. return;
  118. }
  119. /* intial values for Newton-Raphson method */
  120. lp_lat = xy_y; lp_lon = xy_x;
  121. do {
  122. iter = 0;
  123. do {
  124. sl = sin(lp_lon * 0.5); cl = cos(lp_lon * 0.5);
  125. sp = sin(lp_lat); cp = cos(lp_lat);
  126. D = cp * cl;
  127. C = 1. - D * D;
  128. D = acos(D) / math::pow(C, T(1.5));
  129. f1 = 2. * D * C * cp * sl;
  130. f2 = D * C * sp;
  131. f1p = 2.* (sl * cl * sp * cp / C - D * sp * sl);
  132. f1l = cp * cp * sl * sl / C + D * cp * cl * sp * sp;
  133. f2p = sp * sp * cl / C + D * sl * sl * cp;
  134. f2l = 0.5 * (sp * cp * sl / C - D * sp * cp * cp * sl * cl);
  135. if (this->m_proj_parm.mode == mode_winkel_tripel) { /* Winkel Tripel */
  136. f1 = 0.5 * (f1 + lp_lon * this->m_proj_parm.cosphi1);
  137. f2 = 0.5 * (f2 + lp_lat);
  138. f1p *= 0.5;
  139. f1l = 0.5 * (f1l + this->m_proj_parm.cosphi1);
  140. f2p = 0.5 * (f2p + 1.);
  141. f2l *= 0.5;
  142. }
  143. f1 -= xy_x; f2 -= xy_y;
  144. dl = (f2 * f1p - f1 * f2p) / (dp = f1p * f2l - f2p * f1l);
  145. dp = (f1 * f2l - f2 * f1l) / dp;
  146. dl = fmod(dl, pi); /* set to interval [-M_PI, M_PI] */
  147. lp_lat -= dp; lp_lon -= dl;
  148. } while ((fabs(dp) > epsilon || fabs(dl) > epsilon) && (iter++ < max_iter));
  149. if (lp_lat > two_pi) lp_lat -= 2.*(lp_lat-two_pi); /* correct if symmetrical solution for Aitoff */
  150. if (lp_lat < -two_pi) lp_lat -= 2.*(lp_lat+two_pi); /* correct if symmetrical solution for Aitoff */
  151. if ((fabs(fabs(lp_lat) - two_pi) < epsilon) && (!this->m_proj_parm.mode)) lp_lon = 0.; /* if pole in Aitoff, return longitude of 0 */
  152. /* calculate x,y coordinates with solution obtained */
  153. if((D = acos(cos(lp_lat) * cos(C = 0.5 * lp_lon))) != 0.0) {/* Aitoff */
  154. x = 2. * D * cos(lp_lat) * sin(C) * (y = 1. / sin(D));
  155. y *= D * sin(lp_lat);
  156. } else
  157. x = y = 0.;
  158. if (this->m_proj_parm.mode == mode_winkel_tripel) { /* Winkel Tripel */
  159. x = (x + lp_lon * this->m_proj_parm.cosphi1) * 0.5;
  160. y = (y + lp_lat) * 0.5;
  161. }
  162. /* if too far from given values of x,y, repeat with better approximation of phi,lam */
  163. } while (((fabs(xy_x-x) > epsilon) || (fabs(xy_y-y) > epsilon)) && (round++ < max_round));
  164. if (iter == max_iter && round == max_round)
  165. {
  166. BOOST_THROW_EXCEPTION( projection_exception(error_non_convergent) );
  167. //fprintf(stderr, "Warning: Accuracy of 1e-12 not reached. Last increments: dlat=%e and dlon=%e\n", dp, dl);
  168. }
  169. }
  170. static inline std::string get_name()
  171. {
  172. return "aitoff_spheroid";
  173. }
  174. };
  175. template <typename Parameters>
  176. inline void setup(Parameters& par)
  177. {
  178. par.es = 0.;
  179. }
  180. // Aitoff
  181. template <typename Parameters, typename T>
  182. inline void setup_aitoff(Parameters& par, par_aitoff<T>& proj_parm)
  183. {
  184. proj_parm.mode = mode_aitoff;
  185. setup(par);
  186. }
  187. // Winkel Tripel
  188. template <typename Params, typename Parameters, typename T>
  189. inline void setup_wintri(Params& params, Parameters& par, par_aitoff<T>& proj_parm)
  190. {
  191. static const T two_div_pi = detail::two_div_pi<T>();
  192. T phi1;
  193. proj_parm.mode = mode_winkel_tripel;
  194. if (pj_param_r<srs::spar::lat_1>(params, "lat_1", srs::dpar::lat_1, phi1)) {
  195. if ((proj_parm.cosphi1 = cos(phi1)) == 0.)
  196. BOOST_THROW_EXCEPTION( projection_exception(error_lat_larger_than_90) );
  197. } else /* 50d28' or phi1=acos(2/pi) */
  198. proj_parm.cosphi1 = two_div_pi;
  199. setup(par);
  200. }
  201. }} // namespace detail::aitoff
  202. #endif // doxygen
  203. /*!
  204. \brief Aitoff projection
  205. \ingroup projections
  206. \tparam Geographic latlong point type
  207. \tparam Cartesian xy point type
  208. \tparam Parameters parameter type
  209. \par Projection characteristics
  210. - Miscellaneous
  211. - Spheroid
  212. \par Example
  213. \image html ex_aitoff.gif
  214. */
  215. template <typename T, typename Parameters>
  216. struct aitoff_spheroid : public detail::aitoff::base_aitoff_spheroid<T, Parameters>
  217. {
  218. template <typename Params>
  219. inline aitoff_spheroid(Params const& , Parameters const& par)
  220. : detail::aitoff::base_aitoff_spheroid<T, Parameters>(par)
  221. {
  222. detail::aitoff::setup_aitoff(this->m_par, this->m_proj_parm);
  223. }
  224. };
  225. /*!
  226. \brief Winkel Tripel projection
  227. \ingroup projections
  228. \tparam Geographic latlong point type
  229. \tparam Cartesian xy point type
  230. \tparam Parameters parameter type
  231. \par Projection characteristics
  232. - Miscellaneous
  233. - Spheroid
  234. \par Projection parameters
  235. - lat_1: Latitude of first standard parallel (degrees)
  236. \par Example
  237. \image html ex_wintri.gif
  238. */
  239. template <typename T, typename Parameters>
  240. struct wintri_spheroid : public detail::aitoff::base_aitoff_spheroid<T, Parameters>
  241. {
  242. template <typename Params>
  243. inline wintri_spheroid(Params const& params, Parameters const& par)
  244. : detail::aitoff::base_aitoff_spheroid<T, Parameters>(par)
  245. {
  246. detail::aitoff::setup_wintri(params, this->m_par, this->m_proj_parm);
  247. }
  248. };
  249. #ifndef DOXYGEN_NO_DETAIL
  250. namespace detail
  251. {
  252. // Static projection
  253. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::spar::proj_aitoff, aitoff_spheroid, aitoff_spheroid)
  254. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::spar::proj_wintri, wintri_spheroid, wintri_spheroid)
  255. // Factory entry(s)
  256. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(aitoff_entry, aitoff_spheroid)
  257. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(wintri_entry, wintri_spheroid)
  258. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(aitoff_init)
  259. {
  260. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(aitoff, aitoff_entry)
  261. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(wintri, wintri_entry)
  262. }
  263. } // namespace detail
  264. #endif // doxygen
  265. } // namespace projections
  266. }} // namespace boost::geometry
  267. #endif // BOOST_GEOMETRY_PROJECTIONS_AITOFF_HPP