Coverage Report

Created: 2026-07-25 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/proj/src/projections/tmerc.cpp
Line
Count
Source
1
/*
2
 *                   Transverse Mercator implementations
3
 *
4
 * In this file two transverse mercator implementations are found. One of Gerald
5
 * Evenden/John Snyder origin and one of Knud Poder/Karsten Engsager origin. The
6
 * former is regarded as "approximate" in the following and the latter is
7
 * "exact". This word choice has been made to distinguish between the two
8
 * algorithms, where the Evenden/Snyder implementation is the faster, less
9
 * accurate implementation and the Poder/Engsager algorithm is a slightly
10
 * slower, but more accurate implementation.
11
 */
12
13
#include <errno.h>
14
#include <math.h>
15
16
#include "proj.h"
17
#include "proj_internal.h"
18
#include <math.h>
19
20
PROJ_HEAD(tmerc, "Transverse Mercator") "\n\tCyl, Sph&Ell\n\tapprox";
21
PROJ_HEAD(etmerc, "Extended Transverse Mercator") "\n\tCyl, Sph";
22
PROJ_HEAD(utm, "Universal Transverse Mercator (UTM)")
23
"\n\tCyl, Ell\n\tzone= south approx";
24
25
namespace { // anonymous namespace
26
27
// Approximate: Evenden/Snyder
28
struct EvendenSnyder {
29
    double esp;
30
    double ml0;
31
    double *en;
32
};
33
34
// More exact: Poder/Engsager
35
struct PoderEngsager {
36
    double Qn;     /* Merid. quad., scaled to the projection */
37
    double Zb;     /* Radius vector in polar coord. systems  */
38
    double cgb[6]; /* Constants for Gauss -> Geo lat */
39
    double cbg[6]; /* Constants for Geo lat -> Gauss */
40
    double utg[6]; /* Constants for transv. merc. -> geo */
41
    double gtu[6]; /* Constants for geo -> transv. merc. */
42
};
43
44
struct tmerc_data {
45
    EvendenSnyder approx;
46
    PoderEngsager exact;
47
};
48
49
} // anonymous namespace
50
51
/* Constants for "approximate" transverse mercator */
52
0
#define EPS10 1.e-10
53
0
#define FC1 1.
54
0
#define FC2 .5
55
0
#define FC3 .16666666666666666666
56
0
#define FC4 .08333333333333333333
57
0
#define FC5 .05
58
0
#define FC6 .03333333333333333333
59
0
#define FC7 .02380952380952380952
60
0
#define FC8 .01785714285714285714
61
62
/* Constant for "exact" transverse mercator */
63
0
#define PROJ_ETMERC_ORDER 6
64
65
/*****************************************************************************/
66
//
67
//                  Approximate Transverse Mercator functions
68
//
69
/*****************************************************************************/
70
71
0
static PJ_XY approx_e_fwd(PJ_LP lp, PJ *P) {
72
0
    PJ_XY xy = {0.0, 0.0};
73
0
    const auto *Q = &(static_cast<struct tmerc_data *>(P->opaque)->approx);
74
0
    double al, als, n, cosphi, sinphi, t;
75
76
    /*
77
     * Fail if our longitude is more than 90 degrees from the
78
     * central meridian since the results are essentially garbage.
79
     * Is error -20 really an appropriate return value?
80
     *
81
     *  http://trac.osgeo.org/proj/ticket/5
82
     */
83
0
    if (lp.lam < -M_HALFPI || lp.lam > M_HALFPI) {
84
0
        xy.x = HUGE_VAL;
85
0
        xy.y = HUGE_VAL;
86
0
        proj_context_errno_set(
87
0
            P->ctx, PROJ_ERR_COORD_TRANSFM_OUTSIDE_PROJECTION_DOMAIN);
88
0
        return xy;
89
0
    }
90
91
0
    sinphi = sin(lp.phi);
92
0
    cosphi = cos(lp.phi);
93
0
    t = fabs(cosphi) > 1e-10 ? sinphi / cosphi : 0.;
94
0
    t *= t;
95
0
    al = cosphi * lp.lam;
96
0
    als = al * al;
97
0
    al /= sqrt(1. - P->es * sinphi * sinphi);
98
0
    n = Q->esp * cosphi * cosphi;
99
0
    xy.x = P->k0 * al *
100
0
           (FC1 + FC3 * als *
101
0
                      (1. - t + n +
102
0
                       FC5 * als *
103
0
                           (5. + t * (t - 18.) + n * (14. - 58. * t) +
104
0
                            FC7 * als * (61. + t * (t * (179. - t) - 479.)))));
105
0
    xy.y =
106
0
        P->k0 *
107
0
        (pj_mlfn(lp.phi, sinphi, cosphi, Q->en) - Q->ml0 +
108
0
         sinphi * al * lp.lam * FC2 *
109
0
             (1. +
110
0
              FC4 * als *
111
0
                  (5. - t + n * (9. + 4. * n) +
112
0
                   FC6 * als *
113
0
                       (61. + t * (t - 58.) + n * (270. - 330 * t) +
114
0
                        FC8 * als * (1385. + t * (t * (543. - t) - 3111.))))));
115
0
    return (xy);
116
0
}
117
118
0
static PJ_XY tmerc_spherical_fwd(PJ_LP lp, PJ *P) {
119
0
    PJ_XY xy = {0.0, 0.0};
120
0
    double b, cosphi;
121
0
    const auto *Q = &(static_cast<struct tmerc_data *>(P->opaque)->approx);
122
123
0
    cosphi = cos(lp.phi);
124
0
    b = cosphi * sin(lp.lam);
125
0
    if (fabs(fabs(b) - 1.) <= EPS10) {
126
0
        proj_errno_set(P, PROJ_ERR_COORD_TRANSFM_OUTSIDE_PROJECTION_DOMAIN);
127
0
        return xy;
128
0
    }
129
130
0
    xy.x = Q->ml0 * log((1. + b) / (1. - b));
131
0
    if (cosphi == 1.0) {
132
0
        if ((lp.lam < -M_HALFPI || lp.lam > M_HALFPI)) {
133
            /* Helps to be able to roundtrip |longitudes| > 90 at lat=0 */
134
            /* We could also map to -M_PI ... */
135
0
            xy.y = M_PI;
136
0
        } else {
137
0
            xy.y = 0;
138
0
        }
139
0
    } else {
140
0
        xy.y = cosphi * cos(lp.lam) / sqrt(1. - b * b);
141
142
0
        b = fabs(xy.y);
143
0
        if (b >= 1.) {
144
0
            if ((b - 1.) > EPS10) {
145
0
                proj_errno_set(
146
0
                    P, PROJ_ERR_COORD_TRANSFM_OUTSIDE_PROJECTION_DOMAIN);
147
0
                return xy;
148
0
            } else
149
0
                xy.y = 0.;
150
0
        } else
151
0
            xy.y = acos(xy.y);
152
0
    }
153
154
0
    if (lp.phi < 0.)
155
0
        xy.y = -xy.y;
156
0
    xy.y = Q->esp * (xy.y - P->phi0);
157
0
    return xy;
158
0
}
159
160
0
static PJ_LP approx_e_inv(PJ_XY xy, PJ *P) {
161
0
    PJ_LP lp = {0.0, 0.0};
162
0
    const auto *Q = &(static_cast<struct tmerc_data *>(P->opaque)->approx);
163
164
0
    lp.phi = pj_inv_mlfn(Q->ml0 + xy.y / P->k0, Q->en);
165
0
    if (fabs(lp.phi) >= M_HALFPI) {
166
0
        lp.phi = xy.y < 0. ? -M_HALFPI : M_HALFPI;
167
0
        lp.lam = 0.;
168
0
    } else {
169
0
        double sinphi = sin(lp.phi), cosphi = cos(lp.phi);
170
0
        double t = fabs(cosphi) > 1e-10 ? sinphi / cosphi : 0.;
171
0
        const double n = Q->esp * cosphi * cosphi;
172
0
        double con = 1. - P->es * sinphi * sinphi;
173
0
        const double d = xy.x * sqrt(con) / P->k0;
174
0
        con *= t;
175
0
        t *= t;
176
0
        const double ds = d * d;
177
0
        lp.phi -=
178
0
            (con * ds / (1. - P->es)) * FC2 *
179
0
            (1. -
180
0
             ds * FC4 *
181
0
                 (5. + t * (3. - 9. * n) + n * (1. - 4 * n) -
182
0
                  ds * FC6 *
183
0
                      (61. + t * (90. - 252. * n + 45. * t) + 46. * n -
184
0
                       ds * FC8 *
185
0
                           (1385. + t * (3633. + t * (4095. + 1575. * t))))));
186
0
        lp.lam = d *
187
0
                 (FC1 -
188
0
                  ds * FC3 *
189
0
                      (1. + 2. * t + n -
190
0
                       ds * FC5 *
191
0
                           (5. + t * (28. + 24. * t + 8. * n) + 6. * n -
192
0
                            ds * FC7 *
193
0
                                (61. + t * (662. + t * (1320. + 720. * t)))))) /
194
0
                 cosphi;
195
0
    }
196
0
    return lp;
197
0
}
198
199
0
static PJ_LP tmerc_spherical_inv(PJ_XY xy, PJ *P) {
200
0
    PJ_LP lp = {0.0, 0.0};
201
0
    double h, g;
202
0
    const auto *Q = &(static_cast<struct tmerc_data *>(P->opaque)->approx);
203
204
0
    h = exp(xy.x / Q->esp);
205
0
    if (h == 0) {
206
0
        proj_errno_set(P, PROJ_ERR_COORD_TRANSFM_OUTSIDE_PROJECTION_DOMAIN);
207
0
        return proj_coord_error().lp;
208
0
    }
209
0
    g = .5 * (h - 1. / h);
210
    /* D, as in equation 8-8 of USGS "Map Projections - A Working Manual" */
211
0
    const double D = P->phi0 + xy.y / Q->esp;
212
0
    h = cos(D);
213
0
    lp.phi = asin(sqrt((1. - h * h) / (1. + g * g)));
214
215
    /* Make sure that phi is on the correct hemisphere when false northing is
216
     * used
217
     */
218
0
    lp.phi = copysign(lp.phi, D);
219
220
0
    lp.lam = (g != 0.0 || h != 0.0) ? atan2(g, h) : 0.;
221
0
    return lp;
222
0
}
223
224
0
static PJ *destructor(PJ *P, int errlev) {
225
0
    if (nullptr == P)
226
0
        return nullptr;
227
228
0
    if (nullptr == P->opaque)
229
0
        return pj_default_destructor(P, errlev);
230
231
0
    free(static_cast<struct tmerc_data *>(P->opaque)->approx.en);
232
0
    return pj_default_destructor(P, errlev);
233
0
}
234
235
0
static PJ *setup_approx(PJ *P) {
236
0
    auto *Q = &(static_cast<struct tmerc_data *>(P->opaque)->approx);
237
238
0
    if (P->es != 0.0) {
239
0
        if (!(Q->en = pj_enfn(P->n)))
240
0
            return pj_default_destructor(P, PROJ_ERR_OTHER /*ENOMEM*/);
241
242
0
        Q->ml0 = pj_mlfn(P->phi0, sin(P->phi0), cos(P->phi0), Q->en);
243
0
        Q->esp = P->es / (1. - P->es);
244
0
    } else {
245
0
        Q->esp = P->k0;
246
0
        Q->ml0 = .5 * Q->esp;
247
0
    }
248
0
    return P;
249
0
}
250
251
/*****************************************************************************/
252
//
253
//                  Exact Transverse Mercator functions
254
//
255
//
256
// The code in this file is largly based upon procedures:
257
//
258
// Written by: Knud Poder and Karsten Engsager
259
//
260
// Based on math from: R.Koenig and K.H. Weise, "Mathematische
261
// Grundlagen der hoeheren Geodaesie und Kartographie,
262
// Springer-Verlag, Berlin/Goettingen" Heidelberg, 1951.
263
//
264
// Modified and used here by permission of Reference Networks
265
// Division, Kort og Matrikelstyrelsen (KMS), Copenhagen, Denmark
266
//
267
/*****************************************************************************/
268
269
/* Complex Clenshaw summation */
270
inline static double clenS(const double *a, int size, double sin_arg_r,
271
                           double cos_arg_r, double sinh_arg_i,
272
0
                           double cosh_arg_i, double *R, double *I) {
273
0
    double r, i, hr, hr1, hr2, hi, hi1, hi2;
274
275
    /* arguments */
276
0
    const double *p = a + size;
277
0
    r = 2 * cos_arg_r * cosh_arg_i;
278
0
    i = -2 * sin_arg_r * sinh_arg_i;
279
280
    /* summation loop */
281
0
    hi1 = hr1 = hi = 0;
282
0
    hr = *--p;
283
0
    for (; a - p;) {
284
0
        hr2 = hr1;
285
0
        hi2 = hi1;
286
0
        hr1 = hr;
287
0
        hi1 = hi;
288
0
        hr = -hr2 + r * hr1 - i * hi1 + *--p;
289
0
        hi = -hi2 + i * hr1 + r * hi1;
290
0
    }
291
292
0
    r = sin_arg_r * cosh_arg_i;
293
0
    i = cos_arg_r * sinh_arg_i;
294
0
    *R = r * hr - i * hi;
295
0
    *I = r * hi + i * hr;
296
0
    return *R;
297
0
}
298
299
/* Ellipsoidal, forward */
300
0
static PJ_XY exact_e_fwd(PJ_LP lp, PJ *P) {
301
0
    PJ_XY xy = {0.0, 0.0};
302
0
    const auto *Q = &(static_cast<struct tmerc_data *>(P->opaque)->exact);
303
304
    /* ell. LAT, LNG -> Gaussian LAT, LNG */
305
0
    double Cn = pj_auxlat_convert(lp.phi, Q->cbg, PROJ_ETMERC_ORDER);
306
    /* Gaussian LAT, LNG -> compl. sph. LAT */
307
0
    const double sin_Cn = sin(Cn);
308
0
    const double cos_Cn = cos(Cn);
309
0
    const double sin_Ce = sin(lp.lam);
310
0
    const double cos_Ce = cos(lp.lam);
311
312
0
    const double cos_Cn_cos_Ce = cos_Cn * cos_Ce;
313
0
    Cn = atan2(sin_Cn, cos_Cn_cos_Ce);
314
315
0
    const double inv_denom_tan_Ce = 1. / hypot(sin_Cn, cos_Cn_cos_Ce);
316
0
    const double tan_Ce = sin_Ce * cos_Cn * inv_denom_tan_Ce;
317
#if 0
318
    // Variant of the above: found not to be measurably faster
319
    const double sin_Ce_cos_Cn = sin_Ce*cos_Cn;
320
    const double denom = sqrt(1 - sin_Ce_cos_Cn * sin_Ce_cos_Cn);
321
    const double tan_Ce = sin_Ce_cos_Cn / denom;
322
#endif
323
324
    /* compl. sph. N, E -> ell. norm. N, E */
325
0
    double Ce = asinh(tan_Ce); /* Replaces: Ce  = log(tan(FORTPI + Ce*0.5)); */
326
327
    /*
328
     *  Non-optimized version:
329
     *  const double sin_arg_r  = sin(2*Cn);
330
     *  const double cos_arg_r  = cos(2*Cn);
331
     *
332
     *  Given:
333
     *      sin(2 * Cn) = 2 sin(Cn) cos(Cn)
334
     *          sin(atan(y)) = y / sqrt(1 + y^2)
335
     *          cos(atan(y)) = 1 / sqrt(1 + y^2)
336
     *      ==> sin(2 * Cn) = 2 tan_Cn / (1 + tan_Cn^2)
337
     *
338
     *      cos(2 * Cn) = 2cos^2(Cn) - 1
339
     *                  = 2 / (1 + tan_Cn^2) - 1
340
     */
341
0
    const double two_inv_denom_tan_Ce = 2 * inv_denom_tan_Ce;
342
0
    const double two_inv_denom_tan_Ce_square =
343
0
        two_inv_denom_tan_Ce * inv_denom_tan_Ce;
344
0
    const double tmp_r = cos_Cn_cos_Ce * two_inv_denom_tan_Ce_square;
345
0
    const double sin_arg_r = sin_Cn * tmp_r;
346
0
    const double cos_arg_r = cos_Cn_cos_Ce * tmp_r - 1;
347
348
    /*
349
     *  Non-optimized version:
350
     *  const double sinh_arg_i = sinh(2*Ce);
351
     *  const double cosh_arg_i = cosh(2*Ce);
352
     *
353
     *  Given
354
     *      sinh(2 * Ce) = 2 sinh(Ce) cosh(Ce)
355
     *          sinh(asinh(y)) = y
356
     *          cosh(asinh(y)) = sqrt(1 + y^2)
357
     *      ==> sinh(2 * Ce) = 2 tan_Ce sqrt(1 + tan_Ce^2)
358
     *
359
     *      cosh(2 * Ce) = 2cosh^2(Ce) - 1
360
     *                   = 2 * (1 + tan_Ce^2) - 1
361
     *
362
     * and 1+tan_Ce^2 = 1 + sin_Ce^2 * cos_Cn^2 / (sin_Cn^2 + cos_Cn^2 *
363
     * cos_Ce^2) = (sin_Cn^2 + cos_Cn^2 * cos_Ce^2 + sin_Ce^2 * cos_Cn^2) /
364
     * (sin_Cn^2 + cos_Cn^2 * cos_Ce^2) = 1. / (sin_Cn^2 + cos_Cn^2 * cos_Ce^2)
365
     * = inv_denom_tan_Ce^2
366
     *
367
     */
368
0
    const double sinh_arg_i = tan_Ce * two_inv_denom_tan_Ce;
369
0
    const double cosh_arg_i = two_inv_denom_tan_Ce_square - 1;
370
371
0
    double dCn, dCe;
372
0
    Cn += clenS(Q->gtu, PROJ_ETMERC_ORDER, sin_arg_r, cos_arg_r, sinh_arg_i,
373
0
                cosh_arg_i, &dCn, &dCe);
374
0
    Ce += dCe;
375
0
    if (fabs(Ce) <= 2.623395162778) {
376
0
        xy.y = Q->Qn * Cn + Q->Zb; /* Northing */
377
0
        xy.x = Q->Qn * Ce;         /* Easting  */
378
0
    } else {
379
0
        proj_errno_set(P, PROJ_ERR_COORD_TRANSFM_OUTSIDE_PROJECTION_DOMAIN);
380
0
        xy.x = xy.y = HUGE_VAL;
381
0
    }
382
0
    return xy;
383
0
}
384
385
/* Ellipsoidal, inverse */
386
0
static PJ_LP exact_e_inv(PJ_XY xy, PJ *P) {
387
0
    PJ_LP lp = {0.0, 0.0};
388
0
    const auto *Q = &(static_cast<struct tmerc_data *>(P->opaque)->exact);
389
390
    /* normalize N, E */
391
0
    double Cn = (xy.y - Q->Zb) / Q->Qn;
392
0
    double Ce = xy.x / Q->Qn;
393
394
0
    if (fabs(Ce) <= 2.623395162778) { /* 150 degrees */
395
        /* norm. N, E -> compl. sph. LAT, LNG */
396
0
        const double sin_arg_r = sin(2 * Cn);
397
0
        const double cos_arg_r = cos(2 * Cn);
398
399
        // const double sinh_arg_i = sinh(2*Ce);
400
        // const double cosh_arg_i = cosh(2*Ce);
401
0
        const double exp_2_Ce = exp(2 * Ce);
402
0
        const double half_inv_exp_2_Ce = 0.5 / exp_2_Ce;
403
0
        const double sinh_arg_i = 0.5 * exp_2_Ce - half_inv_exp_2_Ce;
404
0
        const double cosh_arg_i = 0.5 * exp_2_Ce + half_inv_exp_2_Ce;
405
406
0
        double dCn_ignored, dCe;
407
0
        Cn += clenS(Q->utg, PROJ_ETMERC_ORDER, sin_arg_r, cos_arg_r, sinh_arg_i,
408
0
                    cosh_arg_i, &dCn_ignored, &dCe);
409
0
        Ce += dCe;
410
411
        /* compl. sph. LAT -> Gaussian LAT, LNG */
412
0
        const double sin_Cn = sin(Cn);
413
0
        const double cos_Cn = cos(Cn);
414
415
#if 0
416
        // Non-optimized version:
417
        double sin_Ce, cos_Ce;
418
        Ce = atan (sinh (Ce));  // Replaces: Ce = 2*(atan(exp(Ce)) - FORTPI);
419
        sin_Ce = sin (Ce);
420
        cos_Ce = cos (Ce);
421
        Ce     = atan2 (sin_Ce, cos_Ce*cos_Cn);
422
        Cn     = atan2 (sin_Cn*cos_Ce,  hypot (sin_Ce, cos_Ce*cos_Cn));
423
#else
424
        /*
425
         *      One can divide both member of Ce = atan2(...) by cos_Ce, which
426
         * gives: Ce     = atan2 (tan_Ce, cos_Cn) = atan2(sinh(Ce), cos_Cn)
427
         *
428
         *      and the same for Cn = atan2(...)
429
         *      Cn     = atan2 (sin_Cn, hypot (sin_Ce, cos_Ce*cos_Cn)/cos_Ce)
430
         *             = atan2 (sin_Cn, hypot (sin_Ce/cos_Ce, cos_Cn))
431
         *             = atan2 (sin_Cn, hypot (tan_Ce, cos_Cn))
432
         *             = atan2 (sin_Cn, hypot (sinhCe, cos_Cn))
433
         */
434
0
        const double sinhCe = sinh(Ce);
435
0
        Ce = atan2(sinhCe, cos_Cn);
436
0
        const double modulus_Ce = hypot(sinhCe, cos_Cn),
437
0
                     rr = hypot(sin_Cn, modulus_Ce);
438
0
        Cn = atan2(sin_Cn, modulus_Ce);
439
0
#endif
440
441
        /* Gaussian LAT, LNG -> ell. LAT, LNG */
442
0
        lp.phi = pj_auxlat_convert(Cn, sin_Cn / rr, modulus_Ce / rr, Q->cgb,
443
0
                                   PROJ_ETMERC_ORDER);
444
0
        lp.lam = Ce;
445
0
    } else {
446
0
        proj_errno_set(P, PROJ_ERR_COORD_TRANSFM_OUTSIDE_PROJECTION_DOMAIN);
447
0
        lp.phi = lp.lam = HUGE_VAL;
448
0
    }
449
0
    return lp;
450
0
}
451
452
0
static PJ *setup_exact(PJ *P) {
453
0
    auto *Q = &(static_cast<struct tmerc_data *>(P->opaque)->exact);
454
455
0
    assert(P->es > 0);
456
0
    static_assert(PROJ_ETMERC_ORDER == int(AuxLat::ORDER),
457
0
                  "Inconsistent orders etmerc vs auxorder");
458
    /* third flattening */
459
0
    const double n = P->n;
460
461
    // N.B., Engsager and Poder terminology (simplifying a little here...)
462
    //   geodetic coordinates = geographic latitude
463
    //   Soldner sphere + complex gaussian coordinates = conformal latitude
464
    //   transverse Mercator coordinates = rectifying latitude
465
466
    /* COEF. OF TRIG SERIES GEO <-> GAUSS */
467
    /* cgb := Gaussian -> Geodetic, KW p190 - 191 (61) - (62) */
468
    /* cbg := Geodetic -> Gaussian, KW p186 - 187 (51) - (52) */
469
    /* PROJ_ETMERC_ORDER = 6th degree : Engsager and Poder: ICC2007 */
470
0
    pj_auxlat_coeffs(n, AuxLat::CONFORMAL, AuxLat::GEOGRAPHIC, Q->cgb);
471
0
    pj_auxlat_coeffs(n, AuxLat::GEOGRAPHIC, AuxLat::CONFORMAL, Q->cbg);
472
    /* Constants of the projections */
473
    /* Transverse Mercator (UTM, ITM, etc) */
474
    /* Norm. mer. quad, K&W p.50 (96), p.19 (38b), p.5 (2) */
475
0
    Q->Qn = P->k0 * pj_rectifying_radius(n);
476
    /* coef of trig series */
477
    /* utg := ell. N, E -> sph. N, E,  KW p194 (65) */
478
    /* gtu := sph. N, E -> ell. N, E,  KW p196 (69) */
479
0
    pj_auxlat_coeffs(n, AuxLat::RECTIFYING, AuxLat::CONFORMAL, Q->utg);
480
0
    pj_auxlat_coeffs(n, AuxLat::CONFORMAL, AuxLat::RECTIFYING, Q->gtu);
481
    /* Gaussian latitude value of the origin latitude */
482
0
    const double Z = pj_auxlat_convert(P->phi0, Q->cbg, PROJ_ETMERC_ORDER);
483
484
    /* Origin northing minus true northing at the origin latitude */
485
    /* i.e. true northing = N - P->Zb                         */
486
0
    Q->Zb = -Q->Qn * pj_auxlat_convert(Z, Q->gtu, PROJ_ETMERC_ORDER);
487
488
0
    return P;
489
0
}
490
491
0
static PJ_XY auto_e_fwd(PJ_LP lp, PJ *P) {
492
0
    if (fabs(lp.lam) > 3 * DEG_TO_RAD)
493
0
        return exact_e_fwd(lp, P);
494
0
    else
495
0
        return approx_e_fwd(lp, P);
496
0
}
497
498
0
static PJ_LP auto_e_inv(PJ_XY xy, PJ *P) {
499
    // For k = 1 and long = 3 (from central meridian),
500
    // At lat = 0, we get x ~= 0.052, y = 0
501
    // At lat = 90, we get x = 0, y ~= 1.57
502
    // And the shape of this x=f(y) frontier curve is very very roughly a
503
    // parabola. Hence:
504
0
    if (fabs(xy.x) > 0.053 - 0.022 * xy.y * xy.y)
505
0
        return exact_e_inv(xy, P);
506
0
    else
507
0
        return approx_e_inv(xy, P);
508
0
}
509
510
0
static PJ *setup(PJ *P, TMercAlgo eAlg) {
511
512
0
    struct tmerc_data *Q =
513
0
        static_cast<struct tmerc_data *>(calloc(1, sizeof(struct tmerc_data)));
514
0
    if (nullptr == Q)
515
0
        return pj_default_destructor(P, PROJ_ERR_OTHER /*ENOMEM*/);
516
0
    P->opaque = Q;
517
518
0
    if (P->es == 0)
519
0
        eAlg = TMercAlgo::EVENDEN_SNYDER;
520
521
0
    switch (eAlg) {
522
0
    case TMercAlgo::EVENDEN_SNYDER: {
523
0
        P->destructor = destructor;
524
0
        if (!setup_approx(P))
525
0
            return nullptr;
526
0
        if (P->es == 0) {
527
0
            P->inv = tmerc_spherical_inv;
528
0
            P->fwd = tmerc_spherical_fwd;
529
0
        } else {
530
0
            P->inv = approx_e_inv;
531
0
            P->fwd = approx_e_fwd;
532
0
        }
533
0
        break;
534
0
    }
535
536
0
    case TMercAlgo::PODER_ENGSAGER: {
537
0
        setup_exact(P);
538
0
        P->inv = exact_e_inv;
539
0
        P->fwd = exact_e_fwd;
540
0
        break;
541
0
    }
542
543
0
    case TMercAlgo::AUTO: {
544
0
        P->destructor = destructor;
545
0
        if (!setup_approx(P))
546
0
            return nullptr;
547
0
        setup_exact(P);
548
549
0
        P->inv = auto_e_inv;
550
0
        P->fwd = auto_e_fwd;
551
0
        break;
552
0
    }
553
0
    }
554
0
    return P;
555
0
}
556
557
0
static bool getAlgoFromParams(PJ *P, TMercAlgo &algo) {
558
0
    if (pj_param(P->ctx, P->params, "bapprox").i) {
559
0
        algo = TMercAlgo::EVENDEN_SNYDER;
560
0
        return true;
561
0
    }
562
563
0
    const char *algStr = pj_param(P->ctx, P->params, "salgo").s;
564
0
    if (algStr) {
565
0
        if (strcmp(algStr, "evenden_snyder") == 0) {
566
0
            algo = TMercAlgo::EVENDEN_SNYDER;
567
0
            return true;
568
0
        }
569
0
        if (strcmp(algStr, "poder_engsager") == 0) {
570
0
            algo = TMercAlgo::PODER_ENGSAGER;
571
0
            return true;
572
0
        }
573
0
        if (strcmp(algStr, "auto") == 0) {
574
0
            algo = TMercAlgo::AUTO;
575
            // Don't return so that we can run a later validity check
576
0
        } else {
577
0
            proj_log_error(P, "unknown value for +algo");
578
0
            return false;
579
0
        }
580
0
    } else {
581
0
        pj_load_ini(P->ctx); // if not already done
582
0
        proj_context_errno_set(
583
0
            P->ctx,
584
0
            0); // reset error in case proj.ini couldn't be found
585
0
        algo = P->ctx->defaultTmercAlgo;
586
0
    }
587
588
    // We haven't worked on the criterion on inverse transformation
589
    // when phi0 != 0 or if k0 is not close to 1 or for very oblate
590
    // ellipsoid (es > 0.1 is ~ rf < 200)
591
0
    if (algo == TMercAlgo::AUTO &&
592
0
        (P->es > 0.1 || P->phi0 != 0 || fabs(P->k0 - 1) > 0.01)) {
593
0
        algo = TMercAlgo::PODER_ENGSAGER;
594
0
    }
595
596
0
    return true;
597
0
}
598
599
/*****************************************************************************/
600
//
601
//                                Operation Setups
602
//
603
/*****************************************************************************/
604
605
0
PJ *PJ_PROJECTION(tmerc) {
606
    /* exact transverse mercator only exists in ellipsoidal form, */
607
    /* use approximate version if +a sphere is requested          */
608
609
0
    TMercAlgo algo;
610
0
    if (!getAlgoFromParams(P, algo)) {
611
0
        proj_log_error(P, _("Invalid value for algo"));
612
0
        return pj_default_destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
613
0
    }
614
0
    return setup(P, algo);
615
0
}
616
617
0
PJ *PJ_PROJECTION(etmerc) {
618
0
    if (P->es == 0.0) {
619
0
        proj_log_error(
620
0
            P, _("Invalid value for eccentricity: it should not be zero"));
621
0
        return pj_default_destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
622
0
    }
623
624
0
    return setup(P, TMercAlgo::PODER_ENGSAGER);
625
0
}
626
627
/* UTM uses the Poder/Engsager implementation for the underlying projection */
628
/* UNLESS +approx is set in which case the Evenden/Snyder implementation is
629
 * used. */
