aeqd.hpp 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  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-2020.
  4. // Modifications copyright (c) 2017-2020, 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. // Purpose: Implementation of the aeqd (Azimuthal Equidistant) projection.
  16. // Author: Gerald Evenden
  17. // Copyright (c) 1995, Gerald Evenden
  18. // Permission is hereby granted, free of charge, to any person obtaining a
  19. // copy of this software and associated documentation files (the "Software"),
  20. // to deal in the Software without restriction, including without limitation
  21. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  22. // and/or sell copies of the Software, and to permit persons to whom the
  23. // Software is furnished to do so, subject to the following conditions:
  24. // The above copyright notice and this permission notice shall be included
  25. // in all copies or substantial portions of the Software.
  26. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  27. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  28. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  29. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  30. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  31. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  32. // DEALINGS IN THE SOFTWARE.
  33. #ifndef BOOST_GEOMETRY_PROJECTIONS_AEQD_HPP
  34. #define BOOST_GEOMETRY_PROJECTIONS_AEQD_HPP
  35. #include <type_traits>
  36. #include <boost/config.hpp>
  37. #include <boost/geometry/formulas/vincenty_direct.hpp>
  38. #include <boost/geometry/formulas/vincenty_inverse.hpp>
  39. #include <boost/geometry/srs/projections/impl/aasincos.hpp>
  40. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  41. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  42. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  43. #include <boost/geometry/srs/projections/impl/pj_mlfn.hpp>
  44. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  45. #include <boost/geometry/srs/projections/impl/projects.hpp>
  46. #include <boost/geometry/util/math.hpp>
  47. #include <boost/math/special_functions/hypot.hpp>
  48. namespace boost { namespace geometry
  49. {
  50. namespace projections
  51. {
  52. #ifndef DOXYGEN_NO_DETAIL
  53. namespace detail { namespace aeqd
  54. {
  55. static const double epsilon10 = 1.e-10;
  56. static const double tolerance = 1.e-14;
  57. enum mode_type {
  58. n_pole = 0,
  59. s_pole = 1,
  60. equit = 2,
  61. obliq = 3
  62. };
  63. template <typename T>
  64. struct par_aeqd
  65. {
  66. T sinph0;
  67. T cosph0;
  68. detail::en<T> en;
  69. T M1;
  70. //T N1;
  71. T Mp;
  72. //T He;
  73. //T G;
  74. T b;
  75. mode_type mode;
  76. };
  77. template <typename T, typename Par, typename ProjParm>
  78. inline void e_forward(T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y, Par const& par, ProjParm const& proj_parm)
  79. {
  80. T coslam, cosphi, sinphi, rho;
  81. //T azi1, s12;
  82. //T lam1, phi1, lam2, phi2;
  83. coslam = cos(lp_lon);
  84. cosphi = cos(lp_lat);
  85. sinphi = sin(lp_lat);
  86. switch (proj_parm.mode) {
  87. case n_pole:
  88. coslam = - coslam;
  89. BOOST_FALLTHROUGH;
  90. case s_pole:
  91. xy_x = (rho = fabs(proj_parm.Mp - pj_mlfn(lp_lat, sinphi, cosphi, proj_parm.en))) *
  92. sin(lp_lon);
  93. xy_y = rho * coslam;
  94. break;
  95. case equit:
  96. case obliq:
  97. if (fabs(lp_lon) < epsilon10 && fabs(lp_lat - par.phi0) < epsilon10) {
  98. xy_x = xy_y = 0.;
  99. break;
  100. }
  101. //phi1 = par.phi0; lam1 = par.lam0;
  102. //phi2 = lp_lat; lam2 = lp_lon + par.lam0;
  103. formula::result_inverse<T> const inv =
  104. formula::vincenty_inverse
  105. <
  106. T, true, true
  107. >::apply(par.lam0, par.phi0, lp_lon + par.lam0, lp_lat, srs::spheroid<T>(par.a, proj_parm.b));
  108. //azi1 = inv.azimuth; s12 = inv.distance;
  109. xy_x = inv.distance * sin(inv.azimuth) / par.a;
  110. xy_y = inv.distance * cos(inv.azimuth) / par.a;
  111. break;
  112. }
  113. }
  114. template <typename T, typename Par, typename ProjParm>
  115. inline void e_inverse(T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat, Par const& par, ProjParm const& proj_parm)
  116. {
  117. T c;
  118. if ((c = boost::math::hypot(xy_x, xy_y)) < epsilon10) {
  119. lp_lat = par.phi0;
  120. lp_lon = 0.;
  121. return;
  122. }
  123. if (proj_parm.mode == obliq || proj_parm.mode == equit) {
  124. T const x2 = xy_x * par.a;
  125. T const y2 = xy_y * par.a;
  126. //T const lat1 = par.phi0;
  127. //T const lon1 = par.lam0;
  128. T const azi1 = atan2(x2, y2);
  129. T const s12 = sqrt(x2 * x2 + y2 * y2);
  130. formula::result_direct<T> const dir =
  131. formula::vincenty_direct
  132. <
  133. T, true
  134. >::apply(par.lam0, par.phi0, s12, azi1, srs::spheroid<T>(par.a, proj_parm.b));
  135. lp_lat = dir.lat2;
  136. lp_lon = dir.lon2;
  137. lp_lon -= par.lam0;
  138. } else { /* Polar */
  139. lp_lat = pj_inv_mlfn(proj_parm.mode == n_pole ? proj_parm.Mp - c : proj_parm.Mp + c,
  140. par.es, proj_parm.en);
  141. lp_lon = atan2(xy_x, proj_parm.mode == n_pole ? -xy_y : xy_y);
  142. }
  143. }
  144. template <typename T, typename Par, typename ProjParm>
  145. inline void e_guam_fwd(T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y, Par const& par, ProjParm const& proj_parm)
  146. {
  147. T cosphi, sinphi, t;
  148. cosphi = cos(lp_lat);
  149. sinphi = sin(lp_lat);
  150. t = 1. / sqrt(1. - par.es * sinphi * sinphi);
  151. xy_x = lp_lon * cosphi * t;
  152. xy_y = pj_mlfn(lp_lat, sinphi, cosphi, proj_parm.en) - proj_parm.M1 +
  153. .5 * lp_lon * lp_lon * cosphi * sinphi * t;
  154. }
  155. template <typename T, typename Par, typename ProjParm>
  156. inline void e_guam_inv(T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat, Par const& par, ProjParm const& proj_parm)
  157. {
  158. T x2, t = 0.0;
  159. int i;
  160. x2 = 0.5 * xy_x * xy_x;
  161. lp_lat = par.phi0;
  162. for (i = 0; i < 3; ++i) {
  163. t = par.e * sin(lp_lat);
  164. lp_lat = pj_inv_mlfn(proj_parm.M1 + xy_y -
  165. x2 * tan(lp_lat) * (t = sqrt(1. - t * t)), par.es, proj_parm.en);
  166. }
  167. lp_lon = xy_x * t / cos(lp_lat);
  168. }
  169. template <typename T, typename Par, typename ProjParm>
  170. inline void s_forward(T const& lp_lon, T lp_lat, T& xy_x, T& xy_y, Par const& /*par*/, ProjParm const& proj_parm)
  171. {
  172. static const T half_pi = detail::half_pi<T>();
  173. T coslam, cosphi, sinphi;
  174. sinphi = sin(lp_lat);
  175. cosphi = cos(lp_lat);
  176. coslam = cos(lp_lon);
  177. switch (proj_parm.mode) {
  178. case equit:
  179. xy_y = cosphi * coslam;
  180. goto oblcon;
  181. case obliq:
  182. xy_y = proj_parm.sinph0 * sinphi + proj_parm.cosph0 * cosphi * coslam;
  183. oblcon:
  184. if (fabs(fabs(xy_y) - 1.) < tolerance)
  185. if (xy_y < 0.)
  186. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  187. else
  188. xy_x = xy_y = 0.;
  189. else {
  190. xy_y = acos(xy_y);
  191. xy_y /= sin(xy_y);
  192. xy_x = xy_y * cosphi * sin(lp_lon);
  193. xy_y *= (proj_parm.mode == equit) ? sinphi :
  194. proj_parm.cosph0 * sinphi - proj_parm.sinph0 * cosphi * coslam;
  195. }
  196. break;
  197. case n_pole:
  198. lp_lat = -lp_lat;
  199. coslam = -coslam;
  200. BOOST_FALLTHROUGH;
  201. case s_pole:
  202. if (fabs(lp_lat - half_pi) < epsilon10)
  203. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  204. xy_x = (xy_y = (half_pi + lp_lat)) * sin(lp_lon);
  205. xy_y *= coslam;
  206. break;
  207. }
  208. }
  209. template <typename T, typename Par, typename ProjParm>
  210. inline void s_inverse(T xy_x, T xy_y, T& lp_lon, T& lp_lat, Par const& par, ProjParm const& proj_parm)
  211. {
  212. static const T pi = detail::pi<T>();
  213. static const T half_pi = detail::half_pi<T>();
  214. T cosc, c_rh, sinc;
  215. if ((c_rh = boost::math::hypot(xy_x, xy_y)) > pi) {
  216. if (c_rh - epsilon10 > pi)
  217. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  218. c_rh = pi;
  219. } else if (c_rh < epsilon10) {
  220. lp_lat = par.phi0;
  221. lp_lon = 0.;
  222. return;
  223. }
  224. if (proj_parm.mode == obliq || proj_parm.mode == equit) {
  225. sinc = sin(c_rh);
  226. cosc = cos(c_rh);
  227. if (proj_parm.mode == equit) {
  228. lp_lat = aasin(xy_y * sinc / c_rh);
  229. xy_x *= sinc;
  230. xy_y = cosc * c_rh;
  231. } else {
  232. lp_lat = aasin(cosc * proj_parm.sinph0 + xy_y * sinc * proj_parm.cosph0 /
  233. c_rh);
  234. xy_y = (cosc - proj_parm.sinph0 * sin(lp_lat)) * c_rh;
  235. xy_x *= sinc * proj_parm.cosph0;
  236. }
  237. lp_lon = xy_y == 0. ? 0. : atan2(xy_x, xy_y);
  238. } else if (proj_parm.mode == n_pole) {
  239. lp_lat = half_pi - c_rh;
  240. lp_lon = atan2(xy_x, -xy_y);
  241. } else {
  242. lp_lat = c_rh - half_pi;
  243. lp_lon = atan2(xy_x, xy_y);
  244. }
  245. }
  246. // Azimuthal Equidistant
  247. template <typename Params, typename Parameters, typename T>
  248. inline void setup_aeqd(Params const& params, Parameters& par, par_aeqd<T>& proj_parm, bool is_sphere, bool is_guam)
  249. {
  250. static const T half_pi = detail::half_pi<T>();
  251. par.phi0 = pj_get_param_r<T, srs::spar::lat_0>(params, "lat_0", srs::dpar::lat_0);
  252. if (fabs(fabs(par.phi0) - half_pi) < epsilon10) {
  253. proj_parm.mode = par.phi0 < 0. ? s_pole : n_pole;
  254. proj_parm.sinph0 = par.phi0 < 0. ? -1. : 1.;
  255. proj_parm.cosph0 = 0.;
  256. } else if (fabs(par.phi0) < epsilon10) {
  257. proj_parm.mode = equit;
  258. proj_parm.sinph0 = 0.;
  259. proj_parm.cosph0 = 1.;
  260. } else {
  261. proj_parm.mode = obliq;
  262. proj_parm.sinph0 = sin(par.phi0);
  263. proj_parm.cosph0 = cos(par.phi0);
  264. }
  265. if (is_sphere) {
  266. /* empty */
  267. } else {
  268. proj_parm.en = pj_enfn<T>(par.es);
  269. if (is_guam) {
  270. proj_parm.M1 = pj_mlfn(par.phi0, proj_parm.sinph0, proj_parm.cosph0, proj_parm.en);
  271. } else {
  272. switch (proj_parm.mode) {
  273. case n_pole:
  274. proj_parm.Mp = pj_mlfn<T>(half_pi, 1., 0., proj_parm.en);
  275. break;
  276. case s_pole:
  277. proj_parm.Mp = pj_mlfn<T>(-half_pi, -1., 0., proj_parm.en);
  278. break;
  279. case equit:
  280. case obliq:
  281. //proj_parm.N1 = 1. / sqrt(1. - par.es * proj_parm.sinph0 * proj_parm.sinph0);
  282. //proj_parm.G = proj_parm.sinph0 * (proj_parm.He = par.e / sqrt(par.one_es));
  283. //proj_parm.He *= proj_parm.cosph0;
  284. break;
  285. }
  286. // Boost.Geometry specific, in proj4 geodesic is initialized at the beginning
  287. proj_parm.b = math::sqrt(math::sqr(par.a) * (1. - par.es));
  288. }
  289. }
  290. }
  291. template <typename T, typename Parameters>
  292. struct base_aeqd_e
  293. {
  294. par_aeqd<T> m_proj_parm;
  295. // FORWARD(e_forward) elliptical
  296. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  297. inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  298. {
  299. e_forward(lp_lon, lp_lat, xy_x, xy_y, par, this->m_proj_parm);
  300. }
  301. // INVERSE(e_inverse) elliptical
  302. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  303. inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  304. {
  305. e_inverse(xy_x, xy_y, lp_lon, lp_lat, par, this->m_proj_parm);
  306. }
  307. static inline std::string get_name()
  308. {
  309. return "aeqd_e";
  310. }
  311. };
  312. template <typename T, typename Parameters>
  313. struct base_aeqd_e_guam
  314. {
  315. par_aeqd<T> m_proj_parm;
  316. // FORWARD(e_guam_fwd) Guam elliptical
  317. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  318. inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  319. {
  320. e_guam_fwd(lp_lon, lp_lat, xy_x, xy_y, par, this->m_proj_parm);
  321. }
  322. // INVERSE(e_guam_inv) Guam elliptical
  323. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  324. inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  325. {
  326. e_guam_inv(xy_x, xy_y, lp_lon, lp_lat, par, this->m_proj_parm);
  327. }
  328. static inline std::string get_name()
  329. {
  330. return "aeqd_e_guam";
  331. }
  332. };
  333. template <typename T, typename Parameters>
  334. struct base_aeqd_s
  335. {
  336. par_aeqd<T> m_proj_parm;
  337. // FORWARD(s_forward) spherical
  338. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  339. inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  340. {
  341. s_forward(lp_lon, lp_lat, xy_x, xy_y, par, this->m_proj_parm);
  342. }
  343. // INVERSE(s_inverse) spherical
  344. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  345. inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  346. {
  347. s_inverse(xy_x, xy_y, lp_lon, lp_lat, par, this->m_proj_parm);
  348. }
  349. static inline std::string get_name()
  350. {
  351. return "aeqd_s";
  352. }
  353. };
  354. }} // namespace detail::aeqd
  355. #endif // doxygen
  356. /*!
  357. \brief Azimuthal Equidistant projection
  358. \ingroup projections
  359. \tparam Geographic latlong point type
  360. \tparam Cartesian xy point type
  361. \tparam Parameters parameter type
  362. \par Projection characteristics
  363. - Azimuthal
  364. - Spheroid
  365. - Ellipsoid
  366. \par Projection parameters
  367. - lat_0: Latitude of origin (degrees)
  368. - guam (boolean)
  369. \par Example
  370. \image html ex_aeqd.gif
  371. */
  372. template <typename T, typename Parameters>
  373. struct aeqd_e : public detail::aeqd::base_aeqd_e<T, Parameters>
  374. {
  375. template <typename Params>
  376. inline aeqd_e(Params const& params, Parameters & par)
  377. {
  378. detail::aeqd::setup_aeqd(params, par, this->m_proj_parm, false, false);
  379. }
  380. };
  381. /*!
  382. \brief Azimuthal Equidistant projection
  383. \ingroup projections
  384. \tparam Geographic latlong point type
  385. \tparam Cartesian xy point type
  386. \tparam Parameters parameter type
  387. \par Projection characteristics
  388. - Azimuthal
  389. - Spheroid
  390. - Ellipsoid
  391. \par Projection parameters
  392. - lat_0: Latitude of origin (degrees)
  393. - guam (boolean)
  394. \par Example
  395. \image html ex_aeqd.gif
  396. */
  397. template <typename T, typename Parameters>
  398. struct aeqd_e_guam : public detail::aeqd::base_aeqd_e_guam<T, Parameters>
  399. {
  400. template <typename Params>
  401. inline aeqd_e_guam(Params const& params, Parameters & par)
  402. {
  403. detail::aeqd::setup_aeqd(params, par, this->m_proj_parm, false, true);
  404. }
  405. };
  406. /*!
  407. \brief Azimuthal Equidistant projection
  408. \ingroup projections
  409. \tparam Geographic latlong point type
  410. \tparam Cartesian xy point type
  411. \tparam Parameters parameter type
  412. \par Projection characteristics
  413. - Azimuthal
  414. - Spheroid
  415. - Ellipsoid
  416. \par Projection parameters
  417. - lat_0: Latitude of origin (degrees)
  418. - guam (boolean)
  419. \par Example
  420. \image html ex_aeqd.gif
  421. */
  422. template <typename T, typename Parameters>
  423. struct aeqd_s : public detail::aeqd::base_aeqd_s<T, Parameters>
  424. {
  425. template <typename Params>
  426. inline aeqd_s(Params const& params, Parameters & par)
  427. {
  428. detail::aeqd::setup_aeqd(params, par, this->m_proj_parm, true, false);
  429. }
  430. };
  431. #ifndef DOXYGEN_NO_DETAIL
  432. namespace detail
  433. {
  434. // Static projection
  435. template <typename BGP, typename CT, typename P>
  436. struct static_projection_type<srs::spar::proj_aeqd, srs_sphere_tag, BGP, CT, P>
  437. {
  438. typedef static_wrapper_fi<aeqd_s<CT, P>, P> type;
  439. };
  440. template <typename BGP, typename CT, typename P>
  441. struct static_projection_type<srs::spar::proj_aeqd, srs_spheroid_tag, BGP, CT, P>
  442. {
  443. typedef static_wrapper_fi
  444. <
  445. std::conditional_t
  446. <
  447. std::is_void
  448. <
  449. typename geometry::tuples::find_if
  450. <
  451. BGP,
  452. //srs::par4::detail::is_guam
  453. srs::spar::detail::is_param<srs::spar::guam>::pred
  454. >::type
  455. >::value,
  456. aeqd_e<CT, P>,
  457. aeqd_e_guam<CT, P>
  458. >
  459. , P
  460. > type;
  461. };
  462. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_BEGIN(aeqd_entry)
  463. {
  464. bool const guam = pj_get_param_b<srs::spar::guam>(params, "guam", srs::dpar::guam);
  465. if (parameters.es && ! guam)
  466. return new dynamic_wrapper_fi<aeqd_e<T, Parameters>, T, Parameters>(params, parameters);
  467. else if (parameters.es && guam)
  468. return new dynamic_wrapper_fi<aeqd_e_guam<T, Parameters>, T, Parameters>(params, parameters);
  469. else
  470. return new dynamic_wrapper_fi<aeqd_s<T, Parameters>, T, Parameters>(params, parameters);
  471. }
  472. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_END
  473. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(aeqd_init)
  474. {
  475. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(aeqd, aeqd_entry)
  476. }
  477. } // namespace detail
  478. #endif // doxygen
  479. } // namespace projections
  480. }} // namespace boost::geometry
  481. #endif // BOOST_GEOMETRY_PROJECTIONS_AEQD_HPP