poly.hpp 12 KB

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