airy.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258
  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. // Purpose: Implementation of the airy (Airy) projection.
  16. // Author: Gerald Evenden (1995)
  17. // Thomas Knudsen (2016) - revise/add regression tests
  18. // Copyright (c) 1995, Gerald Evenden
  19. // Permission is hereby granted, free of charge, to any person obtaining a
  20. // copy of this software and associated documentation files (the "Software"),
  21. // to deal in the Software without restriction, including without limitation
  22. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  23. // and/or sell copies of the Software, and to permit persons to whom the
  24. // Software is furnished to do so, subject to the following conditions:
  25. // The above copyright notice and this permission notice shall be included
  26. // in all copies or substantial portions of the Software.
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  28. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  30. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  32. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  33. // DEALINGS IN THE SOFTWARE.
  34. #ifndef BOOST_GEOMETRY_PROJECTIONS_AIRY_HPP
  35. #define BOOST_GEOMETRY_PROJECTIONS_AIRY_HPP
  36. #include <boost/geometry/util/math.hpp>
  37. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  38. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  39. #include <boost/geometry/srs/projections/impl/projects.hpp>
  40. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  41. #include <boost/geometry/srs/projections/par4.hpp> // airy tag
  42. namespace boost { namespace geometry
  43. {
  44. namespace srs { namespace par4
  45. {
  46. // already defined in par4.hpp as ellps name
  47. //struct airy {};
  48. }} //namespace srs::par4
  49. namespace projections
  50. {
  51. #ifndef DOXYGEN_NO_DETAIL
  52. namespace detail { namespace airy
  53. {
  54. static const double epsilon = 1.e-10;
  55. enum mode_type {
  56. n_pole = 0,
  57. s_pole = 1,
  58. equit = 2,
  59. obliq = 3
  60. };
  61. template <typename T>
  62. struct par_airy
  63. {
  64. T p_halfpi;
  65. T sinph0;
  66. T cosph0;
  67. T Cb;
  68. mode_type mode;
  69. int no_cut; /* do not cut at hemisphere limit */
  70. };
  71. // template class, using CRTP to implement forward/inverse
  72. template <typename T, typename Parameters>
  73. struct base_airy_spheroid
  74. : public base_t_f<base_airy_spheroid<T, Parameters>, T, Parameters>
  75. {
  76. par_airy<T> m_proj_parm;
  77. inline base_airy_spheroid(const Parameters& par)
  78. : base_t_f<base_airy_spheroid<T, Parameters>, T, Parameters>(*this, par)
  79. {}
  80. // FORWARD(s_forward) spheroid
  81. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  82. inline void fwd(T& lp_lon, T& lp_lat, T& xy_x, T& xy_y) const
  83. {
  84. static const T half_pi = detail::half_pi<T>();
  85. T sinlam, coslam, cosphi, sinphi, t, s, Krho, cosz;
  86. sinlam = sin(lp_lon);
  87. coslam = cos(lp_lon);
  88. switch (this->m_proj_parm.mode) {
  89. case equit:
  90. case obliq:
  91. sinphi = sin(lp_lat);
  92. cosphi = cos(lp_lat);
  93. cosz = cosphi * coslam;
  94. if (this->m_proj_parm.mode == obliq)
  95. cosz = this->m_proj_parm.sinph0 * sinphi + this->m_proj_parm.cosph0 * cosz;
  96. if (!this->m_proj_parm.no_cut && cosz < -epsilon) {
  97. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  98. }
  99. if (fabs(s = 1. - cosz) > epsilon) {
  100. t = 0.5 * (1. + cosz);
  101. Krho = -log(t)/s - this->m_proj_parm.Cb / t;
  102. } else
  103. Krho = 0.5 - this->m_proj_parm.Cb;
  104. xy_x = Krho * cosphi * sinlam;
  105. if (this->m_proj_parm.mode == obliq)
  106. xy_y = Krho * (this->m_proj_parm.cosph0 * sinphi -
  107. this->m_proj_parm.sinph0 * cosphi * coslam);
  108. else
  109. xy_y = Krho * sinphi;
  110. break;
  111. case s_pole:
  112. case n_pole:
  113. lp_lat = fabs(this->m_proj_parm.p_halfpi - lp_lat);
  114. if (!this->m_proj_parm.no_cut && (lp_lat - epsilon) > half_pi)
  115. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  116. if ((lp_lat *= 0.5) > epsilon) {
  117. t = tan(lp_lat);
  118. Krho = -2.*(log(cos(lp_lat)) / t + t * this->m_proj_parm.Cb);
  119. xy_x = Krho * sinlam;
  120. xy_y = Krho * coslam;
  121. if (this->m_proj_parm.mode == n_pole)
  122. xy_y = -xy_y;
  123. } else
  124. xy_x = xy_y = 0.;
  125. }
  126. }
  127. static inline std::string get_name()
  128. {
  129. return "airy_spheroid";
  130. }
  131. };
  132. // Airy
  133. template <typename Parameters, typename T>
  134. inline void setup_airy(Parameters& par, par_airy<T>& proj_parm)
  135. {
  136. static const T half_pi = detail::half_pi<T>();
  137. T beta;
  138. proj_parm.no_cut = pj_get_param_b(par.params, "no_cut");
  139. beta = 0.5 * (half_pi - pj_get_param_r(par.params, "lat_b"));
  140. if (fabs(beta) < epsilon)
  141. proj_parm.Cb = -0.5;
  142. else {
  143. proj_parm.Cb = 1./tan(beta);
  144. proj_parm.Cb *= proj_parm.Cb * log(cos(beta));
  145. }
  146. if (fabs(fabs(par.phi0) - half_pi) < epsilon)
  147. if (par.phi0 < 0.) {
  148. proj_parm.p_halfpi = -half_pi;
  149. proj_parm.mode = s_pole;
  150. } else {
  151. proj_parm.p_halfpi = half_pi;
  152. proj_parm.mode = n_pole;
  153. }
  154. else {
  155. if (fabs(par.phi0) < epsilon)
  156. proj_parm.mode = equit;
  157. else {
  158. proj_parm.mode = obliq;
  159. proj_parm.sinph0 = sin(par.phi0);
  160. proj_parm.cosph0 = cos(par.phi0);
  161. }
  162. }
  163. par.es = 0.;
  164. }
  165. }} // namespace detail::airy
  166. #endif // doxygen
  167. /*!
  168. \brief Airy projection
  169. \ingroup projections
  170. \tparam Geographic latlong point type
  171. \tparam Cartesian xy point type
  172. \tparam Parameters parameter type
  173. \par Projection characteristics
  174. - Miscellaneous
  175. - Spheroid
  176. - no inverse
  177. \par Projection parameters
  178. - no_cut: Do not cut at hemisphere limit (boolean)
  179. - lat_b (degrees)
  180. \par Example
  181. \image html ex_airy.gif
  182. */
  183. template <typename T, typename Parameters>
  184. struct airy_spheroid : public detail::airy::base_airy_spheroid<T, Parameters>
  185. {
  186. inline airy_spheroid(const Parameters& par) : detail::airy::base_airy_spheroid<T, Parameters>(par)
  187. {
  188. detail::airy::setup_airy(this->m_par, this->m_proj_parm);
  189. }
  190. };
  191. #ifndef DOXYGEN_NO_DETAIL
  192. namespace detail
  193. {
  194. // Static projection
  195. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::airy, airy_spheroid, airy_spheroid)
  196. // Factory entry(s)
  197. template <typename T, typename Parameters>
  198. class airy_entry : public detail::factory_entry<T, Parameters>
  199. {
  200. public :
  201. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  202. {
  203. return new base_v_f<airy_spheroid<T, Parameters>, T, Parameters>(par);
  204. }
  205. };
  206. template <typename T, typename Parameters>
  207. inline void airy_init(detail::base_factory<T, Parameters>& factory)
  208. {
  209. factory.add_to_factory("airy", new airy_entry<T, Parameters>);
  210. }
  211. } // namespace detail
  212. #endif // doxygen
  213. } // namespace projections
  214. }} // namespace boost::geometry
  215. #endif // BOOST_GEOMETRY_PROJECTIONS_AIRY_HPP