stere.hpp 22 KB

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