lcc.hpp 11 KB

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