laea.hpp 17 KB

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