labrd.hpp 11 KB

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