nsper.hpp 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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_NSPER_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_NSPER_HPP
  32. #include <boost/config.hpp>
  33. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  34. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  35. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  36. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  37. #include <boost/geometry/srs/projections/impl/projects.hpp>
  38. #include <boost/geometry/util/math.hpp>
  39. #include <boost/math/special_functions/hypot.hpp>
  40. namespace boost { namespace geometry
  41. {
  42. namespace projections
  43. {
  44. #ifndef DOXYGEN_NO_DETAIL
  45. namespace detail { namespace nsper
  46. {
  47. static const double epsilon10 = 1.e-10;
  48. enum mode_type {
  49. n_pole = 0,
  50. s_pole = 1,
  51. equit = 2,
  52. obliq = 3
  53. };
  54. template <typename T>
  55. struct par_nsper
  56. {
  57. T height;
  58. T sinph0;
  59. T cosph0;
  60. T p;
  61. T rp;
  62. T pn1;
  63. T pfact;
  64. T h;
  65. T cg;
  66. T sg;
  67. T sw;
  68. T cw;
  69. mode_type mode;
  70. bool tilt;
  71. };
  72. template <typename T, typename Parameters>
  73. struct base_nsper_spheroid
  74. {
  75. par_nsper<T> m_proj_parm;
  76. // FORWARD(s_forward) spheroid
  77. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  78. inline void fwd(Parameters const& , T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  79. {
  80. T coslam, cosphi, sinphi;
  81. sinphi = sin(lp_lat);
  82. cosphi = cos(lp_lat);
  83. coslam = cos(lp_lon);
  84. switch (this->m_proj_parm.mode) {
  85. case obliq:
  86. xy_y = this->m_proj_parm.sinph0 * sinphi + this->m_proj_parm.cosph0 * cosphi * coslam;
  87. break;
  88. case equit:
  89. xy_y = cosphi * coslam;
  90. break;
  91. case s_pole:
  92. xy_y = - sinphi;
  93. break;
  94. case n_pole:
  95. xy_y = sinphi;
  96. break;
  97. }
  98. if (xy_y < this->m_proj_parm.rp) {
  99. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  100. }
  101. xy_y = this->m_proj_parm.pn1 / (this->m_proj_parm.p - xy_y);
  102. xy_x = xy_y * cosphi * sin(lp_lon);
  103. switch (this->m_proj_parm.mode) {
  104. case obliq:
  105. xy_y *= (this->m_proj_parm.cosph0 * sinphi -
  106. this->m_proj_parm.sinph0 * cosphi * coslam);
  107. break;
  108. case equit:
  109. xy_y *= sinphi;
  110. break;
  111. case n_pole:
  112. coslam = - coslam;
  113. BOOST_FALLTHROUGH;
  114. case s_pole:
  115. xy_y *= cosphi * coslam;
  116. break;
  117. }
  118. if (this->m_proj_parm.tilt) {
  119. T yt, ba;
  120. yt = xy_y * this->m_proj_parm.cg + xy_x * this->m_proj_parm.sg;
  121. ba = 1. / (yt * this->m_proj_parm.sw * this->m_proj_parm.h + this->m_proj_parm.cw);
  122. xy_x = (xy_x * this->m_proj_parm.cg - xy_y * this->m_proj_parm.sg) * this->m_proj_parm.cw * ba;
  123. xy_y = yt * ba;
  124. }
  125. }
  126. // INVERSE(s_inverse) spheroid
  127. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  128. inline void inv(Parameters const& par, T xy_x, T xy_y, T& lp_lon, T& lp_lat) const
  129. {
  130. T rh, cosz, sinz;
  131. if (this->m_proj_parm.tilt) {
  132. T bm, bq, yt;
  133. yt = 1./(this->m_proj_parm.pn1 - xy_y * this->m_proj_parm.sw);
  134. bm = this->m_proj_parm.pn1 * xy_x * yt;
  135. bq = this->m_proj_parm.pn1 * xy_y * this->m_proj_parm.cw * yt;
  136. xy_x = bm * this->m_proj_parm.cg + bq * this->m_proj_parm.sg;
  137. xy_y = bq * this->m_proj_parm.cg - bm * this->m_proj_parm.sg;
  138. }
  139. rh = boost::math::hypot(xy_x, xy_y);
  140. if ((sinz = 1. - rh * rh * this->m_proj_parm.pfact) < 0.) {
  141. BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
  142. }
  143. sinz = (this->m_proj_parm.p - sqrt(sinz)) / (this->m_proj_parm.pn1 / rh + rh / this->m_proj_parm.pn1);
  144. cosz = sqrt(1. - sinz * sinz);
  145. if (fabs(rh) <= epsilon10) {
  146. lp_lon = 0.;
  147. lp_lat = par.phi0;
  148. } else {
  149. switch (this->m_proj_parm.mode) {
  150. case obliq:
  151. lp_lat = asin(cosz * this->m_proj_parm.sinph0 + xy_y * sinz * this->m_proj_parm.cosph0 / rh);
  152. xy_y = (cosz - this->m_proj_parm.sinph0 * sin(lp_lat)) * rh;
  153. xy_x *= sinz * this->m_proj_parm.cosph0;
  154. break;
  155. case equit:
  156. lp_lat = asin(xy_y * sinz / rh);
  157. xy_y = cosz * rh;
  158. xy_x *= sinz;
  159. break;
  160. case n_pole:
  161. lp_lat = asin(cosz);
  162. xy_y = -xy_y;
  163. break;
  164. case s_pole:
  165. lp_lat = - asin(cosz);
  166. break;
  167. }
  168. lp_lon = atan2(xy_x, xy_y);
  169. }
  170. }
  171. static inline std::string get_name()
  172. {
  173. return "nsper_spheroid";
  174. }
  175. };
  176. template <typename Params, typename Parameters, typename T>
  177. inline void setup(Params const& params, Parameters& par, par_nsper<T>& proj_parm)
  178. {
  179. proj_parm.height = pj_get_param_f<T, srs::spar::h>(params, "h", srs::dpar::h);
  180. if (proj_parm.height <= 0.)
  181. BOOST_THROW_EXCEPTION( projection_exception(error_h_less_than_zero) );
  182. if (fabs(fabs(par.phi0) - geometry::math::half_pi<T>()) < epsilon10)
  183. proj_parm.mode = par.phi0 < 0. ? s_pole : n_pole;
  184. else if (fabs(par.phi0) < epsilon10)
  185. proj_parm.mode = equit;
  186. else {
  187. proj_parm.mode = obliq;
  188. proj_parm.sinph0 = sin(par.phi0);
  189. proj_parm.cosph0 = cos(par.phi0);
  190. }
  191. proj_parm.pn1 = proj_parm.height / par.a; /* normalize by radius */
  192. proj_parm.p = 1. + proj_parm.pn1;
  193. proj_parm.rp = 1. / proj_parm.p;
  194. proj_parm.h = 1. / proj_parm.pn1;
  195. proj_parm.pfact = (proj_parm.p + 1.) * proj_parm.h;
  196. par.es = 0.;
  197. }
  198. // Near-sided perspective
  199. template <typename Params, typename Parameters, typename T>
  200. inline void setup_nsper(Params const& params, Parameters& par, par_nsper<T>& proj_parm)
  201. {
  202. proj_parm.tilt = false;
  203. setup(params, par, proj_parm);
  204. }
  205. // Tilted perspective
  206. template <typename Params, typename Parameters, typename T>
  207. inline void setup_tpers(Params const& params, Parameters& par, par_nsper<T>& proj_parm)
  208. {
  209. T const omega = pj_get_param_r<T, srs::spar::tilt>(params, "tilt", srs::dpar::tilt);
  210. T const gamma = pj_get_param_r<T, srs::spar::azi>(params, "azi", srs::dpar::azi);
  211. proj_parm.tilt = true;
  212. proj_parm.cg = cos(gamma); proj_parm.sg = sin(gamma);
  213. proj_parm.cw = cos(omega); proj_parm.sw = sin(omega);
  214. setup(params, par, proj_parm);
  215. }
  216. }} // namespace detail::nsper
  217. #endif // doxygen
  218. /*!
  219. \brief Near-sided perspective projection
  220. \ingroup projections
  221. \tparam Geographic latlong point type
  222. \tparam Cartesian xy point type
  223. \tparam Parameters parameter type
  224. \par Projection characteristics
  225. - Azimuthal
  226. - Spheroid
  227. \par Projection parameters
  228. - h: Height
  229. \par Example
  230. \image html ex_nsper.gif
  231. */
  232. template <typename T, typename Parameters>
  233. struct nsper_spheroid : public detail::nsper::base_nsper_spheroid<T, Parameters>
  234. {
  235. template <typename Params>
  236. inline nsper_spheroid(Params const& params, Parameters & par)
  237. {
  238. detail::nsper::setup_nsper(params, par, this->m_proj_parm);
  239. }
  240. };
  241. /*!
  242. \brief Tilted perspective projection
  243. \ingroup projections
  244. \tparam Geographic latlong point type
  245. \tparam Cartesian xy point type
  246. \tparam Parameters parameter type
  247. \par Projection characteristics
  248. - Azimuthal
  249. - Spheroid
  250. \par Projection parameters
  251. - tilt: Tilt, or Omega (real)
  252. - azi: Azimuth (or Gamma) (real)
  253. - h: Height
  254. \par Example
  255. \image html ex_tpers.gif
  256. */
  257. template <typename T, typename Parameters>
  258. struct tpers_spheroid : public detail::nsper::base_nsper_spheroid<T, Parameters>
  259. {
  260. template <typename Params>
  261. inline tpers_spheroid(Params const& params, Parameters & par)
  262. {
  263. detail::nsper::setup_tpers(params, par, this->m_proj_parm);
  264. }
  265. };
  266. #ifndef DOXYGEN_NO_DETAIL
  267. namespace detail
  268. {
  269. // Static projection
  270. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_F(srs::spar::proj_nsper, nsper_spheroid)
  271. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_F(srs::spar::proj_tpers, tpers_spheroid)
  272. // Factory entry(s)
  273. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(nsper_entry, nsper_spheroid)
  274. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(tpers_entry, tpers_spheroid)
  275. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(nsper_init)
  276. {
  277. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(nsper, nsper_entry)
  278. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(tpers, tpers_entry)
  279. }
  280. } // namespace detail
  281. #endif // doxygen
  282. } // namespace projections
  283. }} // namespace boost::geometry
  284. #endif // BOOST_GEOMETRY_PROJECTIONS_NSPER_HPP