natearth.hpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // The Natural Earth projection was designed by Tom Patterson, US National Park
  31. // Service, in 2007, using Flex Projector. The shape of the original projection
  32. // was defined at every 5 degrees and piece-wise cubic spline interpolation was
  33. // used to compute the complete graticule.
  34. // The code here uses polynomial functions instead of cubic splines and
  35. // is therefore much simpler to program. The polynomial approximation was
  36. // developed by Bojan Savric, in collaboration with Tom Patterson and Bernhard
  37. // Jenny, Institute of Cartography, ETH Zurich. It slightly deviates from
  38. // Patterson's original projection by adding additional curvature to meridians
  39. // where they meet the horizontal pole line. This improvement is by intention
  40. // and designed in collaboration with Tom Patterson.
  41. // Port to PROJ.4 by Bernhard Jenny, 6 June 2011
  42. #ifndef BOOST_GEOMETRY_PROJECTIONS_NATEARTH_HPP
  43. #define BOOST_GEOMETRY_PROJECTIONS_NATEARTH_HPP
  44. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  45. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  46. #include <boost/geometry/srs/projections/impl/projects.hpp>
  47. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  48. namespace boost { namespace geometry
  49. {
  50. namespace projections
  51. {
  52. #ifndef DOXYGEN_NO_DETAIL
  53. namespace detail { namespace natearth
  54. {
  55. static const double A0 = 0.8707;
  56. static const double A1 = -0.131979;
  57. static const double A2 = -0.013791;
  58. static const double A3 = 0.003971;
  59. static const double A4 = -0.001529;
  60. static const double B0 = 1.007226;
  61. static const double B1 = 0.015085;
  62. static const double B2 = -0.044475;
  63. static const double B3 = 0.028874;
  64. static const double B4 = -0.005916;
  65. static const double C0 = B0;
  66. static const double C1 = (3 * B1);
  67. static const double C2 = (7 * B2);
  68. static const double C3 = (9 * B3);
  69. static const double C4 = (11 * B4);
  70. static const double epsilon = 1e-11;
  71. template <typename T>
  72. inline T max_y() { return (0.8707 * 0.52 * detail::pi<T>()); }
  73. /* Not sure at all of the appropriate number for max_iter... */
  74. static const int max_iter = 100;
  75. // template class, using CRTP to implement forward/inverse
  76. template <typename T, typename Parameters>
  77. struct base_natearth_spheroid
  78. : public base_t_fi<base_natearth_spheroid<T, Parameters>, T, Parameters>
  79. {
  80. inline base_natearth_spheroid(const Parameters& par)
  81. : base_t_fi<base_natearth_spheroid<T, Parameters>, T, Parameters>(*this, par)
  82. {}
  83. // FORWARD(s_forward) spheroid
  84. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  85. inline void fwd(T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  86. {
  87. T phi2, phi4;
  88. phi2 = lp_lat * lp_lat;
  89. phi4 = phi2 * phi2;
  90. xy_x = lp_lon * (A0 + phi2 * (A1 + phi2 * (A2 + phi4 * phi2 * (A3 + phi2 * A4))));
  91. xy_y = lp_lat * (B0 + phi2 * (B1 + phi4 * (B2 + B3 * phi2 + B4 * phi4)));
  92. }
  93. // INVERSE(s_inverse) spheroid
  94. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  95. inline void inv(T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  96. {
  97. static const T max_y = natearth::max_y<T>();
  98. T yc, tol, y2, y4, f, fder;
  99. int i;
  100. /* make sure y is inside valid range */
  101. if (xy_y > max_y) {
  102. xy_y = max_y;
  103. } else if (xy_y < -max_y) {
  104. xy_y = -max_y;
  105. }
  106. /* latitude */
  107. yc = xy_y;
  108. for (i = max_iter; i ; --i) { /* Newton-Raphson */
  109. y2 = yc * yc;
  110. y4 = y2 * y2;
  111. f = (yc * (B0 + y2 * (B1 + y4 * (B2 + B3 * y2 + B4 * y4)))) - xy_y;
  112. fder = C0 + y2 * (C1 + y4 * (C2 + C3 * y2 + C4 * y4));
  113. yc -= tol = f / fder;
  114. if (fabs(tol) < epsilon) {
  115. break;
  116. }
  117. }
  118. if( i == 0 )
  119. BOOST_THROW_EXCEPTION( projection_exception(error_non_convergent) );
  120. lp_lat = yc;
  121. /* longitude */
  122. y2 = yc * yc;
  123. lp_lon = xy_x / (A0 + y2 * (A1 + y2 * (A2 + y2 * y2 * y2 * (A3 + y2 * A4))));
  124. }
  125. static inline std::string get_name()
  126. {
  127. return "natearth_spheroid";
  128. }
  129. };
  130. // Natural Earth
  131. template <typename Parameters>
  132. inline void setup_natearth(Parameters& par)
  133. {
  134. par.es = 0;
  135. }
  136. }} // namespace detail::natearth
  137. #endif // doxygen
  138. /*!
  139. \brief Natural Earth projection
  140. \ingroup projections
  141. \tparam Geographic latlong point type
  142. \tparam Cartesian xy point type
  143. \tparam Parameters parameter type
  144. \par Projection characteristics
  145. - Pseudocylindrical
  146. - Spheroid
  147. \par Example
  148. \image html ex_natearth.gif
  149. */
  150. template <typename T, typename Parameters>
  151. struct natearth_spheroid : public detail::natearth::base_natearth_spheroid<T, Parameters>
  152. {
  153. template <typename Params>
  154. inline natearth_spheroid(Params const& , Parameters const& par)
  155. : detail::natearth::base_natearth_spheroid<T, Parameters>(par)
  156. {
  157. detail::natearth::setup_natearth(this->m_par);
  158. }
  159. };
  160. #ifndef DOXYGEN_NO_DETAIL
  161. namespace detail
  162. {
  163. // Static projection
  164. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::spar::proj_natearth, natearth_spheroid, natearth_spheroid)
  165. // Factory entry(s)
  166. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(natearth_entry, natearth_spheroid)
  167. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(natearth_init)
  168. {
  169. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(natearth, natearth_entry)
  170. }
  171. } // namespace detail
  172. #endif // doxygen
  173. } // namespace projections
  174. }} // namespace boost::geometry
  175. #endif // BOOST_GEOMETRY_PROJECTIONS_NATEARTH_HPP