gn_sinu.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374
  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_GN_SINU_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_GN_SINU_HPP
  32. #include <boost/geometry/srs/projections/impl/aasincos.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/factory_entry.hpp>
  36. #include <boost/geometry/srs/projections/impl/pj_mlfn.hpp>
  37. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  38. #include <boost/geometry/srs/projections/impl/projects.hpp>
  39. #include <boost/geometry/util/math.hpp>
  40. namespace boost { namespace geometry
  41. {
  42. namespace projections
  43. {
  44. #ifndef DOXYGEN_NO_DETAIL
  45. namespace detail { namespace gn_sinu
  46. {
  47. static const double epsilon10 = 1e-10;
  48. static const int max_iter = 8;
  49. static const double loop_tol = 1e-7;
  50. template <typename T>
  51. struct par_gn_sinu_e
  52. {
  53. detail::en<T> en;
  54. };
  55. template <typename T>
  56. struct par_gn_sinu_s
  57. {
  58. T m, n, C_x, C_y;
  59. };
  60. /* Ellipsoidal Sinusoidal only */
  61. template <typename T, typename Parameters>
  62. struct base_gn_sinu_ellipsoid
  63. {
  64. par_gn_sinu_e<T> m_proj_parm;
  65. // FORWARD(e_forward) ellipsoid
  66. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  67. inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  68. {
  69. T s, c;
  70. xy_y = pj_mlfn(lp_lat, s = sin(lp_lat), c = cos(lp_lat), this->m_proj_parm.en);
  71. xy_x = lp_lon * c / sqrt(1. - par.es * s * s);
  72. }
  73. // INVERSE(e_inverse) ellipsoid
  74. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  75. inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  76. {
  77. static const T half_pi = detail::half_pi<T>();
  78. T s;
  79. if ((s = fabs(lp_lat = pj_inv_mlfn(xy_y, par.es, this->m_proj_parm.en))) < half_pi) {
  80. s = sin(lp_lat);
  81. lp_lon = xy_x * sqrt(1. - par.es * s * s) / cos(lp_lat);
  82. } else if ((s - epsilon10) < half_pi)
  83. lp_lon = 0.;
  84. else
  85. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  86. }
  87. /* General spherical sinusoidals */
  88. static inline std::string get_name()
  89. {
  90. return "gn_sinu_ellipsoid";
  91. }
  92. };
  93. template <typename T, typename Parameters>
  94. struct base_gn_sinu_spheroid
  95. {
  96. par_gn_sinu_s<T> m_proj_parm;
  97. // FORWARD(s_forward) sphere
  98. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  99. inline void fwd(Parameters const& , T const& lp_lon, T lp_lat, T& xy_x, T& xy_y) const
  100. {
  101. if (this->m_proj_parm.m == 0.0)
  102. lp_lat = this->m_proj_parm.n != 1. ? aasin(this->m_proj_parm.n * sin(lp_lat)): lp_lat;
  103. else {
  104. T k, V;
  105. int i;
  106. k = this->m_proj_parm.n * sin(lp_lat);
  107. for (i = max_iter; i ; --i) {
  108. lp_lat -= V = (this->m_proj_parm.m * lp_lat + sin(lp_lat) - k) /
  109. (this->m_proj_parm.m + cos(lp_lat));
  110. if (fabs(V) < loop_tol)
  111. break;
  112. }
  113. if (!i) {
  114. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  115. }
  116. }
  117. xy_x = this->m_proj_parm.C_x * lp_lon * (this->m_proj_parm.m + cos(lp_lat));
  118. xy_y = this->m_proj_parm.C_y * lp_lat;
  119. }
  120. // INVERSE(s_inverse) sphere
  121. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  122. inline void inv(Parameters const& , T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  123. {
  124. xy_y /= this->m_proj_parm.C_y;
  125. lp_lat = (this->m_proj_parm.m != 0.0) ? aasin((this->m_proj_parm.m * xy_y + sin(xy_y)) / this->m_proj_parm.n) :
  126. ( this->m_proj_parm.n != 1. ? aasin(sin(xy_y) / this->m_proj_parm.n) : xy_y );
  127. lp_lon = xy_x / (this->m_proj_parm.C_x * (this->m_proj_parm.m + cos(xy_y)));
  128. }
  129. static inline std::string get_name()
  130. {
  131. return "gn_sinu_spheroid";
  132. }
  133. };
  134. template <typename Parameters, typename T>
  135. inline void setup(Parameters& par, par_gn_sinu_s<T>& proj_parm)
  136. {
  137. par.es = 0;
  138. proj_parm.C_x = (proj_parm.C_y = sqrt((proj_parm.m + 1.) / proj_parm.n))/(proj_parm.m + 1.);
  139. }
  140. // General Sinusoidal Series
  141. template <typename Params, typename Parameters, typename T>
  142. inline void setup_gn_sinu(Params const& params, Parameters& par, par_gn_sinu_s<T>& proj_parm)
  143. {
  144. if (pj_param_f<srs::spar::n>(params, "n", srs::dpar::n, proj_parm.n)
  145. && pj_param_f<srs::spar::m>(params, "m", srs::dpar::m, proj_parm.m)) {
  146. if (proj_parm.n <= 0 || proj_parm.m < 0)
  147. BOOST_THROW_EXCEPTION( projection_exception(error_invalid_m_or_n) );
  148. } else
  149. BOOST_THROW_EXCEPTION( projection_exception(error_invalid_m_or_n) );
  150. setup(par, proj_parm);
  151. }
  152. // Sinusoidal (Sanson-Flamsteed)
  153. template <typename Parameters, typename T>
  154. inline void setup_sinu(Parameters const& par, par_gn_sinu_e<T>& proj_parm)
  155. {
  156. proj_parm.en = pj_enfn<T>(par.es);
  157. }
  158. // Sinusoidal (Sanson-Flamsteed)
  159. template <typename Parameters, typename T>
  160. inline void setup_sinu(Parameters& par, par_gn_sinu_s<T>& proj_parm)
  161. {
  162. proj_parm.n = 1.;
  163. proj_parm.m = 0.;
  164. setup(par, proj_parm);
  165. }
  166. // Eckert VI
  167. template <typename Parameters, typename T>
  168. inline void setup_eck6(Parameters& par, par_gn_sinu_s<T>& proj_parm)
  169. {
  170. proj_parm.m = 1.;
  171. proj_parm.n = 2.570796326794896619231321691;
  172. setup(par, proj_parm);
  173. }
  174. // McBryde-Thomas Flat-Polar Sinusoidal
  175. template <typename Parameters, typename T>
  176. inline void setup_mbtfps(Parameters& par, par_gn_sinu_s<T>& proj_parm)
  177. {
  178. proj_parm.m = 0.5;
  179. proj_parm.n = 1.785398163397448309615660845;
  180. setup(par, proj_parm);
  181. }
  182. }} // namespace detail::gn_sinu
  183. #endif // doxygen
  184. /*!
  185. \brief General Sinusoidal Series projection
  186. \ingroup projections
  187. \tparam Geographic latlong point type
  188. \tparam Cartesian xy point type
  189. \tparam Parameters parameter type
  190. \par Projection characteristics
  191. - Pseudocylindrical
  192. - Spheroid
  193. \par Projection parameters
  194. - m (real)
  195. - n (real)
  196. \par Example
  197. \image html ex_gn_sinu.gif
  198. */
  199. template <typename T, typename Parameters>
  200. struct gn_sinu_spheroid : public detail::gn_sinu::base_gn_sinu_spheroid<T, Parameters>
  201. {
  202. template <typename Params>
  203. inline gn_sinu_spheroid(Params const& params, Parameters & par)
  204. {
  205. detail::gn_sinu::setup_gn_sinu(params, par, this->m_proj_parm);
  206. }
  207. };
  208. /*!
  209. \brief Sinusoidal (Sanson-Flamsteed) projection
  210. \ingroup projections
  211. \tparam Geographic latlong point type
  212. \tparam Cartesian xy point type
  213. \tparam Parameters parameter type
  214. \par Projection characteristics
  215. - Pseudocylindrical
  216. - Spheroid
  217. - Ellipsoid
  218. \par Example
  219. \image html ex_sinu.gif
  220. */
  221. template <typename T, typename Parameters>
  222. struct sinu_ellipsoid : public detail::gn_sinu::base_gn_sinu_ellipsoid<T, Parameters>
  223. {
  224. template <typename Params>
  225. inline sinu_ellipsoid(Params const& , Parameters & par)
  226. {
  227. detail::gn_sinu::setup_sinu(par, this->m_proj_parm);
  228. }
  229. };
  230. /*!
  231. \brief Sinusoidal (Sanson-Flamsteed) projection
  232. \ingroup projections
  233. \tparam Geographic latlong point type
  234. \tparam Cartesian xy point type
  235. \tparam Parameters parameter type
  236. \par Projection characteristics
  237. - Pseudocylindrical
  238. - Spheroid
  239. - Ellipsoid
  240. \par Example
  241. \image html ex_sinu.gif
  242. */
  243. template <typename T, typename Parameters>
  244. struct sinu_spheroid : public detail::gn_sinu::base_gn_sinu_spheroid<T, Parameters>
  245. {
  246. template <typename Params>
  247. inline sinu_spheroid(Params const& , Parameters & par)
  248. {
  249. detail::gn_sinu::setup_sinu(par, this->m_proj_parm);
  250. }
  251. };
  252. /*!
  253. \brief Eckert VI projection
  254. \ingroup projections
  255. \tparam Geographic latlong point type
  256. \tparam Cartesian xy point type
  257. \tparam Parameters parameter type
  258. \par Projection characteristics
  259. - Pseudocylindrical
  260. - Spheroid
  261. \par Example
  262. \image html ex_eck6.gif
  263. */
  264. template <typename T, typename Parameters>
  265. struct eck6_spheroid : public detail::gn_sinu::base_gn_sinu_spheroid<T, Parameters>
  266. {
  267. template <typename Params>
  268. inline eck6_spheroid(Params const& , Parameters & par)
  269. {
  270. detail::gn_sinu::setup_eck6(par, this->m_proj_parm);
  271. }
  272. };
  273. /*!
  274. \brief McBryde-Thomas Flat-Polar Sinusoidal projection
  275. \ingroup projections
  276. \tparam Geographic latlong point type
  277. \tparam Cartesian xy point type
  278. \tparam Parameters parameter type
  279. \par Projection characteristics
  280. - Pseudocylindrical
  281. - Spheroid
  282. \par Example
  283. \image html ex_mbtfps.gif
  284. */
  285. template <typename T, typename Parameters>
  286. struct mbtfps_spheroid : public detail::gn_sinu::base_gn_sinu_spheroid<T, Parameters>
  287. {
  288. template <typename Params>
  289. inline mbtfps_spheroid(Params const& , Parameters & par)
  290. {
  291. detail::gn_sinu::setup_mbtfps(par, this->m_proj_parm);
  292. }
  293. };
  294. #ifndef DOXYGEN_NO_DETAIL
  295. namespace detail
  296. {
  297. // Static projection
  298. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_gn_sinu, gn_sinu_spheroid)
  299. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_sinu, sinu_spheroid, sinu_ellipsoid)
  300. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_eck6, eck6_spheroid)
  301. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_mbtfps, mbtfps_spheroid)
  302. // Factory entry(s)
  303. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(gn_sinu_entry, gn_sinu_spheroid)
  304. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(sinu_entry, sinu_spheroid, sinu_ellipsoid)
  305. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(eck6_entry, eck6_spheroid)
  306. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(mbtfps_entry, mbtfps_spheroid)
  307. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(gn_sinu_init)
  308. {
  309. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(gn_sinu, gn_sinu_entry);
  310. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(sinu, sinu_entry);
  311. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(eck6, eck6_entry);
  312. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(mbtfps, mbtfps_entry);
  313. }
  314. } // namespace detail
  315. #endif // doxygen
  316. } // namespace projections
  317. }} // namespace boost::geometry
  318. #endif // BOOST_GEOMETRY_PROJECTIONS_GN_SINU_HPP