bipc.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  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_BIPC_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_BIPC_HPP
  32. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  33. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  34. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  35. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  36. #include <boost/geometry/srs/projections/impl/projects.hpp>
  37. #include <boost/geometry/util/math.hpp>
  38. #include <boost/math/special_functions/hypot.hpp>
  39. namespace boost { namespace geometry
  40. {
  41. namespace projections
  42. {
  43. #ifndef DOXYGEN_NO_DETAIL
  44. namespace detail { namespace bipc
  45. {
  46. static const double epsilon = 1e-10;
  47. static const double epsilon10 = 1e-10;
  48. static const double one_plus_eps = 1.000000001;
  49. static const int n_iter = 10;
  50. static const double lamB = -.34894976726250681539;
  51. static const double n = .63055844881274687180;
  52. static const double F = 1.89724742567461030582;
  53. static const double Azab = .81650043674686363166;
  54. static const double Azba = 1.82261843856185925133;
  55. static const double const_T = 1.27246578267089012270;
  56. static const double rhoc = 1.20709121521568721927;
  57. static const double cAzc = .69691523038678375519;
  58. static const double sAzc = .71715351331143607555;
  59. static const double C45 = .70710678118654752469;
  60. static const double S45 = .70710678118654752410;
  61. static const double C20 = .93969262078590838411;
  62. static const double S20 = -.34202014332566873287;
  63. static const double R110 = 1.91986217719376253360;
  64. static const double R104 = 1.81514242207410275904;
  65. struct par_bipc
  66. {
  67. int noskew;
  68. };
  69. // template class, using CRTP to implement forward/inverse
  70. template <typename T, typename Parameters>
  71. struct base_bipc_spheroid
  72. : public base_t_fi<base_bipc_spheroid<T, Parameters>, T, Parameters>
  73. {
  74. par_bipc m_proj_parm;
  75. inline base_bipc_spheroid(const Parameters& par)
  76. : base_t_fi<base_bipc_spheroid<T, Parameters>, T, Parameters>(*this, par)
  77. {}
  78. // FORWARD(s_forward) spheroid
  79. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  80. inline void fwd(T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  81. {
  82. static const T half_pi = detail::half_pi<T>();
  83. static const T pi = detail::pi<T>();
  84. T cphi, sphi, tphi, t, al, Az, z, Av, cdlam, sdlam, r;
  85. int tag;
  86. cphi = cos(lp_lat);
  87. sphi = sin(lp_lat);
  88. cdlam = cos(sdlam = lamB - lp_lon);
  89. sdlam = sin(sdlam);
  90. if (fabs(fabs(lp_lat) - half_pi) < epsilon10) {
  91. Az = lp_lat < 0. ? pi : 0.;
  92. tphi = HUGE_VAL;
  93. } else {
  94. tphi = sphi / cphi;
  95. Az = atan2(sdlam , C45 * (tphi - cdlam));
  96. }
  97. if( (tag = (Az > Azba)) ) {
  98. cdlam = cos(sdlam = lp_lon + R110);
  99. sdlam = sin(sdlam);
  100. z = S20 * sphi + C20 * cphi * cdlam;
  101. if (fabs(z) > 1.) {
  102. if (fabs(z) > one_plus_eps)
  103. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  104. else
  105. z = z < 0. ? -1. : 1.;
  106. } else
  107. z = acos(z);
  108. if (tphi != HUGE_VAL)
  109. Az = atan2(sdlam, (C20 * tphi - S20 * cdlam));
  110. Av = Azab;
  111. xy_y = rhoc;
  112. } else {
  113. z = S45 * (sphi + cphi * cdlam);
  114. if (fabs(z) > 1.) {
  115. if (fabs(z) > one_plus_eps)
  116. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  117. else
  118. z = z < 0. ? -1. : 1.;
  119. } else
  120. z = acos(z);
  121. Av = Azba;
  122. xy_y = -rhoc;
  123. }
  124. if (z < 0.) {
  125. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  126. }
  127. r = F * (t = math::pow(tan(T(0.5) * z), n));
  128. if ((al = .5 * (R104 - z)) < 0.) {
  129. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  130. }
  131. al = (t + math::pow(al, n)) / const_T;
  132. if (fabs(al) > 1.) {
  133. if (fabs(al) > one_plus_eps)
  134. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  135. else
  136. al = al < 0. ? -1. : 1.;
  137. } else
  138. al = acos(al);
  139. if (fabs(t = n * (Av - Az)) < al)
  140. r /= cos(al + (tag ? t : -t));
  141. xy_x = r * sin(t);
  142. xy_y += (tag ? -r : r) * cos(t);
  143. if (this->m_proj_parm.noskew) {
  144. t = xy_x;
  145. xy_x = -xy_x * cAzc - xy_y * sAzc;
  146. xy_y = -xy_y * cAzc + t * sAzc;
  147. }
  148. }
  149. // INVERSE(s_inverse) spheroid
  150. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  151. inline void inv(T xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  152. {
  153. T t, r, rp, rl, al, z, fAz, Az, s, c, Av;
  154. int neg, i;
  155. if (this->m_proj_parm.noskew) {
  156. t = xy_x;
  157. xy_x = -xy_x * cAzc + xy_y * sAzc;
  158. xy_y = -xy_y * cAzc - t * sAzc;
  159. }
  160. if( (neg = (xy_x < 0.)) ) {
  161. xy_y = rhoc - xy_y;
  162. s = S20;
  163. c = C20;
  164. Av = Azab;
  165. } else {
  166. xy_y += rhoc;
  167. s = S45;
  168. c = C45;
  169. Av = Azba;
  170. }
  171. rl = rp = r = boost::math::hypot(xy_x, xy_y);
  172. fAz = fabs(Az = atan2(xy_x, xy_y));
  173. for (i = n_iter; i ; --i) {
  174. z = 2. * atan(math::pow(r / F,T(1) / n));
  175. al = acos((math::pow(tan(T(0.5) * z), n) +
  176. math::pow(tan(T(0.5) * (R104 - z)), n)) / const_T);
  177. if (fAz < al)
  178. r = rp * cos(al + (neg ? Az : -Az));
  179. if (fabs(rl - r) < epsilon)
  180. break;
  181. rl = r;
  182. }
  183. if (! i)
  184. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  185. Az = Av - Az / n;
  186. lp_lat = asin(s * cos(z) + c * sin(z) * cos(Az));
  187. lp_lon = atan2(sin(Az), c / tan(z) - s * cos(Az));
  188. if (neg)
  189. lp_lon -= R110;
  190. else
  191. lp_lon = lamB - lp_lon;
  192. }
  193. static inline std::string get_name()
  194. {
  195. return "bipc_spheroid";
  196. }
  197. };
  198. // Bipolar conic of western hemisphere
  199. template <typename Params, typename Parameters>
  200. inline void setup_bipc(Params const& params, Parameters& par, par_bipc& proj_parm)
  201. {
  202. proj_parm.noskew = pj_get_param_b<srs::spar::ns>(params, "ns", srs::dpar::ns);
  203. par.es = 0.;
  204. }
  205. }} // namespace detail::bipc
  206. #endif // doxygen
  207. /*!
  208. \brief Bipolar conic of western hemisphere projection
  209. \ingroup projections
  210. \tparam Geographic latlong point type
  211. \tparam Cartesian xy point type
  212. \tparam Parameters parameter type
  213. \par Projection characteristics
  214. - Conic
  215. - Spheroid
  216. \par Projection parameters
  217. - ns (boolean)
  218. \par Example
  219. \image html ex_bipc.gif
  220. */
  221. template <typename T, typename Parameters>
  222. struct bipc_spheroid : public detail::bipc::base_bipc_spheroid<T, Parameters>
  223. {
  224. template <typename Params>
  225. inline bipc_spheroid(Params const& params, Parameters const& par)
  226. : detail::bipc::base_bipc_spheroid<T, Parameters>(par)
  227. {
  228. detail::bipc::setup_bipc(params, this->m_par, this->m_proj_parm);
  229. }
  230. };
  231. #ifndef DOXYGEN_NO_DETAIL
  232. namespace detail
  233. {
  234. // Static projection
  235. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::spar::proj_bipc, bipc_spheroid, bipc_spheroid)
  236. // Factory entry(s)
  237. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(bipc_entry, bipc_spheroid)
  238. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(bipc_init)
  239. {
  240. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(bipc, bipc_entry)
  241. }
  242. } // namespace detail
  243. #endif // doxygen
  244. } // namespace projections
  245. }} // namespace boost::geometry
  246. #endif // BOOST_GEOMETRY_PROJECTIONS_BIPC_HPP