lcca.hpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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 srs { namespace par4
  73. {
  74. struct lcca {};
  75. }} //namespace srs::par4
  76. namespace projections
  77. {
  78. #ifndef DOXYGEN_NO_DETAIL
  79. namespace detail { namespace lcca
  80. {
  81. static const int max_iter = 10;
  82. static const double del_tol = 1e-12;
  83. template <typename T>
  84. struct par_lcca
  85. {
  86. detail::en<T> en;
  87. T r0, l, M0;
  88. T C;
  89. };
  90. template <typename T> /* func to compute dr */
  91. inline T fS(T const& S, T const& C)
  92. {
  93. return(S * ( 1. + S * S * C));
  94. }
  95. template <typename T> /* deriv of fs */
  96. inline T fSp(T const& S, T const& C)
  97. {
  98. return(1. + 3.* S * S * C);
  99. }
  100. // template class, using CRTP to implement forward/inverse
  101. template <typename T, typename Parameters>
  102. struct base_lcca_ellipsoid
  103. : public base_t_fi<base_lcca_ellipsoid<T, Parameters>, T, Parameters>
  104. {
  105. par_lcca<T> m_proj_parm;
  106. inline base_lcca_ellipsoid(const Parameters& par)
  107. : base_t_fi<base_lcca_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  108. {}
  109. // FORWARD(e_forward) ellipsoid
  110. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  111. inline void fwd(T& lp_lon, T& lp_lat, T& xy_x, T& xy_y) const
  112. {
  113. T S, r, dr;
  114. S = pj_mlfn(lp_lat, sin(lp_lat), cos(lp_lat), this->m_proj_parm.en) - this->m_proj_parm.M0;
  115. dr = fS(S, this->m_proj_parm.C);
  116. r = this->m_proj_parm.r0 - dr;
  117. xy_x = this->m_par.k0 * (r * sin( lp_lon *= this->m_proj_parm.l ) );
  118. xy_y = this->m_par.k0 * (this->m_proj_parm.r0 - r * cos(lp_lon) );
  119. }
  120. // INVERSE(e_inverse) ellipsoid & spheroid
  121. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  122. inline void inv(T& xy_x, T& xy_y, T& lp_lon, T& lp_lat) const
  123. {
  124. T theta, dr, S, dif;
  125. int i;
  126. xy_x /= this->m_par.k0;
  127. xy_y /= this->m_par.k0;
  128. theta = atan2(xy_x , this->m_proj_parm.r0 - xy_y);
  129. dr = xy_y - xy_x * tan(0.5 * theta);
  130. lp_lon = theta / this->m_proj_parm.l;
  131. S = dr;
  132. for (i = max_iter; i ; --i) {
  133. S -= (dif = (fS(S, this->m_proj_parm.C) - dr) / fSp(S, this->m_proj_parm.C));
  134. if (fabs(dif) < del_tol) break;
  135. }
  136. if (!i) {
  137. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  138. }
  139. lp_lat = pj_inv_mlfn(S + this->m_proj_parm.M0, this->m_par.es, this->m_proj_parm.en);
  140. }
  141. static inline std::string get_name()
  142. {
  143. return "lcca_ellipsoid";
  144. }
  145. };
  146. // Lambert Conformal Conic Alternative
  147. template <typename Parameters, typename T>
  148. inline void setup_lcca(Parameters& par, par_lcca<T>& proj_parm)
  149. {
  150. T s2p0, N0, R0, tan0;
  151. proj_parm.en = pj_enfn<T>(par.es);
  152. if (par.phi0 == 0.) {
  153. BOOST_THROW_EXCEPTION( projection_exception(error_lat_0_is_zero) );
  154. }
  155. proj_parm.l = sin(par.phi0);
  156. proj_parm.M0 = pj_mlfn(par.phi0, proj_parm.l, cos(par.phi0), proj_parm.en);
  157. s2p0 = proj_parm.l * proj_parm.l;
  158. R0 = 1. / (1. - par.es * s2p0);
  159. N0 = sqrt(R0);
  160. R0 *= par.one_es * N0;
  161. tan0 = tan(par.phi0);
  162. proj_parm.r0 = N0 / tan0;
  163. proj_parm.C = 1. / (6. * R0 * N0);
  164. }
  165. }} // namespace detail::lcca
  166. #endif // doxygen
  167. /*!
  168. \brief Lambert Conformal Conic Alternative projection
  169. \ingroup projections
  170. \tparam Geographic latlong point type
  171. \tparam Cartesian xy point type
  172. \tparam Parameters parameter type
  173. \par Projection characteristics
  174. - Conic
  175. - Spheroid
  176. - Ellipsoid
  177. \par Projection parameters
  178. - lat_0: Latitude of origin
  179. \par Example
  180. \image html ex_lcca.gif
  181. */
  182. template <typename T, typename Parameters>
  183. struct lcca_ellipsoid : public detail::lcca::base_lcca_ellipsoid<T, Parameters>
  184. {
  185. inline lcca_ellipsoid(const Parameters& par) : detail::lcca::base_lcca_ellipsoid<T, Parameters>(par)
  186. {
  187. detail::lcca::setup_lcca(this->m_par, this->m_proj_parm);
  188. }
  189. };
  190. #ifndef DOXYGEN_NO_DETAIL
  191. namespace detail
  192. {
  193. // Static projection
  194. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::lcca, lcca_ellipsoid, lcca_ellipsoid)
  195. // Factory entry(s)
  196. template <typename T, typename Parameters>
  197. class lcca_entry : public detail::factory_entry<T, Parameters>
  198. {
  199. public :
  200. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  201. {
  202. return new base_v_fi<lcca_ellipsoid<T, Parameters>, T, Parameters>(par);
  203. }
  204. };
  205. template <typename T, typename Parameters>
  206. inline void lcca_init(detail::base_factory<T, Parameters>& factory)
  207. {
  208. factory.add_to_factory("lcca", new lcca_entry<T, Parameters>);
  209. }
  210. } // namespace detail
  211. #endif // doxygen
  212. } // namespace projections
  213. }} // namespace boost::geometry
  214. #endif // BOOST_GEOMETRY_PROJECTIONS_LCCA_HPP