lsat.hpp 15 KB

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