aea.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. // Author: Gerald Evenden (1995)
  14. // Thomas Knudsen (2016) - revise/add regression tests
  15. // Last updated version of proj: 5.0.0
  16. // Original copyright notice:
  17. // Purpose: Implementation of the aea (Albers Equal Area) projection.
  18. // Author: Gerald Evenden
  19. // Copyright (c) 1995, Gerald Evenden
  20. // Permission is hereby granted, free of charge, to any person obtaining a
  21. // copy of this software and associated documentation files (the "Software"),
  22. // to deal in the Software without restriction, including without limitation
  23. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  24. // and/or sell copies of the Software, and to permit persons to whom the
  25. // Software is furnished to do so, subject to the following conditions:
  26. // The above copyright notice and this permission notice shall be included
  27. // in all copies or substantial portions of the Software.
  28. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  29. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  30. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  31. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  32. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  33. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  34. // DEALINGS IN THE SOFTWARE.
  35. #ifndef BOOST_GEOMETRY_PROJECTIONS_AEA_HPP
  36. #define BOOST_GEOMETRY_PROJECTIONS_AEA_HPP
  37. #include <boost/core/ignore_unused.hpp>
  38. #include <boost/geometry/util/math.hpp>
  39. #include <boost/math/special_functions/hypot.hpp>
  40. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  41. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  42. #include <boost/geometry/srs/projections/impl/projects.hpp>
  43. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  44. #include <boost/geometry/srs/projections/impl/pj_mlfn.hpp>
  45. #include <boost/geometry/srs/projections/impl/pj_msfn.hpp>
  46. #include <boost/geometry/srs/projections/impl/pj_qsfn.hpp>
  47. namespace boost { namespace geometry
  48. {
  49. namespace srs { namespace par4
  50. {
  51. struct aea {};
  52. struct leac {};
  53. }} //namespace srs::par4
  54. namespace projections
  55. {
  56. #ifndef DOXYGEN_NO_DETAIL
  57. namespace detail { namespace aea
  58. {
  59. static const double epsilon10 = 1.e-10;
  60. static const double tolerance7 = 1.e-7;
  61. static const double epsilon = 1.0e-7;
  62. static const double tolerance = 1.0e-10;
  63. static const int n_iter = 15;
  64. template <typename T>
  65. struct par_aea
  66. {
  67. T ec;
  68. T n;
  69. T c;
  70. T dd;
  71. T n2;
  72. T rho0;
  73. T phi1;
  74. T phi2;
  75. detail::en<T> en;
  76. int ellips;
  77. };
  78. /* determine latitude angle phi-1 */
  79. template <typename T>
  80. inline T phi1_(T const& qs, T const& Te, T const& Tone_es)
  81. {
  82. int i;
  83. T Phi, sinpi, cospi, con, com, dphi;
  84. Phi = asin (.5 * qs);
  85. if (Te < epsilon)
  86. return( Phi );
  87. i = n_iter;
  88. do {
  89. sinpi = sin (Phi);
  90. cospi = cos (Phi);
  91. con = Te * sinpi;
  92. com = 1. - con * con;
  93. dphi = .5 * com * com / cospi * (qs / Tone_es -
  94. sinpi / com + .5 / Te * log ((1. - con) /
  95. (1. + con)));
  96. Phi += dphi;
  97. } while (fabs(dphi) > tolerance && --i);
  98. return( i ? Phi : HUGE_VAL );
  99. }
  100. // template class, using CRTP to implement forward/inverse
  101. template <typename T, typename Parameters>
  102. struct base_aea_ellipsoid
  103. : public base_t_fi<base_aea_ellipsoid<T, Parameters>, T, Parameters>
  104. {
  105. par_aea<T> m_proj_parm;
  106. inline base_aea_ellipsoid(const Parameters& par)
  107. : base_t_fi<base_aea_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  108. {}
  109. // FORWARD(e_forward) ellipsoid & spheroid
  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 rho = this->m_proj_parm.c - (this->m_proj_parm.ellips
  114. ? this->m_proj_parm.n * pj_qsfn(sin(lp_lat), this->m_par.e, this->m_par.one_es)
  115. : this->m_proj_parm.n2 * sin(lp_lat));
  116. if (rho < 0.)
  117. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  118. rho = this->m_proj_parm.dd * sqrt(rho);
  119. xy_x = rho * sin( lp_lon *= this->m_proj_parm.n );
  120. xy_y = this->m_proj_parm.rho0 - rho * cos(lp_lon);
  121. }
  122. // INVERSE(e_inverse) ellipsoid & spheroid
  123. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  124. inline void inv(T& xy_x, T& xy_y, T& lp_lon, T& lp_lat) const
  125. {
  126. static const T half_pi = detail::half_pi<T>();
  127. T rho = 0.0;
  128. if( (rho = boost::math::hypot(xy_x, xy_y = this->m_proj_parm.rho0 - xy_y)) != 0.0 ) {
  129. if (this->m_proj_parm.n < 0.) {
  130. rho = -rho;
  131. xy_x = -xy_x;
  132. xy_y = -xy_y;
  133. }
  134. lp_lat = rho / this->m_proj_parm.dd;
  135. if (this->m_proj_parm.ellips) {
  136. lp_lat = (this->m_proj_parm.c - lp_lat * lp_lat) / this->m_proj_parm.n;
  137. if (fabs(this->m_proj_parm.ec - fabs(lp_lat)) > tolerance7) {
  138. if ((lp_lat = phi1_(lp_lat, this->m_par.e, this->m_par.one_es)) == HUGE_VAL)
  139. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  140. } else
  141. lp_lat = lp_lat < 0. ? -half_pi : half_pi;
  142. } else if (fabs(lp_lat = (this->m_proj_parm.c - lp_lat * lp_lat) / this->m_proj_parm.n2) <= 1.)
  143. lp_lat = asin(lp_lat);
  144. else
  145. lp_lat = lp_lat < 0. ? -half_pi : half_pi;
  146. lp_lon = atan2(xy_x, xy_y) / this->m_proj_parm.n;
  147. } else {
  148. lp_lon = 0.;
  149. lp_lat = this->m_proj_parm.n > 0. ? half_pi : - half_pi;
  150. }
  151. }
  152. static inline std::string get_name()
  153. {
  154. return "aea_ellipsoid";
  155. }
  156. };
  157. template <typename Parameters, typename T>
  158. inline void setup(Parameters& par, par_aea<T>& proj_parm)
  159. {
  160. T cosphi, sinphi;
  161. int secant;
  162. if (fabs(proj_parm.phi1 + proj_parm.phi2) < epsilon10)
  163. BOOST_THROW_EXCEPTION( projection_exception(error_conic_lat_equal) );
  164. proj_parm.n = sinphi = sin(proj_parm.phi1);
  165. cosphi = cos(proj_parm.phi1);
  166. secant = fabs(proj_parm.phi1 - proj_parm.phi2) >= epsilon10;
  167. if( (proj_parm.ellips = (par.es > 0.))) {
  168. T ml1, m1;
  169. proj_parm.en = pj_enfn<T>(par.es);
  170. m1 = pj_msfn(sinphi, cosphi, par.es);
  171. ml1 = pj_qsfn(sinphi, par.e, par.one_es);
  172. if (secant) { /* secant cone */
  173. T ml2, m2;
  174. sinphi = sin(proj_parm.phi2);
  175. cosphi = cos(proj_parm.phi2);
  176. m2 = pj_msfn(sinphi, cosphi, par.es);
  177. ml2 = pj_qsfn(sinphi, par.e, par.one_es);
  178. if (ml2 == ml1)
  179. BOOST_THROW_EXCEPTION( projection_exception(0) );
  180. proj_parm.n = (m1 * m1 - m2 * m2) / (ml2 - ml1);
  181. }
  182. proj_parm.ec = 1. - .5 * par.one_es * log((1. - par.e) /
  183. (1. + par.e)) / par.e;
  184. proj_parm.c = m1 * m1 + proj_parm.n * ml1;
  185. proj_parm.dd = 1. / proj_parm.n;
  186. proj_parm.rho0 = proj_parm.dd * sqrt(proj_parm.c - proj_parm.n * pj_qsfn(sin(par.phi0),
  187. par.e, par.one_es));
  188. } else {
  189. if (secant) proj_parm.n = .5 * (proj_parm.n + sin(proj_parm.phi2));
  190. proj_parm.n2 = proj_parm.n + proj_parm.n;
  191. proj_parm.c = cosphi * cosphi + proj_parm.n2 * sinphi;
  192. proj_parm.dd = 1. / proj_parm.n;
  193. proj_parm.rho0 = proj_parm.dd * sqrt(proj_parm.c - proj_parm.n2 * sin(par.phi0));
  194. }
  195. }
  196. // Albers Equal Area
  197. template <typename Parameters, typename T>
  198. inline void setup_aea(Parameters& par, par_aea<T>& proj_parm)
  199. {
  200. proj_parm.phi1 = pj_get_param_r(par.params, "lat_1");
  201. proj_parm.phi2 = pj_get_param_r(par.params, "lat_2");
  202. setup(par, proj_parm);
  203. }
  204. // Lambert Equal Area Conic
  205. template <typename Parameters, typename T>
  206. inline void setup_leac(Parameters& par, par_aea<T>& proj_parm)
  207. {
  208. static const T half_pi = detail::half_pi<T>();
  209. proj_parm.phi2 = pj_get_param_r(par.params, "lat_1");
  210. proj_parm.phi1 = pj_get_param_b(par.params, "south") ? -half_pi : half_pi;
  211. setup(par, proj_parm);
  212. }
  213. }} // namespace detail::aea
  214. #endif // doxygen
  215. /*!
  216. \brief Albers Equal Area projection
  217. \ingroup projections
  218. \tparam Geographic latlong point type
  219. \tparam Cartesian xy point type
  220. \tparam Parameters parameter type
  221. \par Projection characteristics
  222. - Conic
  223. - Spheroid
  224. - Ellipsoid
  225. \par Projection parameters
  226. - lat_1: Latitude of first standard parallel (degrees)
  227. - lat_2: Latitude of second standard parallel (degrees)
  228. \par Example
  229. \image html ex_aea.gif
  230. */
  231. template <typename T, typename Parameters>
  232. struct aea_ellipsoid : public detail::aea::base_aea_ellipsoid<T, Parameters>
  233. {
  234. inline aea_ellipsoid(const Parameters& par) : detail::aea::base_aea_ellipsoid<T, Parameters>(par)
  235. {
  236. detail::aea::setup_aea(this->m_par, this->m_proj_parm);
  237. }
  238. };
  239. /*!
  240. \brief Lambert Equal Area Conic projection
  241. \ingroup projections
  242. \tparam Geographic latlong point type
  243. \tparam Cartesian xy point type
  244. \tparam Parameters parameter type
  245. \par Projection characteristics
  246. - Conic
  247. - Spheroid
  248. - Ellipsoid
  249. \par Projection parameters
  250. - lat_1: Latitude of first standard parallel (degrees)
  251. - south: Denotes southern hemisphere UTM zone (boolean)
  252. \par Example
  253. \image html ex_leac.gif
  254. */
  255. template <typename T, typename Parameters>
  256. struct leac_ellipsoid : public detail::aea::base_aea_ellipsoid<T, Parameters>
  257. {
  258. inline leac_ellipsoid(const Parameters& par) : detail::aea::base_aea_ellipsoid<T, Parameters>(par)
  259. {
  260. detail::aea::setup_leac(this->m_par, this->m_proj_parm);
  261. }
  262. };
  263. #ifndef DOXYGEN_NO_DETAIL
  264. namespace detail
  265. {
  266. // Static projection
  267. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::aea, aea_ellipsoid, aea_ellipsoid)
  268. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::leac, leac_ellipsoid, leac_ellipsoid)
  269. // Factory entry(s)
  270. template <typename T, typename Parameters>
  271. class aea_entry : public detail::factory_entry<T, Parameters>
  272. {
  273. public :
  274. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  275. {
  276. return new base_v_fi<aea_ellipsoid<T, Parameters>, T, Parameters>(par);
  277. }
  278. };
  279. template <typename T, typename Parameters>
  280. class leac_entry : public detail::factory_entry<T, Parameters>
  281. {
  282. public :
  283. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  284. {
  285. return new base_v_fi<leac_ellipsoid<T, Parameters>, T, Parameters>(par);
  286. }
  287. };
  288. template <typename T, typename Parameters>
  289. inline void aea_init(detail::base_factory<T, Parameters>& factory)
  290. {
  291. factory.add_to_factory("aea", new aea_entry<T, Parameters>);
  292. factory.add_to_factory("leac", new leac_entry<T, Parameters>);
  293. }
  294. } // namespace detail
  295. #endif // doxygen
  296. } // namespace projections
  297. }} // namespace boost::geometry
  298. #endif // BOOST_GEOMETRY_PROJECTIONS_AEA_HPP