omerc.hpp 15 KB

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