stere.hpp 21 KB

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