somerc.hpp 8.4 KB

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