PageRenderTime 16ms CodeModel.GetById 0ms RepoModel.GetById 0ms app.codeStats 0ms

/Proj4/PJ_tmerc.c

http://github.com/route-me/route-me
C | 148 lines | 143 code | 5 blank | 0 comment | 22 complexity | e34753a3c0172d7c2f4850c9462f3595 MD5 | raw file
  1. #ifndef lint
  2. static const char SCCSID[]="@(#)PJ_tmerc.c 4.2 94/06/02 GIE REL";
  3. #endif
  4. #define PROJ_PARMS__ \
  5. double esp; \
  6. double ml0; \
  7. double *en;
  8. #define PJ_LIB__
  9. #include "projects.h"
  10. PROJ_HEAD(tmerc, "Transverse Mercator") "\n\tCyl, Sph&Ell";
  11. PROJ_HEAD(utm, "Universal Transverse Mercator (UTM)")
  12. "\n\tCyl, Sph\n\tzone= south";
  13. #define EPS10 1.e-10
  14. #define aks0 P->esp
  15. #define aks5 P->ml0
  16. #define FC1 1.
  17. #define FC2 .5
  18. #define FC3 .16666666666666666666
  19. #define FC4 .08333333333333333333
  20. #define FC5 .05
  21. #define FC6 .03333333333333333333
  22. #define FC7 .02380952380952380952
  23. #define FC8 .01785714285714285714
  24. FORWARD(e_forward); /* ellipse */
  25. double al, als, n, cosphi, sinphi, t;
  26. sinphi = sin(lp.phi); cosphi = cos(lp.phi);
  27. t = fabs(cosphi) > 1e-10 ? sinphi/cosphi : 0.;
  28. t *= t;
  29. al = cosphi * lp.lam;
  30. als = al * al;
  31. al /= sqrt(1. - P->es * sinphi * sinphi);
  32. n = P->esp * cosphi * cosphi;
  33. xy.x = P->k0 * al * (FC1 +
  34. FC3 * als * (1. - t + n +
  35. FC5 * als * (5. + t * (t - 18.) + n * (14. - 58. * t)
  36. + FC7 * als * (61. + t * ( t * (179. - t) - 479. ) )
  37. )));
  38. xy.y = P->k0 * (pj_mlfn(lp.phi, sinphi, cosphi, P->en) - P->ml0 +
  39. sinphi * al * lp.lam * FC2 * ( 1. +
  40. FC4 * als * (5. - t + n * (9. + 4. * n) +
  41. FC6 * als * (61. + t * (t - 58.) + n * (270. - 330 * t)
  42. + FC8 * als * (1385. + t * ( t * (543. - t) - 3111.) )
  43. ))));
  44. return (xy);
  45. }
  46. FORWARD(s_forward); /* sphere */
  47. double b, cosphi;
  48. b = (cosphi = cos(lp.phi)) * sin(lp.lam);
  49. if (fabs(fabs(b) - 1.) <= EPS10) F_ERROR;
  50. xy.x = aks5 * log((1. + b) / (1. - b));
  51. if ((b = fabs( xy.y = cosphi * cos(lp.lam) / sqrt(1. - b * b) )) >= 1.) {
  52. if ((b - 1.) > EPS10) F_ERROR
  53. else xy.y = 0.;
  54. } else
  55. xy.y = acos(xy.y);
  56. if (lp.phi < 0.) xy.y = -xy.y;
  57. xy.y = aks0 * (xy.y - P->phi0);
  58. return (xy);
  59. }
  60. INVERSE(e_inverse); /* ellipsoid */
  61. double n, con, cosphi, d, ds, sinphi, t;
  62. lp.phi = pj_inv_mlfn(P->ml0 + xy.y / P->k0, P->es, P->en);
  63. if (fabs(lp.phi) >= HALFPI) {
  64. lp.phi = xy.y < 0. ? -HALFPI : HALFPI;
  65. lp.lam = 0.;
  66. } else {
  67. sinphi = sin(lp.phi);
  68. cosphi = cos(lp.phi);
  69. t = fabs(cosphi) > 1e-10 ? sinphi/cosphi : 0.;
  70. n = P->esp * cosphi * cosphi;
  71. d = xy.x * sqrt(con = 1. - P->es * sinphi * sinphi) / P->k0;
  72. con *= t;
  73. t *= t;
  74. ds = d * d;
  75. lp.phi -= (con * ds / (1.-P->es)) * FC2 * (1. -
  76. ds * FC4 * (5. + t * (3. - 9. * n) + n * (1. - 4 * n) -
  77. ds * FC6 * (61. + t * (90. - 252. * n +
  78. 45. * t) + 46. * n
  79. - ds * FC8 * (1385. + t * (3633. + t * (4095. + 1574. * t)) )
  80. )));
  81. lp.lam = d*(FC1 -
  82. ds*FC3*( 1. + 2.*t + n -
  83. ds*FC5*(5. + t*(28. + 24.*t + 8.*n) + 6.*n
  84. - ds * FC7 * (61. + t * (662. + t * (1320. + 720. * t)) )
  85. ))) / cosphi;
  86. }
  87. return (lp);
  88. }
  89. INVERSE(s_inverse); /* sphere */
  90. double h, g;
  91. h = exp(xy.x / aks0);
  92. g = .5 * (h - 1. / h);
  93. h = cos(P->phi0 + xy.y / aks0);
  94. lp.phi = asin(sqrt((1. - h * h) / (1. + g * g)));
  95. if (xy.y < 0.) lp.phi = -lp.phi;
  96. lp.lam = (g || h) ? atan2(g, h) : 0.;
  97. return (lp);
  98. }
  99. FREEUP;
  100. if (P) {
  101. if (P->en)
  102. pj_dalloc(P->en);
  103. pj_dalloc(P);
  104. }
  105. }
  106. static PJ *
  107. setup(PJ *P) { /* general initialization */
  108. if (P->es) {
  109. if (!(P->en = pj_enfn(P->es)))
  110. E_ERROR_0;
  111. P->ml0 = pj_mlfn(P->phi0, sin(P->phi0), cos(P->phi0), P->en);
  112. P->esp = P->es / (1. - P->es);
  113. P->inv = e_inverse;
  114. P->fwd = e_forward;
  115. } else {
  116. aks0 = P->k0;
  117. aks5 = .5 * aks0;
  118. P->inv = s_inverse;
  119. P->fwd = s_forward;
  120. }
  121. return P;
  122. }
  123. ENTRY1(tmerc, en)
  124. ENDENTRY(setup(P))
  125. ENTRY1(utm, en)
  126. int zone;
  127. if (!P->es) E_ERROR(-34);
  128. P->y0 = pj_param(P->params, "bsouth").i ? 10000000. : 0.;
  129. P->x0 = 500000.;
  130. if (pj_param(P->params, "tzone").i) /* zone input ? */
  131. if ((zone = pj_param(P->params, "izone").i) > 0 && zone <= 60)
  132. --zone;
  133. else
  134. E_ERROR(-35)
  135. else /* nearest central meridian input */
  136. if ((zone = floor((adjlon(P->lam0) + PI) * 30. / PI)) < 0)
  137. zone = 0;
  138. else if (zone >= 60)
  139. zone = 59;
  140. P->lam0 = (zone + .5) * PI / 30. - PI;
  141. P->k0 = 0.9996;
  142. P->phi0 = 0.;
  143. ENDENTRY(setup(P))