labrd.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  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_LABRD_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_LABRD_HPP
  32. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  33. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  34. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  35. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  36. #include <boost/geometry/srs/projections/impl/projects.hpp>
  37. namespace boost { namespace geometry
  38. {
  39. namespace projections
  40. {
  41. #ifndef DOXYGEN_NO_DETAIL
  42. namespace detail { namespace labrd
  43. {
  44. static const double epsilon = 1.e-10;
  45. template <typename T>
  46. struct par_labrd
  47. {
  48. T Az, kRg, p0s, A, C, Ca, Cb, Cc, Cd;
  49. int rot;
  50. };
  51. // template class, using CRTP to implement forward/inverse
  52. template <typename T, typename Parameters>
  53. struct base_labrd_ellipsoid
  54. : public base_t_fi<base_labrd_ellipsoid<T, Parameters>, T, Parameters>
  55. {
  56. par_labrd<T> m_proj_parm;
  57. inline base_labrd_ellipsoid(const Parameters& par)
  58. : base_t_fi<base_labrd_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  59. {}
  60. // FORWARD(e_forward)
  61. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  62. inline void fwd(T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  63. {
  64. static const T fourth_pi = detail::fourth_pi<T>();
  65. T V1, V2, ps, sinps, cosps, sinps2, cosps2;
  66. T I1, I2, I3, I4, I5, I6, x2, y2, t;
  67. V1 = this->m_proj_parm.A * log( tan(fourth_pi + .5 * lp_lat) );
  68. t = this->m_par.e * sin(lp_lat);
  69. V2 = .5 * this->m_par.e * this->m_proj_parm.A * log ((1. + t)/(1. - t));
  70. ps = 2. * (atan(exp(V1 - V2 + this->m_proj_parm.C)) - fourth_pi);
  71. I1 = ps - this->m_proj_parm.p0s;
  72. cosps = cos(ps); cosps2 = cosps * cosps;
  73. sinps = sin(ps); sinps2 = sinps * sinps;
  74. I4 = this->m_proj_parm.A * cosps;
  75. I2 = .5 * this->m_proj_parm.A * I4 * sinps;
  76. I3 = I2 * this->m_proj_parm.A * this->m_proj_parm.A * (5. * cosps2 - sinps2) / 12.;
  77. I6 = I4 * this->m_proj_parm.A * this->m_proj_parm.A;
  78. I5 = I6 * (cosps2 - sinps2) / 6.;
  79. I6 *= this->m_proj_parm.A * this->m_proj_parm.A *
  80. (5. * cosps2 * cosps2 + sinps2 * (sinps2 - 18. * cosps2)) / 120.;
  81. t = lp_lon * lp_lon;
  82. xy_x = this->m_proj_parm.kRg * lp_lon * (I4 + t * (I5 + t * I6));
  83. xy_y = this->m_proj_parm.kRg * (I1 + t * (I2 + t * I3));
  84. x2 = xy_x * xy_x;
  85. y2 = xy_y * xy_y;
  86. V1 = 3. * xy_x * y2 - xy_x * x2;
  87. V2 = xy_y * y2 - 3. * x2 * xy_y;
  88. xy_x += this->m_proj_parm.Ca * V1 + this->m_proj_parm.Cb * V2;
  89. xy_y += this->m_proj_parm.Ca * V2 - this->m_proj_parm.Cb * V1;
  90. }
  91. // INVERSE(e_inverse) ellipsoid & spheroid
  92. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  93. inline void inv(T xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  94. {
  95. static const T fourth_pi = detail::fourth_pi<T>();
  96. /* t = 0.0 optimization is to avoid a false positive cppcheck warning */
  97. /* (cppcheck git beaf29c15867984aa3c2a15cf15bd7576ccde2b3). Might no */
  98. /* longer be necessary with later versions. */
  99. T x2, y2, V1, V2, V3, V4, t = 0.0, t2, ps, pe, tpe, s;
  100. T I7, I8, I9, I10, I11, d, Re;
  101. int i;
  102. x2 = xy_x * xy_x;
  103. y2 = xy_y * xy_y;
  104. V1 = 3. * xy_x * y2 - xy_x * x2;
  105. V2 = xy_y * y2 - 3. * x2 * xy_y;
  106. V3 = xy_x * (5. * y2 * y2 + x2 * (-10. * y2 + x2 ));
  107. V4 = xy_y * (5. * x2 * x2 + y2 * (-10. * x2 + y2 ));
  108. xy_x += - this->m_proj_parm.Ca * V1 - this->m_proj_parm.Cb * V2 + this->m_proj_parm.Cc * V3 + this->m_proj_parm.Cd * V4;
  109. xy_y += this->m_proj_parm.Cb * V1 - this->m_proj_parm.Ca * V2 - this->m_proj_parm.Cd * V3 + this->m_proj_parm.Cc * V4;
  110. ps = this->m_proj_parm.p0s + xy_y / this->m_proj_parm.kRg;
  111. pe = ps + this->m_par.phi0 - this->m_proj_parm.p0s;
  112. for ( i = 20; i; --i) {
  113. V1 = this->m_proj_parm.A * log(tan(fourth_pi + .5 * pe));
  114. tpe = this->m_par.e * sin(pe);
  115. V2 = .5 * this->m_par.e * this->m_proj_parm.A * log((1. + tpe)/(1. - tpe));
  116. t = ps - 2. * (atan(exp(V1 - V2 + this->m_proj_parm.C)) - fourth_pi);
  117. pe += t;
  118. if (fabs(t) < epsilon)
  119. break;
  120. }
  121. t = this->m_par.e * sin(pe);
  122. t = 1. - t * t;
  123. Re = this->m_par.one_es / ( t * sqrt(t) );
  124. t = tan(ps);
  125. t2 = t * t;
  126. s = this->m_proj_parm.kRg * this->m_proj_parm.kRg;
  127. d = Re * this->m_par.k0 * this->m_proj_parm.kRg;
  128. I7 = t / (2. * d);
  129. I8 = t * (5. + 3. * t2) / (24. * d * s);
  130. d = cos(ps) * this->m_proj_parm.kRg * this->m_proj_parm.A;
  131. I9 = 1. / d;
  132. d *= s;
  133. I10 = (1. + 2. * t2) / (6. * d);
  134. I11 = (5. + t2 * (28. + 24. * t2)) / (120. * d * s);
  135. x2 = xy_x * xy_x;
  136. lp_lat = pe + x2 * (-I7 + I8 * x2);
  137. lp_lon = xy_x * (I9 + x2 * (-I10 + x2 * I11));
  138. }
  139. static inline std::string get_name()
  140. {
  141. return "labrd_ellipsoid";
  142. }
  143. };
  144. // Laborde
  145. template <typename Params, typename Parameters, typename T>
  146. inline void setup_labrd(Params const& params, Parameters& par, par_labrd<T>& proj_parm)
  147. {
  148. static const T fourth_pi = detail::fourth_pi<T>();
  149. T Az, sinp, R, N, t;
  150. proj_parm.rot = pj_get_param_b<srs::spar::no_rot>(params, "no_rot", srs::dpar::no_rot);
  151. Az = pj_get_param_r<T, srs::spar::azi>(params, "azi", srs::dpar::azi);
  152. sinp = sin(par.phi0);
  153. t = 1. - par.es * sinp * sinp;
  154. N = 1. / sqrt(t);
  155. R = par.one_es * N / t;
  156. proj_parm.kRg = par.k0 * sqrt( N * R );
  157. proj_parm.p0s = atan( sqrt(R / N) * tan(par.phi0) );
  158. proj_parm.A = sinp / sin(proj_parm.p0s);
  159. t = par.e * sinp;
  160. proj_parm.C = .5 * par.e * proj_parm.A * log((1. + t)/(1. - t)) +
  161. - proj_parm.A * log( tan(fourth_pi + .5 * par.phi0))
  162. + log( tan(fourth_pi + .5 * proj_parm.p0s));
  163. t = Az + Az;
  164. proj_parm.Ca = (1. - cos(t)) * ( proj_parm.Cb = 1. / (12. * proj_parm.kRg * proj_parm.kRg) );
  165. proj_parm.Cb *= sin(t);
  166. proj_parm.Cc = 3. * (proj_parm.Ca * proj_parm.Ca - proj_parm.Cb * proj_parm.Cb);
  167. proj_parm.Cd = 6. * proj_parm.Ca * proj_parm.Cb;
  168. }
  169. }} // namespace detail::labrd
  170. #endif // doxygen
  171. /*!
  172. \brief Laborde projection
  173. \ingroup projections
  174. \tparam Geographic latlong point type
  175. \tparam Cartesian xy point type
  176. \tparam Parameters parameter type
  177. \par Projection characteristics
  178. - Cylindrical
  179. - Spheroid
  180. - Special for Madagascar
  181. \par Projection parameters
  182. - no_rot: No rotation (boolean)
  183. - azi: Azimuth (or Gamma) (degrees)
  184. \par Example
  185. \image html ex_labrd.gif
  186. */
  187. template <typename T, typename Parameters>
  188. struct labrd_ellipsoid : public detail::labrd::base_labrd_ellipsoid<T, Parameters>
  189. {
  190. template <typename Params>
  191. inline labrd_ellipsoid(Params const& params, Parameters const& par)
  192. : detail::labrd::base_labrd_ellipsoid<T, Parameters>(par)
  193. {
  194. detail::labrd::setup_labrd(params, this->m_par, this->m_proj_parm);
  195. }
  196. };
  197. #ifndef DOXYGEN_NO_DETAIL
  198. namespace detail
  199. {
  200. // Static projection
  201. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::spar::proj_labrd, labrd_ellipsoid, labrd_ellipsoid)
  202. // Factory entry(s)
  203. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(labrd_entry, labrd_ellipsoid)
  204. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(labrd_init)
  205. {
  206. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(labrd, labrd_entry)
  207. }
  208. } // namespace detail
  209. #endif // doxygen
  210. } // namespace projections
  211. }} // namespace boost::geometry
  212. #endif // BOOST_GEOMETRY_PROJECTIONS_LABRD_HPP