labrd.hpp 10 KB

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