Coverage Report

Created: 2025-10-10 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PROJ/src/projections/nzmg.cpp
Line
Count
Source
1
/******************************************************************************
2
 * Project:  PROJ.4
3
 * Purpose:  Implementation of the nzmg (New Zealand Map Grid) projection.
4
 *           Very loosely based upon DMA code by Bradford W. Drew
5
 * Author:   Gerald Evenden
6
 *
7
 ******************************************************************************
8
 * Copyright (c) 1995, Gerald Evenden
9
 *
10
 * Permission is hereby granted, free of charge, to any person obtaining a
11
 * copy of this software and associated documentation files (the "Software"),
12
 * to deal in the Software without restriction, including without limitation
13
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
14
 * and/or sell copies of the Software, and to permit persons to whom the
15
 * Software is furnished to do so, subject to the following conditions:
16
 *
17
 * The above copyright notice and this permission notice shall be included
18
 * in all copies or substantial portions of the Software.
19
 *
20
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
21
 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
23
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
25
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
26
 * DEALINGS IN THE SOFTWARE.
27
 *****************************************************************************/
28
29
#include <math.h>
30
31
#include "proj.h"
32
#include "proj_internal.h"
33
34
PROJ_HEAD(nzmg, "New Zealand Map Grid") "\n\tfixed Earth";
35
36
0
#define EPSLN 1e-10
37
0
#define SEC5_TO_RAD 0.4848136811095359935899141023
38
0
#define RAD_TO_SEC5 2.062648062470963551564733573
39
40
static const COMPLEX bf[] = {
41
    {.7557853228, 0.0},         {.249204646, 0.003371507},
42
    {-.001541739, 0.041058560}, {-.10162907, 0.01727609},
43
    {-.26623489, -0.36249218},  {-.6870983, -1.1651967}};
44
45
0
#define Nbf 5
46
0
#define Ntpsi 9
47
0
#define Ntphi 8
48
49
0
static PJ_XY nzmg_e_forward(PJ_LP lp, PJ *P) { /* Ellipsoidal, forward */
50
0
    PJ_XY xy = {0.0, 0.0};
51
0
    COMPLEX p;
52
0
    const double *C;
53
0
    int i;
54
55
0
    static const double tpsi[] = {
56
0
        .6399175073, -.1358797613, .063294409, -.02526853, .0117879,
57
0
        -.0055161,   .0026906,     -.001333,   .00067,     -.00034};
58
59
0
    lp.phi = (lp.phi - P->phi0) * RAD_TO_SEC5;
60
0
    i = Ntpsi;
61
0
    C = tpsi + i;
62
0
    for (p.r = *C; i; --i) {
63
0
        --C;
64
0
        p.r = *C + lp.phi * p.r;
65
0
    }
66
0
    p.r *= lp.phi;
67
0
    p.i = lp.lam;
68
0
    p = pj_zpoly1(p, bf, Nbf);
69
0
    xy.x = p.i;
70
0
    xy.y = p.r;
71
72
0
    return xy;
73
0
}
74
75
0
static PJ_LP nzmg_e_inverse(PJ_XY xy, PJ *P) { /* Ellipsoidal, inverse */
76
0
    PJ_LP lp = {0.0, 0.0};
77
0
    int nn, i;
78
0
    COMPLEX p, f, fp, dp;
79
0
    double den;
80
0
    const double *C;
81
82
0
    static const double tphi[] = {1.5627014243, .5185406398, -.03333098,
83
0
                                  -.1052906,    -.0368594,   .007317,
84
0
                                  .01220,       .00394,      -.0013};
85
86
0
    p.r = xy.y;
87
0
    p.i = xy.x;
88
0
    for (nn = 20; nn; --nn) {
89
0
        f = pj_zpolyd1(p, bf, Nbf, &fp);
90
0
        f.r -= xy.y;
91
0
        f.i -= xy.x;
92
0
        den = fp.r * fp.r + fp.i * fp.i;
93
0
        dp.r = -(f.r * fp.r + f.i * fp.i) / den;
94
0
        dp.i = -(f.i * fp.r - f.r * fp.i) / den;
95
0
        p.r += dp.r;
96
0
        p.i += dp.i;
97
0
        if ((fabs(dp.r) + fabs(dp.i)) <= EPSLN)
98
0
            break;
99
0
    }
100
0
    if (nn) {
101
0
        lp.lam = p.i;
102
0
        i = Ntphi;
103
0
        C = tphi + i;
104
0
        for (lp.phi = *C; i; --i) {
105
0
            --C;
106
0
            lp.phi = *C + p.r * lp.phi;
107
0
        }
108
0
        lp.phi = P->phi0 + p.r * lp.phi * SEC5_TO_RAD;
109
0
    } else
110
0
        lp.lam = lp.phi = HUGE_VAL;
111
112
0
    return lp;
113
0
}
114
115
18
PJ *PJ_PROJECTION(nzmg) {
116
    /* force to International major axis */
117
18
    P->ra = 1. / (P->a = 6378388.0);
118
18
    P->lam0 = DEG_TO_RAD * 173.;
119
18
    P->phi0 = DEG_TO_RAD * -41.;
120
18
    P->x0 = 2510000.;
121
18
    P->y0 = 6023150.;
122
123
18
    P->inv = nzmg_e_inverse;
124
18
    P->fwd = nzmg_e_forward;
125
126
18
    return P;
127
18
}
128
129
#undef EPSLN
130
#undef SEC5_TO_RAD
131
#undef RAD_TO_SEC5
132
#undef Nbf
133
#undef Ntpsi
134
#undef Ntphi