123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717 |
- #ifndef BOOST_GEOMETRY_PROJECTIONS_TMERC_HPP
- #define BOOST_GEOMETRY_PROJECTIONS_TMERC_HPP
- #include <boost/geometry/srs/projections/impl/base_static.hpp>
- #include <boost/geometry/srs/projections/impl/base_dynamic.hpp>
- #include <boost/geometry/srs/projections/impl/projects.hpp>
- #include <boost/geometry/srs/projections/impl/factory_entry.hpp>
- #include <boost/geometry/srs/projections/impl/function_overloads.hpp>
- #include <boost/geometry/srs/projections/impl/pj_mlfn.hpp>
- #include <boost/geometry/util/condition.hpp>
- #include <boost/geometry/util/math.hpp>
- namespace boost { namespace geometry
- {
- namespace projections
- {
- #ifndef DOXYGEN_NO_DETAIL
- namespace detail { namespace tmerc
- {
- static const double epsilon10 = 1.e-10;
-
- static const int proj_etmerc_order = 6;
- template <typename T>
- inline T FC1() { return 1.; }
- template <typename T>
- inline T FC2() { return .5; }
- template <typename T>
- inline T FC3() { return .16666666666666666666666666666666666666; }
- template <typename T>
- inline T FC4() { return .08333333333333333333333333333333333333; }
- template <typename T>
- inline T FC5() { return .05; }
- template <typename T>
- inline T FC6() { return .03333333333333333333333333333333333333; }
- template <typename T>
- inline T FC7() { return .02380952380952380952380952380952380952; }
- template <typename T>
- inline T FC8() { return .01785714285714285714285714285714285714; }
- template <typename T>
- struct par_tmerc
- {
- T esp;
- T ml0;
- detail::en<T> en;
- };
-
- template <typename T>
- struct par_tmerc_exact
- {
- T Qn;
- T Zb;
- T cgb[6];
- T cbg[6];
- T utg[6];
- T gtu[6];
- };
- template <typename T, typename Parameters>
- struct base_tmerc_ellipsoid
- {
- par_tmerc<T> m_proj_parm;
-
-
- inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
- {
- static const T half_pi = detail::half_pi<T>();
- static const T FC1 = tmerc::FC1<T>();
- static const T FC2 = tmerc::FC2<T>();
- static const T FC3 = tmerc::FC3<T>();
- static const T FC4 = tmerc::FC4<T>();
- static const T FC5 = tmerc::FC5<T>();
- static const T FC6 = tmerc::FC6<T>();
- static const T FC7 = tmerc::FC7<T>();
- static const T FC8 = tmerc::FC8<T>();
- T al, als, n, cosphi, sinphi, t;
-
- if( lp_lon < -half_pi || lp_lon > half_pi )
- {
- xy_x = HUGE_VAL;
- xy_y = HUGE_VAL;
- BOOST_THROW_EXCEPTION( projection_exception(error_lat_or_lon_exceed_limit) );
- return;
- }
- sinphi = sin(lp_lat);
- cosphi = cos(lp_lat);
- t = fabs(cosphi) > 1e-10 ? sinphi/cosphi : 0.;
- t *= t;
- al = cosphi * lp_lon;
- als = al * al;
- al /= sqrt(1. - par.es * sinphi * sinphi);
- n = this->m_proj_parm.esp * cosphi * cosphi;
- xy_x = par.k0 * al * (FC1 +
- FC3 * als * (1. - t + n +
- FC5 * als * (5. + t * (t - 18.) + n * (14. - 58. * t)
- + FC7 * als * (61. + t * ( t * (179. - t) - 479. ) )
- )));
- xy_y = par.k0 * (pj_mlfn(lp_lat, sinphi, cosphi, this->m_proj_parm.en) - this->m_proj_parm.ml0 +
- sinphi * al * lp_lon * FC2 * ( 1. +
- FC4 * als * (5. - t + n * (9. + 4. * n) +
- FC6 * als * (61. + t * (t - 58.) + n * (270. - 330 * t)
- + FC8 * als * (1385. + t * ( t * (543. - t) - 3111.) )
- ))));
- }
-
-
- inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
- {
- static const T half_pi = detail::half_pi<T>();
- static const T FC1 = tmerc::FC1<T>();
- static const T FC2 = tmerc::FC2<T>();
- static const T FC3 = tmerc::FC3<T>();
- static const T FC4 = tmerc::FC4<T>();
- static const T FC5 = tmerc::FC5<T>();
- static const T FC6 = tmerc::FC6<T>();
- static const T FC7 = tmerc::FC7<T>();
- static const T FC8 = tmerc::FC8<T>();
- T n, con, cosphi, d, ds, sinphi, t;
- lp_lat = pj_inv_mlfn(this->m_proj_parm.ml0 + xy_y / par.k0, par.es, this->m_proj_parm.en);
- if (fabs(lp_lat) >= half_pi) {
- lp_lat = xy_y < 0. ? -half_pi : half_pi;
- lp_lon = 0.;
- } else {
- sinphi = sin(lp_lat);
- cosphi = cos(lp_lat);
- t = fabs(cosphi) > 1e-10 ? sinphi/cosphi : 0.;
- n = this->m_proj_parm.esp * cosphi * cosphi;
- d = xy_x * sqrt(con = 1. - par.es * sinphi * sinphi) / par.k0;
- con *= t;
- t *= t;
- ds = d * d;
- lp_lat -= (con * ds / (1.-par.es)) * FC2 * (1. -
- ds * FC4 * (5. + t * (3. - 9. * n) + n * (1. - 4 * n) -
- ds * FC6 * (61. + t * (90. - 252. * n +
- 45. * t) + 46. * n
- - ds * FC8 * (1385. + t * (3633. + t * (4095. + 1574. * t)) )
- )));
- lp_lon = d*(FC1 -
- ds*FC3*( 1. + 2.*t + n -
- ds*FC5*(5. + t*(28. + 24.*t + 8.*n) + 6.*n
- - ds * FC7 * (61. + t * (662. + t * (1320. + 720. * t)) )
- ))) / cosphi;
- }
- }
- static inline std::string get_name()
- {
- return "tmerc_ellipsoid";
- }
- };
- template <typename T, typename Parameters>
- struct base_tmerc_ellipsoid_exact
- {
- par_tmerc_exact<T> m_proj_parm;
- static inline std::string get_name()
- {
- return "tmerc_ellipsoid";
- }
-
- inline
- static T gatg(const T *p1, int len_p1, T B, T cos_2B, T sin_2B)
- {
- T h = 0, h1, h2 = 0;
- const T two_cos_2B = 2*cos_2B;
- const T* p = p1 + len_p1;
- h1 = *--p;
- while (p - p1) {
- h = -h2 + two_cos_2B*h1 + *--p;
- h2 = h1;
- h1 = h;
- }
- return (B + h*sin_2B);
- }
-
- inline
- static T clenS(const T *a, int size,
- T sin_arg_r, T cos_arg_r,
- T sinh_arg_i, T cosh_arg_i,
- T *R, T *I)
- {
- T r, i, hr, hr1, hr2, hi, hi1, hi2;
-
- const T* p = a + size;
- r = 2*cos_arg_r*cosh_arg_i;
- i = -2*sin_arg_r*sinh_arg_i;
-
- hi1 = hr1 = hi = 0;
- hr = *--p;
- for (; a - p;) {
- hr2 = hr1;
- hi2 = hi1;
- hr1 = hr;
- hi1 = hi;
- hr = -hr2 + r*hr1 - i*hi1 + *--p;
- hi = -hi2 + i*hr1 + r*hi1;
- }
- r = sin_arg_r*cosh_arg_i;
- i = cos_arg_r*sinh_arg_i;
- *R = r*hr - i*hi;
- *I = r*hi + i*hr;
- return *R;
- }
-
- static T clens(const T *a, int size, T arg_r)
- {
- T r, hr, hr1, hr2, cos_arg_r;
- const T* p = a + size;
- cos_arg_r = cos(arg_r);
- r = 2*cos_arg_r;
-
- hr1 = 0;
- hr = *--p;
- for (; a - p;) {
- hr2 = hr1;
- hr1 = hr;
- hr = -hr2 + r*hr1 + *--p;
- }
- return sin(arg_r)*hr;
- }
-
-
- inline void fwd(Parameters const& ,
- T const& lp_lon,
- T const& lp_lat,
- T& xy_x, T& xy_y) const
- {
-
-
-
- T Cn = gatg (this->m_proj_parm.cbg, proj_etmerc_order, lp_lat,
- cos(2*lp_lat), sin(2*lp_lat));
-
- const T sin_Cn = sin (Cn);
- const T cos_Cn = cos (Cn);
- const T sin_Ce = sin (lp_lon);
- const T cos_Ce = cos (lp_lon);
- const T cos_Cn_cos_Ce = cos_Cn*cos_Ce;
- Cn = atan2 (sin_Cn, cos_Cn_cos_Ce);
- const T inv_denom_tan_Ce = 1. / hypot (sin_Cn, cos_Cn_cos_Ce);
- const T tan_Ce = sin_Ce*cos_Cn * inv_denom_tan_Ce;
- #if 0
-
- const T sin_Ce_cos_Cn = sin_Ce*cos_Cn;
- const T denom = sqrt(1 - sin_Ce_cos_Cn * sin_Ce_cos_Cn);
- const T tan_Ce = sin_Ce_cos_Cn / denom;
- #endif
-
- T Ce = asinh ( tan_Ce );
-
- const T two_inv_denom_tan_Ce = 2 * inv_denom_tan_Ce;
- const T two_inv_denom_tan_Ce_square = two_inv_denom_tan_Ce * inv_denom_tan_Ce;
- const T tmp_r = cos_Cn_cos_Ce * two_inv_denom_tan_Ce_square;
- const T sin_arg_r = sin_Cn * tmp_r;
- const T cos_arg_r = cos_Cn_cos_Ce * tmp_r - 1;
-
- const T sinh_arg_i = tan_Ce * two_inv_denom_tan_Ce;
- const T cosh_arg_i = two_inv_denom_tan_Ce_square - 1;
- T dCn, dCe;
- Cn += clenS (this->m_proj_parm.gtu, proj_etmerc_order,
- sin_arg_r, cos_arg_r, sinh_arg_i, cosh_arg_i,
- &dCn, &dCe);
- Ce += dCe;
- if (fabs (Ce) <= 2.623395162778) {
- xy_y = this->m_proj_parm.Qn * Cn + this->m_proj_parm.Zb;
- xy_x = this->m_proj_parm.Qn * Ce;
- } else {
- BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
- xy_x = xy_y = HUGE_VAL;
- }
- }
-
- inline void inv(Parameters const& ,
- T const& xy_x,
- T const& xy_y,
- T& lp_lon,
- T& lp_lat) const
- {
-
-
-
- T Cn = (xy_y - this->m_proj_parm.Zb)/this->m_proj_parm.Qn;
- T Ce = xy_x/this->m_proj_parm.Qn;
- if (fabs(Ce) <= 2.623395162778) {
-
- const T sin_arg_r = sin(2*Cn);
- const T cos_arg_r = cos(2*Cn);
-
-
- const T exp_2_Ce = exp(2*Ce);
- const T half_inv_exp_2_Ce = 0.5 / exp_2_Ce;
- const T sinh_arg_i = 0.5 * exp_2_Ce - half_inv_exp_2_Ce;
- const T cosh_arg_i = 0.5 * exp_2_Ce + half_inv_exp_2_Ce;
- T dCn_ignored, dCe;
- Cn += clenS(this->m_proj_parm.utg, proj_etmerc_order,
- sin_arg_r, cos_arg_r, sinh_arg_i, cosh_arg_i,
- &dCn_ignored, &dCe);
- Ce += dCe;
-
- const T sin_Cn = sin (Cn);
- const T cos_Cn = cos (Cn);
- #if 0
-
- T sin_Ce, cos_Ce;
- Ce = atan (sinh (Ce));
- sin_Ce = sin (Ce);
- cos_Ce = cos (Ce);
- Ce = atan2 (sin_Ce, cos_Ce*cos_Cn);
- Cn = atan2 (sin_Cn*cos_Ce, hypot (sin_Ce, cos_Ce*cos_Cn));
- #else
-
- const T sinhCe = sinh (Ce);
- Ce = atan2 (sinhCe, cos_Cn);
- const T modulus_Ce = hypot (sinhCe, cos_Cn);
- Cn = atan2 (sin_Cn, modulus_Ce);
- #endif
-
-
- const T tmp = 2 * modulus_Ce / (sinhCe * sinhCe + 1);
- const T sin_2_Cn = sin_Cn * tmp;
- const T cos_2_Cn = tmp * modulus_Ce - 1.;
-
-
- lp_lat = gatg (this->m_proj_parm.cgb, proj_etmerc_order, Cn, cos_2_Cn, sin_2_Cn);
- lp_lon = Ce;
- }
- else {
- BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
- lp_lat = lp_lon = HUGE_VAL;
- }
- }
- };
- template <typename T, typename Parameters>
- struct base_tmerc_spheroid
- {
- par_tmerc<T> m_proj_parm;
-
-
- inline void fwd(Parameters const& par, T const& lp_lon, T const& lp_lat, T& xy_x, T& xy_y) const
- {
- static const T half_pi = detail::half_pi<T>();
- T b, cosphi;
-
- if( lp_lon < -half_pi || lp_lon > half_pi )
- {
- xy_x = HUGE_VAL;
- xy_y = HUGE_VAL;
- BOOST_THROW_EXCEPTION( projection_exception(error_lat_or_lon_exceed_limit) );
- return;
- }
- cosphi = cos(lp_lat);
- b = cosphi * sin(lp_lon);
- if (fabs(fabs(b) - 1.) <= epsilon10)
- BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
- xy_x = this->m_proj_parm.ml0 * log((1. + b) / (1. - b));
- xy_y = cosphi * cos(lp_lon) / sqrt(1. - b * b);
- b = fabs( xy_y );
- if (b >= 1.) {
- if ((b - 1.) > epsilon10)
- BOOST_THROW_EXCEPTION( projection_exception(error_tolerance_condition) );
- else xy_y = 0.;
- } else
- xy_y = acos(xy_y);
- if (lp_lat < 0.)
- xy_y = -xy_y;
- xy_y = this->m_proj_parm.esp * (xy_y - par.phi0);
- }
-
-
- inline void inv(Parameters const& par, T const& xy_x, T const& xy_y, T& lp_lon, T& lp_lat) const
- {
- T h, g;
- h = exp(xy_x / this->m_proj_parm.esp);
- g = .5 * (h - 1. / h);
- h = cos(par.phi0 + xy_y / this->m_proj_parm.esp);
- lp_lat = asin(sqrt((1. - h * h) / (1. + g * g)));
-
- if (xy_y < 0. && -lp_lat+par.phi0 < 0.0) lp_lat = -lp_lat;
- lp_lon = (g != 0.0 || h != 0.0) ? atan2(g, h) : 0.;
- }
- static inline std::string get_name()
- {
- return "tmerc_spheroid";
- }
- };
- template <typename Parameters, typename T>
- inline void setup(Parameters const& par, par_tmerc<T>& proj_parm)
- {
- if (par.es != 0.0) {
- proj_parm.en = pj_enfn<T>(par.es);
- proj_parm.ml0 = pj_mlfn(par.phi0, sin(par.phi0), cos(par.phi0), proj_parm.en);
- proj_parm.esp = par.es / (1. - par.es);
- } else {
- proj_parm.esp = par.k0;
- proj_parm.ml0 = .5 * proj_parm.esp;
- }
- }
- template <typename Parameters, typename T>
- inline void setup_exact(Parameters const& par, par_tmerc_exact<T>& proj_parm)
- {
- assert( par.es > 0 );
-
-
- const T n = pow(tan(asin(par.e)/2),2);
- T np = n;
-
-
-
-
- proj_parm.cgb[0] = n*( 2 + n*(-2/3.0 + n*(-2 + n*(116/45.0 + n*(26/45.0 +
- n*(-2854/675.0 ))))));
- proj_parm.cbg[0] = n*(-2 + n*( 2/3.0 + n*( 4/3.0 + n*(-82/45.0 + n*(32/45.0 +
- n*( 4642/4725.0))))));
- np *= n;
- proj_parm.cgb[1] = np*(7/3.0 + n*( -8/5.0 + n*(-227/45.0 + n*(2704/315.0 +
- n*( 2323/945.0)))));
- proj_parm.cbg[1] = np*(5/3.0 + n*(-16/15.0 + n*( -13/9.0 + n*( 904/315.0 +
- n*(-1522/945.0)))));
- np *= n;
-
- proj_parm.cgb[2] = np*( 56/15.0 + n*(-136/35.0 + n*(-1262/105.0 +
- n*( 73814/2835.0))));
- proj_parm.cbg[2] = np*(-26/15.0 + n*( 34/21.0 + n*( 8/5.0 +
- n*(-12686/2835.0))));
- np *= n;
-
- proj_parm.cgb[3] = np*(4279/630.0 + n*(-332/35.0 + n*(-399572/14175.0)));
- proj_parm.cbg[3] = np*(1237/630.0 + n*( -12/5.0 + n*( -24832/14175.0)));
- np *= n;
- proj_parm.cgb[4] = np*(4174/315.0 + n*(-144838/6237.0 ));
- proj_parm.cbg[4] = np*(-734/315.0 + n*( 109598/31185.0));
- np *= n;
- proj_parm.cgb[5] = np*(601676/22275.0 );
- proj_parm.cbg[5] = np*(444337/155925.0);
-
-
- np = n*n;
-
- proj_parm.Qn = par.k0/(1 + n) * (1 + np*(1/4.0 + np*(1/64.0 + np/256.0)));
-
-
-
- proj_parm.utg[0] = n*(-0.5 + n*( 2/3.0 + n*(-37/96.0 + n*( 1/360.0 +
- n*( 81/512.0 + n*(-96199/604800.0))))));
- proj_parm.gtu[0] = n*( 0.5 + n*(-2/3.0 + n*( 5/16.0 + n*(41/180.0 +
- n*(-127/288.0 + n*( 7891/37800.0 ))))));
- proj_parm.utg[1] = np*(-1/48.0 + n*(-1/15.0 + n*(437/1440.0 + n*(-46/105.0 +
- n*( 1118711/3870720.0)))));
- proj_parm.gtu[1] = np*(13/48.0 + n*(-3/5.0 + n*(557/1440.0 + n*(281/630.0 +
- n*(-1983433/1935360.0)))));
- np *= n;
- proj_parm.utg[2] = np*(-17/480.0 + n*( 37/840.0 + n*( 209/4480.0 +
- n*( -5569/90720.0 ))));
- proj_parm.gtu[2] = np*( 61/240.0 + n*(-103/140.0 + n*(15061/26880.0 +
- n*(167603/181440.0))));
- np *= n;
- proj_parm.utg[3] = np*(-4397/161280.0 + n*( 11/504.0 + n*( 830251/7257600.0)));
- proj_parm.gtu[3] = np*(49561/161280.0 + n*(-179/168.0 + n*(6601661/7257600.0)));
- np *= n;
- proj_parm.utg[4] = np*(-4583/161280.0 + n*( 108847/3991680.0));
- proj_parm.gtu[4] = np*(34729/80640.0 + n*(-3418889/1995840.0));
- np *= n;
- proj_parm.utg[5] = np*(-20648693/638668800.0);
- proj_parm.gtu[5] = np*(212378941/319334400.0);
-
- const T Z = base_tmerc_ellipsoid_exact<T, Parameters>::gatg (proj_parm.cbg, proj_etmerc_order, par.phi0, cos(2*par.phi0), sin(2*par.phi0));
-
-
- proj_parm.Zb = - proj_parm.Qn*(Z + base_tmerc_ellipsoid_exact<T, Parameters>::clens(proj_parm.gtu, proj_etmerc_order, 2*Z));
- }
- }}
- #endif
-
-
-
- template <typename T, typename Parameters>
- struct tmerc_ellipsoid : public detail::tmerc::base_tmerc_ellipsoid_exact<T, Parameters>
- {
- template <typename Params>
- inline tmerc_ellipsoid(Params const&, Parameters const& par)
- {
- detail::tmerc::setup_exact(par, this->m_proj_parm);
- }
- };
-
- template <typename T, typename Parameters>
- struct tmerc_spheroid : public detail::tmerc::base_tmerc_spheroid<T, Parameters>
- {
- template <typename Params>
- inline tmerc_spheroid(Params const&, Parameters const& par)
- {
- detail::tmerc::setup(par, this->m_proj_parm);
- }
- };
- #ifndef DOXYGEN_NO_DETAIL
- namespace detail
- {
-
- BOOST_GEOMETRY_PROJECTIONS_DETAIL_STATIC_PROJECTION_FI2(srs::spar::proj_tmerc, tmerc_spheroid, tmerc_ellipsoid)
-
- BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_ENTRY_FI2(tmerc_entry, tmerc_spheroid, tmerc_ellipsoid)
- BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_BEGIN(tmerc_init)
- {
- BOOST_GEOMETRY_PROJECTIONS_DETAIL_FACTORY_INIT_ENTRY(tmerc, tmerc_entry)
- }
- }
- #endif
- }
- }}
- #endif
|