somerc.hpp 8.0 KB

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