moll.hpp 9.7 KB

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