Coverage Report

Created: 2025-06-13 06:29

/src/proj/src/projections/ob_tran.cpp
Line
Count
Source (jump to first uncovered line)
1
2
#include <errno.h>
3
#include <math.h>
4
#include <stddef.h>
5
#include <string.h>
6
7
#include "proj.h"
8
#include "proj_internal.h"
9
10
namespace { // anonymous namespace
11
struct pj_ob_tran_data {
12
    struct PJconsts *link;
13
    double lamp;
14
    double cphip, sphip;
15
};
16
} // anonymous namespace
17
18
PROJ_HEAD(ob_tran, "General Oblique Transformation")
19
"\n\tMisc Sph"
20
    "\n\to_proj= plus parameters for projection"
21
    "\n\to_lat_p= o_lon_p= (new pole) or"
22
    "\n\to_alpha= o_lon_c= o_lat_c= or"
23
    "\n\to_lon_1= o_lat_1= o_lon_2= o_lat_2=";
24
25
818
#define TOL 1e-10
26
27
0
static PJ_XY o_forward(PJ_LP lp, PJ *P) { /* spheroid */
28
0
    struct pj_ob_tran_data *Q =
29
0
        static_cast<struct pj_ob_tran_data *>(P->opaque);
30
0
    double coslam, sinphi, cosphi;
31
32
0
    coslam = cos(lp.lam);
33
0
    sinphi = sin(lp.phi);
34
0
    cosphi = cos(lp.phi);
35
    /* Formula (5-8b) of Snyder's "Map projections: a working manual" */
36
0
    lp.lam = adjlon(aatan2(cosphi * sin(lp.lam),
37
0
                           Q->sphip * cosphi * coslam + Q->cphip * sinphi) +
38
0
                    Q->lamp);
39
    /* Formula (5-7) */
40
0
    lp.phi = aasin(P->ctx, Q->sphip * sinphi - Q->cphip * cosphi * coslam);
41
42
0
    return Q->link->fwd(lp, Q->link);
43
0
}
44
45
0
static PJ_XY t_forward(PJ_LP lp, PJ *P) { /* spheroid */
46
0
    struct pj_ob_tran_data *Q =
47
0
        static_cast<struct pj_ob_tran_data *>(P->opaque);
48
0
    double cosphi, coslam;
49
50
0
    cosphi = cos(lp.phi);
51
0
    coslam = cos(lp.lam);
52
0
    lp.lam = adjlon(aatan2(cosphi * sin(lp.lam), sin(lp.phi)) + Q->lamp);
53
0
    lp.phi = aasin(P->ctx, -cosphi * coslam);
54
55
0
    return Q->link->fwd(lp, Q->link);
56
0
}
57
58
0
static PJ_LP o_inverse(PJ_XY xy, PJ *P) { /* spheroid */
59
60
0
    struct pj_ob_tran_data *Q =
61
0
        static_cast<struct pj_ob_tran_data *>(P->opaque);
62
0
    double coslam, sinphi, cosphi;
63
64
0
    PJ_LP lp = Q->link->inv(xy, Q->link);
65
0
    if (lp.lam != HUGE_VAL) {
66
0
        lp.lam -= Q->lamp;
67
0
        coslam = cos(lp.lam);
68
0
        sinphi = sin(lp.phi);
69
0
        cosphi = cos(lp.phi);
70
        /* Formula (5-9) */
71
0
        lp.phi = aasin(P->ctx, Q->sphip * sinphi + Q->cphip * cosphi * coslam);
72
        /* Formula (5-10b) */
73
0
        lp.lam = aatan2(cosphi * sin(lp.lam),
74
0
                        Q->sphip * cosphi * coslam - Q->cphip * sinphi);
75
0
    }
76
0
    return lp;
77
0
}
78
79
0
static PJ_LP t_inverse(PJ_XY xy, PJ *P) { /* spheroid */
80
81
0
    struct pj_ob_tran_data *Q =
82
0
        static_cast<struct pj_ob_tran_data *>(P->opaque);
83
0
    double cosphi, t;
84
85
0
    PJ_LP lp = Q->link->inv(xy, Q->link);
86
0
    if (lp.lam != HUGE_VAL) {
87
0
        cosphi = cos(lp.phi);
88
0
        t = lp.lam - Q->lamp;
89
0
        lp.lam = aatan2(cosphi * sin(t), -sin(lp.phi));
90
0
        lp.phi = aasin(P->ctx, cosphi * cos(t));
91
0
    }
92
0
    return lp;
93
0
}
94
95
447
static PJ *destructor(PJ *P, int errlev) {
96
447
    if (nullptr == P)
97
0
        return nullptr;
98
447
    if (nullptr == P->opaque)
99
0
        return pj_default_destructor(P, errlev);
100
101
447
    if (static_cast<struct pj_ob_tran_data *>(P->opaque)->link)
102
400
        static_cast<struct pj_ob_tran_data *>(P->opaque)->link->destructor(
103
400
            static_cast<struct pj_ob_tran_data *>(P->opaque)->link, errlev);
104
105
447
    return pj_default_destructor(P, errlev);
106
447
}
107
108
/***********************************************************************
109
110
These functions are modified versions of the functions "argc_params"
111
and "argv_params" from PJ_pipeline.c
112
113
Basically, they do the somewhat backwards stunt of turning the paralist
114
representation of the +args back into the original +argv, +argc
115
representation accepted by pj_init_ctx().
116
117
This, however, also begs the question of whether we really need the
118
paralist linked list representation, or if we could do with a simpler
119
null-terminated argv style array? This would simplify some code, and
120
keep memory allocations more localized.
121
122
***********************************************************************/
123
124
typedef struct {
125
    int argc;
126
    char **argv;
127
} ARGS;
128
129
/* count the number of args in the linked list <params> */
130
441
static size_t paralist_params_argc(paralist *params) {
131
441
    size_t argc = 0;
132
13.4k
    for (; params != nullptr; params = params->next)
133
12.9k
        argc++;
134
441
    return argc;
135
441
}
136
137
/* turn paralist into argc/argv style argument list */
138
441
static ARGS ob_tran_target_params(paralist *params) {
139
441
    int i = 0;
140
441
    ARGS args = {0, nullptr};
141
441
    size_t argc = paralist_params_argc(params);
142
441
    if (argc < 2)
143
0
        return args;
144
145
    /* all args except the proj=ob_tran */
146
441
    args.argv = static_cast<char **>(calloc(argc - 1, sizeof(char *)));
147
441
    if (nullptr == args.argv)
148
0
        return args;
149
150
    /* Copy all args *except* the proj=ob_tran or inv arg to the argv array */
151
13.4k
    for (i = 0; params != nullptr; params = params->next) {
152
12.9k
        if (0 == strcmp(params->param, "proj=ob_tran") ||
153
12.9k
            0 == strcmp(params->param, "inv"))
154
1.06k
            continue;
155
11.9k
        args.argv[i++] = params->param;
156
11.9k
    }
157
441
    args.argc = i;
158
159
    /* Then convert the o_proj=xxx element to proj=xxx */
160
6.36k
    for (i = 0; i < args.argc; i++) {
161
6.26k
        if (0 != strncmp(args.argv[i], "o_proj=", 7))
162
5.92k
            continue;
163
342
        args.argv[i] += 2;
164
342
        if (strcmp(args.argv[i], "proj=ob_tran") == 0) {
165
0
            free(args.argv);
166
0
            args.argc = 0;
167
0
            args.argv = nullptr;
168
0
        }
169
342
        break;
170
6.26k
    }
171
172
441
    return args;
173
441
}
174
175
447
PJ *PJ_PROJECTION(ob_tran) {
176
447
    double phip;
177
447
    ARGS args;
178
447
    PJ *R; /* projection to rotate */
179
180
447
    struct pj_ob_tran_data *Q = static_cast<struct pj_ob_tran_data *>(
181
447
        calloc(1, sizeof(struct pj_ob_tran_data)));
182
447
    if (nullptr == Q)
183
0
        return destructor(P, PROJ_ERR_OTHER /*ENOMEM*/);
184
185
447
    P->opaque = Q;
186
447
    P->destructor = destructor;
187
188
    /* get name of projection to be translated */
189
447
    if (pj_param(P->ctx, P->params, "so_proj").s == nullptr) {
190
6
        proj_log_error(P, _("Missing parameter: o_proj"));
191
6
        return destructor(P, PROJ_ERR_INVALID_OP_MISSING_ARG);
192
6
    }
193
194
    /* Create the target projection object to rotate */
195
441
    args = ob_tran_target_params(P->params);
196
    /* avoid endless recursion */
197
441
    if (args.argv == nullptr) {
198
0
        proj_log_error(P, _("Failed to find projection to be rotated"));
199
0
        return destructor(P, PROJ_ERR_INVALID_OP_MISSING_ARG);
200
0
    }
201
441
    R = pj_create_argv_internal(P->ctx, args.argc, args.argv);
202
441
    free(args.argv);
203
204
441
    if (nullptr == R) {
205
41
        proj_log_error(P, _("Projection to be rotated is unknown"));
206
41
        return destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
207
41
    }
208
209
    // Transfer the used flag from the R object to the P object
210
11.7k
    for (auto p = P->params; p; p = p->next) {
211
11.3k
        if (!p->used) {
212
438k
            for (auto r = R->params; r; r = r->next) {
213
431k
                if (r->used && strcmp(r->param, p->param) == 0) {
214
1.21k
                    p->used = 1;
215
1.21k
                    break;
216
1.21k
                }
217
431k
            }
218
8.73k
        }
219
11.3k
    }
220
221
400
    Q->link = R;
222
223
400
    if (pj_param(P->ctx, P->params, "to_alpha").i) {
224
382
        double lamc, phic, alpha;
225
226
382
        lamc = pj_param(P->ctx, P->params, "ro_lon_c").f;
227
382
        phic = pj_param(P->ctx, P->params, "ro_lat_c").f;
228
382
        alpha = pj_param(P->ctx, P->params, "ro_alpha").f;
229
230
382
        if (fabs(fabs(phic) - M_HALFPI) <= TOL) {
231
0
            proj_log_error(
232
0
                P, _("Invalid value for lat_c: |lat_c| should be < 90°"));
233
0
            return destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
234
0
        }
235
236
382
        Q->lamp = lamc + aatan2(-cos(alpha), -sin(alpha) * sin(phic));
237
382
        phip = aasin(P->ctx, cos(phic) * sin(alpha));
238
382
    } else if (pj_param(P->ctx, P->params, "to_lat_p")
239
18
                   .i) { /* specified new pole */
240
0
        Q->lamp = pj_param(P->ctx, P->params, "ro_lon_p").f;
241
0
        phip = pj_param(P->ctx, P->params, "ro_lat_p").f;
242
18
    } else { /* specified new "equator" points */
243
18
        double lam1, lam2, phi1, phi2, con;
244
245
18
        lam1 = pj_param(P->ctx, P->params, "ro_lon_1").f;
246
18
        phi1 = pj_param(P->ctx, P->params, "ro_lat_1").f;
247
18
        lam2 = pj_param(P->ctx, P->params, "ro_lon_2").f;
248
18
        phi2 = pj_param(P->ctx, P->params, "ro_lat_2").f;
249
18
        con = fabs(phi1);
250
251
18
        if (fabs(phi1) > M_HALFPI - TOL) {
252
0
            proj_log_error(
253
0
                P, _("Invalid value for lat_1: |lat_1| should be < 90°"));
254
0
            return destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
255
0
        }
256
18
        if (fabs(phi2) > M_HALFPI - TOL) {
257
0
            proj_log_error(
258
0
                P, _("Invalid value for lat_2: |lat_2| should be < 90°"));
259
0
            return destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
260
0
        }
261
18
        if (fabs(phi1 - phi2) < TOL) {
262
18
            proj_log_error(
263
18
                P, _("Invalid value for lat_1 and lat_2: lat_1 should be "
264
18
                     "different from lat_2"));
265
18
            return destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
266
18
        }
267
0
        if (con < TOL) {
268
0
            proj_log_error(P, _("Invalid value for lat_1: lat_1 should be "
269
0
                                "different from zero"));
270
0
            return destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
271
0
        }
272
273
0
        Q->lamp = atan2(cos(phi1) * sin(phi2) * cos(lam1) -
274
0
                            sin(phi1) * cos(phi2) * cos(lam2),
275
0
                        sin(phi1) * cos(phi2) * sin(lam2) -
276
0
                            cos(phi1) * sin(phi2) * sin(lam1));
277
0
        phip = atan(-cos(Q->lamp - lam1) / tan(phi1));
278
0
    }
279
280
382
    if (fabs(phip) > TOL) { /* oblique */
281
161
        Q->cphip = cos(phip);
282
161
        Q->sphip = sin(phip);
283
161
        P->fwd = Q->link->fwd ? o_forward : nullptr;
284
161
        P->inv = Q->link->inv ? o_inverse : nullptr;
285
221
    } else { /* transverse */
286
221
        P->fwd = Q->link->fwd ? t_forward : nullptr;
287
221
        P->inv = Q->link->inv ? t_inverse : nullptr;
288
221
    }
289
290
    /* Support some rather speculative test cases, where the rotated projection
291
     */
292
    /* is actually latlong. We do not want scaling in that case... */
293
382
    if (Q->link->right == PJ_IO_UNITS_RADIANS)
294
63
        P->right = PJ_IO_UNITS_WHATEVER;
295
296
382
    return P;
297
400
}
298
299
#undef TOL