lcc.hpp 11 KB

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