bonne.hpp 11 KB

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