stere.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546
  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_STERE_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_STERE_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/factory_entry.hpp>
  38. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  39. #include <boost/geometry/srs/projections/impl/pj_tsfn.hpp>
  40. #include <boost/geometry/srs/projections/impl/projects.hpp>
  41. namespace boost { namespace geometry
  42. {
  43. namespace projections
  44. {
  45. #ifndef DOXYGEN_NO_DETAIL
  46. namespace detail { namespace stere
  47. {
  48. static const double epsilon10 = 1.e-10;
  49. static const double tolerance = 1.e-8;
  50. static const int n_iter = 8;
  51. static const double conv_tolerance = 1.e-10;
  52. enum mode_type {
  53. s_pole = 0,
  54. n_pole = 1,
  55. obliq = 2,
  56. equit = 3
  57. };
  58. template <typename T>
  59. struct par_stere
  60. {
  61. T phits;
  62. T sinX1;
  63. T cosX1;
  64. T akm1;
  65. mode_type mode;
  66. bool variant_c;
  67. };
  68. template <typename T>
  69. inline T ssfn_(T const& phit, T sinphi, T const& eccen)
  70. {
  71. static const T half_pi = detail::half_pi<T>();
  72. sinphi *= eccen;
  73. return (tan (.5 * (half_pi + phit)) *
  74. math::pow((T(1) - sinphi) / (T(1) + sinphi), T(0.5) * eccen));
  75. }
  76. template <typename T, typename Parameters>
  77. struct base_stere_ellipsoid
  78. {
  79. par_stere<T> m_proj_parm;
  80. // FORWARD(e_forward) ellipsoid
  81. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  82. inline void fwd(Parameters const& par, T const& lp_lon, T lp_lat, T& xy_x, T& xy_y) const
  83. {
  84. static const T half_pi = detail::half_pi<T>();
  85. T coslam, sinlam, sinX=0.0, cosX=0.0, X, A = 0.0, sinphi;
  86. coslam = cos(lp_lon);
  87. sinlam = sin(lp_lon);
  88. sinphi = sin(lp_lat);
  89. if (this->m_proj_parm.mode == obliq || this->m_proj_parm.mode == equit) {
  90. sinX = sin(X = 2. * atan(ssfn_(lp_lat, sinphi, par.e)) - half_pi);
  91. cosX = cos(X);
  92. }
  93. switch (this->m_proj_parm.mode) {
  94. case obliq:
  95. A = this->m_proj_parm.akm1 / (this->m_proj_parm.cosX1 * (1. + this->m_proj_parm.sinX1 * sinX +
  96. this->m_proj_parm.cosX1 * cosX * coslam));
  97. xy_y = A * (this->m_proj_parm.cosX1 * sinX - this->m_proj_parm.sinX1 * cosX * coslam);
  98. goto xmul; /* but why not just xy.x = A * cosX; break; ? */
  99. case equit:
  100. // TODO: calculate denominator once
  101. /* avoid zero division */
  102. if (1. + cosX * coslam == 0.0) {
  103. xy_y = HUGE_VAL;
  104. } else {
  105. A = this->m_proj_parm.akm1 / (1. + cosX * coslam);
  106. xy_y = A * sinX;
  107. }
  108. xmul:
  109. xy_x = A * cosX;
  110. break;
  111. case s_pole:
  112. lp_lat = -lp_lat;
  113. coslam = - coslam;
  114. sinphi = -sinphi;
  115. BOOST_FALLTHROUGH;
  116. case n_pole:
  117. // see IOGP Publication 373-7-2 – Geomatics Guidance Note number 7, part 2
  118. // December 2021 pg. 82
  119. if( m_proj_parm.variant_c )
  120. {
  121. auto t = pj_tsfn(lp_lat, sinphi, par.e);
  122. auto tf = pj_tsfn(this->m_proj_parm.phits,
  123. sin(this->m_proj_parm.phits),
  124. par.e);
  125. xy_x = this->m_proj_parm.akm1 * t;
  126. auto mf = this->m_proj_parm.akm1 * tf;
  127. xy_y = - xy_x * coslam - mf;
  128. } else {
  129. xy_x = this->m_proj_parm.akm1 * pj_tsfn(lp_lat, sinphi, par.e);
  130. xy_y = - xy_x * coslam;
  131. }
  132. break;
  133. }
  134. xy_x = xy_x * sinlam;
  135. }
  136. // INVERSE(e_inverse) ellipsoid
  137. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  138. inline void inv(Parameters const& par, T xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  139. {
  140. static const T half_pi = detail::half_pi<T>();
  141. T cosphi, sinphi, tp=0.0, phi_l=0.0, rho, halfe=0.0, halfpi=0.0;
  142. T mf = 0;
  143. int i;
  144. rho = boost::math::hypot(xy_x, xy_y);
  145. switch (this->m_proj_parm.mode) {
  146. case obliq:
  147. case equit:
  148. cosphi = cos( tp = 2. * atan2(rho * this->m_proj_parm.cosX1 , this->m_proj_parm.akm1) );
  149. sinphi = sin(tp);
  150. if( rho == 0.0 )
  151. phi_l = asin(cosphi * this->m_proj_parm.sinX1);
  152. else
  153. phi_l = asin(cosphi * this->m_proj_parm.sinX1 + (xy_y * sinphi * this->m_proj_parm.cosX1 / rho));
  154. tp = tan(.5 * (half_pi + phi_l));
  155. xy_x *= sinphi;
  156. xy_y = rho * this->m_proj_parm.cosX1 * cosphi - xy_y * this->m_proj_parm.sinX1* sinphi;
  157. halfpi = half_pi;
  158. halfe = .5 * par.e;
  159. break;
  160. case n_pole:
  161. xy_y = -xy_y;
  162. BOOST_FALLTHROUGH;
  163. case s_pole:
  164. // see IOGP Publication 373-7-2 – Geomatics Guidance Note number 7, part 2
  165. // December 2021 pg. 82
  166. if( m_proj_parm.variant_c )
  167. {
  168. auto tf = pj_tsfn(this->m_proj_parm.phits,
  169. sin(this->m_proj_parm.phits),
  170. par.e);
  171. mf = this->m_proj_parm.akm1 * tf;
  172. rho = boost::math::hypot(xy_x, xy_y + mf);
  173. }
  174. phi_l = half_pi - 2. * atan(tp = - rho / this->m_proj_parm.akm1);
  175. halfpi = -half_pi;
  176. halfe = -.5 * par.e;
  177. break;
  178. }
  179. for (i = n_iter; i--; phi_l = lp_lat) {
  180. sinphi = par.e * sin(phi_l);
  181. lp_lat = T(2) * atan(tp * math::pow((T(1)+sinphi)/(T(1)-sinphi), halfe)) - halfpi;
  182. if (fabs(phi_l - lp_lat) < conv_tolerance) {
  183. if (this->m_proj_parm.mode == s_pole)
  184. lp_lat = -lp_lat;
  185. lp_lon = (xy_x == 0. && xy_y == 0.) ? 0. : atan2(xy_x, xy_y + mf);
  186. return;
  187. }
  188. }
  189. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  190. }
  191. static inline std::string get_name()
  192. {
  193. return "stere_ellipsoid";
  194. }
  195. };
  196. template <typename T, typename Parameters>
  197. struct base_stere_spheroid
  198. {
  199. par_stere<T> m_proj_parm;
  200. // FORWARD(s_forward) spheroid
  201. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  202. inline void fwd(Parameters const& , T const& lp_lon, T lp_lat, T& xy_x, T& xy_y) const
  203. {
  204. static const T fourth_pi = detail::fourth_pi<T>();
  205. static const T half_pi = detail::half_pi<T>();
  206. T sinphi, cosphi, coslam, sinlam;
  207. sinphi = sin(lp_lat);
  208. cosphi = cos(lp_lat);
  209. coslam = cos(lp_lon);
  210. sinlam = sin(lp_lon);
  211. switch (this->m_proj_parm.mode) {
  212. case equit:
  213. xy_y = 1. + cosphi * coslam;
  214. goto oblcon;
  215. case obliq:
  216. xy_y = 1. + this->m_proj_parm.sinX1 * sinphi + this->m_proj_parm.cosX1 * cosphi * coslam;
  217. oblcon:
  218. if (xy_y <= epsilon10) {
  219. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  220. }
  221. xy_x = (xy_y = this->m_proj_parm.akm1 / xy_y) * cosphi * sinlam;
  222. xy_y *= (this->m_proj_parm.mode == equit) ? sinphi :
  223. this->m_proj_parm.cosX1 * sinphi - this->m_proj_parm.sinX1 * cosphi * coslam;
  224. break;
  225. case n_pole:
  226. coslam = - coslam;
  227. lp_lat = - lp_lat;
  228. BOOST_FALLTHROUGH;
  229. case s_pole:
  230. if (fabs(lp_lat - half_pi) < tolerance) {
  231. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  232. }
  233. xy_x = sinlam * ( xy_y = this->m_proj_parm.akm1 * tan(fourth_pi + .5 * lp_lat) );
  234. xy_y *= coslam;
  235. break;
  236. }
  237. }
  238. // INVERSE(s_inverse) spheroid
  239. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  240. inline void inv(Parameters const& par, T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  241. {
  242. T c, rh, sinc, cosc;
  243. sinc = sin(c = 2. * atan((rh = boost::math::hypot(xy_x, xy_y)) / this->m_proj_parm.akm1));
  244. cosc = cos(c);
  245. lp_lon = 0.;
  246. switch (this->m_proj_parm.mode) {
  247. case equit:
  248. if (fabs(rh) <= epsilon10)
  249. lp_lat = 0.;
  250. else
  251. lp_lat = asin(xy_y * sinc / rh);
  252. if (cosc != 0. || xy_x != 0.)
  253. lp_lon = atan2(xy_x * sinc, cosc * rh);
  254. break;
  255. case obliq:
  256. if (fabs(rh) <= epsilon10)
  257. lp_lat = par.phi0;
  258. else
  259. lp_lat = asin(cosc * this->m_proj_parm.sinX1 + xy_y * sinc * this->m_proj_parm.cosX1 / rh);
  260. if ((c = cosc - this->m_proj_parm.sinX1 * sin(lp_lat)) != 0. || xy_x != 0.)
  261. lp_lon = atan2(xy_x * sinc * this->m_proj_parm.cosX1, c * rh);
  262. break;
  263. case n_pole:
  264. xy_y = -xy_y;
  265. BOOST_FALLTHROUGH;
  266. case s_pole:
  267. if (fabs(rh) <= epsilon10)
  268. lp_lat = par.phi0;
  269. else
  270. lp_lat = asin(this->m_proj_parm.mode == s_pole ? - cosc : cosc);
  271. lp_lon = (xy_x == 0. && xy_y == 0.) ? 0. : atan2(xy_x, xy_y);
  272. break;
  273. }
  274. }
  275. static inline std::string get_name()
  276. {
  277. return "stere_spheroid";
  278. }
  279. };
  280. template <typename Parameters, typename T>
  281. inline void setup(Parameters const& par, par_stere<T>& proj_parm) /* general initialization */
  282. {
  283. static const T fourth_pi = detail::fourth_pi<T>();
  284. static const T half_pi = detail::half_pi<T>();
  285. T t;
  286. if (fabs((t = fabs(par.phi0)) - half_pi) < epsilon10)
  287. proj_parm.mode = par.phi0 < 0. ? s_pole : n_pole;
  288. else
  289. proj_parm.mode = t > epsilon10 ? obliq : equit;
  290. proj_parm.phits = fabs(proj_parm.phits);
  291. if (par.es != 0.0) {
  292. T X;
  293. switch (proj_parm.mode) {
  294. case n_pole:
  295. case s_pole:
  296. if (fabs(proj_parm.phits - half_pi) < epsilon10)
  297. proj_parm.akm1 = 2. * par.k0 /
  298. sqrt(math::pow(T(1)+par.e,T(1)+par.e)*math::pow(T(1)-par.e,T(1)-par.e));
  299. else {
  300. proj_parm.akm1 = cos(proj_parm.phits) /
  301. pj_tsfn(proj_parm.phits, t = sin(proj_parm.phits), par.e);
  302. t *= par.e;
  303. proj_parm.akm1 /= sqrt(1. - t * t);
  304. }
  305. break;
  306. case equit:
  307. case obliq:
  308. t = sin(par.phi0);
  309. X = 2. * atan(ssfn_(par.phi0, t, par.e)) - half_pi;
  310. t *= par.e;
  311. proj_parm.akm1 = 2. * par.k0 * cos(par.phi0) / sqrt(1. - t * t);
  312. proj_parm.sinX1 = sin(X);
  313. proj_parm.cosX1 = cos(X);
  314. break;
  315. }
  316. } else {
  317. switch (proj_parm.mode) {
  318. case obliq:
  319. proj_parm.sinX1 = sin(par.phi0);
  320. proj_parm.cosX1 = cos(par.phi0);
  321. BOOST_FALLTHROUGH;
  322. case equit:
  323. proj_parm.akm1 = 2. * par.k0;
  324. break;
  325. case s_pole:
  326. case n_pole:
  327. proj_parm.akm1 = fabs(proj_parm.phits - half_pi) >= epsilon10 ?
  328. cos(proj_parm.phits) / tan(fourth_pi - .5 * proj_parm.phits) :
  329. 2. * par.k0 ;
  330. break;
  331. }
  332. }
  333. }
  334. // Stereographic
  335. template <typename Params, typename Parameters, typename T>
  336. inline void setup_stere(Params const& params, Parameters const& par, par_stere<T>& proj_parm)
  337. {
  338. static const T half_pi = detail::half_pi<T>();
  339. if (! pj_param_r<srs::spar::lat_ts>(params, "lat_ts", srs::dpar::lat_ts, proj_parm.phits))
  340. proj_parm.phits = half_pi;
  341. proj_parm.variant_c = false;
  342. if (pj_param_exists<srs::spar::variant_c>(params, "variant_c", srs::dpar::variant_c))
  343. proj_parm.variant_c = true;
  344. setup(par, proj_parm);
  345. }
  346. // Universal Polar Stereographic
  347. template <typename Params, typename Parameters, typename T>
  348. inline void setup_ups(Params const& params, Parameters& par, par_stere<T>& proj_parm)
  349. {
  350. static const T half_pi = detail::half_pi<T>();
  351. /* International Ellipsoid */
  352. par.phi0 = pj_get_param_b<srs::spar::south>(params, "south", srs::dpar::south) ? -half_pi: half_pi;
  353. if (par.es == 0.0) {
  354. BOOST_THROW_EXCEPTION( projection_exception(error_ellipsoid_use_required) );
  355. }
  356. par.k0 = .994;
  357. par.x0 = 2000000.;
  358. par.y0 = 2000000.;
  359. proj_parm.phits = half_pi;
  360. par.lam0 = 0.;
  361. setup(par, proj_parm);
  362. }
  363. }} // namespace detail::stere
  364. #endif // doxygen
  365. /*!
  366. \brief Stereographic projection
  367. \ingroup projections
  368. \tparam Geographic latlong point type
  369. \tparam Cartesian xy point type
  370. \tparam Parameters parameter type
  371. \par Projection characteristics
  372. - Azimuthal
  373. - Spheroid
  374. - Ellipsoid
  375. \par Projection parameters
  376. - lat_ts: Latitude of true scale (degrees)
  377. \par Example
  378. \image html ex_stere.gif
  379. */
  380. template <typename T, typename Parameters>
  381. struct stere_ellipsoid : public detail::stere::base_stere_ellipsoid<T, Parameters>
  382. {
  383. template <typename Params>
  384. inline stere_ellipsoid(Params const& params, Parameters const& par)
  385. {
  386. detail::stere::setup_stere(params, par, this->m_proj_parm);
  387. }
  388. };
  389. /*!
  390. \brief Stereographic projection
  391. \ingroup projections
  392. \tparam Geographic latlong point type
  393. \tparam Cartesian xy point type
  394. \tparam Parameters parameter type
  395. \par Projection characteristics
  396. - Azimuthal
  397. - Spheroid
  398. - Ellipsoid
  399. \par Projection parameters
  400. - lat_ts: Latitude of true scale (degrees)
  401. \par Example
  402. \image html ex_stere.gif
  403. */
  404. template <typename T, typename Parameters>
  405. struct stere_spheroid : public detail::stere::base_stere_spheroid<T, Parameters>
  406. {
  407. template <typename Params>
  408. inline stere_spheroid(Params const& params, Parameters const& par)
  409. {
  410. detail::stere::setup_stere(params, par, this->m_proj_parm);
  411. }
  412. };
  413. /*!
  414. \brief Universal Polar Stereographic projection
  415. \ingroup projections
  416. \tparam Geographic latlong point type
  417. \tparam Cartesian xy point type
  418. \tparam Parameters parameter type
  419. \par Projection characteristics
  420. - Azimuthal
  421. - Spheroid
  422. - Ellipsoid
  423. \par Projection parameters
  424. - south: Denotes southern hemisphere UTM zone (boolean)
  425. \par Example
  426. \image html ex_ups.gif
  427. */
  428. template <typename T, typename Parameters>
  429. struct ups_ellipsoid : public detail::stere::base_stere_ellipsoid<T, Parameters>
  430. {
  431. template <typename Params>
  432. inline ups_ellipsoid(Params const& params, Parameters & par)
  433. {
  434. detail::stere::setup_ups(params, par, this->m_proj_parm);
  435. }
  436. };
  437. /*!
  438. \brief Universal Polar Stereographic projection
  439. \ingroup projections
  440. \tparam Geographic latlong point type
  441. \tparam Cartesian xy point type
  442. \tparam Parameters parameter type
  443. \par Projection characteristics
  444. - Azimuthal
  445. - Spheroid
  446. - Ellipsoid
  447. \par Projection parameters
  448. - south: Denotes southern hemisphere UTM zone (boolean)
  449. \par Example
  450. \image html ex_ups.gif
  451. */
  452. template <typename T, typename Parameters>
  453. struct ups_spheroid : public detail::stere::base_stere_spheroid<T, Parameters>
  454. {
  455. template <typename Params>
  456. inline ups_spheroid(Params const& params, Parameters & par)
  457. {
  458. detail::stere::setup_ups(params, par, this->m_proj_parm);
  459. }
  460. };
  461. #ifndef DOXYGEN_NO_DETAIL
  462. namespace detail
  463. {
  464. // Static projection
  465. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_stere, stere_spheroid, stere_ellipsoid)
  466. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_ups, ups_spheroid, ups_ellipsoid)
  467. // Factory entry(s)
  468. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(stere_entry, stere_spheroid, stere_ellipsoid)
  469. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(ups_entry, ups_spheroid, ups_ellipsoid)
  470. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(stere_init)
  471. {
  472. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(stere, stere_entry)
  473. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(ups, ups_entry)
  474. }
  475. } // namespace detail
  476. #endif // doxygen
  477. } // namespace projections
  478. }} // namespace boost::geometry
  479. #endif // BOOST_GEOMETRY_PROJECTIONS_STERE_HPP