bipc.hpp 11 KB

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