1 #define PJ_LIB__
2 #include    <projects.h>
3 
4 PROJ_HEAD(tmerc, "Transverse Mercator") "\n\tCyl, Sph&Ell";
5 
6 
7 struct pj_opaque {
8     double  esp; \
9     double  ml0; \
10     double  *en;
11 };
12 
13 #define EPS10   1.e-10
14 #define FC1 1.
15 #define FC2 .5
16 #define FC3 .16666666666666666666
17 #define FC4 .08333333333333333333
18 #define FC5 .05
19 #define FC6 .03333333333333333333
20 #define FC7 .02380952380952380952
21 #define FC8 .01785714285714285714
22 
23 
e_forward(LP lp,PJ * P)24 static XY e_forward (LP lp, PJ *P) {          /* Ellipsoidal, forward */
25     XY xy = {0.0, 0.0};
26     struct pj_opaque *Q = P->opaque;
27     double al, als, n, cosphi, sinphi, t;
28 
29     /*
30      * Fail if our longitude is more than 90 degrees from the
31      * central meridian since the results are essentially garbage.
32      * Is error -20 really an appropriate return value?
33      *
34      *  http://trac.osgeo.org/proj/ticket/5
35      */
36     if( lp.lam < -M_HALFPI || lp.lam > M_HALFPI ) {
37         xy.x = HUGE_VAL;
38         xy.y = HUGE_VAL;
39         pj_ctx_set_errno( P->ctx, -14 );
40         return xy;
41     }
42 
43     sinphi = sin (lp.phi);
44     cosphi = cos (lp.phi);
45     t = fabs (cosphi) > 1e-10 ? sinphi/cosphi : 0.;
46     t *= t;
47     al = cosphi * lp.lam;
48     als = al * al;
49     al /= sqrt (1. - P->es * sinphi * sinphi);
50     n = Q->esp * cosphi * cosphi;
51     xy.x = P->k0 * al * (FC1 +
52         FC3 * als * (1. - t + n +
53         FC5 * als * (5. + t * (t - 18.) + n * (14. - 58. * t)
54         + FC7 * als * (61. + t * ( t * (179. - t) - 479. ) )
55         )));
56     xy.y = P->k0 * (pj_mlfn(lp.phi, sinphi, cosphi, Q->en) - Q->ml0 +
57         sinphi * al * lp.lam * FC2 * ( 1. +
58         FC4 * als * (5. - t + n * (9. + 4. * n) +
59         FC6 * als * (61. + t * (t - 58.) + n * (270. - 330 * t)
60         + FC8 * als * (1385. + t * ( t * (543. - t) - 3111.) )
61         ))));
62     return (xy);
63 }
64 
65 
s_forward(LP lp,PJ * P)66 static XY s_forward (LP lp, PJ *P) {           /* Spheroidal, forward */
67     XY xy = {0.0,0.0};
68     double b, cosphi;
69 
70     /*
71      * Fail if our longitude is more than 90 degrees from the
72      * central meridian since the results are essentially garbage.
73      * Is error -20 really an appropriate return value?
74      *
75      *  http://trac.osgeo.org/proj/ticket/5
76      */
77     if( lp.lam < -M_HALFPI || lp.lam > M_HALFPI ) {
78         xy.x = HUGE_VAL;
79         xy.y = HUGE_VAL;
80         pj_ctx_set_errno( P->ctx, -14 );
81         return xy;
82     }
83 
84     cosphi = cos(lp.phi);
85     b = cosphi * sin (lp.lam);
86     if (fabs (fabs (b) - 1.) <= EPS10)
87         F_ERROR;
88 
89     xy.x = P->opaque->ml0 * log ((1. + b) / (1. - b));
90     xy.y = cosphi * cos (lp.lam) / sqrt (1. - b * b);
91 
92     b = fabs ( xy.y );
93     if (b >= 1.) {
94         if ((b - 1.) > EPS10)
95             F_ERROR
96         else xy.y = 0.;
97     } else
98         xy.y = acos (xy.y);
99 
100     if (lp.phi < 0.)
101         xy.y = -xy.y;
102     xy.y = P->opaque->esp * (xy.y - P->phi0);
103     return xy;
104 }
105 
106 
e_inverse(XY xy,PJ * P)107 static LP e_inverse (XY xy, PJ *P) {          /* Ellipsoidal, inverse */
108     LP lp = {0.0,0.0};
109     struct pj_opaque *Q = P->opaque;
110     double n, con, cosphi, d, ds, sinphi, t;
111 
112     lp.phi = pj_inv_mlfn(P->ctx, Q->ml0 + xy.y / P->k0, P->es, Q->en);
113     if (fabs(lp.phi) >= M_HALFPI) {
114         lp.phi = xy.y < 0. ? -M_HALFPI : M_HALFPI;
115         lp.lam = 0.;
116     } else {
117         sinphi = sin(lp.phi);
118         cosphi = cos(lp.phi);
119         t = fabs (cosphi) > 1e-10 ? sinphi/cosphi : 0.;
120         n = Q->esp * cosphi * cosphi;
121         d = xy.x * sqrt (con = 1. - P->es * sinphi * sinphi) / P->k0;
122         con *= t;
123         t *= t;
124         ds = d * d;
125         lp.phi -= (con * ds / (1.-P->es)) * FC2 * (1. -
126             ds * FC4 * (5. + t * (3. - 9. *  n) + n * (1. - 4 * n) -
127             ds * FC6 * (61. + t * (90. - 252. * n +
128                 45. * t) + 46. * n
129            - ds * FC8 * (1385. + t * (3633. + t * (4095. + 1574. * t)) )
130             )));
131         lp.lam = d*(FC1 -
132             ds*FC3*( 1. + 2.*t + n -
133             ds*FC5*(5. + t*(28. + 24.*t + 8.*n) + 6.*n
134            - ds * FC7 * (61. + t * (662. + t * (1320. + 720. * t)) )
135         ))) / cosphi;
136     }
137     return lp;
138 }
139 
140 
s_inverse(XY xy,PJ * P)141 static LP s_inverse (XY xy, PJ *P) {           /* Spheroidal, inverse */
142     LP lp = {0.0, 0.0};
143     double h, g;
144 
145     h = exp(xy.x / P->opaque->esp);
146     g = .5 * (h - 1. / h);
147     h = cos (P->phi0 + xy.y / P->opaque->esp);
148     lp.phi = asin(sqrt((1. - h * h) / (1. + g * g)));
149     if (xy.y < 0.) lp.phi = -lp.phi;
150     lp.lam = (g || h) ? atan2 (g, h) : 0.;
151     return lp;
152 }
153 
154 
freeup_new(PJ * P)155 static void *freeup_new (PJ *P) {                       /* Destructor */
156     if (0==P)
157         return 0;
158     if (0==P->opaque)
159         return pj_dealloc (P);
160     pj_dealloc (P->opaque->en);
161     pj_dealloc (P->opaque);
162     return pj_dealloc(P);
163 }
164 
freeup(PJ * P)165 static void freeup (PJ *P) {
166     freeup_new (P);
167     return;
168 }
169 
setup(PJ * P)170 static PJ *setup(PJ *P) {                   /* general initialization */
171     struct pj_opaque *Q = P->opaque;
172     if (P->es) {
173         if (!(Q->en = pj_enfn(P->es)))
174             E_ERROR_0;
175         Q->ml0 = pj_mlfn(P->phi0, sin(P->phi0), cos(P->phi0), Q->en);
176         Q->esp = P->es / (1. - P->es);
177         P->inv = e_inverse;
178         P->fwd = e_forward;
179     } else {
180         Q->esp = P->k0;
181         Q->ml0 = .5 * Q->esp;
182         P->inv = s_inverse;
183         P->fwd = s_forward;
184     }
185     return P;
186 }
187 
188 
PROJECTION(tmerc)189 PJ *PROJECTION(tmerc) {
190     struct pj_opaque *Q = pj_calloc (1, sizeof (struct pj_opaque));
191     if (0==Q)
192         return freeup_new (P);
193     P->opaque = Q;
194     return setup(P);
195 }
196 
197 
198 #ifndef PJ_SELFTEST
pj_tmerc_selftest(void)199 int pj_tmerc_selftest (void) {return 0;}
200 #else
pj_tmerc_selftest(void)201 int pj_tmerc_selftest (void) {
202     double tolerance_lp = 1e-10;
203     double tolerance_xy = 1e-7;
204 
205     char e_args[] = {"+proj=tmerc   +ellps=GRS80  +lat_1=0.5 +lat_2=2 +n=0.5"};
206     char s_args[] = {"+proj=tmerc   +a=6400000    +lat_1=0.5 +lat_2=2 +n=0.5"};
207 
208     LP fwd_in[] = {
209         { 2, 1},
210         { 2,-1},
211         {-2, 1},
212         {-2,-1}
213     };
214 
215     XY e_fwd_expect[] = {
216         { 222650.79679577847,  110642.22941192707},
217         { 222650.79679577847, -110642.22941192707},
218         {-222650.79679577847,  110642.22941192707},
219         {-222650.79679577847, -110642.22941192707},
220     };
221 
222     XY s_fwd_expect[] = {
223         { 223413.46640632232,  111769.14504059685},
224         { 223413.46640632232, -111769.14504059685},
225         {-223413.46640632208,  111769.14504059685},
226         {-223413.46640632208, -111769.14504059685},
227     };
228 
229     XY inv_in[] = {
230         { 200, 100},
231         { 200,-100},
232         {-200, 100},
233         {-200,-100}
234     };
235 
236     LP e_inv_expect[] = {
237         { 0.0017966305681649396,  0.00090436947663183841},
238         { 0.0017966305681649396, -0.00090436947663183841},
239         {-0.0017966305681649396,  0.00090436947663183841},
240         {-0.0017966305681649396, -0.00090436947663183841},
241     };
242 
243     LP s_inv_expect[] = {
244         { 0.0017904931097048034,  0.00089524670602767842},
245         { 0.0017904931097048034, -0.00089524670602767842},
246         {-0.001790493109714345,   0.00089524670602767842},
247         {-0.001790493109714345,  -0.00089524670602767842},
248     };
249 
250     return pj_generic_selftest (e_args, s_args, tolerance_xy, tolerance_lp, 4, 4, fwd_in, e_fwd_expect, s_fwd_expect, inv_in, e_inv_expect, s_inv_expect);
251 }
252 #endif
253