630
0
PJ *PJ_PROJECTION(utm) {
631
0
    long zone;
632
0
    if (P->es == 0.0) {
633
0
        proj_log_error(
634
0
            P, _("Invalid value for eccentricity: it should not be zero"));
635
0
        return pj_default_destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
636
0
    }
637
0
    if (P->lam0 < -1000.0 || P->lam0 > 1000.0) {
638
0
        proj_log_error(P, _("Invalid value for lon_0"));
639
0
        return pj_default_destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
640
0
    }
641
642
0
    P->y0 = pj_param(P->ctx, P->params, "bsouth").i ? 10000000. : 0.;
643
0
    P->x0 = 500000.;
644
0
    if (pj_param(P->ctx, P->params, "tzone").i) /* zone input ? */
645
0
    {
646
0
        zone = pj_param(P->ctx, P->params, "izone").i;
647
0
        if (zone > 0 && zone <= 60)
648
0
            --zone;
649
0
        else {
650
0
            proj_log_error(P, _("Invalid value for zone"));
651
0
            return pj_default_destructor(P,
652
0
                                         PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
653
0
        }
654
0
    } else /* nearest central meridian input */
655
0
    {
656
0
        zone = lround((floor((adjlon(P->lam0) + M_PI) * 30. / M_PI)));
657
0
        if (zone < 0)
658
0
            zone = 0;
659
0
        else if (zone >= 60)
660
0
            zone = 59;
661
0
    }
662
0
    P->lam0 = (zone + .5) * M_PI / 30. - M_PI;
663
0
    P->k0 = 0.9996;
664
0
    P->phi0 = 0.;
665
666
0
    TMercAlgo algo;
667
0
    if (!getAlgoFromParams(P, algo)) {
668
0
        proj_log_error(P, _("Invalid value for algo"));
669
0
        return pj_default_destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
670
0
    }
671
0
    return setup(P, algo);
672
0
}