aitoff.hpp 13 KB

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