etmerc.hpp 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445
  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) 2008 Gerald I. Evenden
  16. // Permission is hereby granted, free of charge, to any person obtaining a
  17. // copy of this software and associated documentation files (the "Software"),
  18. // to deal in the Software without restriction, including without limitation
  19. // the rights to use, copy, modify, merge, publish, distribute, sublicense,
  20. // and/or sell copies of the Software, and to permit persons to whom the
  21. // Software is furnished to do so, subject to the following conditions:
  22. // The above copyright notice and this permission notice shall be included
  23. // in all copies or substantial portions of the Software.
  24. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
  25. // OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  26. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
  27. // THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  28. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  29. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
  30. // DEALINGS IN THE SOFTWARE.
  31. /* The code in this file is largly based upon procedures:
  32. *
  33. * Written by: Knud Poder and Karsten Engsager
  34. *
  35. * Based on math from: R.Koenig and K.H. Weise, "Mathematische
  36. * Grundlagen der hoeheren Geodaesie und Kartographie,
  37. * Springer-Verlag, Berlin/Goettingen" Heidelberg, 1951.
  38. *
  39. * Modified and used here by permission of Reference Networks
  40. * Division, Kort og Matrikelstyrelsen (KMS), Copenhagen, Denmark
  41. */
  42. #ifndef BOOST_GEOMETRY_PROJECTIONS_ETMERC_HPP
  43. #define BOOST_GEOMETRY_PROJECTIONS_ETMERC_HPP
  44. #include <boost/geometry/srs/projections/impl/base_static.hpp>
  45. #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
  46. #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
  47. #include <boost/geometry/srs/projections/impl/function_overloads.hpp>
  48. #include <boost/geometry/srs/projections/impl/pj_param.hpp>
  49. #include <boost/geometry/srs/projections/impl/projects.hpp>
  50. #include <boost/math/special_functions/hypot.hpp>
  51. namespace boost { namespace geometry
  52. {
  53. namespace projections
  54. {
  55. #ifndef DOXYGEN_NO_DETAIL
  56. namespace detail { namespace etmerc
  57. {
  58. static const int PROJ_ETMERC_ORDER = 6;
  59. template <typename T>
  60. struct par_etmerc
  61. {
  62. T Qn; /* Merid. quad., scaled to the projection */
  63. T Zb; /* Radius vector in polar coord. systems */
  64. T cgb[6]; /* Constants for Gauss -> Geo lat */
  65. T cbg[6]; /* Constants for Geo lat -> Gauss */
  66. T utg[6]; /* Constants for transv. merc. -> geo */
  67. T gtu[6]; /* Constants for geo -> transv. merc. */
  68. };
  69. template <typename T>
  70. inline T log1py(T const& x) { /* Compute log(1+x) accurately */
  71. volatile T
  72. y = 1 + x,
  73. z = y - 1;
  74. /* Here's the explanation for this magic: y = 1 + z, exactly, and z
  75. * approx x, thus log(y)/z (which is nearly constant near z = 0) returns
  76. * a good approximation to the true log(1 + x)/x. The multiplication x *
  77. * (log(y)/z) introduces little additional error. */
  78. return z == 0 ? x : x * log(y) / z;
  79. }
  80. template <typename T>
  81. inline T asinhy(T const& x) { /* Compute asinh(x) accurately */
  82. T y = fabs(x); /* Enforce odd parity */
  83. y = log1py(y * (1 + y/(boost::math::hypot(1.0, y) + 1)));
  84. return x < 0 ? -y : y;
  85. }
  86. template <typename T>
  87. inline T gatg(const T *p1, int len_p1, T const& B) {
  88. const T *p;
  89. T h = 0, h1, h2 = 0, cos_2B;
  90. cos_2B = 2*cos(2*B);
  91. for (p = p1 + len_p1, h1 = *--p; p - p1; h2 = h1, h1 = h)
  92. h = -h2 + cos_2B*h1 + *--p;
  93. return (B + h*sin(2*B));
  94. }
  95. /* Complex Clenshaw summation */
  96. template <typename T>
  97. inline T clenS(const T *a, int size, T const& arg_r, T const& arg_i, T *R, T *I) {
  98. T r, i, hr, hr1, hr2, hi, hi1, hi2;
  99. T sin_arg_r, cos_arg_r, sinh_arg_i, cosh_arg_i;
  100. /* arguments */
  101. const T* p = a + size;
  102. sin_arg_r = sin(arg_r);
  103. cos_arg_r = cos(arg_r);
  104. sinh_arg_i = sinh(arg_i);
  105. cosh_arg_i = cosh(arg_i);
  106. r = 2*cos_arg_r*cosh_arg_i;
  107. i = -2*sin_arg_r*sinh_arg_i;
  108. /* summation loop */
  109. for (hi1 = hr1 = hi = 0, hr = *--p; a - p;) {
  110. hr2 = hr1;
  111. hi2 = hi1;
  112. hr1 = hr;
  113. hi1 = hi;
  114. hr = -hr2 + r*hr1 - i*hi1 + *--p;
  115. hi = -hi2 + i*hr1 + r*hi1;
  116. }
  117. r = sin_arg_r*cosh_arg_i;
  118. i = cos_arg_r*sinh_arg_i;
  119. *R = r*hr - i*hi;
  120. *I = r*hi + i*hr;
  121. return(*R);
  122. }
  123. /* Real Clenshaw summation */
  124. template <typename T>
  125. inline T clens(const T *a, int size, T const& arg_r) {
  126. T r, hr, hr1, hr2, cos_arg_r;
  127. const T* p = a + size;
  128. cos_arg_r = cos(arg_r);
  129. r = 2*cos_arg_r;
  130. /* summation loop */
  131. for (hr1 = 0, hr = *--p; a - p;) {
  132. hr2 = hr1;
  133. hr1 = hr;
  134. hr = -hr2 + r*hr1 + *--p;
  135. }
  136. return(sin(arg_r)*hr);
  137. }
  138. template <typename T, typename Parameters>
  139. struct base_etmerc_ellipsoid
  140. {
  141. par_etmerc<T> m_proj_parm;
  142. // FORWARD(e_forward) ellipsoid
  143. // Project coordinates from geographic (lon, lat) to cartesian (x, y)
  144. inline void fwd(Parameters const& , T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
  145. {
  146. T sin_Cn, cos_Cn, cos_Ce, sin_Ce, dCn, dCe;
  147. T Cn = lp_lat, Ce = lp_lon;
  148. /* ell. LAT, LNG -> Gaussian LAT, LNG */
  149. Cn = gatg(this->m_proj_parm.cbg, PROJ_ETMERC_ORDER, Cn);
  150. /* Gaussian LAT, LNG -> compl. sph. LAT */
  151. sin_Cn = sin(Cn);
  152. cos_Cn = cos(Cn);
  153. sin_Ce = sin(Ce);
  154. cos_Ce = cos(Ce);
  155. Cn = atan2(sin_Cn, cos_Ce*cos_Cn);
  156. Ce = atan2(sin_Ce*cos_Cn, boost::math::hypot(sin_Cn, cos_Cn*cos_Ce));
  157. /* compl. sph. N, E -> ell. norm. N, E */
  158. Ce = asinhy(tan(Ce)); /* Replaces: Ce = log(tan(fourth_pi + Ce*0.5)); */
  159. Cn += clenS(this->m_proj_parm.gtu, PROJ_ETMERC_ORDER, 2*Cn, 2*Ce, &dCn, &dCe);
  160. Ce += dCe;
  161. if (fabs(Ce) <= 2.623395162778) {
  162. xy_y = this->m_proj_parm.Qn * Cn + this->m_proj_parm.Zb; /* Northing */
  163. xy_x = this->m_proj_parm.Qn * Ce; /* Easting */
  164. } else
  165. xy_x = xy_y = HUGE_VAL;
  166. }
  167. // INVERSE(e_inverse) ellipsoid
  168. // Project coordinates from cartesian (x, y) to geographic (lon, lat)
  169. inline void inv(Parameters const& , T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
  170. {
  171. T sin_Cn, cos_Cn, cos_Ce, sin_Ce, dCn, dCe;
  172. T Cn = xy_y, Ce = xy_x;
  173. /* normalize N, E */
  174. Cn = (Cn - this->m_proj_parm.Zb)/this->m_proj_parm.Qn;
  175. Ce = Ce/this->m_proj_parm.Qn;
  176. if (fabs(Ce) <= 2.623395162778) { /* 150 degrees */
  177. /* norm. N, E -> compl. sph. LAT, LNG */
  178. Cn += clenS(this->m_proj_parm.utg, PROJ_ETMERC_ORDER, 2*Cn, 2*Ce, &dCn, &dCe);
  179. Ce += dCe;
  180. Ce = atan(sinh(Ce)); /* Replaces: Ce = 2*(atan(exp(Ce)) - fourth_pi); */
  181. /* compl. sph. LAT -> Gaussian LAT, LNG */
  182. sin_Cn = sin(Cn);
  183. cos_Cn = cos(Cn);
  184. sin_Ce = sin(Ce);
  185. cos_Ce = cos(Ce);
  186. Ce = atan2(sin_Ce, cos_Ce*cos_Cn);
  187. Cn = atan2(sin_Cn*cos_Ce, boost::math::hypot(sin_Ce, cos_Ce*cos_Cn));
  188. /* Gaussian LAT, LNG -> ell. LAT, LNG */
  189. lp_lat = gatg(this->m_proj_parm.cgb, PROJ_ETMERC_ORDER, Cn);
  190. lp_lon = Ce;
  191. }
  192. else
  193. lp_lat = lp_lon = HUGE_VAL;
  194. }
  195. static inline std::string get_name()
  196. {
  197. return "etmerc_ellipsoid";
  198. }
  199. };
  200. template <typename Parameters, typename T>
  201. inline void setup(Parameters& par, par_etmerc<T>& proj_parm)
  202. {
  203. T f, n, np, Z;
  204. if (par.es <= 0) {
  205. BOOST_THROW_EXCEPTION( projection_exception(error_ellipsoid_use_required) );
  206. }
  207. f = par.es / (1 + sqrt(1 - par.es)); /* Replaces: f = 1 - sqrt(1-par.es); */
  208. /* third flattening */
  209. np = n = f/(2 - f);
  210. /* COEF. OF TRIG SERIES GEO <-> GAUSS */
  211. /* cgb := Gaussian -> Geodetic, KW p190 - 191 (61) - (62) */
  212. /* cbg := Geodetic -> Gaussian, KW p186 - 187 (51) - (52) */
  213. /* PROJ_ETMERC_ORDER = 6th degree : Engsager and Poder: ICC2007 */
  214. proj_parm.cgb[0] = n*( 2 + n*(-2/3.0 + n*(-2 + n*(116/45.0 + n*(26/45.0 +
  215. n*(-2854/675.0 ))))));
  216. proj_parm.cbg[0] = n*(-2 + n*( 2/3.0 + n*( 4/3.0 + n*(-82/45.0 + n*(32/45.0 +
  217. n*( 4642/4725.0))))));
  218. np *= n;
  219. proj_parm.cgb[1] = np*(7/3.0 + n*( -8/5.0 + n*(-227/45.0 + n*(2704/315.0 +
  220. n*( 2323/945.0)))));
  221. proj_parm.cbg[1] = np*(5/3.0 + n*(-16/15.0 + n*( -13/9.0 + n*( 904/315.0 +
  222. n*(-1522/945.0)))));
  223. np *= n;
  224. /* n^5 coeff corrected from 1262/105 -> -1262/105 */
  225. proj_parm.cgb[2] = np*( 56/15.0 + n*(-136/35.0 + n*(-1262/105.0 +
  226. n*( 73814/2835.0))));
  227. proj_parm.cbg[2] = np*(-26/15.0 + n*( 34/21.0 + n*( 8/5.0 +
  228. n*(-12686/2835.0))));
  229. np *= n;
  230. /* n^5 coeff corrected from 322/35 -> 332/35 */
  231. proj_parm.cgb[3] = np*(4279/630.0 + n*(-332/35.0 + n*(-399572/14175.0)));
  232. proj_parm.cbg[3] = np*(1237/630.0 + n*( -12/5.0 + n*( -24832/14175.0)));
  233. np *= n;
  234. proj_parm.cgb[4] = np*(4174/315.0 + n*(-144838/6237.0 ));
  235. proj_parm.cbg[4] = np*(-734/315.0 + n*( 109598/31185.0));
  236. np *= n;
  237. proj_parm.cgb[5] = np*(601676/22275.0 );
  238. proj_parm.cbg[5] = np*(444337/155925.0);
  239. /* Constants of the projections */
  240. /* Transverse Mercator (UTM, ITM, etc) */
  241. np = n*n;
  242. /* Norm. mer. quad, K&W p.50 (96), p.19 (38b), p.5 (2) */
  243. proj_parm.Qn = par.k0/(1 + n) * (1 + np*(1/4.0 + np*(1/64.0 + np/256.0)));
  244. /* coef of trig series */
  245. /* utg := ell. N, E -> sph. N, E, KW p194 (65) */
  246. /* gtu := sph. N, E -> ell. N, E, KW p196 (69) */
  247. proj_parm.utg[0] = n*(-0.5 + n*( 2/3.0 + n*(-37/96.0 + n*( 1/360.0 +
  248. n*( 81/512.0 + n*(-96199/604800.0))))));
  249. proj_parm.gtu[0] = n*( 0.5 + n*(-2/3.0 + n*( 5/16.0 + n*(41/180.0 +
  250. n*(-127/288.0 + n*( 7891/37800.0 ))))));
  251. proj_parm.utg[1] = np*(-1/48.0 + n*(-1/15.0 + n*(437/1440.0 + n*(-46/105.0 +
  252. n*( 1118711/3870720.0)))));
  253. proj_parm.gtu[1] = np*(13/48.0 + n*(-3/5.0 + n*(557/1440.0 + n*(281/630.0 +
  254. n*(-1983433/1935360.0)))));
  255. np *= n;
  256. proj_parm.utg[2] = np*(-17/480.0 + n*( 37/840.0 + n*( 209/4480.0 +
  257. n*( -5569/90720.0 ))));
  258. proj_parm.gtu[2] = np*( 61/240.0 + n*(-103/140.0 + n*(15061/26880.0 +
  259. n*(167603/181440.0))));
  260. np *= n;
  261. proj_parm.utg[3] = np*(-4397/161280.0 + n*( 11/504.0 + n*( 830251/7257600.0)));
  262. proj_parm.gtu[3] = np*(49561/161280.0 + n*(-179/168.0 + n*(6601661/7257600.0)));
  263. np *= n;
  264. proj_parm.utg[4] = np*(-4583/161280.0 + n*( 108847/3991680.0));
  265. proj_parm.gtu[4] = np*(34729/80640.0 + n*(-3418889/1995840.0));
  266. np *= n;
  267. proj_parm.utg[5] = np*(-20648693/638668800.0);
  268. proj_parm.gtu[5] = np*(212378941/319334400.0);
  269. /* Gaussian latitude value of the origin latitude */
  270. Z = gatg(proj_parm.cbg, PROJ_ETMERC_ORDER, par.phi0);
  271. /* Origin northing minus true northing at the origin latitude */
  272. /* i.e. true northing = N - proj_parm.Zb */
  273. proj_parm.Zb = - proj_parm.Qn*(Z + clens(proj_parm.gtu, PROJ_ETMERC_ORDER, 2*Z));
  274. }
  275. // Extended Transverse Mercator
  276. template <typename Parameters, typename T>
  277. inline void setup_etmerc(Parameters& par, par_etmerc<T>& proj_parm)
  278. {
  279. setup(par, proj_parm);
  280. }
  281. // Universal Transverse Mercator (UTM)
  282. template <typename Params, typename Parameters, typename T>
  283. inline void setup_utm(Params const& params, Parameters& par, par_etmerc<T>& proj_parm)
  284. {
  285. static const T pi = detail::pi<T>();
  286. int zone;
  287. if (par.es == 0.0) {
  288. BOOST_THROW_EXCEPTION( projection_exception(error_ellipsoid_use_required) );
  289. }
  290. par.y0 = pj_get_param_b<srs::spar::south>(params, "south", srs::dpar::south) ? 10000000. : 0.;
  291. par.x0 = 500000.;
  292. if (pj_param_i<srs::spar::zone>(params, "zone", srs::dpar::zone, zone)) /* zone input ? */
  293. {
  294. if (zone > 0 && zone <= 60)
  295. --zone;
  296. else {
  297. BOOST_THROW_EXCEPTION( projection_exception(error_invalid_utm_zone) );
  298. }
  299. }
  300. else /* nearest central meridian input */
  301. {
  302. zone = int_floor((adjlon(par.lam0) + pi) * 30. / pi);
  303. if (zone < 0)
  304. zone = 0;
  305. else if (zone >= 60)
  306. zone = 59;
  307. }
  308. par.lam0 = (zone + .5) * pi / 30. - pi;
  309. par.k0 = 0.9996;
  310. par.phi0 = 0.;
  311. setup(par, proj_parm);
  312. }
  313. }} // namespace detail::etmerc
  314. #endif // doxygen
  315. /*!
  316. \brief Extended Transverse Mercator projection
  317. \ingroup projections
  318. \tparam Geographic latlong point type
  319. \tparam Cartesian xy point type
  320. \tparam Parameters parameter type
  321. \par Projection characteristics
  322. - Cylindrical
  323. - Spheroid
  324. \par Projection parameters
  325. - lat_ts: Latitude of true scale
  326. - lat_0: Latitude of origin
  327. \par Example
  328. \image html ex_etmerc.gif
  329. */
  330. template <typename T, typename Parameters>
  331. struct etmerc_ellipsoid : public detail::etmerc::base_etmerc_ellipsoid<T, Parameters>
  332. {
  333. template <typename Params>
  334. inline etmerc_ellipsoid(Params const& , Parameters & par)
  335. {
  336. detail::etmerc::setup_etmerc(par, this->m_proj_parm);
  337. }
  338. };
  339. /*!
  340. \brief Universal Transverse Mercator (UTM) projection
  341. \ingroup projections
  342. \tparam Geographic latlong point type
  343. \tparam Cartesian xy point type
  344. \tparam Parameters parameter type
  345. \par Projection characteristics
  346. - Cylindrical
  347. - Spheroid
  348. \par Projection parameters
  349. - zone: UTM Zone (integer)
  350. - south: Denotes southern hemisphere UTM zone (boolean)
  351. \par Example
  352. \image html ex_utm.gif
  353. */
  354. template <typename T, typename Parameters>
  355. struct utm_ellipsoid : public detail::etmerc::base_etmerc_ellipsoid<T, Parameters>
  356. {
  357. template <typename Params>
  358. inline utm_ellipsoid(Params const& params, Parameters & par)
  359. {
  360. detail::etmerc::setup_utm(params, par, this->m_proj_parm);
  361. }
  362. };
  363. #ifndef DOXYGEN_NO_DETAIL
  364. namespace detail
  365. {
  366. // Static projection
  367. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_etmerc, etmerc_ellipsoid)
  368. BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI(srs::spar::proj_utm, utm_ellipsoid)
  369. // Factory entry(s)
  370. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(etmerc_entry, etmerc_ellipsoid)
  371. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI(utm_entry, utm_ellipsoid)
  372. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(etmerc_init)
  373. {
  374. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(etmerc, etmerc_entry);
  375. BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(utm, utm_entry);
  376. }
  377. } // namespace detail
  378. #endif // doxygen
  379. } // namespace projections
  380. }} // namespace boost::geometry
  381. #endif // BOOST_GEOMETRY_PROJECTIONS_ETMERC_HPP