natearth.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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. // 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 <typename T, typename Parameters>
  76. struct base_natearth_spheroid
  77. {
  78. // FORWARD(s_forward) spheroid
  79. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  80. inline void fwd(Parameters const& , T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  81. {
  82. T phi2, phi4;
  83. phi2 = lp_lat * lp_lat;
  84. phi4 = phi2 * phi2;
  85. xy_x = lp_lon * (A0 + phi2 * (A1 + phi2 * (A2 + phi4 * phi2 * (A3 + phi2 * A4))));
  86. xy_y = lp_lat * (B0 + phi2 * (B1 + phi4 * (B2 + B3 * phi2 + B4 * phi4)));
  87. }
  88. // INVERSE(s_inverse) spheroid
  89. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  90. inline void inv(Parameters const& , T const& xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  91. {
  92. static const T max_y = natearth::max_y<T>();
  93. T yc, tol, y2, y4, f, fder;
  94. int i;
  95. /* make sure y is inside valid range */
  96. if (xy_y > max_y) {
  97. xy_y = max_y;
  98. } else if (xy_y < -max_y) {
  99. xy_y = -max_y;
  100. }
  101. /* latitude */
  102. yc = xy_y;
  103. for (i = max_iter; i ; --i) { /* Newton-Raphson */
  104. y2 = yc * yc;
  105. y4 = y2 * y2;
  106. f = (yc * (B0 + y2 * (B1 + y4 * (B2 + B3 * y2 + B4 * y4)))) - xy_y;
  107. fder = C0 + y2 * (C1 + y4 * (C2 + C3 * y2 + C4 * y4));
  108. yc -= tol = f / fder;
  109. if (fabs(tol) < epsilon) {
  110. break;
  111. }
  112. }
  113. if( i == 0 )
  114. BOOST_THROW_EXCEPTION( projection_exception(error_non_convergent) );
  115. lp_lat = yc;
  116. /* longitude */
  117. y2 = yc * yc;
  118. lp_lon = xy_x / (A0 + y2 * (A1 + y2 * (A2 + y2 * y2 * y2 * (A3 + y2 * A4))));
  119. }
  120. static inline std::string get_name()
  121. {
  122. return "natearth_spheroid";
  123. }
  124. };
  125. // Natural Earth
  126. template <typename Parameters>
  127. inline void setup_natearth(Parameters& par)
  128. {
  129. par.es = 0;
  130. }
  131. }} // namespace detail::natearth
  132. #endif // doxygen
  133. /*!
  134. \brief Natural Earth projection
  135. \ingroup projections
  136. \tparam Geographic latlong point type
  137. \tparam Cartesian xy point type
  138. \tparam Parameters parameter type
  139. \par Projection characteristics
  140. - Pseudocylindrical
  141. - Spheroid
  142. \par Example
  143. \image html ex_natearth.gif
  144. */
  145. template <typename T, typename Parameters>
  146. struct natearth_spheroid : public detail::natearth::base_natearth_spheroid<T, Parameters>
  147. {
  148. template <typename Params>
  149. inline natearth_spheroid(Params const& , Parameters & par)
  150. {
  151. detail::natearth::setup_natearth(par);
  152. }
  153. };
  154. #ifndef DOXYGEN_NO_DETAIL
  155. namespace detail
  156. {
  157. // Static projection
  158. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_natearth, natearth_spheroid)
  159. // Factory entry(s)
  160. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(natearth_entry, natearth_spheroid)
  161. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(natearth_init)
  162. {
  163. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(natearth, natearth_entry)
  164. }
  165. } // namespace detail
  166. #endif // doxygen
  167. } // namespace projections
  168. }} // namespace boost::geometry
  169. #endif // BOOST_GEOMETRY_PROJECTIONS_NATEARTH_HPP