natearth.hpp 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  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 srs { namespace par4
  51. {
  52. struct natearth {}; // Natural Earth
  53. }} //namespace srs::par4
  54. namespace projections
  55. {
  56. #ifndef DOXYGEN_NO_DETAIL
  57. namespace detail { namespace natearth
  58. {
  59. static const double A0 = 0.8707;
  60. static const double A1 = -0.131979;
  61. static const double A2 = -0.013791;
  62. static const double A3 = 0.003971;
  63. static const double A4 = -0.001529;
  64. static const double B0 = 1.007226;
  65. static const double B1 = 0.015085;
  66. static const double B2 = -0.044475;
  67. static const double B3 = 0.028874;
  68. static const double B4 = -0.005916;
  69. static const double C0 = B0;
  70. static const double C1 = (3 * B1);
  71. static const double C2 = (7 * B2);
  72. static const double C3 = (9 * B3);
  73. static const double C4 = (11 * B4);
  74. static const double epsilon = 1e-11;
  75. template <typename T>
  76. inline T max_y() { return (0.8707 * 0.52 * detail::pi<T>()); }
  77. /* Not sure at all of the appropriate number for max_iter... */
  78. static const int max_iter = 100;
  79. // template class, using CRTP to implement forward/inverse
  80. template <typename T, typename Parameters>
  81. struct base_natearth_spheroid
  82. : public base_t_fi<base_natearth_spheroid<T, Parameters>, T, Parameters>
  83. {
  84. inline base_natearth_spheroid(const Parameters& par)
  85. : base_t_fi<base_natearth_spheroid<T, Parameters>, T, Parameters>(*this, par)
  86. {}
  87. // FORWARD(s_forward) spheroid
  88. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  89. inline void fwd(T& lp_lon, T& lp_lat, T& xy_x, T& xy_y) const
  90. {
  91. T phi2, phi4;
  92. phi2 = lp_lat * lp_lat;
  93. phi4 = phi2 * phi2;
  94. xy_x = lp_lon * (A0 + phi2 * (A1 + phi2 * (A2 + phi4 * phi2 * (A3 + phi2 * A4))));
  95. xy_y = lp_lat * (B0 + phi2 * (B1 + phi4 * (B2 + B3 * phi2 + B4 * phi4)));
  96. }
  97. // INVERSE(s_inverse) spheroid
  98. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  99. inline void inv(T& xy_x, T& xy_y, T& lp_lon, T& lp_lat) const
  100. {
  101. static const T max_y = natearth::max_y<T>();
  102. T yc, tol, y2, y4, f, fder;
  103. int i;
  104. /* make sure y is inside valid range */
  105. if (xy_y > max_y) {
  106. xy_y = max_y;
  107. } else if (xy_y < -max_y) {
  108. xy_y = -max_y;
  109. }
  110. /* latitude */
  111. yc = xy_y;
  112. for (i = max_iter; i ; --i) { /* Newton-Raphson */
  113. y2 = yc * yc;
  114. y4 = y2 * y2;
  115. f = (yc * (B0 + y2 * (B1 + y4 * (B2 + B3 * y2 + B4 * y4)))) - xy_y;
  116. fder = C0 + y2 * (C1 + y4 * (C2 + C3 * y2 + C4 * y4));
  117. yc -= tol = f / fder;
  118. if (fabs(tol) < epsilon) {
  119. break;
  120. }
  121. }
  122. if( i == 0 )
  123. BOOST_THROW_EXCEPTION( projection_exception(error_non_convergent) );
  124. lp_lat = yc;
  125. /* longitude */
  126. y2 = yc * yc;
  127. lp_lon = xy_x / (A0 + y2 * (A1 + y2 * (A2 + y2 * y2 * y2 * (A3 + y2 * A4))));
  128. }
  129. static inline std::string get_name()
  130. {
  131. return "natearth_spheroid";
  132. }
  133. };
  134. // Natural Earth
  135. template <typename Parameters>
  136. inline void setup_natearth(Parameters& par)
  137. {
  138. par.es = 0;
  139. }
  140. }} // namespace detail::natearth
  141. #endif // doxygen
  142. /*!
  143. \brief Natural Earth projection
  144. \ingroup projections
  145. \tparam Geographic latlong point type
  146. \tparam Cartesian xy point type
  147. \tparam Parameters parameter type
  148. \par Projection characteristics
  149. - Pseudocylindrical
  150. - Spheroid
  151. \par Example
  152. \image html ex_natearth.gif
  153. */
  154. template <typename T, typename Parameters>
  155. struct natearth_spheroid : public detail::natearth::base_natearth_spheroid<T, Parameters>
  156. {
  157. inline natearth_spheroid(const Parameters& par) : detail::natearth::base_natearth_spheroid<T, Parameters>(par)
  158. {
  159. detail::natearth::setup_natearth(this->m_par);
  160. }
  161. };
  162. #ifndef DOXYGEN_NO_DETAIL
  163. namespace detail
  164. {
  165. // Static projection
  166. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::natearth, natearth_spheroid, natearth_spheroid)
  167. // Factory entry(s)
  168. template <typename T, typename Parameters>
  169. class natearth_entry : public detail::factory_entry<T, Parameters>
  170. {
  171. public :
  172. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  173. {
  174. return new base_v_fi<natearth_spheroid<T, Parameters>, T, Parameters>(par);
  175. }
  176. };
  177. template <typename T, typename Parameters>
  178. inline void natearth_init(detail::base_factory<T, Parameters>& factory)
  179. {
  180. factory.add_to_factory("natearth", new natearth_entry<T, Parameters>);
  181. }
  182. } // namespace detail
  183. #endif // doxygen
  184. } // namespace projections
  185. }} // namespace boost::geometry
  186. #endif // BOOST_GEOMETRY_PROJECTIONS_NATEARTH_HPP