lcc.hpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  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_LCC_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_LCC_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_msfn.hpp>
  39. #include <boost/geometry/srs/projections/impl/pj_phi2.hpp>
  40. #include <boost/geometry/srs/projections/impl/pj_tsfn.hpp>
  41. namespace boost { namespace geometry
  42. {
  43. namespace srs { namespace par4
  44. {
  45. struct lcc {}; // Lambert Conformal Conic
  46. }} //namespace srs::par4
  47. namespace projections
  48. {
  49. #ifndef DOXYGEN_NO_DETAIL
  50. namespace detail { namespace lcc
  51. {
  52. static const double epsilon10 = 1.e-10;
  53. template <typename T>
  54. struct par_lcc
  55. {
  56. T phi1;
  57. T phi2;
  58. T n;
  59. T rho0;
  60. T c;
  61. int ellips;
  62. };
  63. // template class, using CRTP to implement forward/inverse
  64. template <typename T, typename Parameters>
  65. struct base_lcc_ellipsoid
  66. : public base_t_fi<base_lcc_ellipsoid<T, Parameters>, T, Parameters>
  67. {
  68. par_lcc<T> m_proj_parm;
  69. inline base_lcc_ellipsoid(const Parameters& par)
  70. : base_t_fi<base_lcc_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  71. {}
  72. // FORWARD(e_forward) ellipsoid & spheroid
  73. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  74. inline void fwd(T& lp_lon, T& lp_lat, T& xy_x, T& xy_y) const
  75. {
  76. static const T fourth_pi = detail::fourth_pi<T>();
  77. static const T half_pi = detail::half_pi<T>();
  78. T rho;
  79. if (fabs(fabs(lp_lat) - half_pi) < epsilon10) {
  80. if ((lp_lat * this->m_proj_parm.n) <= 0.) {
  81. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  82. }
  83. rho = 0.;
  84. } else {
  85. rho = this->m_proj_parm.c * (this->m_proj_parm.ellips
  86. ? math::pow(pj_tsfn(lp_lat, sin(lp_lat), this->m_par.e), this->m_proj_parm.n)
  87. : math::pow(tan(fourth_pi + T(0.5) * lp_lat), -this->m_proj_parm.n));
  88. }
  89. lp_lon *= this->m_proj_parm.n;
  90. xy_x = this->m_par.k0 * (rho * sin( lp_lon) );
  91. xy_y = this->m_par.k0 * (this->m_proj_parm.rho0 - rho * cos(lp_lon) );
  92. }
  93. // INVERSE(e_inverse) ellipsoid & spheroid
  94. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  95. inline void inv(T& xy_x, T& xy_y, T& lp_lon, T& lp_lat) const
  96. {
  97. static const T half_pi = detail::half_pi<T>();
  98. T rho;
  99. xy_x /= this->m_par.k0;
  100. xy_y /= this->m_par.k0;
  101. xy_y = this->m_proj_parm.rho0 - xy_y;
  102. rho = boost::math::hypot(xy_x, xy_y);
  103. if(rho != 0.0) {
  104. if (this->m_proj_parm.n < 0.) {
  105. rho = -rho;
  106. xy_x = -xy_x;
  107. xy_y = -xy_y;
  108. }
  109. if (this->m_proj_parm.ellips) {
  110. lp_lat = pj_phi2(math::pow(rho / this->m_proj_parm.c, T(1)/this->m_proj_parm.n), this->m_par.e);
  111. if (lp_lat == HUGE_VAL) {
  112. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  113. }
  114. } else
  115. lp_lat = 2. * atan(math::pow(this->m_proj_parm.c / rho, T(1)/this->m_proj_parm.n)) - half_pi;
  116. lp_lon = atan2(xy_x, xy_y) / this->m_proj_parm.n;
  117. } else {
  118. lp_lon = 0.;
  119. lp_lat = this->m_proj_parm.n > 0. ? half_pi : -half_pi;
  120. }
  121. }
  122. static inline std::string get_name()
  123. {
  124. return "lcc_ellipsoid";
  125. }
  126. };
  127. // Lambert Conformal Conic
  128. template <typename Parameters, typename T>
  129. inline void setup_lcc(Parameters& par, par_lcc<T>& proj_parm)
  130. {
  131. static const T fourth_pi = detail::fourth_pi<T>();
  132. static const T half_pi = detail::half_pi<T>();
  133. T cosphi, sinphi;
  134. int secant;
  135. proj_parm.phi1 = pj_get_param_r(par.params, "lat_1");
  136. if (pj_param_r(par.params, "lat_2", proj_parm.phi2)) {
  137. /* empty */
  138. } else {
  139. proj_parm.phi2 = proj_parm.phi1;
  140. if (!pj_param_exists(par.params, "lat_0"))
  141. par.phi0 = proj_parm.phi1;
  142. }
  143. if (fabs(proj_parm.phi1 + proj_parm.phi2) < epsilon10)
  144. BOOST_THROW_EXCEPTION( projection_exception(error_conic_lat_equal) );
  145. proj_parm.n = sinphi = sin(proj_parm.phi1);
  146. cosphi = cos(proj_parm.phi1);
  147. secant = fabs(proj_parm.phi1 - proj_parm.phi2) >= epsilon10;
  148. if( (proj_parm.ellips = (par.es != 0.)) ) {
  149. double ml1, m1;
  150. par.e = sqrt(par.es);
  151. m1 = pj_msfn(sinphi, cosphi, par.es);
  152. ml1 = pj_tsfn(proj_parm.phi1, sinphi, par.e);
  153. if (secant) { /* secant cone */
  154. sinphi = sin(proj_parm.phi2);
  155. proj_parm.n = log(m1 / pj_msfn(sinphi, cos(proj_parm.phi2), par.es));
  156. proj_parm.n /= log(ml1 / pj_tsfn(proj_parm.phi2, sinphi, par.e));
  157. }
  158. proj_parm.c = (proj_parm.rho0 = m1 * math::pow(ml1, -proj_parm.n) / proj_parm.n);
  159. proj_parm.rho0 *= (fabs(fabs(par.phi0) - half_pi) < epsilon10) ? T(0) :
  160. math::pow(pj_tsfn(par.phi0, sin(par.phi0), par.e), proj_parm.n);
  161. } else {
  162. if (secant)
  163. proj_parm.n = log(cosphi / cos(proj_parm.phi2)) /
  164. log(tan(fourth_pi + .5 * proj_parm.phi2) /
  165. tan(fourth_pi + .5 * proj_parm.phi1));
  166. proj_parm.c = cosphi * math::pow(tan(fourth_pi + T(0.5) * proj_parm.phi1), proj_parm.n) / proj_parm.n;
  167. proj_parm.rho0 = (fabs(fabs(par.phi0) - half_pi) < epsilon10) ? 0. :
  168. proj_parm.c * math::pow(tan(fourth_pi + T(0.5) * par.phi0), -proj_parm.n);
  169. }
  170. }
  171. }} // namespace detail::lcc
  172. #endif // doxygen
  173. /*!
  174. \brief Lambert Conformal Conic projection
  175. \ingroup projections
  176. \tparam Geographic latlong point type
  177. \tparam Cartesian xy point type
  178. \tparam Parameters parameter type
  179. \par Projection characteristics
  180. - Conic
  181. - Spheroid
  182. - Ellipsoid
  183. \par Projection parameters
  184. - lat_1: Latitude of first standard parallel (degrees)
  185. - lat_2: Latitude of second standard parallel (degrees)
  186. - lat_0: Latitude of origin
  187. \par Example
  188. \image html ex_lcc.gif
  189. */
  190. template <typename T, typename Parameters>
  191. struct lcc_ellipsoid : public detail::lcc::base_lcc_ellipsoid<T, Parameters>
  192. {
  193. inline lcc_ellipsoid(const Parameters& par) : detail::lcc::base_lcc_ellipsoid<T, Parameters>(par)
  194. {
  195. detail::lcc::setup_lcc(this->m_par, this->m_proj_parm);
  196. }
  197. };
  198. #ifndef DOXYGEN_NO_DETAIL
  199. namespace detail
  200. {
  201. // Static projection
  202. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::lcc, lcc_ellipsoid, lcc_ellipsoid)
  203. // Factory entry(s)
  204. template <typename T, typename Parameters>
  205. class lcc_entry : public detail::factory_entry<T, Parameters>
  206. {
  207. public :
  208. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  209. {
  210. return new base_v_fi<lcc_ellipsoid<T, Parameters>, T, Parameters>(par);
  211. }
  212. };
  213. template <typename T, typename Parameters>
  214. inline void lcc_init(detail::base_factory<T, Parameters>& factory)
  215. {
  216. factory.add_to_factory("lcc", new lcc_entry<T, Parameters>);
  217. }
  218. } // namespace detail
  219. #endif // doxygen
  220. } // namespace projections
  221. }} // namespace boost::geometry
  222. #endif // BOOST_GEOMETRY_PROJECTIONS_LCC_HPP