moll.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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_MOLL_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_MOLL_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/aasincos.hpp>
  38. namespace boost { namespace geometry
  39. {
  40. namespace srs { namespace par4
  41. {
  42. struct moll {}; // Mollweide
  43. struct wag4 {}; // Wagner IV
  44. struct wag5 {}; // Wagner V
  45. }} //namespace srs::par4
  46. namespace projections
  47. {
  48. #ifndef DOXYGEN_NO_DETAIL
  49. namespace detail { namespace moll
  50. {
  51. static const int max_iter = 10;
  52. static const double loop_tol = 1e-7;
  53. template <typename T>
  54. struct par_moll
  55. {
  56. T C_x, C_y, C_p;
  57. };
  58. // template class, using CRTP to implement forward/inverse
  59. template <typename T, typename Parameters>
  60. struct base_moll_spheroid
  61. : public base_t_fi<base_moll_spheroid<T, Parameters>, T, Parameters>
  62. {
  63. par_moll<T> m_proj_parm;
  64. inline base_moll_spheroid(const Parameters& par)
  65. : base_t_fi<base_moll_spheroid<T, Parameters>, T, Parameters>(*this, par)
  66. {}
  67. // FORWARD(s_forward) spheroid
  68. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  69. inline void fwd(T& lp_lon, T& lp_lat, T& xy_x, T& xy_y) const
  70. {
  71. static const T half_pi = detail::half_pi<T>();
  72. T k, V;
  73. int i;
  74. k = this->m_proj_parm.C_p * sin(lp_lat);
  75. for (i = max_iter; i ; --i) {
  76. lp_lat -= V = (lp_lat + sin(lp_lat) - k) /
  77. (1. + cos(lp_lat));
  78. if (fabs(V) < loop_tol)
  79. break;
  80. }
  81. if (!i)
  82. lp_lat = (lp_lat < 0.) ? -half_pi : half_pi;
  83. else
  84. lp_lat *= 0.5;
  85. xy_x = this->m_proj_parm.C_x * lp_lon * cos(lp_lat);
  86. xy_y = this->m_proj_parm.C_y * sin(lp_lat);
  87. }
  88. // INVERSE(s_inverse) spheroid
  89. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  90. inline void inv(T& xy_x, T& xy_y, T& lp_lon, T& lp_lat) const
  91. {
  92. static const T pi = detail::pi<T>();
  93. lp_lat = aasin(xy_y / this->m_proj_parm.C_y);
  94. lp_lon = xy_x / (this->m_proj_parm.C_x * cos(lp_lat));
  95. if (fabs(lp_lon) < pi) {
  96. lp_lat += lp_lat;
  97. lp_lat = aasin((lp_lat + sin(lp_lat)) / this->m_proj_parm.C_p);
  98. } else {
  99. lp_lon = lp_lat = HUGE_VAL;
  100. }
  101. }
  102. static inline std::string get_name()
  103. {
  104. return "moll_spheroid";
  105. }
  106. };
  107. template <typename Parameters, typename T>
  108. inline void setup(Parameters& par, par_moll<T>& proj_parm, T const& p)
  109. {
  110. T r, sp, p2 = p + p;
  111. par.es = 0;
  112. sp = sin(p);
  113. r = sqrt(geometry::math::two_pi<T>() * sp / (p2 + sin(p2)));
  114. proj_parm.C_x = 2. * r / geometry::math::pi<T>();
  115. proj_parm.C_y = r / sp;
  116. proj_parm.C_p = p2 + sin(p2);
  117. }
  118. // Mollweide
  119. template <typename Parameters, typename T>
  120. inline void setup_moll(Parameters& par, par_moll<T>& proj_parm)
  121. {
  122. setup(par, proj_parm, geometry::math::half_pi<T>());
  123. }
  124. // Wagner IV
  125. template <typename Parameters, typename T>
  126. inline void setup_wag4(Parameters& par, par_moll<T>& proj_parm)
  127. {
  128. setup(par, proj_parm, geometry::math::pi<T>()/3.);
  129. }
  130. // Wagner V
  131. template <typename Parameters, typename T>
  132. inline void setup_wag5(Parameters& par, par_moll<T>& proj_parm)
  133. {
  134. par.es = 0;
  135. proj_parm.C_x = 0.90977;
  136. proj_parm.C_y = 1.65014;
  137. proj_parm.C_p = 3.00896;
  138. }
  139. }} // namespace detail::moll
  140. #endif // doxygen
  141. /*!
  142. \brief Mollweide projection
  143. \ingroup projections
  144. \tparam Geographic latlong point type
  145. \tparam Cartesian xy point type
  146. \tparam Parameters parameter type
  147. \par Projection characteristics
  148. - Pseudocylindrical
  149. - Spheroid
  150. \par Example
  151. \image html ex_moll.gif
  152. */
  153. template <typename T, typename Parameters>
  154. struct moll_spheroid : public detail::moll::base_moll_spheroid<T, Parameters>
  155. {
  156. inline moll_spheroid(const Parameters& par) : detail::moll::base_moll_spheroid<T, Parameters>(par)
  157. {
  158. detail::moll::setup_moll(this->m_par, this->m_proj_parm);
  159. }
  160. };
  161. /*!
  162. \brief Wagner IV projection
  163. \ingroup projections
  164. \tparam Geographic latlong point type
  165. \tparam Cartesian xy point type
  166. \tparam Parameters parameter type
  167. \par Projection characteristics
  168. - Pseudocylindrical
  169. - Spheroid
  170. \par Example
  171. \image html ex_wag4.gif
  172. */
  173. template <typename T, typename Parameters>
  174. struct wag4_spheroid : public detail::moll::base_moll_spheroid<T, Parameters>
  175. {
  176. inline wag4_spheroid(const Parameters& par) : detail::moll::base_moll_spheroid<T, Parameters>(par)
  177. {
  178. detail::moll::setup_wag4(this->m_par, this->m_proj_parm);
  179. }
  180. };
  181. /*!
  182. \brief Wagner V projection
  183. \ingroup projections
  184. \tparam Geographic latlong point type
  185. \tparam Cartesian xy point type
  186. \tparam Parameters parameter type
  187. \par Projection characteristics
  188. - Pseudocylindrical
  189. - Spheroid
  190. \par Example
  191. \image html ex_wag5.gif
  192. */
  193. template <typename T, typename Parameters>
  194. struct wag5_spheroid : public detail::moll::base_moll_spheroid<T, Parameters>
  195. {
  196. inline wag5_spheroid(const Parameters& par) : detail::moll::base_moll_spheroid<T, Parameters>(par)
  197. {
  198. detail::moll::setup_wag5(this->m_par, this->m_proj_parm);
  199. }
  200. };
  201. #ifndef DOXYGEN_NO_DETAIL
  202. namespace detail
  203. {
  204. // Static projection
  205. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::moll, moll_spheroid, moll_spheroid)
  206. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::wag4, wag4_spheroid, wag4_spheroid)
  207. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::wag5, wag5_spheroid, wag5_spheroid)
  208. // Factory entry(s)
  209. template <typename T, typename Parameters>
  210. class moll_entry : public detail::factory_entry<T, Parameters>
  211. {
  212. public :
  213. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  214. {
  215. return new base_v_fi<moll_spheroid<T, Parameters>, T, Parameters>(par);
  216. }
  217. };
  218. template <typename T, typename Parameters>
  219. class wag4_entry : public detail::factory_entry<T, Parameters>
  220. {
  221. public :
  222. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  223. {
  224. return new base_v_fi<wag4_spheroid<T, Parameters>, T, Parameters>(par);
  225. }
  226. };
  227. template <typename T, typename Parameters>
  228. class wag5_entry : public detail::factory_entry<T, Parameters>
  229. {
  230. public :
  231. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  232. {
  233. return new base_v_fi<wag5_spheroid<T, Parameters>, T, Parameters>(par);
  234. }
  235. };
  236. template <typename T, typename Parameters>
  237. inline void moll_init(detail::base_factory<T, Parameters>& factory)
  238. {
  239. factory.add_to_factory("moll", new moll_entry<T, Parameters>);
  240. factory.add_to_factory("wag4", new wag4_entry<T, Parameters>);
  241. factory.add_to_factory("wag5", new wag5_entry<T, Parameters>);
  242. }
  243. } // namespace detail
  244. #endif // doxygen
  245. } // namespace projections
  246. }} // namespace boost::geometry
  247. #endif // BOOST_GEOMETRY_PROJECTIONS_MOLL_HPP