aitoff.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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/util/math.hpp>
  40. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  41. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  42. #include <boost/geometry/srs/projections/impl/projects.hpp>
  43. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  44. namespace boost { namespace geometry
  45. {
  46. namespace srs { namespace par4
  47. {
  48. struct aitoff {};
  49. struct wintri {};
  50. }} //namespace srs::par4
  51. namespace projections
  52. {
  53. #ifndef DOXYGEN_NO_DETAIL
  54. namespace detail { namespace aitoff
  55. {
  56. enum mode_type {
  57. mode_aitoff = 0,
  58. mode_winkel_tripel = 1
  59. };
  60. template <typename T>
  61. struct par_aitoff
  62. {
  63. T cosphi1;
  64. mode_type mode;
  65. };
  66. // template class, using CRTP to implement forward/inverse
  67. template <typename T, typename Parameters>
  68. struct base_aitoff_spheroid
  69. : public base_t_fi<base_aitoff_spheroid<T, Parameters>, T, Parameters>
  70. {
  71. par_aitoff<T> m_proj_parm;
  72. inline base_aitoff_spheroid(const Parameters& par)
  73. : base_t_fi<base_aitoff_spheroid<T, Parameters>, T, Parameters>(*this, par)
  74. {}
  75. // FORWARD(s_forward) spheroid
  76. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  77. inline void fwd(T& lp_lon, T& lp_lat, T& xy_x, T& xy_y) const
  78. {
  79. T c, d;
  80. if((d = acos(cos(lp_lat) * cos(c = 0.5 * lp_lon)))) {/* basic Aitoff */
  81. xy_x = 2. * d * cos(lp_lat) * sin(c) * (xy_y = 1. / sin(d));
  82. xy_y *= d * sin(lp_lat);
  83. } else
  84. xy_x = xy_y = 0.;
  85. if (this->m_proj_parm.mode == mode_winkel_tripel) { /* Winkel Tripel */
  86. xy_x = (xy_x + lp_lon * this->m_proj_parm.cosphi1) * 0.5;
  87. xy_y = (xy_y + lp_lat) * 0.5;
  88. }
  89. }
  90. /***********************************************************************************
  91. *
  92. * Inverse functions added by Drazen Tutic and Lovro Gradiser based on paper:
  93. *
  94. * I.Özbug Biklirici and Cengizhan Ipbüker. A General Algorithm for the Inverse
  95. * Transformation of Map Projections Using Jacobian Matrices. In Proceedings of the
  96. * Third International Symposium Mathematical & Computational Applications,
  97. * pages 175{182, Turkey, September 2002.
  98. *
  99. * Expected accuracy is defined by epsilon = 1e-12. Should be appropriate for
  100. * most applications of Aitoff and Winkel Tripel projections.
  101. *
  102. * Longitudes of 180W and 180E can be mixed in solution obtained.
  103. *
  104. * Inverse for Aitoff projection in poles is undefined, longitude value of 0 is assumed.
  105. *
  106. * Contact : dtutic@geof.hr
  107. * Date: 2015-02-16
  108. *
  109. ************************************************************************************/
  110. // INVERSE(s_inverse) sphere
  111. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  112. inline void inv(T& xy_x, T& xy_y, T& lp_lon, T& lp_lat) const
  113. {
  114. static const T pi = detail::pi<T>();
  115. static const T two_pi = detail::two_pi<T>();
  116. static const T epsilon = 1e-12;
  117. int iter, max_iter = 10, round = 0, max_round = 20;
  118. T D, C, f1, f2, f1p, f1l, f2p, f2l, dp, dl, sl, sp, cp, cl, x, y;
  119. if ((fabs(xy_x) < epsilon) && (fabs(xy_y) < epsilon )) {
  120. lp_lat = 0.; lp_lon = 0.;
  121. return;
  122. }
  123. /* intial values for Newton-Raphson method */
  124. lp_lat = xy_y; lp_lon = xy_x;
  125. do {
  126. iter = 0;
  127. do {
  128. sl = sin(lp_lon * 0.5); cl = cos(lp_lon * 0.5);
  129. sp = sin(lp_lat); cp = cos(lp_lat);
  130. D = cp * cl;
  131. C = 1. - D * D;
  132. D = acos(D) / math::pow(C, T(1.5));
  133. f1 = 2. * D * C * cp * sl;
  134. f2 = D * C * sp;
  135. f1p = 2.* (sl * cl * sp * cp / C - D * sp * sl);
  136. f1l = cp * cp * sl * sl / C + D * cp * cl * sp * sp;
  137. f2p = sp * sp * cl / C + D * sl * sl * cp;
  138. f2l = 0.5 * (sp * cp * sl / C - D * sp * cp * cp * sl * cl);
  139. if (this->m_proj_parm.mode == mode_winkel_tripel) { /* Winkel Tripel */
  140. f1 = 0.5 * (f1 + lp_lon * this->m_proj_parm.cosphi1);
  141. f2 = 0.5 * (f2 + lp_lat);
  142. f1p *= 0.5;
  143. f1l = 0.5 * (f1l + this->m_proj_parm.cosphi1);
  144. f2p = 0.5 * (f2p + 1.);
  145. f2l *= 0.5;
  146. }
  147. f1 -= xy_x; f2 -= xy_y;
  148. dl = (f2 * f1p - f1 * f2p) / (dp = f1p * f2l - f2p * f1l);
  149. dp = (f1 * f2l - f2 * f1l) / dp;
  150. dl = fmod(dl, pi); /* set to interval [-M_PI, M_PI] */
  151. lp_lat -= dp; lp_lon -= dl;
  152. } while ((fabs(dp) > epsilon || fabs(dl) > epsilon) && (iter++ < max_iter));
  153. if (lp_lat > two_pi) lp_lat -= 2.*(lp_lat-two_pi); /* correct if symmetrical solution for Aitoff */
  154. if (lp_lat < -two_pi) lp_lat -= 2.*(lp_lat+two_pi); /* correct if symmetrical solution for Aitoff */
  155. if ((fabs(fabs(lp_lat) - two_pi) < epsilon) && (!this->m_proj_parm.mode)) lp_lon = 0.; /* if pole in Aitoff, return longitude of 0 */
  156. /* calculate x,y coordinates with solution obtained */
  157. if((D = acos(cos(lp_lat) * cos(C = 0.5 * lp_lon))) != 0.0) {/* Aitoff */
  158. x = 2. * D * cos(lp_lat) * sin(C) * (y = 1. / sin(D));
  159. y *= D * sin(lp_lat);
  160. } else
  161. x = y = 0.;
  162. if (this->m_proj_parm.mode == mode_winkel_tripel) { /* Winkel Tripel */
  163. x = (x + lp_lon * this->m_proj_parm.cosphi1) * 0.5;
  164. y = (y + lp_lat) * 0.5;
  165. }
  166. /* if too far from given values of x,y, repeat with better approximation of phi,lam */
  167. } while (((fabs(xy_x-x) > epsilon) || (fabs(xy_y-y) > epsilon)) && (round++ < max_round));
  168. if (iter == max_iter && round == max_round)
  169. {
  170. BOOST_THROW_EXCEPTION( projection_exception(error_non_convergent) );
  171. //fprintf(stderr, "Warning: Accuracy of 1e-12 not reached. Last increments: dlat=%e and dlon=%e\n", dp, dl);
  172. }
  173. }
  174. static inline std::string get_name()
  175. {
  176. return "aitoff_spheroid";
  177. }
  178. };
  179. template <typename Parameters>
  180. inline void setup(Parameters& par)
  181. {
  182. par.es = 0.;
  183. }
  184. // Aitoff
  185. template <typename Parameters, typename T>
  186. inline void setup_aitoff(Parameters& par, par_aitoff<T>& proj_parm)
  187. {
  188. proj_parm.mode = mode_aitoff;
  189. setup(par);
  190. }
  191. // Winkel Tripel
  192. template <typename Parameters, typename T>
  193. inline void setup_wintri(Parameters& par, par_aitoff<T>& proj_parm)
  194. {
  195. static const T two_div_pi = detail::two_div_pi<T>();
  196. T phi1;
  197. proj_parm.mode = mode_winkel_tripel;
  198. if (pj_param_r(par.params, "lat_1", phi1)) {
  199. if ((proj_parm.cosphi1 = cos(phi1)) == 0.)
  200. BOOST_THROW_EXCEPTION( projection_exception(error_lat_larger_than_90) );
  201. } else /* 50d28' or phi1=acos(2/pi) */
  202. proj_parm.cosphi1 = two_div_pi;
  203. setup(par);
  204. }
  205. }} // namespace detail::aitoff
  206. #endif // doxygen
  207. /*!
  208. \brief Aitoff projection
  209. \ingroup projections
  210. \tparam Geographic latlong point type
  211. \tparam Cartesian xy point type
  212. \tparam Parameters parameter type
  213. \par Projection characteristics
  214. - Miscellaneous
  215. - Spheroid
  216. \par Example
  217. \image html ex_aitoff.gif
  218. */
  219. template <typename T, typename Parameters>
  220. struct aitoff_spheroid : public detail::aitoff::base_aitoff_spheroid<T, Parameters>
  221. {
  222. inline aitoff_spheroid(const Parameters& par) : detail::aitoff::base_aitoff_spheroid<T, Parameters>(par)
  223. {
  224. detail::aitoff::setup_aitoff(this->m_par, this->m_proj_parm);
  225. }
  226. };
  227. /*!
  228. \brief Winkel Tripel projection
  229. \ingroup projections
  230. \tparam Geographic latlong point type
  231. \tparam Cartesian xy point type
  232. \tparam Parameters parameter type
  233. \par Projection characteristics
  234. - Miscellaneous
  235. - Spheroid
  236. \par Projection parameters
  237. - lat_1: Latitude of first standard parallel (degrees)
  238. \par Example
  239. \image html ex_wintri.gif
  240. */
  241. template <typename T, typename Parameters>
  242. struct wintri_spheroid : public detail::aitoff::base_aitoff_spheroid<T, Parameters>
  243. {
  244. inline wintri_spheroid(const Parameters& par) : detail::aitoff::base_aitoff_spheroid<T, Parameters>(par)
  245. {
  246. detail::aitoff::setup_wintri(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::par4::aitoff, aitoff_spheroid, aitoff_spheroid)
  254. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::wintri, wintri_spheroid, wintri_spheroid)
  255. // Factory entry(s)
  256. template <typename T, typename Parameters>
  257. class aitoff_entry : public detail::factory_entry<T, Parameters>
  258. {
  259. public :
  260. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  261. {
  262. return new base_v_fi<aitoff_spheroid<T, Parameters>, T, Parameters>(par);
  263. }
  264. };
  265. template <typename T, typename Parameters>
  266. class wintri_entry : public detail::factory_entry<T, Parameters>
  267. {
  268. public :
  269. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  270. {
  271. return new base_v_fi<wintri_spheroid<T, Parameters>, T, Parameters>(par);
  272. }
  273. };
  274. template <typename T, typename Parameters>
  275. inline void aitoff_init(detail::base_factory<T, Parameters>& factory)
  276. {
  277. factory.add_to_factory("aitoff", new aitoff_entry<T, Parameters>);
  278. factory.add_to_factory("wintri", new wintri_entry<T, Parameters>);
  279. }
  280. } // namespace detail
  281. #endif // doxygen
  282. } // namespace projections
  283. }} // namespace boost::geometry
  284. #endif // BOOST_GEOMETRY_PROJECTIONS_AITOFF_HPP