stere.hpp 21 KB

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