moll.hpp 9.2 KB

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