Coverage Report

Created: 2025-06-13 06:29

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