lcca.hpp 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  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. /*****************************************************************************
  31. Lambert Conformal Conic Alternative
  32. -----------------------------------
  33. This is Gerald Evenden's 2003 implementation of an alternative
  34. "almost" LCC, which has been in use historically, but which
  35. should NOT be used for new projects - i.e: use this implementation
  36. if you need interoperability with old data represented in this
  37. projection, but not in any other case.
  38. The code was originally discussed on the PROJ.4 mailing list in
  39. a thread archived over at
  40. http://lists.maptools.org/pipermail/proj/2003-March/000644.html
  41. It was discussed again in the thread starting at
  42. http://lists.maptools.org/pipermail/proj/2017-October/007828.html
  43. and continuing at
  44. http://lists.maptools.org/pipermail/proj/2017-November/007831.html
  45. which prompted Clifford J. Mugnier to add these clarifying notes:
  46. The French Army Truncated Cubic Lambert (partially conformal) Conic
  47. projection is the Legal system for the projection in France between
  48. the late 1800s and 1948 when the French Legislature changed the law
  49. to recognize the fully conformal version.
  50. It was (might still be in one or two North African prior French
  51. Colonies) used in North Africa in Algeria, Tunisia, & Morocco, as
  52. well as in Syria during the Levant.
  53. Last time I have seen it used was about 30+ years ago in
  54. Algeria when it was used to define Lease Block boundaries for
  55. Petroleum Exploration & Production.
  56. (signed)
  57. Clifford J. Mugnier, c.p., c.m.s.
  58. Chief of Geodesy
  59. LSU Center for GeoInformatics
  60. Dept. of Civil Engineering
  61. LOUISIANA STATE UNIVERSITY
  62. *****************************************************************************/
  63. #ifndef BOOST_GEOMETRY_PROJECTIONS_LCCA_HPP
  64. #define BOOST_GEOMETRY_PROJECTIONS_LCCA_HPP
  65. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  66. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  67. #include <boost/geometry/srs/projections/impl/projects.hpp>
  68. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  69. #include <boost/geometry/srs/projections/impl/pj_mlfn.hpp>
  70. namespace boost { namespace geometry
  71. {
  72. namespace projections
  73. {
  74. #ifndef DOXYGEN_NO_DETAIL
  75. namespace detail { namespace lcca
  76. {
  77. static const int max_iter = 10;
  78. static const double del_tol = 1e-12;
  79. template <typename T>
  80. struct par_lcca
  81. {
  82. detail::en<T> en;
  83. T r0, l, M0;
  84. T C;
  85. };
  86. template <typename T> /* func to compute dr */
  87. inline T fS(T const& S, T const& C)
  88. {
  89. return(S * ( 1. + S * S * C));
  90. }
  91. template <typename T> /* deriv of fs */
  92. inline T fSp(T const& S, T const& C)
  93. {
  94. return(1. + 3.* S * S * C);
  95. }
  96. // template class, using CRTP to implement forward/inverse
  97. template <typename T, typename Parameters>
  98. struct base_lcca_ellipsoid
  99. : public base_t_fi<base_lcca_ellipsoid<T, Parameters>, T, Parameters>
  100. {
  101. par_lcca<T> m_proj_parm;
  102. inline base_lcca_ellipsoid(const Parameters& par)
  103. : base_t_fi<base_lcca_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  104. {}
  105. // FORWARD(e_forward) ellipsoid
  106. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  107. inline void fwd(T lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  108. {
  109. T S, r, dr;
  110. S = pj_mlfn(lp_lat, sin(lp_lat), cos(lp_lat), this->m_proj_parm.en) - this->m_proj_parm.M0;
  111. dr = fS(S, this->m_proj_parm.C);
  112. r = this->m_proj_parm.r0 - dr;
  113. xy_x = this->m_par.k0 * (r * sin( lp_lon *= this->m_proj_parm.l ) );
  114. xy_y = this->m_par.k0 * (this->m_proj_parm.r0 - r * cos(lp_lon) );
  115. }
  116. // INVERSE(e_inverse) ellipsoid & spheroid
  117. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  118. inline void inv(T xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  119. {
  120. T theta, dr, S, dif;
  121. int i;
  122. xy_x /= this->m_par.k0;
  123. xy_y /= this->m_par.k0;
  124. theta = atan2(xy_x , this->m_proj_parm.r0 - xy_y);
  125. dr = xy_y - xy_x * tan(0.5 * theta);
  126. lp_lon = theta / this->m_proj_parm.l;
  127. S = dr;
  128. for (i = max_iter; i ; --i) {
  129. S -= (dif = (fS(S, this->m_proj_parm.C) - dr) / fSp(S, this->m_proj_parm.C));
  130. if (fabs(dif) < del_tol) break;
  131. }
  132. if (!i) {
  133. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  134. }
  135. lp_lat = pj_inv_mlfn(S + this->m_proj_parm.M0, this->m_par.es, this->m_proj_parm.en);
  136. }
  137. static inline std::string get_name()
  138. {
  139. return "lcca_ellipsoid";
  140. }
  141. };
  142. // Lambert Conformal Conic Alternative
  143. template <typename Parameters, typename T>
  144. inline void setup_lcca(Parameters& par, par_lcca<T>& proj_parm)
  145. {
  146. T s2p0, N0, R0, tan0;
  147. proj_parm.en = pj_enfn<T>(par.es);
  148. if (par.phi0 == 0.) {
  149. BOOST_THROW_EXCEPTION( projection_exception(error_lat_0_is_zero) );
  150. }
  151. proj_parm.l = sin(par.phi0);
  152. proj_parm.M0 = pj_mlfn(par.phi0, proj_parm.l, cos(par.phi0), proj_parm.en);
  153. s2p0 = proj_parm.l * proj_parm.l;
  154. R0 = 1. / (1. - par.es * s2p0);
  155. N0 = sqrt(R0);
  156. R0 *= par.one_es * N0;
  157. tan0 = tan(par.phi0);
  158. proj_parm.r0 = N0 / tan0;
  159. proj_parm.C = 1. / (6. * R0 * N0);
  160. }
  161. }} // namespace detail::lcca
  162. #endif // doxygen
  163. /*!
  164. \brief Lambert Conformal Conic Alternative projection
  165. \ingroup projections
  166. \tparam Geographic latlong point type
  167. \tparam Cartesian xy point type
  168. \tparam Parameters parameter type
  169. \par Projection characteristics
  170. - Conic
  171. - Spheroid
  172. - Ellipsoid
  173. \par Projection parameters
  174. - lat_0: Latitude of origin
  175. \par Example
  176. \image html ex_lcca.gif
  177. */
  178. template <typename T, typename Parameters>
  179. struct lcca_ellipsoid : public detail::lcca::base_lcca_ellipsoid<T, Parameters>
  180. {
  181. template <typename Params>
  182. inline lcca_ellipsoid(Params const& , Parameters const& par)
  183. : detail::lcca::base_lcca_ellipsoid<T, Parameters>(par)
  184. {
  185. detail::lcca::setup_lcca(this->m_par, this->m_proj_parm);
  186. }
  187. };
  188. #ifndef DOXYGEN_NO_DETAIL
  189. namespace detail
  190. {
  191. // Static projection
  192. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::spar::proj_lcca, lcca_ellipsoid, lcca_ellipsoid)
  193. // Factory entry(s)
  194. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(lcca_entry, lcca_ellipsoid)
  195. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(lcca_init)
  196. {
  197. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(lcca, lcca_entry)
  198. }
  199. } // namespace detail
  200. #endif // doxygen
  201. } // namespace projections
  202. }} // namespace boost::geometry
  203. #endif // BOOST_GEOMETRY_PROJECTIONS_LCCA_HPP