poly.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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_POLY_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_POLY_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/projects.hpp>
  35. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  36. #include <boost/geometry/srs/projections/impl/pj_mlfn.hpp>
  37. #include <boost/geometry/srs/projections/impl/pj_msfn.hpp>
  38. namespace boost { namespace geometry
  39. {
  40. namespace projections
  41. {
  42. #ifndef DOXYGEN_NO_DETAIL
  43. namespace detail { namespace poly
  44. {
  45. static const double tolerance = 1e-10;
  46. static const double conv_tolerance = 1e-10;
  47. static const int n_iter = 10;
  48. static const int i_iter = 20;
  49. static const double i_tolerance = 1.e-12;
  50. template <typename T>
  51. struct par_poly
  52. {
  53. T ml0;
  54. detail::en<T> en;
  55. };
  56. // template class, using CRTP to implement forward/inverse
  57. template <typename T, typename Parameters>
  58. struct base_poly_ellipsoid
  59. : public base_t_fi<base_poly_ellipsoid<T, Parameters>, T, Parameters>
  60. {
  61. par_poly<T> m_proj_parm;
  62. inline base_poly_ellipsoid(const Parameters& par)
  63. : base_t_fi<base_poly_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  64. {}
  65. // FORWARD(e_forward) ellipsoid
  66. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  67. inline void fwd(T lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  68. {
  69. T ms, sp, cp;
  70. if (fabs(lp_lat) <= tolerance) {
  71. xy_x = lp_lon;
  72. xy_y = -this->m_proj_parm.ml0;
  73. } else {
  74. sp = sin(lp_lat);
  75. ms = fabs(cp = cos(lp_lat)) > tolerance ? pj_msfn(sp, cp, this->m_par.es) / sp : 0.;
  76. xy_x = ms * sin(lp_lon *= sp);
  77. xy_y = (pj_mlfn(lp_lat, sp, cp, this->m_proj_parm.en) - this->m_proj_parm.ml0) + ms * (1. - cos(lp_lon));
  78. }
  79. }
  80. // INVERSE(e_inverse) ellipsoid
  81. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  82. inline void inv(T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  83. {
  84. xy_y += this->m_proj_parm.ml0;
  85. if (fabs(xy_y) <= tolerance) {
  86. lp_lon = xy_x;
  87. lp_lat = 0.;
  88. } else {
  89. T r, c, sp, cp, s2ph, ml, mlb, mlp, dPhi;
  90. int i;
  91. r = xy_y * xy_y + xy_x * xy_x;
  92. for (lp_lat = xy_y, i = i_iter; i ; --i) {
  93. sp = sin(lp_lat);
  94. s2ph = sp * ( cp = cos(lp_lat));
  95. if (fabs(cp) < i_tolerance) {
  96. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  97. }
  98. c = sp * (mlp = sqrt(1. - this->m_par.es * sp * sp)) / cp;
  99. ml = pj_mlfn(lp_lat, sp, cp, this->m_proj_parm.en);
  100. mlb = ml * ml + r;
  101. mlp = this->m_par.one_es / (mlp * mlp * mlp);
  102. lp_lat += ( dPhi =
  103. ( ml + ml + c * mlb - 2. * xy_y * (c * ml + 1.) ) / (
  104. this->m_par.es * s2ph * (mlb - 2. * xy_y * ml) / c +
  105. 2.* (xy_y - ml) * (c * mlp - 1. / s2ph) - mlp - mlp ));
  106. if (fabs(dPhi) <= i_tolerance)
  107. break;
  108. }
  109. if (!i) {
  110. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  111. }
  112. c = sin(lp_lat);
  113. lp_lon = asin(xy_x * tan(lp_lat) * sqrt(1. - this->m_par.es * c * c)) / sin(lp_lat);
  114. }
  115. }
  116. static inline std::string get_name()
  117. {
  118. return "poly_ellipsoid";
  119. }
  120. };
  121. // template class, using CRTP to implement forward/inverse
  122. template <typename T, typename Parameters>
  123. struct base_poly_spheroid
  124. : public base_t_fi<base_poly_spheroid<T, Parameters>, T, Parameters>
  125. {
  126. par_poly<T> m_proj_parm;
  127. inline base_poly_spheroid(const Parameters& par)
  128. : base_t_fi<base_poly_spheroid<T, Parameters>, T, Parameters>(*this, par)
  129. {}
  130. // FORWARD(s_forward) spheroid
  131. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  132. inline void fwd(T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  133. {
  134. T cot, E;
  135. if (fabs(lp_lat) <= tolerance) {
  136. xy_x = lp_lon;
  137. xy_y = this->m_proj_parm.ml0;
  138. } else {
  139. cot = 1. / tan(lp_lat);
  140. xy_x = sin(E = lp_lon * sin(lp_lat)) * cot;
  141. xy_y = lp_lat - this->m_par.phi0 + cot * (1. - cos(E));
  142. }
  143. }
  144. // INVERSE(s_inverse) spheroid
  145. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  146. inline void inv(T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  147. {
  148. T B, dphi, tp;
  149. int i;
  150. if (fabs(xy_y = this->m_par.phi0 + xy_y) <= tolerance) {
  151. lp_lon = xy_x;
  152. lp_lat = 0.;
  153. } else {
  154. lp_lat = xy_y;
  155. B = xy_x * xy_x + xy_y * xy_y;
  156. i = n_iter;
  157. do {
  158. tp = tan(lp_lat);
  159. lp_lat -= (dphi = (xy_y * (lp_lat * tp + 1.) - lp_lat -
  160. .5 * ( lp_lat * lp_lat + B) * tp) /
  161. ((lp_lat - xy_y) / tp - 1.));
  162. } while (fabs(dphi) > conv_tolerance && --i);
  163. if (! i) {
  164. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  165. }
  166. lp_lon = asin(xy_x * tan(lp_lat)) / sin(lp_lat);
  167. }
  168. }
  169. static inline std::string get_name()
  170. {
  171. return "poly_spheroid";
  172. }
  173. };
  174. // Polyconic (American)
  175. template <typename Parameters, typename T>
  176. inline void setup_poly(Parameters& par, par_poly<T>& proj_parm)
  177. {
  178. if (par.es != 0.0) {
  179. proj_parm.en = pj_enfn<T>(par.es);
  180. proj_parm.ml0 = pj_mlfn(par.phi0, sin(par.phi0), cos(par.phi0), proj_parm.en);
  181. } else {
  182. proj_parm.ml0 = -par.phi0;
  183. }
  184. }
  185. }} // namespace detail::poly
  186. #endif // doxygen
  187. /*!
  188. \brief Polyconic (American) projection
  189. \ingroup projections
  190. \tparam Geographic latlong point type
  191. \tparam Cartesian xy point type
  192. \tparam Parameters parameter type
  193. \par Projection characteristics
  194. - Conic
  195. - Spheroid
  196. - Ellipsoid
  197. \par Example
  198. \image html ex_poly.gif
  199. */
  200. template <typename T, typename Parameters>
  201. struct poly_ellipsoid : public detail::poly::base_poly_ellipsoid<T, Parameters>
  202. {
  203. template <typename Params>
  204. inline poly_ellipsoid(Params const& , Parameters const& par)
  205. : detail::poly::base_poly_ellipsoid<T, Parameters>(par)
  206. {
  207. detail::poly::setup_poly(this->m_par, this->m_proj_parm);
  208. }
  209. };
  210. /*!
  211. \brief Polyconic (American) projection
  212. \ingroup projections
  213. \tparam Geographic latlong point type
  214. \tparam Cartesian xy point type
  215. \tparam Parameters parameter type
  216. \par Projection characteristics
  217. - Conic
  218. - Spheroid
  219. - Ellipsoid
  220. \par Example
  221. \image html ex_poly.gif
  222. */
  223. template <typename T, typename Parameters>
  224. struct poly_spheroid : public detail::poly::base_poly_spheroid<T, Parameters>
  225. {
  226. template <typename Params>
  227. inline poly_spheroid(Params const& , Parameters const& par)
  228. : detail::poly::base_poly_spheroid<T, Parameters>(par)
  229. {
  230. detail::poly::setup_poly(this->m_par, this->m_proj_parm);
  231. }
  232. };
  233. #ifndef DOXYGEN_NO_DETAIL
  234. namespace detail
  235. {
  236. // Static projection
  237. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::spar::proj_poly, poly_spheroid, poly_ellipsoid)
  238. // Factory entry(s)
  239. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(poly_entry, poly_spheroid, poly_ellipsoid)
  240. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(poly_init)
  241. {
  242. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(poly, poly_entry)
  243. }
  244. } // namespace detail
  245. #endif // doxygen
  246. } // namespace projections
  247. }} // namespace boost::geometry
  248. #endif // BOOST_GEOMETRY_PROJECTIONS_POLY_HPP