omerc.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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/projects.hpp>
  37. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  38. #include <boost/geometry/srs/projections/impl/pj_phi2.hpp>
  39. #include <boost/geometry/srs/projections/impl/pj_tsfn.hpp>
  40. namespace boost { namespace geometry
  41. {
  42. namespace srs { namespace par4
  43. {
  44. struct omerc {}; // Oblique Mercator
  45. }} //namespace srs::par4
  46. namespace projections
  47. {
  48. #ifndef DOXYGEN_NO_DETAIL
  49. namespace detail { namespace omerc
  50. {
  51. template <typename T>
  52. struct par_omerc
  53. {
  54. T A, B, E, AB, ArB, BrA, rB, singam, cosgam, sinrot, cosrot;
  55. T v_pole_n, v_pole_s, u_0;
  56. int no_rot;
  57. };
  58. static const double tolerance = 1.e-7;
  59. static const double epsilon = 1.e-10;
  60. // template class, using CRTP to implement forward/inverse
  61. template <typename T, typename Parameters>
  62. struct base_omerc_ellipsoid
  63. : public base_t_fi<base_omerc_ellipsoid<T, Parameters>, T, Parameters>
  64. {
  65. par_omerc<T> m_proj_parm;
  66. inline base_omerc_ellipsoid(const Parameters& par)
  67. : base_t_fi<base_omerc_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  68. {}
  69. // FORWARD(e_forward) ellipsoid
  70. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  71. inline void fwd(T& lp_lon, T& lp_lat, T& xy_x, T& xy_y) const
  72. {
  73. static const T half_pi = detail::half_pi<T>();
  74. T s, t, U, V, W, temp, u, v;
  75. if (fabs(fabs(lp_lat) - half_pi) > epsilon) {
  76. W = this->m_proj_parm.E / math::pow(pj_tsfn(lp_lat, sin(lp_lat), this->m_par.e), this->m_proj_parm.B);
  77. temp = 1. / W;
  78. s = .5 * (W - temp);
  79. t = .5 * (W + temp);
  80. V = sin(this->m_proj_parm.B * lp_lon);
  81. U = (s * this->m_proj_parm.singam - V * this->m_proj_parm.cosgam) / t;
  82. if (fabs(fabs(U) - 1.0) < epsilon) {
  83. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  84. }
  85. v = 0.5 * this->m_proj_parm.ArB * log((1. - U)/(1. + U));
  86. temp = cos(this->m_proj_parm.B * lp_lon);
  87. if(fabs(temp) < tolerance) {
  88. u = this->m_proj_parm.A * lp_lon;
  89. } else {
  90. u = this->m_proj_parm.ArB * atan2((s * this->m_proj_parm.cosgam + V * this->m_proj_parm.singam), temp);
  91. }
  92. } else {
  93. v = lp_lat > 0 ? this->m_proj_parm.v_pole_n : this->m_proj_parm.v_pole_s;
  94. u = this->m_proj_parm.ArB * lp_lat;
  95. }
  96. if (this->m_proj_parm.no_rot) {
  97. xy_x = u;
  98. xy_y = v;
  99. } else {
  100. u -= this->m_proj_parm.u_0;
  101. xy_x = v * this->m_proj_parm.cosrot + u * this->m_proj_parm.sinrot;
  102. xy_y = u * this->m_proj_parm.cosrot - v * this->m_proj_parm.sinrot;
  103. }
  104. }
  105. // INVERSE(e_inverse) ellipsoid
  106. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  107. inline void inv(T& xy_x, T& xy_y, T& lp_lon, T& lp_lat) const
  108. {
  109. static const T half_pi = detail::half_pi<T>();
  110. T u, v, Qp, Sp, Tp, Vp, Up;
  111. if (this->m_proj_parm.no_rot) {
  112. v = xy_y;
  113. u = xy_x;
  114. } else {
  115. v = xy_x * this->m_proj_parm.cosrot - xy_y * this->m_proj_parm.sinrot;
  116. u = xy_y * this->m_proj_parm.cosrot + xy_x * this->m_proj_parm.sinrot + this->m_proj_parm.u_0;
  117. }
  118. Qp = exp(- this->m_proj_parm.BrA * v);
  119. Sp = .5 * (Qp - 1. / Qp);
  120. Tp = .5 * (Qp + 1. / Qp);
  121. Vp = sin(this->m_proj_parm.BrA * u);
  122. Up = (Vp * this->m_proj_parm.cosgam + Sp * this->m_proj_parm.singam) / Tp;
  123. if (fabs(fabs(Up) - 1.) < epsilon) {
  124. lp_lon = 0.;
  125. lp_lat = Up < 0. ? -half_pi : half_pi;
  126. } else {
  127. lp_lat = this->m_proj_parm.E / sqrt((1. + Up) / (1. - Up));
  128. if ((lp_lat = pj_phi2(math::pow(lp_lat, T(1) / this->m_proj_parm.B), this->m_par.e)) == HUGE_VAL) {
  129. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  130. }
  131. lp_lon = - this->m_proj_parm.rB * atan2((Sp * this->m_proj_parm.cosgam -
  132. Vp * this->m_proj_parm.singam), cos(this->m_proj_parm.BrA * u));
  133. }
  134. }
  135. static inline std::string get_name()
  136. {
  137. return "omerc_ellipsoid";
  138. }
  139. };
  140. // Oblique Mercator
  141. template <typename Parameters, typename T>
  142. inline void setup_omerc(Parameters& par, par_omerc<T>& proj_parm)
  143. {
  144. static const T fourth_pi = detail::fourth_pi<T>();
  145. static const T half_pi = detail::half_pi<T>();
  146. static const T pi = detail::pi<T>();
  147. static const T two_pi = detail::two_pi<T>();
  148. T con, com, cosph0, D, F, H, L, sinph0, p, J, gamma=0,
  149. gamma0, lamc=0, lam1=0, lam2=0, phi1=0, phi2=0, alpha_c=0;
  150. int alp, gam, no_off = 0;
  151. proj_parm.no_rot = pj_get_param_b(par.params, "no_rot");
  152. alp = pj_param_r(par.params, "alpha", alpha_c);
  153. gam = pj_param_r(par.params, "gamma", gamma);
  154. if (alp || gam) {
  155. lamc = pj_get_param_r(par.params, "lonc");
  156. // NOTE: This is not needed in Boost.Geometry
  157. //no_off =
  158. // /* For libproj4 compatability */
  159. // pj_param_exists(par.params, "no_off")
  160. // /* for backward compatibility */
  161. // || pj_param_exists(par.params, "no_uoff");
  162. //if( no_off )
  163. //{
  164. // /* Mark the parameter as used, so that the pj_get_def() return them */
  165. // pj_get_param_s(par.params, "no_uoff");
  166. // pj_get_param_s(par.params, "no_off");
  167. //}
  168. } else {
  169. lam1 = pj_get_param_r(par.params, "lon_1");
  170. phi1 = pj_get_param_r(par.params, "lat_1");
  171. lam2 = pj_get_param_r(par.params, "lon_2");
  172. phi2 = pj_get_param_r(par.params, "lat_2");
  173. if (fabs(phi1 - phi2) <= tolerance ||
  174. (con = fabs(phi1)) <= tolerance ||
  175. fabs(con - half_pi) <= tolerance ||
  176. fabs(fabs(par.phi0) - half_pi) <= tolerance ||
  177. fabs(fabs(phi2) - half_pi) <= tolerance)
  178. BOOST_THROW_EXCEPTION( projection_exception(error_lat_0_or_alpha_eq_90) );
  179. }
  180. com = sqrt(par.one_es);
  181. if (fabs(par.phi0) > epsilon) {
  182. sinph0 = sin(par.phi0);
  183. cosph0 = cos(par.phi0);
  184. con = 1. - par.es * sinph0 * sinph0;
  185. proj_parm.B = cosph0 * cosph0;
  186. proj_parm.B = sqrt(1. + par.es * proj_parm.B * proj_parm.B / par.one_es);
  187. proj_parm.A = proj_parm.B * par.k0 * com / con;
  188. D = proj_parm.B * com / (cosph0 * sqrt(con));
  189. if ((F = D * D - 1.) <= 0.)
  190. F = 0.;
  191. else {
  192. F = sqrt(F);
  193. if (par.phi0 < 0.)
  194. F = -F;
  195. }
  196. proj_parm.E = F += D;
  197. proj_parm.E *= math::pow(pj_tsfn(par.phi0, sinph0, par.e), proj_parm.B);
  198. } else {
  199. proj_parm.B = 1. / com;
  200. proj_parm.A = par.k0;
  201. proj_parm.E = D = F = 1.;
  202. }
  203. if (alp || gam) {
  204. if (alp) {
  205. gamma0 = aasin(sin(alpha_c) / D);
  206. if (!gam)
  207. gamma = alpha_c;
  208. } else
  209. alpha_c = aasin(D*sin(gamma0 = gamma));
  210. par.lam0 = lamc - aasin(.5 * (F - 1. / F) *
  211. tan(gamma0)) / proj_parm.B;
  212. } else {
  213. H = math::pow(pj_tsfn(phi1, sin(phi1), par.e), proj_parm.B);
  214. L = math::pow(pj_tsfn(phi2, sin(phi2), par.e), proj_parm.B);
  215. F = proj_parm.E / H;
  216. p = (L - H) / (L + H);
  217. J = proj_parm.E * proj_parm.E;
  218. J = (J - L * H) / (J + L * H);
  219. if ((con = lam1 - lam2) < -pi)
  220. lam2 -= two_pi;
  221. else if (con > pi)
  222. lam2 += two_pi;
  223. par.lam0 = adjlon(.5 * (lam1 + lam2) - atan(
  224. J * tan(.5 * proj_parm.B * (lam1 - lam2)) / p) / proj_parm.B);
  225. gamma0 = atan(2. * sin(proj_parm.B * adjlon(lam1 - par.lam0)) /
  226. (F - 1. / F));
  227. gamma = alpha_c = aasin(D * sin(gamma0));
  228. }
  229. proj_parm.singam = sin(gamma0);
  230. proj_parm.cosgam = cos(gamma0);
  231. proj_parm.sinrot = sin(gamma);
  232. proj_parm.cosrot = cos(gamma);
  233. proj_parm.BrA = 1. / (proj_parm.ArB = proj_parm.A * (proj_parm.rB = 1. / proj_parm.B));
  234. proj_parm.AB = proj_parm.A * proj_parm.B;
  235. if (no_off)
  236. proj_parm.u_0 = 0;
  237. else {
  238. proj_parm.u_0 = fabs(proj_parm.ArB * atan(sqrt(D * D - 1.) / cos(alpha_c)));
  239. if (par.phi0 < 0.)
  240. proj_parm.u_0 = - proj_parm.u_0;
  241. }
  242. F = 0.5 * gamma0;
  243. proj_parm.v_pole_n = proj_parm.ArB * log(tan(fourth_pi - F));
  244. proj_parm.v_pole_s = proj_parm.ArB * log(tan(fourth_pi + F));
  245. }
  246. }} // namespace detail::omerc
  247. #endif // doxygen
  248. /*!
  249. \brief Oblique Mercator projection
  250. \ingroup projections
  251. \tparam Geographic latlong point type
  252. \tparam Cartesian xy point type
  253. \tparam Parameters parameter type
  254. \par Projection characteristics
  255. - Cylindrical
  256. - Spheroid
  257. - Ellipsoid
  258. \par Projection parameters
  259. - no_rot: No rotation
  260. - alpha: Alpha (degrees)
  261. - gamma: Gamma (degrees)
  262. - no_off: Only for compatibility with libproj, proj4 (string)
  263. - lonc: Longitude (only used if alpha (or gamma) is specified) (degrees)
  264. - lon_1 (degrees)
  265. - lat_1: Latitude of first standard parallel (degrees)
  266. - lon_2 (degrees)
  267. - lat_2: Latitude of second standard parallel (degrees)
  268. - no_uoff (string)
  269. \par Example
  270. \image html ex_omerc.gif
  271. */
  272. template <typename T, typename Parameters>
  273. struct omerc_ellipsoid : public detail::omerc::base_omerc_ellipsoid<T, Parameters>
  274. {
  275. inline omerc_ellipsoid(const Parameters& par) : detail::omerc::base_omerc_ellipsoid<T, Parameters>(par)
  276. {
  277. detail::omerc::setup_omerc(this->m_par, this->m_proj_parm);
  278. }
  279. };
  280. #ifndef DOXYGEN_NO_DETAIL
  281. namespace detail
  282. {
  283. // Static projection
  284. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::omerc, omerc_ellipsoid, omerc_ellipsoid)
  285. // Factory entry(s)
  286. template <typename T, typename Parameters>
  287. class omerc_entry : public detail::factory_entry<T, Parameters>
  288. {
  289. public :
  290. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  291. {
  292. return new base_v_fi<omerc_ellipsoid<T, Parameters>, T, Parameters>(par);
  293. }
  294. };
  295. template <typename T, typename Parameters>
  296. inline void omerc_init(detail::base_factory<T, Parameters>& factory)
  297. {
  298. factory.add_to_factory("omerc", new omerc_entry<T, Parameters>);
  299. }
  300. } // namespace detail
  301. #endif // doxygen
  302. } // namespace projections
  303. }} // namespace boost::geometry
  304. #endif // BOOST_GEOMETRY_PROJECTIONS_OMERC_HPP