bipc.hpp 12 KB

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