lsat.hpp 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353
  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_LSAT_HPP
  31. #define BOOST_GEOMETRY_PROJECTIONS_LSAT_HPP
  32. #include <boost/geometry/util/math.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/projects.hpp>
  36. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  37. #include <boost/geometry/srs/projections/impl/aasincos.hpp>
  38. namespace boost { namespace geometry
  39. {
  40. namespace srs { namespace par4
  41. {
  42. struct lsat {}; // Space oblique for LANDSAT
  43. }} //namespace srs::par4
  44. namespace projections
  45. {
  46. #ifndef DOXYGEN_NO_DETAIL
  47. namespace detail { namespace lsat
  48. {
  49. static const double tolerance = 1e-7;
  50. template <typename T>
  51. struct par_lsat
  52. {
  53. T a2, a4, b, c1, c3;
  54. T q, t, u, w, p22, sa, ca, xj, rlm, rlm2;
  55. };
  56. /* based upon Snyder and Linck, USGS-NMD */
  57. template <typename T>
  58. inline void
  59. seraz0(T lam, T const& mult, par_lsat<T>& proj_parm)
  60. {
  61. T sdsq, h, s, fc, sd, sq, d__1 = 0;
  62. lam *= geometry::math::d2r<T>();
  63. sd = sin(lam);
  64. sdsq = sd * sd;
  65. s = proj_parm.p22 * proj_parm.sa * cos(lam) * sqrt((1. + proj_parm.t * sdsq)
  66. / ((1. + proj_parm.w * sdsq) * (1. + proj_parm.q * sdsq)));
  67. d__1 = 1. + proj_parm.q * sdsq;
  68. h = sqrt((1. + proj_parm.q * sdsq) / (1. + proj_parm.w * sdsq)) * ((1. + proj_parm.w * sdsq)
  69. / (d__1 * d__1) - proj_parm.p22 * proj_parm.ca);
  70. sq = sqrt(proj_parm.xj * proj_parm.xj + s * s);
  71. fc = mult * (h * proj_parm.xj - s * s) / sq;
  72. proj_parm.b += fc;
  73. proj_parm.a2 += fc * cos(lam + lam);
  74. proj_parm.a4 += fc * cos(lam * 4.);
  75. fc = mult * s * (h + proj_parm.xj) / sq;
  76. proj_parm.c1 += fc * cos(lam);
  77. proj_parm.c3 += fc * cos(lam * 3.);
  78. }
  79. // template class, using CRTP to implement forward/inverse
  80. template <typename T, typename Parameters>
  81. struct base_lsat_ellipsoid
  82. : public base_t_fi<base_lsat_ellipsoid<T, Parameters>, T, Parameters>
  83. {
  84. par_lsat<T> m_proj_parm;
  85. inline base_lsat_ellipsoid(const Parameters& par)
  86. : base_t_fi<base_lsat_ellipsoid<T, Parameters>, T, Parameters>(*this, par)
  87. {}
  88. // FORWARD(e_forward) ellipsoid
  89. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  90. inline void fwd(T& lp_lon, T& lp_lat, T& xy_x, T& xy_y) const
  91. {
  92. static const T fourth_pi = detail::fourth_pi<T>();
  93. static const T half_pi = detail::half_pi<T>();
  94. static const T one_and_half_pi = detail::one_and_half_pi<T>();
  95. static const T two_and_half_pi = detail::two_and_half_pi<T>();
  96. int l, nn;
  97. T lamt = 0.0, xlam, sdsq, c, d, s, lamdp = 0.0, phidp, lampp, tanph;
  98. T lamtp, cl, sd, sp, sav, tanphi;
  99. if (lp_lat > half_pi)
  100. lp_lat = half_pi;
  101. else if (lp_lat < -half_pi)
  102. lp_lat = -half_pi;
  103. if (lp_lat >= 0. )
  104. lampp = half_pi;
  105. else
  106. lampp = one_and_half_pi;
  107. tanphi = tan(lp_lat);
  108. for (nn = 0;;) {
  109. T fac;
  110. sav = lampp;
  111. lamtp = lp_lon + this->m_proj_parm.p22 * lampp;
  112. cl = cos(lamtp);
  113. if (fabs(cl) < tolerance)
  114. lamtp -= tolerance;
  115. if( cl < 0 )
  116. fac = lampp + sin(lampp) * half_pi;
  117. else
  118. fac = lampp - sin(lampp) * half_pi;
  119. for (l = 50; l; --l) {
  120. lamt = lp_lon + this->m_proj_parm.p22 * sav;
  121. c = cos(lamt);
  122. if (fabs(c) < tolerance)
  123. lamt -= tolerance;
  124. xlam = (this->m_par.one_es * tanphi * this->m_proj_parm.sa + sin(lamt) * this->m_proj_parm.ca) / c;
  125. lamdp = atan(xlam) + fac;
  126. if (fabs(fabs(sav) - fabs(lamdp)) < tolerance)
  127. break;
  128. sav = lamdp;
  129. }
  130. if (!l || ++nn >= 3 || (lamdp > this->m_proj_parm.rlm && lamdp < this->m_proj_parm.rlm2))
  131. break;
  132. if (lamdp <= this->m_proj_parm.rlm)
  133. lampp = two_and_half_pi;
  134. else if (lamdp >= this->m_proj_parm.rlm2)
  135. lampp = half_pi;
  136. }
  137. if (l) {
  138. sp = sin(lp_lat);
  139. phidp = aasin((this->m_par.one_es * this->m_proj_parm.ca * sp - this->m_proj_parm.sa * cos(lp_lat) *
  140. sin(lamt)) / sqrt(1. - this->m_par.es * sp * sp));
  141. tanph = log(tan(fourth_pi + .5 * phidp));
  142. sd = sin(lamdp);
  143. sdsq = sd * sd;
  144. s = this->m_proj_parm.p22 * this->m_proj_parm.sa * cos(lamdp) * sqrt((1. + this->m_proj_parm.t * sdsq)
  145. / ((1. + this->m_proj_parm.w * sdsq) * (1. + this->m_proj_parm.q * sdsq)));
  146. d = sqrt(this->m_proj_parm.xj * this->m_proj_parm.xj + s * s);
  147. xy_x = this->m_proj_parm.b * lamdp + this->m_proj_parm.a2 * sin(2. * lamdp) + this->m_proj_parm.a4 *
  148. sin(lamdp * 4.) - tanph * s / d;
  149. xy_y = this->m_proj_parm.c1 * sd + this->m_proj_parm.c3 * sin(lamdp * 3.) + tanph * this->m_proj_parm.xj / d;
  150. } else
  151. xy_x = xy_y = HUGE_VAL;
  152. }
  153. // INVERSE(e_inverse) ellipsoid
  154. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  155. inline void inv(T& xy_x, T& xy_y, T& lp_lon, T& lp_lat) const
  156. {
  157. static const T fourth_pi = detail::fourth_pi<T>();
  158. static const T half_pi = detail::half_pi<T>();
  159. int nn;
  160. T lamt, sdsq, s, lamdp, phidp, sppsq, dd, sd, sl, fac, scl, sav, spp;
  161. lamdp = xy_x / this->m_proj_parm.b;
  162. nn = 50;
  163. do {
  164. sav = lamdp;
  165. sd = sin(lamdp);
  166. sdsq = sd * sd;
  167. s = this->m_proj_parm.p22 * this->m_proj_parm.sa * cos(lamdp) * sqrt((1. + this->m_proj_parm.t * sdsq)
  168. / ((1. + this->m_proj_parm.w * sdsq) * (1. + this->m_proj_parm.q * sdsq)));
  169. lamdp = xy_x + xy_y * s / this->m_proj_parm.xj - this->m_proj_parm.a2 * sin(
  170. 2. * lamdp) - this->m_proj_parm.a4 * sin(lamdp * 4.) - s / this->m_proj_parm.xj * (
  171. this->m_proj_parm.c1 * sin(lamdp) + this->m_proj_parm.c3 * sin(lamdp * 3.));
  172. lamdp /= this->m_proj_parm.b;
  173. } while (fabs(lamdp - sav) >= tolerance && --nn);
  174. sl = sin(lamdp);
  175. fac = exp(sqrt(1. + s * s / this->m_proj_parm.xj / this->m_proj_parm.xj) * (xy_y -
  176. this->m_proj_parm.c1 * sl - this->m_proj_parm.c3 * sin(lamdp * 3.)));
  177. phidp = 2. * (atan(fac) - fourth_pi);
  178. dd = sl * sl;
  179. if (fabs(cos(lamdp)) < tolerance)
  180. lamdp -= tolerance;
  181. spp = sin(phidp);
  182. sppsq = spp * spp;
  183. lamt = atan(((1. - sppsq * this->m_par.rone_es) * tan(lamdp) *
  184. this->m_proj_parm.ca - spp * this->m_proj_parm.sa * sqrt((1. + this->m_proj_parm.q * dd) * (
  185. 1. - sppsq) - sppsq * this->m_proj_parm.u) / cos(lamdp)) / (1. - sppsq
  186. * (1. + this->m_proj_parm.u)));
  187. sl = lamt >= 0. ? 1. : -1.;
  188. scl = cos(lamdp) >= 0. ? 1. : -1;
  189. lamt -= half_pi * (1. - scl) * sl;
  190. lp_lon = lamt - this->m_proj_parm.p22 * lamdp;
  191. if (fabs(this->m_proj_parm.sa) < tolerance)
  192. lp_lat = aasin(spp / sqrt(this->m_par.one_es * this->m_par.one_es + this->m_par.es * sppsq));
  193. else
  194. lp_lat = atan((tan(lamdp) * cos(lamt) - this->m_proj_parm.ca * sin(lamt)) /
  195. (this->m_par.one_es * this->m_proj_parm.sa));
  196. }
  197. static inline std::string get_name()
  198. {
  199. return "lsat_ellipsoid";
  200. }
  201. };
  202. // Space oblique for LANDSAT
  203. template <typename Parameters, typename T>
  204. inline void setup_lsat(Parameters& par, par_lsat<T>& proj_parm)
  205. {
  206. static T const d2r = geometry::math::d2r<T>();
  207. static T const pi = detail::pi<T>();
  208. static T const two_pi = detail::two_pi<T>();
  209. int land, path;
  210. T lam, alf, esc, ess;
  211. land = pj_get_param_i(par.params, "lsat");
  212. if (land <= 0 || land > 5)
  213. BOOST_THROW_EXCEPTION( projection_exception(error_lsat_not_in_range) );
  214. path = pj_get_param_i(par.params, "path");
  215. if (path <= 0 || path > (land <= 3 ? 251 : 233))
  216. BOOST_THROW_EXCEPTION( projection_exception(error_path_not_in_range) );
  217. if (land <= 3) {
  218. par.lam0 = d2r * 128.87 - two_pi / 251. * path;
  219. proj_parm.p22 = 103.2669323;
  220. alf = d2r * 99.092;
  221. } else {
  222. par.lam0 = d2r * 129.3 - two_pi / 233. * path;
  223. proj_parm.p22 = 98.8841202;
  224. alf = d2r * 98.2;
  225. }
  226. proj_parm.p22 /= 1440.;
  227. proj_parm.sa = sin(alf);
  228. proj_parm.ca = cos(alf);
  229. if (fabs(proj_parm.ca) < 1e-9)
  230. proj_parm.ca = 1e-9;
  231. esc = par.es * proj_parm.ca * proj_parm.ca;
  232. ess = par.es * proj_parm.sa * proj_parm.sa;
  233. proj_parm.w = (1. - esc) * par.rone_es;
  234. proj_parm.w = proj_parm.w * proj_parm.w - 1.;
  235. proj_parm.q = ess * par.rone_es;
  236. proj_parm.t = ess * (2. - par.es) * par.rone_es * par.rone_es;
  237. proj_parm.u = esc * par.rone_es;
  238. proj_parm.xj = par.one_es * par.one_es * par.one_es;
  239. proj_parm.rlm = pi * (1. / 248. + .5161290322580645);
  240. proj_parm.rlm2 = proj_parm.rlm + two_pi;
  241. proj_parm.a2 = proj_parm.a4 = proj_parm.b = proj_parm.c1 = proj_parm.c3 = 0.;
  242. seraz0(0., 1., proj_parm);
  243. for (lam = 9.; lam <= 81.0001; lam += 18.)
  244. seraz0(lam, 4., proj_parm);
  245. for (lam = 18; lam <= 72.0001; lam += 18.)
  246. seraz0(lam, 2., proj_parm);
  247. seraz0(90., 1., proj_parm);
  248. proj_parm.a2 /= 30.;
  249. proj_parm.a4 /= 60.;
  250. proj_parm.b /= 30.;
  251. proj_parm.c1 /= 15.;
  252. proj_parm.c3 /= 45.;
  253. }
  254. }} // namespace detail::lsat
  255. #endif // doxygen
  256. /*!
  257. \brief Space oblique for LANDSAT projection
  258. \ingroup projections
  259. \tparam Geographic latlong point type
  260. \tparam Cartesian xy point type
  261. \tparam Parameters parameter type
  262. \par Projection characteristics
  263. - Cylindrical
  264. - Spheroid
  265. - Ellipsoid
  266. \par Projection parameters
  267. - lsat (integer)
  268. - path (integer)
  269. \par Example
  270. \image html ex_lsat.gif
  271. */
  272. template <typename T, typename Parameters>
  273. struct lsat_ellipsoid : public detail::lsat::base_lsat_ellipsoid<T, Parameters>
  274. {
  275. inline lsat_ellipsoid(const Parameters& par) : detail::lsat::base_lsat_ellipsoid<T, Parameters>(par)
  276. {
  277. detail::lsat::setup_lsat(this->m_par, this->m_proj_parm);
  278. }
  279. };
  280. #ifndef DOXYGEN_NO_DETAIL
  281. namespace detail
  282. {
  283. // Static projection
  284. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION(srs::par4::lsat, lsat_ellipsoid, lsat_ellipsoid)
  285. // Factory entry(s)
  286. template <typename T, typename Parameters>
  287. class lsat_entry : public detail::factory_entry<T, Parameters>
  288. {
  289. public :
  290. virtual base_v<T, Parameters>* create_new(const Parameters& par) const
  291. {
  292. return new base_v_fi<lsat_ellipsoid<T, Parameters>, T, Parameters>(par);
  293. }
  294. };
  295. template <typename T, typename Parameters>
  296. inline void lsat_init(detail::base_factory<T, Parameters>& factory)
  297. {
  298. factory.add_to_factory("lsat", new lsat_entry<T, Parameters>);
  299. }
  300. } // namespace detail
  301. #endif // doxygen
  302. } // namespace projections
  303. }} // namespace boost::geometry
  304. #endif // BOOST_GEOMETRY_PROJECTIONS_LSAT_HPP