cass.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, 2019, 2022.
  4. // Modifications copyright (c) 2017-2022, Oracle and/or its affiliates.
  5. // Contributed and/or modified by Vissarion Fysikopoulos, on behalf of Oracle.
  6. // Contributed and/or modified by Adam Wulkiewicz, on behalf of Oracle.
  7. // Use, modification and distribution is subject to the Boost Software License,
  8. // Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  9. // http://www.boost.org/LICENSE_1_0.txt)
  10. // This file is converted from PROJ4, http://trac.osgeo.org/proj
  11. // PROJ4 is originally written by Gerald Evenden (then of the USGS)
  12. // PROJ4 is maintained by Frank Warmerdam
  13. // PROJ4 is converted to Boost.Geometry by Barend Gehrels
  14. // Last updated version of proj: 9.0.0
  15. // Original copyright notice:
  16. // Permission is hereby granted, free of charge, to any person obtaining a
  17. // copy of this software and associated documentation files (the "Software"),
  18. // to deal in the Software without restriction, including without limitation
  19. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  20. // and/or sell copies of the Software, and to permit persons to whom the
  21. // Software is furnished to do so, subject to the following conditions:
  22. // The above copyright notice and this permission notice shall be included
  23. // in all copies or substantial portions of the Software.
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  25. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  30. // DEALINGS IN THE SOFTWARE.
  31. #ifndef BOOST_GEOMETRY_PROJECTIONS_CASS_HPP
  32. #define BOOST_GEOMETRY_PROJECTIONS_CASS_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/pj_generic_inverse.hpp>
  38. #include <boost/geometry/srs/projections/impl/pj_mlfn.hpp>
  39. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  40. namespace boost { namespace geometry
  41. {
  42. namespace projections
  43. {
  44. #ifndef DOXYGEN_NO_DETAIL
  45. namespace detail { namespace cass
  46. {
  47. //static const double epsilon10 = 1e-10;
  48. //static const double C1 = .16666666666666666666;
  49. //static const double C2 = .00833333333333333333;
  50. //static const double C3 = .04166666666666666666;
  51. //static const double C4 = .33333333333333333333;
  52. //static const double C5 = .06666666666666666666;
  53. template <typename T>
  54. inline T C1() { return .16666666666666666666666666666666666666; }
  55. template <typename T>
  56. inline T C2() { return .00833333333333333333333333333333333333; }
  57. template <typename T>
  58. inline T C3() { return .04166666666666666666666666666666666666; }
  59. template <typename T>
  60. inline T C4() { return .33333333333333333333333333333333333333; }
  61. template <typename T>
  62. inline T C5() { return .06666666666666666666666666666666666666; }
  63. template <typename T>
  64. struct par_cass
  65. {
  66. T m0;
  67. detail::en<T> en;
  68. bool hyperbolic;
  69. };
  70. template <typename T, typename Parameters>
  71. struct base_cass_ellipsoid
  72. {
  73. par_cass<T> m_proj_parm;
  74. // FORWARD(e_forward) ellipsoid
  75. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  76. inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat,
  77. T& xy_x, T& xy_y) const
  78. {
  79. static T const C1 = cass::C1<T>();
  80. static T const C2 = cass::C2<T>();
  81. static T const C3 = cass::C3<T>();
  82. T sinlplat = sin(lp_lat);
  83. T c = cos(lp_lat);
  84. xy_y = pj_mlfn(lp_lat, sinlplat, c, this->m_proj_parm.en);
  85. T n_square = 1./(1. - par.es * sinlplat * sinlplat);
  86. T n = sqrt(n_square);
  87. T tn = tan(lp_lat);
  88. T t = tn * tn;
  89. T a1 = lp_lon * c;
  90. c *= par.es * c / (1 - par.es);
  91. T a2 = a1 * a1;
  92. xy_x = n * a1 * (1. - a2 * t *
  93. (C1 - (8. - t + 8. * c) * a2 * C2));
  94. xy_y -= this->m_proj_parm.m0 - n * tn * a2 *
  95. (.5 + (5. - t + 6. * c) * a2 * C3);
  96. if ( this->m_proj_parm.hyperbolic )
  97. {
  98. T const rho = n_square * (1. - par.es) * n;
  99. xy_y -= xy_y * xy_y * xy_y / (6 * rho * n);
  100. }
  101. }
  102. // INVERSE(e_inverse) ellipsoid
  103. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  104. inline void inv(Parameters const& par, T const& xy_x, T const& xy_y,
  105. T& lp_lon, T& lp_lat) const
  106. {
  107. static const T C3 = cass::C3<T>();
  108. static const T C4 = cass::C4<T>();
  109. static const T C5 = cass::C5<T>();
  110. T ph1;
  111. ph1 = pj_inv_mlfn(this->m_proj_parm.m0 + xy_y, par.es, this->m_proj_parm.en);
  112. T tn = tan(ph1); T t = tn * tn;
  113. T n = sin(ph1);
  114. T r = 1. / (1. - par.es * n * n);
  115. n = sqrt(r);
  116. r *= (1. - par.es) * n;
  117. T dd = xy_x / n;
  118. T d2 = dd * dd;
  119. lp_lat = ph1 - (n * tn / r) * d2 *
  120. (.5 - (1. + 3. * t) * d2 * C3);
  121. lp_lon = dd * (1. + t * d2 *
  122. (-C4 + (1. + 3. * t) * d2 * C5)) / cos(ph1);
  123. if ( this->m_proj_parm.hyperbolic )
  124. {
  125. // EPSG guidance note 7-2 suggests a custom approximation for the
  126. // 'Vanua Levu 1915 / Vanua Levu Grid' case, but better use the
  127. // generic inversion method
  128. pj_generic_inverse_2d(xy_x, xy_y, par, this, lp_lat, lp_lon);
  129. }
  130. }
  131. static inline std::string get_name()
  132. {
  133. return "cass_ellipsoid";
  134. }
  135. };
  136. template <typename T, typename Parameters>
  137. struct base_cass_spheroid
  138. {
  139. par_cass<T> m_proj_parm;
  140. // FORWARD(s_forward) spheroid
  141. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  142. inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat,
  143. T& xy_x, T& xy_y) const
  144. {
  145. xy_x = asin(cos(lp_lat) * sin(lp_lon));
  146. xy_y = atan2(tan(lp_lat) , cos(lp_lon)) - par.phi0;
  147. }
  148. // INVERSE(s_inverse) spheroid
  149. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  150. inline void inv(Parameters const& par, T const& xy_x, T const& xy_y,
  151. T& lp_lon, T& lp_lat) const
  152. {
  153. T dd = xy_y + par.phi0;
  154. lp_lat = asin(sin(dd) * cos(xy_x));
  155. lp_lon = atan2(tan(xy_x), cos(dd));
  156. }
  157. static inline std::string get_name()
  158. {
  159. return "cass_spheroid";
  160. }
  161. };
  162. // Cassini
  163. template <typename Params, typename Parameters, typename T>
  164. inline void setup_cass(Params const& params, Parameters& par, par_cass<T>& proj_parm)
  165. {
  166. if (par.es) {
  167. proj_parm.en = pj_enfn<T>(par.es);
  168. proj_parm.m0 = pj_mlfn(par.phi0, sin(par.phi0), cos(par.phi0), proj_parm.en);
  169. } else {
  170. }
  171. proj_parm.hyperbolic = false;
  172. if (pj_param_exists<srs::spar::hyperbolic>(params, "hyperbolic", srs::dpar::hyperbolic))
  173. proj_parm.hyperbolic = true;
  174. }
  175. }} // namespace detail::cass
  176. #endif // doxygen
  177. /*!
  178. \brief Cassini projection
  179. \ingroup projections
  180. \tparam Geographic latlong point type
  181. \tparam Cartesian xy point type
  182. \tparam Parameters parameter type
  183. \par Projection characteristics
  184. - Cylindrical
  185. - Spheroid
  186. - Ellipsoid
  187. \par Example
  188. \image html ex_cass.gif
  189. */
  190. template <typename T, typename Parameters>
  191. struct cass_ellipsoid : public detail::cass::base_cass_ellipsoid<T, Parameters>
  192. {
  193. template <typename Params>
  194. inline cass_ellipsoid(Params const& params, Parameters const& par)
  195. {
  196. detail::cass::setup_cass(params, par, this->m_proj_parm);
  197. }
  198. };
  199. /*!
  200. \brief Cassini projection
  201. \ingroup projections
  202. \tparam Geographic latlong point type
  203. \tparam Cartesian xy point type
  204. \tparam Parameters parameter type
  205. \par Projection characteristics
  206. - Cylindrical
  207. - Spheroid
  208. - Ellipsoid
  209. \par Example
  210. \image html ex_cass.gif
  211. */
  212. template <typename T, typename Parameters>
  213. struct cass_spheroid : public detail::cass::base_cass_spheroid<T, Parameters>
  214. {
  215. template <typename Params>
  216. inline cass_spheroid(Params const& params, Parameters const& par)
  217. {
  218. detail::cass::setup_cass(params, par, this->m_proj_parm);
  219. }
  220. };
  221. #ifndef DOXYGEN_NO_DETAIL
  222. namespace detail
  223. {
  224. // Static projection
  225. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_cass, cass_spheroid,
  226. cass_ellipsoid)
  227. // Factory entry(s)
  228. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(cass_entry, cass_spheroid, cass_ellipsoid)
  229. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(cass_init)
  230. {
  231. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(cass, cass_entry);
  232. }
  233. } // namespace detail
  234. #endif // doxygen
  235. } // namespace projections
  236. }} // namespace boost::geometry
  237. #endif // BOOST_GEOMETRY_PROJECTIONS_CASS_HPP