Coverage Report

Created: 2026-07-14 06:21

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