poly.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278
  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.
  4. // Modifications copyright (c) 2017-2019, 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 <typename T, typename Parameters>
  57. struct base_poly_ellipsoid
  58. {
  59. par_poly<T> m_proj_parm;
  60. // FORWARD(e_forward) ellipsoid
  61. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  62. inline void fwd(Parameters const& par, T lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  63. {
  64. T ms, sp, cp;
  65. if (fabs(lp_lat) <= tolerance) {
  66. xy_x = lp_lon;
  67. xy_y = -this->m_proj_parm.ml0;
  68. } else {
  69. sp = sin(lp_lat);
  70. ms = fabs(cp = cos(lp_lat)) > tolerance ? pj_msfn(sp, cp, par.es) / sp : 0.;
  71. xy_x = ms * sin(lp_lon *= sp);
  72. xy_y = (pj_mlfn(lp_lat, sp, cp, this->m_proj_parm.en) - this->m_proj_parm.ml0) + ms * (1. - cos(lp_lon));
  73. }
  74. }
  75. // INVERSE(e_inverse) ellipsoid
  76. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  77. inline void inv(Parameters const& par, T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  78. {
  79. xy_y += this->m_proj_parm.ml0;
  80. if (fabs(xy_y) <= tolerance) {
  81. lp_lon = xy_x;
  82. lp_lat = 0.;
  83. } else {
  84. T r, c, sp, cp, s2ph, ml, mlb, mlp, dPhi;
  85. int i;
  86. r = xy_y * xy_y + xy_x * xy_x;
  87. for (lp_lat = xy_y, i = i_iter; i ; --i) {
  88. sp = sin(lp_lat);
  89. s2ph = sp * ( cp = cos(lp_lat));
  90. if (fabs(cp) < i_tolerance) {
  91. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  92. }
  93. c = sp * (mlp = sqrt(1. - par.es * sp * sp)) / cp;
  94. ml = pj_mlfn(lp_lat, sp, cp, this->m_proj_parm.en);
  95. mlb = ml * ml + r;
  96. mlp = par.one_es / (mlp * mlp * mlp);
  97. lp_lat += ( dPhi =
  98. ( ml + ml + c * mlb - 2. * xy_y * (c * ml + 1.) ) / (
  99. par.es * s2ph * (mlb - 2. * xy_y * ml) / c +
  100. 2.* (xy_y - ml) * (c * mlp - 1. / s2ph) - mlp - mlp ));
  101. if (fabs(dPhi) <= i_tolerance)
  102. break;
  103. }
  104. if (!i) {
  105. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  106. }
  107. c = sin(lp_lat);
  108. lp_lon = asin(xy_x * tan(lp_lat) * sqrt(1. - par.es * c * c)) / sin(lp_lat);
  109. }
  110. }
  111. static inline std::string get_name()
  112. {
  113. return "poly_ellipsoid";
  114. }
  115. };
  116. template <typename T, typename Parameters>
  117. struct base_poly_spheroid
  118. {
  119. par_poly<T> m_proj_parm;
  120. // FORWARD(s_forward) spheroid
  121. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  122. inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  123. {
  124. T cot, E;
  125. if (fabs(lp_lat) <= tolerance) {
  126. xy_x = lp_lon;
  127. xy_y = this->m_proj_parm.ml0;
  128. } else {
  129. cot = 1. / tan(lp_lat);
  130. xy_x = sin(E = lp_lon * sin(lp_lat)) * cot;
  131. xy_y = lp_lat - par.phi0 + cot * (1. - cos(E));
  132. }
  133. }
  134. // INVERSE(s_inverse) spheroid
  135. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  136. inline void inv(Parameters const& par, T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  137. {
  138. T B, dphi, tp;
  139. int i;
  140. if (fabs(xy_y = par.phi0 + xy_y) <= tolerance) {
  141. lp_lon = xy_x;
  142. lp_lat = 0.;
  143. } else {
  144. lp_lat = xy_y;
  145. B = xy_x * xy_x + xy_y * xy_y;
  146. i = n_iter;
  147. do {
  148. tp = tan(lp_lat);
  149. lp_lat -= (dphi = (xy_y * (lp_lat * tp + 1.) - lp_lat -
  150. .5 * ( lp_lat * lp_lat + B) * tp) /
  151. ((lp_lat - xy_y) / tp - 1.));
  152. } while (fabs(dphi) > conv_tolerance && --i);
  153. if (! i) {
  154. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  155. }
  156. lp_lon = asin(xy_x * tan(lp_lat)) / sin(lp_lat);
  157. }
  158. }
  159. static inline std::string get_name()
  160. {
  161. return "poly_spheroid";
  162. }
  163. };
  164. // Polyconic (American)
  165. template <typename Parameters, typename T>
  166. inline void setup_poly(Parameters const& par, par_poly<T>& proj_parm)
  167. {
  168. if (par.es != 0.0) {
  169. proj_parm.en = pj_enfn<T>(par.es);
  170. proj_parm.ml0 = pj_mlfn(par.phi0, sin(par.phi0), cos(par.phi0), proj_parm.en);
  171. } else {
  172. proj_parm.ml0 = -par.phi0;
  173. }
  174. }
  175. }} // namespace detail::poly
  176. #endif // doxygen
  177. /*!
  178. \brief Polyconic (American) 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. - Conic
  185. - Spheroid
  186. - Ellipsoid
  187. \par Example
  188. \image html ex_poly.gif
  189. */
  190. template <typename T, typename Parameters>
  191. struct poly_ellipsoid : public detail::poly::base_poly_ellipsoid<T, Parameters>
  192. {
  193. template <typename Params>
  194. inline poly_ellipsoid(Params const& , Parameters const& par)
  195. {
  196. detail::poly::setup_poly(par, this->m_proj_parm);
  197. }
  198. };
  199. /*!
  200. \brief Polyconic (American) 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. - Conic
  207. - Spheroid
  208. - Ellipsoid
  209. \par Example
  210. \image html ex_poly.gif
  211. */
  212. template <typename T, typename Parameters>
  213. struct poly_spheroid : public detail::poly::base_poly_spheroid<T, Parameters>
  214. {
  215. template <typename Params>
  216. inline poly_spheroid(Params const& , Parameters const& par)
  217. {
  218. detail::poly::setup_poly(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_poly, poly_spheroid, poly_ellipsoid)
  226. // Factory entry(s)
  227. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(poly_entry, poly_spheroid, poly_ellipsoid)
  228. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(poly_init)
  229. {
  230. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(poly, poly_entry)
  231. }
  232. } // namespace detail
  233. #endif // doxygen
  234. } // namespace projections
  235. }} // namespace boost::geometry
  236. #endif // BOOST_GEOMETRY_PROJECTIONS_POLY_HPP