geos.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. // Copyright (c) 2004 Gerald I. Evenden
  16. // Copyright (c) 2012 Martin Raspaud
  17. // See also (section 4.4.3.2):
  18. // http://www.eumetsat.int/en/area4/msg/news/us_doc/cgms_03_26.pdf
  19. // Permission is hereby granted, free of charge, to any person obtaining a
  20. // copy of this software and associated documentation files (the "Software"),
  21. // to deal in the Software without restriction, including without limitation
  22. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  23. // and/or sell copies of the Software, and to permit persons to whom the
  24. // Software is furnished to do so, subject to the following conditions:
  25. // The above copyright notice and this permission notice shall be included
  26. // in all copies or substantial portions of the Software.
  27. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  28. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  29. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  30. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  31. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  32. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  33. // DEALINGS IN THE SOFTWARE.
  34. #ifndef BOOST_GEOMETRY_PROJECTIONS_GEOS_HPP
  35. #define BOOST_GEOMETRY_PROJECTIONS_GEOS_HPP
  36. #include <boost/math/special_functions/hypot.hpp>
  37. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  38. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  39. #include <boost/geometry/srs/projections/impl/projects.hpp>
  40. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  41. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  42. namespace boost { namespace geometry
  43. {
  44. namespace projections
  45. {
  46. #ifndef DOXYGEN_NO_DETAIL
  47. namespace detail { namespace geos
  48. {
  49. template <typename T>
  50. struct par_geos
  51. {
  52. T h;
  53. T radius_p;
  54. T radius_p2;
  55. T radius_p_inv2;
  56. T radius_g;
  57. T radius_g_1;
  58. T C;
  59. bool flip_axis;
  60. };
  61. // template class, using CRTP to implement forward/inverse
  62. template <typename T, typename Parameters>
  63. struct base_geos_ellipsoid
  64. : public base_t_fi<base_geos_ellipsoid<T, Parameters>, T, Parameters>
  65. {
  66. par_geos<T> m_proj_parm;
  67. inline base_geos_ellipsoid(const Parameters& par)
  68. : base_t_fi<base_geos_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  69. {}
  70. // FORWARD(e_forward) ellipsoid
  71. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  72. inline void fwd(T const& lp_lon, T lp_lat, T& xy_x, T& xy_y) const
  73. {
  74. T r, Vx, Vy, Vz, tmp;
  75. /* Calculation of geocentric latitude. */
  76. lp_lat = atan (this->m_proj_parm.radius_p2 * tan (lp_lat));
  77. /* Calculation of the three components of the vector from satellite to
  78. ** position on earth surface (lon,lat).*/
  79. r = (this->m_proj_parm.radius_p) / boost::math::hypot(this->m_proj_parm.radius_p * cos (lp_lat), sin (lp_lat));
  80. Vx = r * cos (lp_lon) * cos (lp_lat);
  81. Vy = r * sin (lp_lon) * cos (lp_lat);
  82. Vz = r * sin (lp_lat);
  83. /* Check visibility. */
  84. if (((this->m_proj_parm.radius_g - Vx) * Vx - Vy * Vy - Vz * Vz * this->m_proj_parm.radius_p_inv2) < 0.) {
  85. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  86. }
  87. /* Calculation based on view angles from satellite. */
  88. tmp = this->m_proj_parm.radius_g - Vx;
  89. if(this->m_proj_parm.flip_axis) {
  90. xy_x = this->m_proj_parm.radius_g_1 * atan (Vy / boost::math::hypot (Vz, tmp));
  91. xy_y = this->m_proj_parm.radius_g_1 * atan (Vz / tmp);
  92. } else {
  93. xy_x = this->m_proj_parm.radius_g_1 * atan (Vy / tmp);
  94. xy_y = this->m_proj_parm.radius_g_1 * atan (Vz / boost::math::hypot (Vy, tmp));
  95. }
  96. }
  97. // INVERSE(e_inverse) ellipsoid
  98. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  99. inline void inv(T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  100. {
  101. T Vx, Vy, Vz, a, b, det, k;
  102. /* Setting three components of vector from satellite to position.*/
  103. Vx = -1.0;
  104. if(this->m_proj_parm.flip_axis) {
  105. Vz = tan (xy_y / this->m_proj_parm.radius_g_1);
  106. Vy = tan (xy_x / this->m_proj_parm.radius_g_1) * boost::math::hypot(1.0, Vz);
  107. } else {
  108. Vy = tan (xy_x / this->m_proj_parm.radius_g_1);
  109. Vz = tan (xy_y / this->m_proj_parm.radius_g_1) * boost::math::hypot(1.0, Vy);
  110. }
  111. /* Calculation of terms in cubic equation and determinant.*/
  112. a = Vz / this->m_proj_parm.radius_p;
  113. a = Vy * Vy + a * a + Vx * Vx;
  114. b = 2 * this->m_proj_parm.radius_g * Vx;
  115. if ((det = (b * b) - 4 * a * this->m_proj_parm.C) < 0.) {
  116. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  117. }
  118. /* Calculation of three components of vector from satellite to position.*/
  119. k = (-b - sqrt(det)) / (2. * a);
  120. Vx = this->m_proj_parm.radius_g + k * Vx;
  121. Vy *= k;
  122. Vz *= k;
  123. /* Calculation of longitude and latitude.*/
  124. lp_lon = atan2 (Vy, Vx);
  125. lp_lat = atan (Vz * cos (lp_lon) / Vx);
  126. lp_lat = atan (this->m_proj_parm.radius_p_inv2 * tan (lp_lat));
  127. }
  128. static inline std::string get_name()
  129. {
  130. return "geos_ellipsoid";
  131. }
  132. };
  133. // template class, using CRTP to implement forward/inverse
  134. template <typename T, typename Parameters>
  135. struct base_geos_spheroid
  136. : public base_t_fi<base_geos_spheroid<T, Parameters>, T, Parameters>
  137. {
  138. par_geos<T> m_proj_parm;
  139. inline base_geos_spheroid(const Parameters& par)
  140. : base_t_fi<base_geos_spheroid<T, Parameters>, T, Parameters>(*this, par)
  141. {}
  142. // FORWARD(s_forward) spheroid
  143. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  144. inline void fwd(T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  145. {
  146. T Vx, Vy, Vz, tmp;
  147. /* Calculation of the three components of the vector from satellite to
  148. ** position on earth surface (lon,lat).*/
  149. tmp = cos(lp_lat);
  150. Vx = cos (lp_lon) * tmp;
  151. Vy = sin (lp_lon) * tmp;
  152. Vz = sin (lp_lat);
  153. /* Check visibility.*/
  154. // TODO: in proj4 5.0.0 this check is not present
  155. if (((this->m_proj_parm.radius_g - Vx) * Vx - Vy * Vy - Vz * Vz) < 0.)
  156. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  157. /* Calculation based on view angles from satellite.*/
  158. tmp = this->m_proj_parm.radius_g - Vx;
  159. if(this->m_proj_parm.flip_axis) {
  160. xy_x = this->m_proj_parm.radius_g_1 * atan(Vy / boost::math::hypot(Vz, tmp));
  161. xy_y = this->m_proj_parm.radius_g_1 * atan(Vz / tmp);
  162. } else {
  163. xy_x = this->m_proj_parm.radius_g_1 * atan(Vy / tmp);
  164. xy_y = this->m_proj_parm.radius_g_1 * atan(Vz / boost::math::hypot(Vy, tmp));
  165. }
  166. }
  167. // INVERSE(s_inverse) spheroid
  168. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  169. inline void inv(T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  170. {
  171. T Vx, Vy, Vz, a, b, det, k;
  172. /* Setting three components of vector from satellite to position.*/
  173. Vx = -1.0;
  174. if(this->m_proj_parm.flip_axis) {
  175. Vz = tan (xy_y / (this->m_proj_parm.radius_g - 1.0));
  176. Vy = tan (xy_x / (this->m_proj_parm.radius_g - 1.0)) * sqrt (1.0 + Vz * Vz);
  177. } else {
  178. Vy = tan (xy_x / (this->m_proj_parm.radius_g - 1.0));
  179. Vz = tan (xy_y / (this->m_proj_parm.radius_g - 1.0)) * sqrt (1.0 + Vy * Vy);
  180. }
  181. /* Calculation of terms in cubic equation and determinant.*/
  182. a = Vy * Vy + Vz * Vz + Vx * Vx;
  183. b = 2 * this->m_proj_parm.radius_g * Vx;
  184. if ((det = (b * b) - 4 * a * this->m_proj_parm.C) < 0.) {
  185. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  186. }
  187. /* Calculation of three components of vector from satellite to position.*/
  188. k = (-b - sqrt(det)) / (2 * a);
  189. Vx = this->m_proj_parm.radius_g + k * Vx;
  190. Vy *= k;
  191. Vz *= k;
  192. /* Calculation of longitude and latitude.*/
  193. lp_lon = atan2 (Vy, Vx);
  194. lp_lat = atan (Vz * cos (lp_lon) / Vx);
  195. }
  196. static inline std::string get_name()
  197. {
  198. return "geos_spheroid";
  199. }
  200. };
  201. inline bool geos_flip_axis(srs::detail::proj4_parameters const& params)
  202. {
  203. std::string sweep_axis = pj_get_param_s(params, "sweep");
  204. if (sweep_axis.empty())
  205. return false;
  206. else {
  207. if (sweep_axis[1] != '\0' || (sweep_axis[0] != 'x' && sweep_axis[0] != 'y'))
  208. BOOST_THROW_EXCEPTION( projection_exception(error_invalid_sweep_axis) );
  209. if (sweep_axis[0] == 'x')
  210. return true;
  211. else
  212. return false;
  213. }
  214. }
  215. template <typename T>
  216. inline bool geos_flip_axis(srs::dpar::parameters<T> const& params)
  217. {
  218. typename srs::dpar::parameters<T>::const_iterator
  219. it = pj_param_find(params, srs::dpar::sweep);
  220. if (it == params.end()) {
  221. return false;
  222. } else {
  223. srs::dpar::value_sweep s = static_cast<srs::dpar::value_sweep>(it->template get_value<int>());
  224. return s == srs::dpar::sweep_x;
  225. }
  226. }
  227. // Geostationary Satellite View
  228. template <typename Params, typename Parameters, typename T>
  229. inline void setup_geos(Params const& params, Parameters& par, par_geos<T>& proj_parm)
  230. {
  231. std::string sweep_axis;
  232. if ((proj_parm.h = pj_get_param_f<T, srs::spar::h>(params, "h", srs::dpar::h)) <= 0.)
  233. BOOST_THROW_EXCEPTION( projection_exception(error_h_less_than_zero) );
  234. if (par.phi0 != 0.0)
  235. BOOST_THROW_EXCEPTION( projection_exception(error_unknown_prime_meridian) );
  236. proj_parm.flip_axis = geos_flip_axis(params);
  237. proj_parm.radius_g_1 = proj_parm.h / par.a;
  238. proj_parm.radius_g = 1. + proj_parm.radius_g_1;
  239. proj_parm.C = proj_parm.radius_g * proj_parm.radius_g - 1.0;
  240. if (par.es != 0.0) {
  241. proj_parm.radius_p = sqrt (par.one_es);
  242. proj_parm.radius_p2 = par.one_es;
  243. proj_parm.radius_p_inv2 = par.rone_es;
  244. } else {
  245. proj_parm.radius_p = proj_parm.radius_p2 = proj_parm.radius_p_inv2 = 1.0;
  246. }
  247. }
  248. }} // namespace detail::geos
  249. #endif // doxygen
  250. /*!
  251. \brief Geostationary Satellite View projection
  252. \ingroup projections
  253. \tparam Geographic latlong point type
  254. \tparam Cartesian xy point type
  255. \tparam Parameters parameter type
  256. \par Projection characteristics
  257. - Azimuthal
  258. - Spheroid
  259. - Ellipsoid
  260. \par Projection parameters
  261. - h: Height (real)
  262. - sweep: Sweep axis ('x' or 'y') (string)
  263. \par Example
  264. \image html ex_geos.gif
  265. */
  266. template <typename T, typename Parameters>
  267. struct geos_ellipsoid : public detail::geos::base_geos_ellipsoid<T, Parameters>
  268. {
  269. template <typename Params>
  270. inline geos_ellipsoid(Params const& params, Parameters const& par)
  271. : detail::geos::base_geos_ellipsoid<T, Parameters>(par)
  272. {
  273. detail::geos::setup_geos(params, this->m_par, this->m_proj_parm);
  274. }
  275. };
  276. /*!
  277. \brief Geostationary Satellite View projection
  278. \ingroup projections
  279. \tparam Geographic latlong point type
  280. \tparam Cartesian xy point type
  281. \tparam Parameters parameter type
  282. \par Projection characteristics
  283. - Azimuthal
  284. - Spheroid
  285. - Ellipsoid
  286. \par Projection parameters
  287. - h: Height (real)
  288. - sweep: Sweep axis ('x' or 'y') (string)
  289. \par Example
  290. \image html ex_geos.gif
  291. */
  292. template <typename T, typename Parameters>
  293. struct geos_spheroid : public detail::geos::base_geos_spheroid<T, Parameters>
  294. {
  295. template <typename Params>
  296. inline geos_spheroid(Params const& params, Parameters const& par)
  297. : detail::geos::base_geos_spheroid<T, Parameters>(par)
  298. {
  299. detail::geos::setup_geos(params, this->m_par, this->m_proj_parm);
  300. }
  301. };
  302. #ifndef DOXYGEN_NO_DETAIL
  303. namespace detail
  304. {
  305. // Static projection
  306. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::spar::proj_geos, geos_spheroid, geos_ellipsoid)
  307. // Factory entry(s)
  308. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(geos_entry, geos_spheroid, geos_ellipsoid)
  309. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(geos_init)
  310. {
  311. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(geos, geos_entry);
  312. }
  313. } // namespace detail
  314. #endif // doxygen
  315. } // namespace projections
  316. }} // namespace boost::geometry
  317. #endif // BOOST_GEOMETRY_PROJECTIONS_GEOS_HPP