somerc.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  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_SOMERC_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_SOMERC_HPP
  32. #include <boost/geometry/util/math.hpp>
  33. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  34. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  35. #include <boost/geometry/srs/projections/impl/projects.hpp>
  36. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  37. #include <boost/geometry/srs/projections/impl/aasincos.hpp>
  38. namespace boost { namespace geometry
  39. {
  40. namespace srs { namespace par4
  41. {
  42. struct somerc {}; // Swiss. Obl. Mercator
  43. }} //namespace srs::par4
  44. namespace projections
  45. {
  46. #ifndef DOXYGEN_NO_DETAIL
  47. namespace detail { namespace somerc
  48. {
  49. static const double epsilon = 1.e-10;
  50. static const int n_iter = 6;
  51. template <typename T>
  52. struct par_somerc
  53. {
  54. T K, c, hlf_e, kR, cosp0, sinp0;
  55. };
  56. // template class, using CRTP to implement forward/inverse
  57. template <typename T, typename Parameters>
  58. struct base_somerc_ellipsoid
  59. : public base_t_fi<base_somerc_ellipsoid<T, Parameters>, T, Parameters>
  60. {
  61. par_somerc<T> m_proj_parm;
  62. inline base_somerc_ellipsoid(const Parameters& par)
  63. : base_t_fi<base_somerc_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  64. {}
  65. // FORWARD(e_forward)
  66. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  67. inline void fwd(T& lp_lon, T& lp_lat, T& xy_x, T& xy_y) const
  68. {
  69. static const T fourth_pi = detail::fourth_pi<T>();
  70. static const T half_pi = detail::half_pi<T>();
  71. T phip, lamp, phipp, lampp, sp, cp;
  72. sp = this->m_par.e * sin(lp_lat);
  73. phip = 2.* atan( exp( this->m_proj_parm.c * (
  74. log(tan(fourth_pi + 0.5 * lp_lat)) - this->m_proj_parm.hlf_e * log((1. + sp)/(1. - sp)))
  75. + this->m_proj_parm.K)) - half_pi;
  76. lamp = this->m_proj_parm.c * lp_lon;
  77. cp = cos(phip);
  78. phipp = aasin(this->m_proj_parm.cosp0 * sin(phip) - this->m_proj_parm.sinp0 * cp * cos(lamp));
  79. lampp = aasin(cp * sin(lamp) / cos(phipp));
  80. xy_x = this->m_proj_parm.kR * lampp;
  81. xy_y = this->m_proj_parm.kR * log(tan(fourth_pi + 0.5 * phipp));
  82. }
  83. // INVERSE(e_inverse) ellipsoid & spheroid
  84. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  85. inline void inv(T& xy_x, T& xy_y, T& lp_lon, T& lp_lat) const
  86. {
  87. static const T fourth_pi = detail::fourth_pi<T>();
  88. T phip, lamp, phipp, lampp, cp, esp, con, delp;
  89. int i;
  90. phipp = 2. * (atan(exp(xy_y / this->m_proj_parm.kR)) - fourth_pi);
  91. lampp = xy_x / this->m_proj_parm.kR;
  92. cp = cos(phipp);
  93. phip = aasin(this->m_proj_parm.cosp0 * sin(phipp) + this->m_proj_parm.sinp0 * cp * cos(lampp));
  94. lamp = aasin(cp * sin(lampp) / cos(phip));
  95. con = (this->m_proj_parm.K - log(tan(fourth_pi + 0.5 * phip)))/this->m_proj_parm.c;
  96. for (i = n_iter; i ; --i) {
  97. esp = this->m_par.e * sin(phip);
  98. delp = (con + log(tan(fourth_pi + 0.5 * phip)) - this->m_proj_parm.hlf_e *
  99. log((1. + esp)/(1. - esp)) ) *
  100. (1. - esp * esp) * cos(phip) * this->m_par.rone_es;
  101. phip -= delp;
  102. if (fabs(delp) < epsilon)
  103. break;
  104. }
  105. if (i) {
  106. lp_lat = phip;
  107. lp_lon = lamp / this->m_proj_parm.c;
  108. } else {
  109. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  110. }
  111. }
  112. static inline std::string get_name()
  113. {
  114. return "somerc_ellipsoid";
  115. }
  116. };
  117. // Swiss. Obl. Mercator
  118. template <typename Parameters, typename T>
  119. inline void setup_somerc(Parameters& par, par_somerc<T>& proj_parm)
  120. {
  121. static const T fourth_pi = detail::fourth_pi<T>();
  122. T cp, phip0, sp;
  123. proj_parm.hlf_e = 0.5 * par.e;
  124. cp = cos(par.phi0);
  125. cp *= cp;
  126. proj_parm.c = sqrt(1 + par.es * cp * cp * par.rone_es);
  127. sp = sin(par.phi0);
  128. proj_parm.cosp0 = cos( phip0 = aasin(proj_parm.sinp0 = sp / proj_parm.c) );
  129. sp *= par.e;
  130. proj_parm.K = log(tan(fourth_pi + 0.5 * phip0)) - proj_parm.c * (
  131. log(tan(fourth_pi + 0.5 * par.phi0)) - proj_parm.hlf_e *
  132. log((1. + sp) / (1. - sp)));
  133. proj_parm.kR = par.k0 * sqrt(par.one_es) / (1. - sp * sp);
  134. }
  135. }} // namespace detail::somerc
  136. #endif // doxygen
  137. /*!
  138. \brief Swiss. Obl. Mercator projection
  139. \ingroup projections
  140. \tparam Geographic latlong point type
  141. \tparam Cartesian xy point type
  142. \tparam Parameters parameter type
  143. \par Projection characteristics
  144. - Cylindrical
  145. - Ellipsoid
  146. - For CH1903
  147. \par Example
  148. \image html ex_somerc.gif
  149. */
  150. template <typename T, typename Parameters>
  151. struct somerc_ellipsoid : public detail::somerc::base_somerc_ellipsoid<T, Parameters>
  152. {
  153. inline somerc_ellipsoid(const Parameters& par) : detail::somerc::base_somerc_ellipsoid<T, Parameters>(par)
  154. {
  155. detail::somerc::setup_somerc(this->m_par, this->m_proj_parm);
  156. }
  157. };
  158. #ifndef DOXYGEN_NO_DETAIL
  159. namespace detail
  160. {
  161. // Static projection
  162. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::somerc, somerc_ellipsoid, somerc_ellipsoid)
  163. // Factory entry(s)
  164. template <typename T, typename Parameters>
  165. class somerc_entry : public detail::factory_entry<T, Parameters>
  166. {
  167. public :
  168. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  169. {
  170. return new base_v_fi<somerc_ellipsoid<T, Parameters>, T, Parameters>(par);
  171. }
  172. };
  173. template <typename T, typename Parameters>
  174. inline void somerc_init(detail::base_factory<T, Parameters>& factory)
  175. {
  176. factory.add_to_factory("somerc", new somerc_entry<T, Parameters>);
  177. }
  178. } // namespace detail
  179. #endif // doxygen
  180. } // namespace projections
  181. }} // namespace boost::geometry
  182. #endif // BOOST_GEOMETRY_PROJECTIONS_SOMERC_HPP