omerc.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326
  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, 2022.
  4. // Modifications copyright (c) 2017-2022, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. // This file is converted from PROJ4, http://trac.osgeo.org/proj
  11. // PROJ4 is originally written by Gerald Evenden (then of the USGS)
  12. // PROJ4 is maintained by Frank Warmerdam
  13. // PROJ4 is converted to Boost.Geometry by Barend Gehrels
  14. // Last updated version of proj: 5.0.0
  15. // Original copyright notice:
  16. // Copyright (c) 2003, 2006 Gerald I. Evenden
  17. // Permission is hereby granted, free of charge, to any person obtaining a
  18. // copy of this software and associated documentation files (the "Software"),
  19. // to deal in the Software without restriction, including without limitation
  20. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  21. // and/or sell copies of the Software, and to permit persons to whom the
  22. // Software is furnished to do so, subject to the following conditions:
  23. // The above copyright notice and this permission notice shall be included
  24. // in all copies or substantial portions of the Software.
  25. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  26. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  27. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  28. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  29. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  30. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  31. // DEALINGS IN THE SOFTWARE.
  32. #ifndef BOOST_GEOMETRY_PROJECTIONS_OMERC_HPP
  33. #define BOOST_GEOMETRY_PROJECTIONS_OMERC_HPP
  34. #include <boost/geometry/util/math.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_phi2.hpp>
  40. #include <boost/geometry/srs/projections/impl/pj_tsfn.hpp>
  41. #include <boost/geometry/srs/projections/impl/projects.hpp>
  42. namespace boost { namespace geometry
  43. {
  44. namespace projections
  45. {
  46. #ifndef DOXYGEN_NO_DETAIL
  47. namespace detail { namespace omerc
  48. {
  49. template <typename T>
  50. struct par_omerc
  51. {
  52. T A, B, E, AB, ArB, BrA, rB, singam, cosgam, sinrot, cosrot;
  53. T v_pole_n, v_pole_s, u_0;
  54. bool no_rot;
  55. };
  56. static const double tolerance = 1.e-7;
  57. static const double epsilon = 1.e-10;
  58. template <typename T, typename Parameters>
  59. struct base_omerc_ellipsoid
  60. {
  61. par_omerc<T> m_proj_parm;
  62. // FORWARD(e_forward) ellipsoid
  63. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  64. inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x,
  65. T& xy_y) const
  66. {
  67. static const T half_pi = detail::half_pi<T>();
  68. T s, t, U, V, W, temp, u, v;
  69. if (fabs(fabs(lp_lat) - half_pi) > epsilon) {
  70. W = this->m_proj_parm.E / math::pow(pj_tsfn(lp_lat, sin(lp_lat), par.e),
  71. this->m_proj_parm.B);
  72. temp = 1. / W;
  73. s = .5 * (W - temp);
  74. t = .5 * (W + temp);
  75. V = sin(this->m_proj_parm.B * lp_lon);
  76. U = (s * this->m_proj_parm.singam - V * this->m_proj_parm.cosgam) / t;
  77. if (fabs(fabs(U) - 1.0) < epsilon) {
  78. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  79. }
  80. v = 0.5 * this->m_proj_parm.ArB * log((1. - U)/(1. + U));
  81. temp = cos(this->m_proj_parm.B * lp_lon);
  82. if(fabs(temp) < tolerance) {
  83. u = this->m_proj_parm.A * lp_lon;
  84. } else {
  85. u = this->m_proj_parm.ArB * atan2((s * this->m_proj_parm.cosgam
  86. + V * this->m_proj_parm.singam), temp);
  87. }
  88. } else {
  89. v = lp_lat > 0 ? this->m_proj_parm.v_pole_n : this->m_proj_parm.v_pole_s;
  90. u = this->m_proj_parm.ArB * lp_lat;
  91. }
  92. if (this->m_proj_parm.no_rot) {
  93. xy_x = u;
  94. xy_y = v;
  95. } else {
  96. u -= this->m_proj_parm.u_0;
  97. xy_x = v * this->m_proj_parm.cosrot + u * this->m_proj_parm.sinrot;
  98. xy_y = u * this->m_proj_parm.cosrot - v * this->m_proj_parm.sinrot;
  99. }
  100. }
  101. // INVERSE(e_inverse) ellipsoid
  102. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  103. inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon,
  104. T& lp_lat) const
  105. {
  106. static const T half_pi = detail::half_pi<T>();
  107. T u, v, Qp, Sp, Tp, Vp, Up;
  108. if (this->m_proj_parm.no_rot) {
  109. v = xy_y;
  110. u = xy_x;
  111. } else {
  112. v = xy_x * this->m_proj_parm.cosrot - xy_y * this->m_proj_parm.sinrot;
  113. u = xy_y * this->m_proj_parm.cosrot + xy_x * this->m_proj_parm.sinrot
  114. + this->m_proj_parm.u_0;
  115. }
  116. Qp = exp(- this->m_proj_parm.BrA * v);
  117. Sp = .5 * (Qp - 1. / Qp);
  118. Tp = .5 * (Qp + 1. / Qp);
  119. Vp = sin(this->m_proj_parm.BrA * u);
  120. Up = (Vp * this->m_proj_parm.cosgam + Sp * this->m_proj_parm.singam) / Tp;
  121. if (fabs(fabs(Up) - 1.) < epsilon) {
  122. lp_lon = 0.;
  123. lp_lat = Up < 0. ? -half_pi : half_pi;
  124. } else {
  125. lp_lat = this->m_proj_parm.E / sqrt((1. + Up) / (1. - Up));
  126. if ((lp_lat = pj_phi2(math::pow(lp_lat, T(1) / this->m_proj_parm.B), par.e))
  127. == HUGE_VAL) {
  128. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  129. }
  130. lp_lon = - this->m_proj_parm.rB * atan2((Sp * this->m_proj_parm.cosgam -
  131. Vp * this->m_proj_parm.singam), cos(this->m_proj_parm.BrA * u));
  132. }
  133. }
  134. static inline std::string get_name()
  135. {
  136. return "omerc_ellipsoid";
  137. }
  138. };
  139. // Oblique Mercator
  140. template <typename Params, typename Parameters, typename T>
  141. inline void setup_omerc(Params const& params, Parameters & par, par_omerc<T>& proj_parm)
  142. {
  143. static const T fourth_pi = detail::fourth_pi<T>();
  144. static const T half_pi = detail::half_pi<T>();
  145. static const T pi = detail::pi<T>();
  146. static const T two_pi = detail::two_pi<T>();
  147. T con, com, cosph0, D, F, H, L, sinph0, p, J, gamma=0,
  148. gamma0, lamc=0, lam1=0, lam2=0, phi1=0, phi2=0, alpha_c=0;
  149. int alp, gam, no_off = 0;
  150. proj_parm.no_rot = pj_get_param_b<srs::spar::no_rot>(params, "no_rot",
  151. srs::dpar::no_rot);
  152. alp = pj_param_r<srs::spar::alpha>(params, "alpha", srs::dpar::alpha, alpha_c);
  153. gam = pj_param_r<srs::spar::gamma>(params, "gamma", srs::dpar::gamma, gamma);
  154. if (alp || gam) {
  155. lamc = pj_get_param_r<T, srs::spar::lonc>(params, "lonc", srs::dpar::lonc);
  156. // NOTE: This is needed for Hotline Oblique Mercator variant A projection
  157. no_off = pj_get_param_b<srs::spar::no_off>(params, "no_off", srs::dpar::no_off);
  158. } else {
  159. lam1 = pj_get_param_r<T, srs::spar::lon_1>(params, "lon_1", srs::dpar::lon_1);
  160. phi1 = pj_get_param_r<T, srs::spar::lat_1>(params, "lat_1", srs::dpar::lat_1);
  161. lam2 = pj_get_param_r<T, srs::spar::lon_2>(params, "lon_2", srs::dpar::lon_2);
  162. phi2 = pj_get_param_r<T, srs::spar::lat_2>(params, "lat_2", srs::dpar::lat_2);
  163. if (fabs(phi1 - phi2) <= tolerance ||
  164. (con = fabs(phi1)) <= tolerance ||
  165. fabs(con - half_pi) <= tolerance ||
  166. fabs(fabs(par.phi0) - half_pi) <= tolerance ||
  167. fabs(fabs(phi2) - half_pi) <= tolerance)
  168. BOOST_THROW_EXCEPTION( projection_exception(error_lat_0_or_alpha_eq_90) );
  169. }
  170. com = sqrt(par.one_es);
  171. if (fabs(par.phi0) > epsilon) {
  172. sinph0 = sin(par.phi0);
  173. cosph0 = cos(par.phi0);
  174. con = 1. - par.es * sinph0 * sinph0;
  175. proj_parm.B = cosph0 * cosph0;
  176. proj_parm.B = sqrt(1. + par.es * proj_parm.B * proj_parm.B / par.one_es);
  177. proj_parm.A = proj_parm.B * par.k0 * com / con;
  178. D = proj_parm.B * com / (cosph0 * sqrt(con));
  179. if ((F = D * D - 1.) <= 0.)
  180. F = 0.;
  181. else {
  182. F = sqrt(F);
  183. if (par.phi0 < 0.)
  184. F = -F;
  185. }
  186. proj_parm.E = F += D;
  187. proj_parm.E *= math::pow(pj_tsfn(par.phi0, sinph0, par.e), proj_parm.B);
  188. } else {
  189. proj_parm.B = 1. / com;
  190. proj_parm.A = par.k0;
  191. proj_parm.E = D = F = 1.;
  192. }
  193. if (alp || gam) {
  194. if (alp) {
  195. gamma0 = aasin(sin(alpha_c) / D);
  196. if (!gam)
  197. gamma = alpha_c;
  198. } else
  199. alpha_c = aasin(D*sin(gamma0 = gamma));
  200. par.lam0 = lamc - aasin(.5 * (F - 1. / F) *
  201. tan(gamma0)) / proj_parm.B;
  202. } else {
  203. H = math::pow(pj_tsfn(phi1, sin(phi1), par.e), proj_parm.B);
  204. L = math::pow(pj_tsfn(phi2, sin(phi2), par.e), proj_parm.B);
  205. F = proj_parm.E / H;
  206. p = (L - H) / (L + H);
  207. J = proj_parm.E * proj_parm.E;
  208. J = (J - L * H) / (J + L * H);
  209. if ((con = lam1 - lam2) < -pi)
  210. lam2 -= two_pi;
  211. else if (con > pi)
  212. lam2 += two_pi;
  213. par.lam0 = adjlon(.5 * (lam1 + lam2) - atan(
  214. J * tan(.5 * proj_parm.B * (lam1 - lam2)) / p) / proj_parm.B);
  215. gamma0 = atan(2. * sin(proj_parm.B * adjlon(lam1 - par.lam0)) /
  216. (F - 1. / F));
  217. gamma = alpha_c = aasin(D * sin(gamma0));
  218. }
  219. proj_parm.singam = sin(gamma0);
  220. proj_parm.cosgam = cos(gamma0);
  221. proj_parm.sinrot = sin(gamma);
  222. proj_parm.cosrot = cos(gamma);
  223. proj_parm.BrA = 1. / (proj_parm.ArB = proj_parm.A * (proj_parm.rB = 1. / proj_parm.B));
  224. proj_parm.AB = proj_parm.A * proj_parm.B;
  225. if (no_off)
  226. proj_parm.u_0 = 0;
  227. else {
  228. proj_parm.u_0 = fabs(proj_parm.ArB * atan(sqrt(D * D - 1.) / cos(alpha_c)));
  229. if (par.phi0 < 0.)
  230. proj_parm.u_0 = - proj_parm.u_0;
  231. }
  232. F = 0.5 * gamma0;
  233. proj_parm.v_pole_n = proj_parm.ArB * log(tan(fourth_pi - F));
  234. proj_parm.v_pole_s = proj_parm.ArB * log(tan(fourth_pi + F));
  235. }
  236. }} // namespace detail::omerc
  237. #endif // doxygen
  238. /*!
  239. \brief Oblique Mercator projection
  240. \ingroup projections
  241. \tparam Geographic latlong point type
  242. \tparam Cartesian xy point type
  243. \tparam Parameters parameter type
  244. \par Projection characteristics
  245. - Cylindrical
  246. - Spheroid
  247. - Ellipsoid
  248. \par Projection parameters
  249. - no_rot: No rotation
  250. - alpha: Alpha (degrees)
  251. - gamma: Gamma (degrees)
  252. - no_off: Do not offset origin to center of projection
  253. (useful for Hotline Oblique Mercator variant A).
  254. - lonc: Longitude (only used if alpha (or gamma) is specified) (degrees)
  255. - lon_1 (degrees)
  256. - lat_1: Latitude of first standard parallel (degrees)
  257. - lon_2 (degrees)
  258. - lat_2: Latitude of second standard parallel (degrees)
  259. - no_uoff: deprecated (string)
  260. \par Example
  261. \image html ex_omerc.gif
  262. */
  263. template <typename T, typename Parameters>
  264. struct omerc_ellipsoid : public detail::omerc::base_omerc_ellipsoid<T, Parameters>
  265. {
  266. template <typename Params>
  267. inline omerc_ellipsoid(Params const& params, Parameters & par)
  268. {
  269. detail::omerc::setup_omerc(params, par, this->m_proj_parm);
  270. }
  271. };
  272. #ifndef DOXYGEN_NO_DETAIL
  273. namespace detail
  274. {
  275. // Static projection
  276. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_omerc, omerc_ellipsoid)
  277. // Factory entry(s)
  278. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(omerc_entry, omerc_ellipsoid)
  279. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(omerc_init)
  280. {
  281. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(omerc, omerc_entry)
  282. }
  283. } // namespace detail
  284. #endif // doxygen
  285. } // namespace projections
  286. }} // namespace boost::geometry
  287. #endif // BOOST_GEOMETRY_PROJECTIONS_OMERC_HPP