bonne.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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_BONNE_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_BONNE_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/factory_entry.hpp>
  35. #include <boost/geometry/srs/projections/impl/pj_mlfn.hpp>
  36. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  37. #include <boost/geometry/srs/projections/impl/projects.hpp>
  38. #include <boost/geometry/util/math.hpp>
  39. #include <boost/math/special_functions/hypot.hpp>
  40. namespace boost { namespace geometry
  41. {
  42. namespace projections
  43. {
  44. #ifndef DOXYGEN_NO_DETAIL
  45. namespace detail { namespace bonne
  46. {
  47. static const double epsilon10 = 1e-10;
  48. template <typename T>
  49. struct par_bonne
  50. {
  51. T phi1;
  52. T cphi1;
  53. T am1;
  54. T m1;
  55. detail::en<T> en;
  56. };
  57. // template class, using CRTP to implement forward/inverse
  58. template <typename T, typename Parameters>
  59. struct base_bonne_ellipsoid
  60. : public base_t_fi<base_bonne_ellipsoid<T, Parameters>, T, Parameters>
  61. {
  62. par_bonne<T> m_proj_parm;
  63. inline base_bonne_ellipsoid(const Parameters& par)
  64. : base_t_fi<base_bonne_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  65. {}
  66. // FORWARD(e_forward) ellipsoid
  67. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  68. inline void fwd(T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  69. {
  70. T rh, E, c;
  71. rh = this->m_proj_parm.am1 + this->m_proj_parm.m1 - pj_mlfn(lp_lat, E = sin(lp_lat), c = cos(lp_lat), this->m_proj_parm.en);
  72. E = c * lp_lon / (rh * sqrt(1. - this->m_par.es * E * E));
  73. xy_x = rh * sin(E);
  74. xy_y = this->m_proj_parm.am1 - rh * cos(E);
  75. }
  76. // INVERSE(e_inverse) ellipsoid
  77. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  78. inline void inv(T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  79. {
  80. static const T half_pi = detail::half_pi<T>();
  81. T s, rh;
  82. rh = boost::math::hypot(xy_x, xy_y = this->m_proj_parm.am1 - xy_y);
  83. lp_lat = pj_inv_mlfn(this->m_proj_parm.am1 + this->m_proj_parm.m1 - rh, this->m_par.es, this->m_proj_parm.en);
  84. if ((s = fabs(lp_lat)) < half_pi) {
  85. s = sin(lp_lat);
  86. lp_lon = rh * atan2(xy_x, xy_y) *
  87. sqrt(1. - this->m_par.es * s * s) / cos(lp_lat);
  88. } else if (fabs(s - half_pi) <= epsilon10)
  89. lp_lon = 0.;
  90. else
  91. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  92. }
  93. static inline std::string get_name()
  94. {
  95. return "bonne_ellipsoid";
  96. }
  97. };
  98. // template class, using CRTP to implement forward/inverse
  99. template <typename T, typename Parameters>
  100. struct base_bonne_spheroid
  101. : public base_t_fi<base_bonne_spheroid<T, Parameters>, T, Parameters>
  102. {
  103. par_bonne<T> m_proj_parm;
  104. inline base_bonne_spheroid(const Parameters& par)
  105. : base_t_fi<base_bonne_spheroid<T, Parameters>, T, Parameters>(*this, par)
  106. {}
  107. // FORWARD(s_forward) spheroid
  108. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  109. inline void fwd(T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  110. {
  111. T E, rh;
  112. rh = this->m_proj_parm.cphi1 + this->m_proj_parm.phi1 - lp_lat;
  113. if (fabs(rh) > epsilon10) {
  114. xy_x = rh * sin(E = lp_lon * cos(lp_lat) / rh);
  115. xy_y = this->m_proj_parm.cphi1 - rh * cos(E);
  116. } else
  117. xy_x = xy_y = 0.;
  118. }
  119. // INVERSE(s_inverse) spheroid
  120. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  121. inline void inv(T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  122. {
  123. static const T half_pi = detail::half_pi<T>();
  124. T rh;
  125. rh = boost::math::hypot(xy_x, xy_y = this->m_proj_parm.cphi1 - xy_y);
  126. lp_lat = this->m_proj_parm.cphi1 + this->m_proj_parm.phi1 - rh;
  127. if (fabs(lp_lat) > half_pi) {
  128. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  129. }
  130. if (fabs(fabs(lp_lat) - half_pi) <= epsilon10)
  131. lp_lon = 0.;
  132. else
  133. lp_lon = rh * atan2(xy_x, xy_y) / cos(lp_lat);
  134. }
  135. static inline std::string get_name()
  136. {
  137. return "bonne_spheroid";
  138. }
  139. };
  140. // Bonne (Werner lat_1=90)
  141. template <typename Params, typename Parameters, typename T>
  142. inline void setup_bonne(Params const& params, Parameters& par, par_bonne<T>& proj_parm)
  143. {
  144. static const T half_pi = detail::half_pi<T>();
  145. T c;
  146. proj_parm.phi1 = pj_get_param_r<T, srs::spar::lat_1>(params, "lat_1", srs::dpar::lat_1);
  147. if (fabs(proj_parm.phi1) < epsilon10)
  148. BOOST_THROW_EXCEPTION( projection_exception(error_lat1_is_zero) );
  149. if (par.es != 0.0) {
  150. proj_parm.en = pj_enfn<T>(par.es);
  151. proj_parm.m1 = pj_mlfn(proj_parm.phi1, proj_parm.am1 = sin(proj_parm.phi1),
  152. c = cos(proj_parm.phi1), proj_parm.en);
  153. proj_parm.am1 = c / (sqrt(1. - par.es * proj_parm.am1 * proj_parm.am1) * proj_parm.am1);
  154. } else {
  155. if (fabs(proj_parm.phi1) + epsilon10 >= half_pi)
  156. proj_parm.cphi1 = 0.;
  157. else
  158. proj_parm.cphi1 = 1. / tan(proj_parm.phi1);
  159. }
  160. }
  161. }} // namespace detail::bonne
  162. #endif // doxygen
  163. /*!
  164. \brief Bonne (Werner lat_1=90) projection
  165. \ingroup projections
  166. \tparam Geographic latlong point type
  167. \tparam Cartesian xy point type
  168. \tparam Parameters parameter type
  169. \par Projection characteristics
  170. - Conic
  171. - Spheroid
  172. - Ellipsoid
  173. \par Projection parameters
  174. - lat_1: Latitude of first standard parallel (degrees)
  175. \par Example
  176. \image html ex_bonne.gif
  177. */
  178. template <typename T, typename Parameters>
  179. struct bonne_ellipsoid : public detail::bonne::base_bonne_ellipsoid<T, Parameters>
  180. {
  181. template <typename Params>
  182. inline bonne_ellipsoid(Params const& params, Parameters const& par)
  183. : detail::bonne::base_bonne_ellipsoid<T, Parameters>(par)
  184. {
  185. detail::bonne::setup_bonne(params, this->m_par, this->m_proj_parm);
  186. }
  187. };
  188. /*!
  189. \brief Bonne (Werner lat_1=90) projection
  190. \ingroup projections
  191. \tparam Geographic latlong point type
  192. \tparam Cartesian xy point type
  193. \tparam Parameters parameter type
  194. \par Projection characteristics
  195. - Conic
  196. - Spheroid
  197. - Ellipsoid
  198. \par Projection parameters
  199. - lat_1: Latitude of first standard parallel (degrees)
  200. \par Example
  201. \image html ex_bonne.gif
  202. */
  203. template <typename T, typename Parameters>
  204. struct bonne_spheroid : public detail::bonne::base_bonne_spheroid<T, Parameters>
  205. {
  206. template <typename Params>
  207. inline bonne_spheroid(Params const& params, Parameters const& par)
  208. : detail::bonne::base_bonne_spheroid<T, Parameters>(par)
  209. {
  210. detail::bonne::setup_bonne(params, this->m_par, this->m_proj_parm);
  211. }
  212. };
  213. #ifndef DOXYGEN_NO_DETAIL
  214. namespace detail
  215. {
  216. // Static projection
  217. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::spar::proj_bonne, bonne_spheroid, bonne_ellipsoid)
  218. // Factory entry(s)
  219. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(bonne_entry, bonne_spheroid, bonne_ellipsoid)
  220. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(bonne_init)
  221. {
  222. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(bonne, bonne_entry);
  223. }
  224. } // namespace detail
  225. #endif // doxygen
  226. } // namespace projections
  227. }} // namespace boost::geometry
  228. #endif // BOOST_GEOMETRY_PROJECTIONS_BONNE_HPP