laea.hpp 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425
  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_LAEA_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_LAEA_HPP
  32. #include <boost/config.hpp>
  33. #include <boost/geometry/util/math.hpp>
  34. #include <boost/math/special_functions/hypot.hpp>
  35. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  36. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  37. #include <boost/geometry/srs/projections/impl/projects.hpp>
  38. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  39. #include <boost/geometry/srs/projections/impl/pj_auth.hpp>
  40. #include <boost/geometry/srs/projections/impl/pj_qsfn.hpp>
  41. namespace boost { namespace geometry
  42. {
  43. namespace projections
  44. {
  45. #ifndef DOXYGEN_NO_DETAIL
  46. namespace detail { namespace laea
  47. {
  48. static const double epsilon10 = 1.e-10;
  49. enum mode_type {
  50. n_pole = 0,
  51. s_pole = 1,
  52. equit = 2,
  53. obliq = 3
  54. };
  55. template <typename T>
  56. struct par_laea
  57. {
  58. T sinb1;
  59. T cosb1;
  60. T xmf;
  61. T ymf;
  62. T mmf;
  63. T qp;
  64. T dd;
  65. T rq;
  66. detail::apa<T> apa;
  67. mode_type mode;
  68. };
  69. template <typename T, typename Parameters>
  70. struct base_laea_ellipsoid
  71. {
  72. par_laea<T> m_proj_parm;
  73. // FORWARD(e_forward) ellipsoid
  74. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  75. inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  76. {
  77. static const T half_pi = detail::half_pi<T>();
  78. T coslam, sinlam, sinphi, q, sinb=0.0, cosb=0.0, b=0.0;
  79. coslam = cos(lp_lon);
  80. sinlam = sin(lp_lon);
  81. sinphi = sin(lp_lat);
  82. q = pj_qsfn(sinphi, par.e, par.one_es);
  83. if (this->m_proj_parm.mode == obliq || this->m_proj_parm.mode == equit) {
  84. sinb = q / this->m_proj_parm.qp;
  85. cosb = sqrt(1. - sinb * sinb);
  86. }
  87. switch (this->m_proj_parm.mode) {
  88. case obliq:
  89. b = 1. + this->m_proj_parm.sinb1 * sinb + this->m_proj_parm.cosb1 * cosb * coslam;
  90. break;
  91. case equit:
  92. b = 1. + cosb * coslam;
  93. break;
  94. case n_pole:
  95. b = half_pi + lp_lat;
  96. q = this->m_proj_parm.qp - q;
  97. break;
  98. case s_pole:
  99. b = lp_lat - half_pi;
  100. q = this->m_proj_parm.qp + q;
  101. break;
  102. }
  103. if (fabs(b) < epsilon10) {
  104. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  105. }
  106. switch (this->m_proj_parm.mode) {
  107. case obliq:
  108. b = sqrt(2. / b);
  109. xy_y = this->m_proj_parm.ymf * b * (this->m_proj_parm.cosb1 * sinb - this->m_proj_parm.sinb1 * cosb * coslam);
  110. goto eqcon;
  111. break;
  112. case equit:
  113. b = sqrt(2. / (1. + cosb * coslam));
  114. xy_y = b * sinb * this->m_proj_parm.ymf;
  115. eqcon:
  116. xy_x = this->m_proj_parm.xmf * b * cosb * sinlam;
  117. break;
  118. case n_pole:
  119. case s_pole:
  120. if (q >= 0.) {
  121. b = sqrt(q);
  122. xy_x = b * sinlam;
  123. xy_y = coslam * (this->m_proj_parm.mode == s_pole ? b : -b);
  124. } else
  125. xy_x = xy_y = 0.;
  126. break;
  127. }
  128. }
  129. // INVERSE(e_inverse) ellipsoid
  130. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  131. inline void inv(Parameters const& par, T xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  132. {
  133. T cCe, sCe, q, rho, ab=0.0;
  134. switch (this->m_proj_parm.mode) {
  135. case equit:
  136. case obliq:
  137. xy_x /= this->m_proj_parm.dd;
  138. xy_y *= this->m_proj_parm.dd;
  139. rho = boost::math::hypot(xy_x, xy_y);
  140. if (rho < epsilon10) {
  141. lp_lon = 0.;
  142. lp_lat = par.phi0;
  143. return;
  144. }
  145. sCe = 2. * asin(.5 * rho / this->m_proj_parm.rq);
  146. cCe = cos(sCe);
  147. sCe = sin(sCe);
  148. xy_x *= sCe;
  149. if (this->m_proj_parm.mode == obliq) {
  150. ab = cCe * this->m_proj_parm.sinb1 + xy_y * sCe * this->m_proj_parm.cosb1 / rho;
  151. xy_y = rho * this->m_proj_parm.cosb1 * cCe - xy_y * this->m_proj_parm.sinb1 * sCe;
  152. } else {
  153. ab = xy_y * sCe / rho;
  154. xy_y = rho * cCe;
  155. }
  156. break;
  157. case n_pole:
  158. xy_y = -xy_y;
  159. BOOST_FALLTHROUGH;
  160. case s_pole:
  161. q = (xy_x * xy_x + xy_y * xy_y);
  162. if (q == 0.0) {
  163. lp_lon = 0.;
  164. lp_lat = par.phi0;
  165. return;
  166. }
  167. ab = 1. - q / this->m_proj_parm.qp;
  168. if (this->m_proj_parm.mode == s_pole)
  169. ab = - ab;
  170. break;
  171. }
  172. lp_lon = atan2(xy_x, xy_y);
  173. lp_lat = pj_authlat(asin(ab), this->m_proj_parm.apa);
  174. }
  175. static inline std::string get_name()
  176. {
  177. return "laea_ellipsoid";
  178. }
  179. };
  180. template <typename T, typename Parameters>
  181. struct base_laea_spheroid
  182. {
  183. par_laea<T> m_proj_parm;
  184. // FORWARD(s_forward) spheroid
  185. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  186. inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  187. {
  188. static const T fourth_pi = detail::fourth_pi<T>();
  189. T coslam, cosphi, sinphi;
  190. sinphi = sin(lp_lat);
  191. cosphi = cos(lp_lat);
  192. coslam = cos(lp_lon);
  193. switch (this->m_proj_parm.mode) {
  194. case equit:
  195. xy_y = 1. + cosphi * coslam;
  196. goto oblcon;
  197. case obliq:
  198. xy_y = 1. + this->m_proj_parm.sinb1 * sinphi + this->m_proj_parm.cosb1 * cosphi * coslam;
  199. oblcon:
  200. if (xy_y <= epsilon10) {
  201. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  202. }
  203. xy_y = sqrt(2. / xy_y);
  204. xy_x = xy_y * cosphi * sin(lp_lon);
  205. xy_y *= this->m_proj_parm.mode == equit ? sinphi :
  206. this->m_proj_parm.cosb1 * sinphi - this->m_proj_parm.sinb1 * cosphi * coslam;
  207. break;
  208. case n_pole:
  209. coslam = -coslam;
  210. BOOST_FALLTHROUGH;
  211. case s_pole:
  212. if (fabs(lp_lat + par.phi0) < epsilon10) {
  213. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  214. }
  215. xy_y = fourth_pi - lp_lat * .5;
  216. xy_y = 2. * (this->m_proj_parm.mode == s_pole ? cos(xy_y) : sin(xy_y));
  217. xy_x = xy_y * sin(lp_lon);
  218. xy_y *= coslam;
  219. break;
  220. }
  221. }
  222. // INVERSE(s_inverse) spheroid
  223. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  224. inline void inv(Parameters const& par, T xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  225. {
  226. static const T half_pi = detail::half_pi<T>();
  227. T cosz=0.0, rh, sinz=0.0;
  228. rh = boost::math::hypot(xy_x, xy_y);
  229. if ((lp_lat = rh * .5 ) > 1.) {
  230. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  231. }
  232. lp_lat = 2. * asin(lp_lat);
  233. if (this->m_proj_parm.mode == obliq || this->m_proj_parm.mode == equit) {
  234. sinz = sin(lp_lat);
  235. cosz = cos(lp_lat);
  236. }
  237. switch (this->m_proj_parm.mode) {
  238. case equit:
  239. lp_lat = fabs(rh) <= epsilon10 ? 0. : asin(xy_y * sinz / rh);
  240. xy_x *= sinz;
  241. xy_y = cosz * rh;
  242. break;
  243. case obliq:
  244. lp_lat = fabs(rh) <= epsilon10 ? par.phi0 :
  245. asin(cosz * this->m_proj_parm.sinb1 + xy_y * sinz * this->m_proj_parm.cosb1 / rh);
  246. xy_x *= sinz * this->m_proj_parm.cosb1;
  247. xy_y = (cosz - sin(lp_lat) * this->m_proj_parm.sinb1) * rh;
  248. break;
  249. case n_pole:
  250. xy_y = -xy_y;
  251. lp_lat = half_pi - lp_lat;
  252. break;
  253. case s_pole:
  254. lp_lat -= half_pi;
  255. break;
  256. }
  257. lp_lon = (xy_y == 0. && (this->m_proj_parm.mode == equit || this->m_proj_parm.mode == obliq)) ?
  258. 0. : atan2(xy_x, xy_y);
  259. }
  260. static inline std::string get_name()
  261. {
  262. return "laea_spheroid";
  263. }
  264. };
  265. // Lambert Azimuthal Equal Area
  266. template <typename Parameters, typename T>
  267. inline void setup_laea(Parameters& par, par_laea<T>& proj_parm)
  268. {
  269. static const T half_pi = detail::half_pi<T>();
  270. T t;
  271. t = fabs(par.phi0);
  272. if (fabs(t - half_pi) < epsilon10)
  273. proj_parm.mode = par.phi0 < 0. ? s_pole : n_pole;
  274. else if (fabs(t) < epsilon10)
  275. proj_parm.mode = equit;
  276. else
  277. proj_parm.mode = obliq;
  278. if (par.es != 0.0) {
  279. double sinphi;
  280. par.e = sqrt(par.es); // TODO : Isn't it already set?
  281. proj_parm.qp = pj_qsfn(1., par.e, par.one_es);
  282. proj_parm.mmf = .5 / (1. - par.es);
  283. proj_parm.apa = pj_authset<T>(par.es);
  284. switch (proj_parm.mode) {
  285. case n_pole:
  286. case s_pole:
  287. proj_parm.dd = 1.;
  288. break;
  289. case equit:
  290. proj_parm.dd = 1. / (proj_parm.rq = sqrt(.5 * proj_parm.qp));
  291. proj_parm.xmf = 1.;
  292. proj_parm.ymf = .5 * proj_parm.qp;
  293. break;
  294. case obliq:
  295. proj_parm.rq = sqrt(.5 * proj_parm.qp);
  296. sinphi = sin(par.phi0);
  297. proj_parm.sinb1 = pj_qsfn(sinphi, par.e, par.one_es) / proj_parm.qp;
  298. proj_parm.cosb1 = sqrt(1. - proj_parm.sinb1 * proj_parm.sinb1);
  299. proj_parm.dd = cos(par.phi0) / (sqrt(1. - par.es * sinphi * sinphi) *
  300. proj_parm.rq * proj_parm.cosb1);
  301. proj_parm.ymf = (proj_parm.xmf = proj_parm.rq) / proj_parm.dd;
  302. proj_parm.xmf *= proj_parm.dd;
  303. break;
  304. }
  305. } else {
  306. if (proj_parm.mode == obliq) {
  307. proj_parm.sinb1 = sin(par.phi0);
  308. proj_parm.cosb1 = cos(par.phi0);
  309. }
  310. }
  311. }
  312. }} // namespace laea
  313. #endif // doxygen
  314. /*!
  315. \brief Lambert Azimuthal Equal Area projection
  316. \ingroup projections
  317. \tparam Geographic latlong point type
  318. \tparam Cartesian xy point type
  319. \tparam Parameters parameter type
  320. \par Projection characteristics
  321. - Azimuthal
  322. - Spheroid
  323. - Ellipsoid
  324. \par Example
  325. \image html ex_laea.gif
  326. */
  327. template <typename T, typename Parameters>
  328. struct laea_ellipsoid : public detail::laea::base_laea_ellipsoid<T, Parameters>
  329. {
  330. template <typename Params>
  331. inline laea_ellipsoid(Params const& , Parameters & par)
  332. {
  333. detail::laea::setup_laea(par, this->m_proj_parm);
  334. }
  335. };
  336. /*!
  337. \brief Lambert Azimuthal Equal Area projection
  338. \ingroup projections
  339. \tparam Geographic latlong point type
  340. \tparam Cartesian xy point type
  341. \tparam Parameters parameter type
  342. \par Projection characteristics
  343. - Azimuthal
  344. - Spheroid
  345. - Ellipsoid
  346. \par Example
  347. \image html ex_laea.gif
  348. */
  349. template <typename T, typename Parameters>
  350. struct laea_spheroid : public detail::laea::base_laea_spheroid<T, Parameters>
  351. {
  352. template <typename Params>
  353. inline laea_spheroid(Params const& , Parameters & par)
  354. {
  355. detail::laea::setup_laea(par, this->m_proj_parm);
  356. }
  357. };
  358. #ifndef DOXYGEN_NO_DETAIL
  359. namespace detail
  360. {
  361. // Static projection
  362. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_laea, laea_spheroid, laea_ellipsoid)
  363. // Factory entry(s)
  364. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(laea_entry, laea_spheroid, laea_ellipsoid)
  365. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(laea_init)
  366. {
  367. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(laea, laea_entry)
  368. }
  369. } // namespace detail
  370. #endif // doxygen
  371. } // namespace projections
  372. }} // namespace boost::geometry
  373. #endif // BOOST_GEOMETRY_PROJECTIONS_LAEA_HPP