nsper.hpp 14 KB

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