geos.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370
  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. // 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 <typename T, typename Parameters>
  62. struct base_geos_ellipsoid
  63. {
  64. par_geos<T> m_proj_parm;
  65. // FORWARD(e_forward) ellipsoid
  66. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  67. inline void fwd(Parameters const& , T const& lp_lon, T lp_lat, T& xy_x, T& xy_y) const
  68. {
  69. T r, Vx, Vy, Vz, tmp;
  70. /* Calculation of geocentric latitude. */
  71. lp_lat = atan (this->m_proj_parm.radius_p2 * tan (lp_lat));
  72. /* Calculation of the three components of the vector from satellite to
  73. ** position on earth surface (lon,lat).*/
  74. r = (this->m_proj_parm.radius_p) / boost::math::hypot(this->m_proj_parm.radius_p * cos (lp_lat), sin (lp_lat));
  75. Vx = r * cos (lp_lon) * cos (lp_lat);
  76. Vy = r * sin (lp_lon) * cos (lp_lat);
  77. Vz = r * sin (lp_lat);
  78. /* Check visibility. */
  79. if (((this->m_proj_parm.radius_g - Vx) * Vx - Vy * Vy - Vz * Vz * this->m_proj_parm.radius_p_inv2) < 0.) {
  80. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  81. }
  82. /* Calculation based on view angles from satellite. */
  83. tmp = this->m_proj_parm.radius_g - Vx;
  84. if(this->m_proj_parm.flip_axis) {
  85. xy_x = this->m_proj_parm.radius_g_1 * atan (Vy / boost::math::hypot (Vz, tmp));
  86. xy_y = this->m_proj_parm.radius_g_1 * atan (Vz / tmp);
  87. } else {
  88. xy_x = this->m_proj_parm.radius_g_1 * atan (Vy / tmp);
  89. xy_y = this->m_proj_parm.radius_g_1 * atan (Vz / boost::math::hypot (Vy, tmp));
  90. }
  91. }
  92. // INVERSE(e_inverse) ellipsoid
  93. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  94. inline void inv(Parameters const& , T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  95. {
  96. T Vx, Vy, Vz, a, b, det, k;
  97. /* Setting three components of vector from satellite to position.*/
  98. Vx = -1.0;
  99. if(this->m_proj_parm.flip_axis) {
  100. Vz = tan (xy_y / this->m_proj_parm.radius_g_1);
  101. Vy = tan (xy_x / this->m_proj_parm.radius_g_1) * boost::math::hypot(1.0, Vz);
  102. } else {
  103. Vy = tan (xy_x / this->m_proj_parm.radius_g_1);
  104. Vz = tan (xy_y / this->m_proj_parm.radius_g_1) * boost::math::hypot(1.0, Vy);
  105. }
  106. /* Calculation of terms in cubic equation and determinant.*/
  107. a = Vz / this->m_proj_parm.radius_p;
  108. a = Vy * Vy + a * a + Vx * Vx;
  109. b = 2 * this->m_proj_parm.radius_g * Vx;
  110. if ((det = (b * b) - 4 * a * this->m_proj_parm.C) < 0.) {
  111. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  112. }
  113. /* Calculation of three components of vector from satellite to position.*/
  114. k = (-b - sqrt(det)) / (2. * a);
  115. Vx = this->m_proj_parm.radius_g + k * Vx;
  116. Vy *= k;
  117. Vz *= k;
  118. /* Calculation of longitude and latitude.*/
  119. lp_lon = atan2 (Vy, Vx);
  120. lp_lat = atan (Vz * cos (lp_lon) / Vx);
  121. lp_lat = atan (this->m_proj_parm.radius_p_inv2 * tan (lp_lat));
  122. }
  123. static inline std::string get_name()
  124. {
  125. return "geos_ellipsoid";
  126. }
  127. };
  128. template <typename T, typename Parameters>
  129. struct base_geos_spheroid
  130. {
  131. par_geos<T> m_proj_parm;
  132. // FORWARD(s_forward) spheroid
  133. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  134. inline void fwd(Parameters const& , T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  135. {
  136. T Vx, Vy, Vz, tmp;
  137. /* Calculation of the three components of the vector from satellite to
  138. ** position on earth surface (lon,lat).*/
  139. tmp = cos(lp_lat);
  140. Vx = cos (lp_lon) * tmp;
  141. Vy = sin (lp_lon) * tmp;
  142. Vz = sin (lp_lat);
  143. /* Check visibility.*/
  144. // TODO: in proj4 5.0.0 this check is not present
  145. if (((this->m_proj_parm.radius_g - Vx) * Vx - Vy * Vy - Vz * Vz) < 0.)
  146. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  147. /* Calculation based on view angles from satellite.*/
  148. tmp = this->m_proj_parm.radius_g - Vx;
  149. if(this->m_proj_parm.flip_axis) {
  150. xy_x = this->m_proj_parm.radius_g_1 * atan(Vy / boost::math::hypot(Vz, tmp));
  151. xy_y = this->m_proj_parm.radius_g_1 * atan(Vz / tmp);
  152. } else {
  153. xy_x = this->m_proj_parm.radius_g_1 * atan(Vy / tmp);
  154. xy_y = this->m_proj_parm.radius_g_1 * atan(Vz / boost::math::hypot(Vy, tmp));
  155. }
  156. }
  157. // INVERSE(s_inverse) spheroid
  158. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  159. inline void inv(Parameters const& , T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  160. {
  161. T Vx, Vy, Vz, a, b, det, k;
  162. /* Setting three components of vector from satellite to position.*/
  163. Vx = -1.0;
  164. if(this->m_proj_parm.flip_axis) {
  165. Vz = tan (xy_y / (this->m_proj_parm.radius_g - 1.0));
  166. Vy = tan (xy_x / (this->m_proj_parm.radius_g - 1.0)) * sqrt (1.0 + Vz * Vz);
  167. } else {
  168. Vy = tan (xy_x / (this->m_proj_parm.radius_g - 1.0));
  169. Vz = tan (xy_y / (this->m_proj_parm.radius_g - 1.0)) * sqrt (1.0 + Vy * Vy);
  170. }
  171. /* Calculation of terms in cubic equation and determinant.*/
  172. a = Vy * Vy + Vz * Vz + Vx * Vx;
  173. b = 2 * this->m_proj_parm.radius_g * Vx;
  174. if ((det = (b * b) - 4 * a * this->m_proj_parm.C) < 0.) {
  175. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  176. }
  177. /* Calculation of three components of vector from satellite to position.*/
  178. k = (-b - sqrt(det)) / (2 * a);
  179. Vx = this->m_proj_parm.radius_g + k * Vx;
  180. Vy *= k;
  181. Vz *= k;
  182. /* Calculation of longitude and latitude.*/
  183. lp_lon = atan2 (Vy, Vx);
  184. lp_lat = atan (Vz * cos (lp_lon) / Vx);
  185. }
  186. static inline std::string get_name()
  187. {
  188. return "geos_spheroid";
  189. }
  190. };
  191. inline bool geos_flip_axis(srs::detail::proj4_parameters const& params)
  192. {
  193. std::string sweep_axis = pj_get_param_s(params, "sweep");
  194. if (sweep_axis.empty())
  195. return false;
  196. else {
  197. if (sweep_axis[1] != '\0' || (sweep_axis[0] != 'x' && sweep_axis[0] != 'y'))
  198. BOOST_THROW_EXCEPTION( projection_exception(error_invalid_sweep_axis) );
  199. if (sweep_axis[0] == 'x')
  200. return true;
  201. else
  202. return false;
  203. }
  204. }
  205. template <typename T>
  206. inline bool geos_flip_axis(srs::dpar::parameters<T> const& params)
  207. {
  208. typename srs::dpar::parameters<T>::const_iterator
  209. it = pj_param_find(params, srs::dpar::sweep);
  210. if (it == params.end()) {
  211. return false;
  212. } else {
  213. srs::dpar::value_sweep s = static_cast<srs::dpar::value_sweep>(it->template get_value<int>());
  214. return s == srs::dpar::sweep_x;
  215. }
  216. }
  217. // Geostationary Satellite View
  218. template <typename Params, typename Parameters, typename T>
  219. inline void setup_geos(Params const& params, Parameters& par, par_geos<T>& proj_parm)
  220. {
  221. std::string sweep_axis;
  222. if ((proj_parm.h = pj_get_param_f<T, srs::spar::h>(params, "h", srs::dpar::h)) <= 0.)
  223. BOOST_THROW_EXCEPTION( projection_exception(error_h_less_than_zero) );
  224. if (par.phi0 != 0.0)
  225. BOOST_THROW_EXCEPTION( projection_exception(error_unknown_prime_meridian) );
  226. proj_parm.flip_axis = geos_flip_axis(params);
  227. proj_parm.radius_g_1 = proj_parm.h / par.a;
  228. proj_parm.radius_g = 1. + proj_parm.radius_g_1;
  229. proj_parm.C = proj_parm.radius_g * proj_parm.radius_g - 1.0;
  230. if (par.es != 0.0) {
  231. proj_parm.radius_p = sqrt (par.one_es);
  232. proj_parm.radius_p2 = par.one_es;
  233. proj_parm.radius_p_inv2 = par.rone_es;
  234. } else {
  235. proj_parm.radius_p = proj_parm.radius_p2 = proj_parm.radius_p_inv2 = 1.0;
  236. }
  237. }
  238. }} // namespace detail::geos
  239. #endif // doxygen
  240. /*!
  241. \brief Geostationary Satellite View projection
  242. \ingroup projections
  243. \tparam Geographic latlong point type
  244. \tparam Cartesian xy point type
  245. \tparam Parameters parameter type
  246. \par Projection characteristics
  247. - Azimuthal
  248. - Spheroid
  249. - Ellipsoid
  250. \par Projection parameters
  251. - h: Height (real)
  252. - sweep: Sweep axis ('x' or 'y') (string)
  253. \par Example
  254. \image html ex_geos.gif
  255. */
  256. template <typename T, typename Parameters>
  257. struct geos_ellipsoid : public detail::geos::base_geos_ellipsoid<T, Parameters>
  258. {
  259. template <typename Params>
  260. inline geos_ellipsoid(Params const& params, Parameters const& par)
  261. {
  262. detail::geos::setup_geos(params, par, this->m_proj_parm);
  263. }
  264. };
  265. /*!
  266. \brief Geostationary Satellite View projection
  267. \ingroup projections
  268. \tparam Geographic latlong point type
  269. \tparam Cartesian xy point type
  270. \tparam Parameters parameter type
  271. \par Projection characteristics
  272. - Azimuthal
  273. - Spheroid
  274. - Ellipsoid
  275. \par Projection parameters
  276. - h: Height (real)
  277. - sweep: Sweep axis ('x' or 'y') (string)
  278. \par Example
  279. \image html ex_geos.gif
  280. */
  281. template <typename T, typename Parameters>
  282. struct geos_spheroid : public detail::geos::base_geos_spheroid<T, Parameters>
  283. {
  284. template <typename Params>
  285. inline geos_spheroid(Params const& params, Parameters const& par)
  286. {
  287. detail::geos::setup_geos(params, par, this->m_proj_parm);
  288. }
  289. };
  290. #ifndef DOXYGEN_NO_DETAIL
  291. namespace detail
  292. {
  293. // Static projection
  294. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_geos, geos_spheroid, geos_ellipsoid)
  295. // Factory entry(s)
  296. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(geos_entry, geos_spheroid, geos_ellipsoid)
  297. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(geos_init)
  298. {
  299. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(geos, geos_entry);
  300. }
  301. } // namespace detail
  302. #endif // doxygen
  303. } // namespace projections
  304. }} // namespace boost::geometry
  305. #endif // BOOST_GEOMETRY_PROJECTIONS_GEOS_HPP