robin.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298
  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. #ifndef BOOST_GEOMETRY_PROJECTIONS_ROBIN_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_ROBIN_HPP
  32. #include <boost/geometry/util/math.hpp>
  33. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  34. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  35. #include <boost/geometry/srs/projections/impl/projects.hpp>
  36. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  37. #include <boost/geometry/srs/projections/impl/function_overloads.hpp>
  38. namespace boost { namespace geometry
  39. {
  40. namespace srs { namespace par4
  41. {
  42. struct robin {}; // Robinson
  43. }} //namespace srs::par4
  44. namespace projections
  45. {
  46. #ifndef DOXYGEN_NO_DETAIL
  47. namespace detail { namespace robin
  48. {
  49. static const double FXC = 0.8487;
  50. static const double FYC = 1.3523;
  51. static const double C1 = 11.45915590261646417544;
  52. static const double RC1 = 0.08726646259971647884;
  53. static const int n_nodes = 18;
  54. static const double one_plus_eps = 1.000001;
  55. static const double epsilon = 1e-8;
  56. /* Not sure at all of the appropriate number for max_iter... */
  57. static const int max_iter = 100;
  58. /*
  59. note: following terms based upon 5 deg. intervals in degrees.
  60. Some background on these coefficients is available at:
  61. http://article.gmane.org/gmane.comp.gis.proj-4.devel/6039
  62. http://trac.osgeo.org/proj/ticket/113
  63. */
  64. template <typename T>
  65. struct coefs {
  66. T c0, c1, c2, c3;
  67. };
  68. template <typename T>
  69. inline const coefs<T> * coefs_x()
  70. {
  71. static const coefs<T> result[] = {
  72. {1.0, 2.2199e-17, -7.15515e-05, 3.1103e-06},
  73. {0.9986, -0.000482243, -2.4897e-05, -1.3309e-06},
  74. {0.9954, -0.00083103, -4.48605e-05, -9.86701e-07},
  75. {0.99, -0.00135364, -5.9661e-05, 3.6777e-06},
  76. {0.9822, -0.00167442, -4.49547e-06, -5.72411e-06},
  77. {0.973, -0.00214868, -9.03571e-05, 1.8736e-08},
  78. {0.96, -0.00305085, -9.00761e-05, 1.64917e-06},
  79. {0.9427, -0.00382792, -6.53386e-05, -2.6154e-06},
  80. {0.9216, -0.00467746, -0.00010457, 4.81243e-06},
  81. {0.8962, -0.00536223, -3.23831e-05, -5.43432e-06},
  82. {0.8679, -0.00609363, -0.000113898, 3.32484e-06},
  83. {0.835, -0.00698325, -6.40253e-05, 9.34959e-07},
  84. {0.7986, -0.00755338, -5.00009e-05, 9.35324e-07},
  85. {0.7597, -0.00798324, -3.5971e-05, -2.27626e-06},
  86. {0.7186, -0.00851367, -7.01149e-05, -8.6303e-06},
  87. {0.6732, -0.00986209, -0.000199569, 1.91974e-05},
  88. {0.6213, -0.010418, 8.83923e-05, 6.24051e-06},
  89. {0.5722, -0.00906601, 0.000182, 6.24051e-06},
  90. {0.5322, -0.00677797, 0.000275608, 6.24051e-06}
  91. };
  92. return result;
  93. }
  94. template <typename T>
  95. inline const coefs<T> * coefs_y()
  96. {
  97. static const coefs<T> result[] = {
  98. {-5.20417e-18, 0.0124, 1.21431e-18, -8.45284e-11},
  99. {0.062, 0.0124, -1.26793e-09, 4.22642e-10},
  100. {0.124, 0.0124, 5.07171e-09, -1.60604e-09},
  101. {0.186, 0.0123999, -1.90189e-08, 6.00152e-09},
  102. {0.248, 0.0124002, 7.10039e-08, -2.24e-08},
  103. {0.31, 0.0123992, -2.64997e-07, 8.35986e-08},
  104. {0.372, 0.0124029, 9.88983e-07, -3.11994e-07},
  105. {0.434, 0.0123893, -3.69093e-06, -4.35621e-07},
  106. {0.4958, 0.0123198, -1.02252e-05, -3.45523e-07},
  107. {0.5571, 0.0121916, -1.54081e-05, -5.82288e-07},
  108. {0.6176, 0.0119938, -2.41424e-05, -5.25327e-07},
  109. {0.6769, 0.011713, -3.20223e-05, -5.16405e-07},
  110. {0.7346, 0.0113541, -3.97684e-05, -6.09052e-07},
  111. {0.7903, 0.0109107, -4.89042e-05, -1.04739e-06},
  112. {0.8435, 0.0103431, -6.4615e-05, -1.40374e-09},
  113. {0.8936, 0.00969686, -6.4636e-05, -8.547e-06},
  114. {0.9394, 0.00840947, -0.000192841, -4.2106e-06},
  115. {0.9761, 0.00616527, -0.000256, -4.2106e-06},
  116. {1.0, 0.00328947, -0.000319159, -4.2106e-06}
  117. };
  118. return result;
  119. }
  120. // template class, using CRTP to implement forward/inverse
  121. template <typename T, typename Parameters>
  122. struct base_robin_spheroid
  123. : public base_t_fi<base_robin_spheroid<T, Parameters>, T, Parameters>
  124. {
  125. inline base_robin_spheroid(const Parameters& par)
  126. : base_t_fi<base_robin_spheroid<T, Parameters>, T, Parameters>(*this, par)
  127. {}
  128. inline T v(coefs<T> const& c, T const& z) const
  129. { return (c.c0 + z * (c.c1 + z * (c.c2 + z * c.c3))); }
  130. inline T dv(coefs<T> const& c, T const& z) const
  131. { return (c.c1 + z * (c.c2 + c.c2 + z * 3. * c.c3)); }
  132. // FORWARD(s_forward) spheroid
  133. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  134. inline void fwd(T& lp_lon, T& lp_lat, T& xy_x, T& xy_y) const
  135. {
  136. int i;
  137. T dphi;
  138. i = int_floor((dphi = fabs(lp_lat)) * C1);
  139. if (i < 0) {
  140. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  141. }
  142. if (i >= n_nodes) i = n_nodes - 1;
  143. dphi = geometry::math::r2d<T>() * (dphi - RC1 * i);
  144. xy_x = v(coefs_x<T>()[i], dphi) * FXC * lp_lon;
  145. xy_y = v(coefs_y<T>()[i], dphi) * FYC;
  146. if (lp_lat < 0.) xy_y = -xy_y;
  147. }
  148. // INVERSE(s_inverse) spheroid
  149. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  150. inline void inv(T& xy_x, T& xy_y, T& lp_lon, T& lp_lat) const
  151. {
  152. static const T half_pi = detail::half_pi<T>();
  153. const coefs<T> * coefs_x = robin::coefs_x<T>();
  154. const coefs<T> * coefs_y = robin::coefs_y<T>();
  155. int i;
  156. T t, t1;
  157. coefs<T> coefs_t;
  158. int iters;
  159. lp_lon = xy_x / FXC;
  160. lp_lat = fabs(xy_y / FYC);
  161. if (lp_lat >= 1.) { /* simple pathologic cases */
  162. if (lp_lat > one_plus_eps) {
  163. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  164. } else {
  165. lp_lat = xy_y < 0. ? -half_pi : half_pi;
  166. lp_lon /= coefs_x[n_nodes].c0;
  167. }
  168. } else { /* general problem */
  169. /* in Y space, reduce to table interval */
  170. i = int_floor(lp_lat * n_nodes);
  171. if( i < 0 || i >= n_nodes ) {
  172. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  173. }
  174. for (;;) {
  175. if (coefs_y[i].c0 > lp_lat) --i;
  176. else if (coefs_y[i+1].c0 <= lp_lat) ++i;
  177. else break;
  178. }
  179. coefs_t = coefs_y[i];
  180. /* first guess, linear interp */
  181. t = 5. * (lp_lat - coefs_t.c0)/(coefs_y[i+1].c0 - coefs_t.c0);
  182. /* make into root */
  183. coefs_t.c0 = (T)(coefs_t.c0 - lp_lat);
  184. for (iters = max_iter; iters ; --iters) { /* Newton-Raphson */
  185. t -= t1 = v(coefs_t,t) / dv(coefs_t,t);
  186. if (fabs(t1) < epsilon)
  187. break;
  188. }
  189. if( iters == 0 )
  190. BOOST_THROW_EXCEPTION( projection_exception(error_non_convergent) );
  191. lp_lat = (5 * i + t) * geometry::math::d2r<T>();
  192. if (xy_y < 0.) lp_lat = -lp_lat;
  193. lp_lon /= v(coefs_x[i], t);
  194. }
  195. }
  196. static inline std::string get_name()
  197. {
  198. return "robin_spheroid";
  199. }
  200. };
  201. // Robinson
  202. template <typename Parameters>
  203. inline void setup_robin(Parameters& par)
  204. {
  205. par.es = 0.;
  206. }
  207. }} // namespace detail::robin
  208. #endif // doxygen
  209. /*!
  210. \brief Robinson projection
  211. \ingroup projections
  212. \tparam Geographic latlong point type
  213. \tparam Cartesian xy point type
  214. \tparam Parameters parameter type
  215. \par Projection characteristics
  216. - Pseudocylindrical
  217. - Spheroid
  218. \par Example
  219. \image html ex_robin.gif
  220. */
  221. template <typename T, typename Parameters>
  222. struct robin_spheroid : public detail::robin::base_robin_spheroid<T, Parameters>
  223. {
  224. inline robin_spheroid(const Parameters& par) : detail::robin::base_robin_spheroid<T, Parameters>(par)
  225. {
  226. detail::robin::setup_robin(this->m_par);
  227. }
  228. };
  229. #ifndef DOXYGEN_NO_DETAIL
  230. namespace detail
  231. {
  232. // Static projection
  233. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::robin, robin_spheroid, robin_spheroid)
  234. // Factory entry(s)
  235. template <typename T, typename Parameters>
  236. class robin_entry : public detail::factory_entry<T, Parameters>
  237. {
  238. public :
  239. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  240. {
  241. return new base_v_fi<robin_spheroid<T, Parameters>, T, Parameters>(par);
  242. }
  243. };
  244. template <typename T, typename Parameters>
  245. inline void robin_init(detail::base_factory<T, Parameters>& factory)
  246. {
  247. factory.add_to_factory("robin", new robin_entry<T, Parameters>);
  248. }
  249. } // namespace detail
  250. #endif // doxygen
  251. } // namespace projections
  252. }} // namespace boost::geometry
  253. #endif // BOOST_GEOMETRY_PROJECTIONS_ROBIN_HPP