Coverage Report

Created: 2025-08-11 09:23

/src/gdal/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
177k
#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
114k
static PJ *destructor(PJ *P, int errlev) {
96
114k
    if (nullptr == P)
97
0
        return nullptr;
98
114k
    if (nullptr == P->opaque)
99
0
        return pj_default_destructor(P, errlev);
100
101
114k
    if (static_cast<struct pj_ob_tran_data *>(P->opaque)->link)
102
104k
        static_cast<struct pj_ob_tran_data *>(P->opaque)->link->destructor(
103
104k
            static_cast<struct pj_ob_tran_data *>(P->opaque)->link, errlev);
104
105
114k
    return pj_default_destructor(P, errlev);
106
114k
}
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
112k
static size_t paralist_params_argc(paralist *params) {
131
112k
    size_t argc = 0;
132
9.20M
    for (; params != nullptr; params = params->next)
133
9.09M
        argc++;
134
112k
    return argc;
135
112k
}
136
137
/* turn paralist into argc/argv style argument list */
138
112k
static ARGS ob_tran_target_params(paralist *params) {
139
112k
    int i = 0;
140
112k
    ARGS args = {0, nullptr};
141
112k
    size_t argc = paralist_params_argc(params);
142
112k
    if (argc < 2)
143
0
        return args;
144
145
    /* all args except the proj=ob_tran */
146
112k
    args.argv = static_cast<char **>(calloc(argc - 1, sizeof(char *)));
147
112k
    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
9.20M
    for (i = 0; params != nullptr; params = params->next) {
152
9.09M
        if (0 == strcmp(params->param, "proj=ob_tran") ||
153
9.09M
            0 == strcmp(params->param, "inv"))
154
211k
            continue;
155
8.88M
        args.argv[i++] = params->param;
156
8.88M
    }
157
112k
    args.argc = i;
158
159
    /* Then convert the o_proj=xxx element to proj=xxx */
160
1.84M
    for (i = 0; i < args.argc; i++) {
161
1.83M
        if (0 != strncmp(args.argv[i], "o_proj=", 7))
162
1.73M
            continue;
163
102k
        args.argv[i] += 2;
164
102k
        if (strcmp(args.argv[i], "proj=ob_tran") == 0) {
165
71
            free(args.argv);
166
71
            args.argc = 0;
167
71
            args.argv = nullptr;
168
71
        }
169
102k
        break;
170
1.83M
    }
171
172
112k
    return args;
173
112k
}
174
175
114k
PJ *PJ_PROJECTION(ob_tran) {
176
114k
    double phip;
177
114k
    ARGS args;
178
114k
    PJ *R; /* projection to rotate */
179
180
114k
    struct pj_ob_tran_data *Q = static_cast<struct pj_ob_tran_data *>(
181
114k
        calloc(1, sizeof(struct pj_ob_tran_data)));
182
114k
    if (nullptr == Q)
183
0
        return destructor(P, PROJ_ERR_OTHER /*ENOMEM*/);
184
185
114k
    P->opaque = Q;
186
114k
    P->destructor = destructor;
187
188
    /* get name of projection to be translated */
189
114k
    if (pj_param(P->ctx, P->params, "so_proj").s == nullptr) {
190
2.04k
        proj_log_error(P, _("Missing parameter: o_proj"));
191
2.04k
        return destructor(P, PROJ_ERR_INVALID_OP_MISSING_ARG);
192
2.04k
    }
193
194
    /* Create the target projection object to rotate */
195
112k
    args = ob_tran_target_params(P->params);
196
    /* avoid endless recursion */
197
112k
    if (args.argv == nullptr) {
198
71
        proj_log_error(P, _("Failed to find projection to be rotated"));
199
71
        return destructor(P, PROJ_ERR_INVALID_OP_MISSING_ARG);
200
71
    }
201
112k
    R = pj_create_argv_internal(P->ctx, args.argc, args.argv);
202
112k
    free(args.argv);
203
204
112k
    if (nullptr == R) {
205
8.22k
        proj_log_error(P, _("Projection to be rotated is unknown"));
206
8.22k
        return destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
207
8.22k
    }
208
209
    // Transfer the used flag from the R object to the P object
210
6.27M
    for (auto p = P->params; p; p = p->next) {
211
6.17M
        if (!p->used) {
212
3.34G
            for (auto r = R->params; r; r = r->next) {
213
3.33G
                if (r->used && strcmp(r->param, p->param) == 0) {
214
262k
                    p->used = 1;
215
262k
                    break;
216
262k
                }
217
3.33G
            }
218
5.75M
        }
219
6.17M
    }
220
221
104k
    Q->link = R;
222
223
104k
    if (pj_param(P->ctx, P->params, "to_alpha").i) {
224
35.2k
        double lamc, phic, alpha;
225
226
35.2k
        lamc = pj_param(P->ctx, P->params, "ro_lon_c").f;
227
35.2k
        phic = pj_param(P->ctx, P->params, "ro_lat_c").f;
228
35.2k
        alpha = pj_param(P->ctx, P->params, "ro_alpha").f;
229
230
35.2k
        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
35.2k
        Q->lamp = lamc + aatan2(-cos(alpha), -sin(alpha) * sin(phic));
237
35.2k
        phip = aasin(P->ctx, cos(phic) * sin(alpha));
238
69.4k
    } else if (pj_param(P->ctx, P->params, "to_lat_p")
239
69.4k
                   .i) { /* specified new pole */
240
55.9k
        Q->lamp = pj_param(P->ctx, P->params, "ro_lon_p").f;
241
55.9k
        phip = pj_param(P->ctx, P->params, "ro_lat_p").f;
242
55.9k
    } else { /* specified new "equator" points */
243
13.4k
        double lam1, lam2, phi1, phi2, con;
244
245
13.4k
        lam1 = pj_param(P->ctx, P->params, "ro_lon_1").f;
246
13.4k
        phi1 = pj_param(P->ctx, P->params, "ro_lat_1").f;
247
13.4k
        lam2 = pj_param(P->ctx, P->params, "ro_lon_2").f;
248
13.4k
        phi2 = pj_param(P->ctx, P->params, "ro_lat_2").f;
249
13.4k
        con = fabs(phi1);
250
251
13.4k
        if (fabs(phi1) > M_HALFPI - TOL) {
252
395
            proj_log_error(
253
395
                P, _("Invalid value for lat_1: |lat_1| should be < 90°"));
254
395
            return destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
255
395
        }
256
13.0k
        if (fabs(phi2) > M_HALFPI - TOL) {
257
153
            proj_log_error(
258
153
                P, _("Invalid value for lat_2: |lat_2| should be < 90°"));
259
153
            return destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
260
153
        }
261
12.9k
        if (fabs(phi1 - phi2) < TOL) {
262
6.71k
            proj_log_error(
263
6.71k
                P, _("Invalid value for lat_1 and lat_2: lat_1 should be "
264
6.71k
                     "different from lat_2"));
265
6.71k
            return destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
266
6.71k
        }
267
6.23k
        if (con < TOL) {
268
549
            proj_log_error(P, _("Invalid value for lat_1: lat_1 should be "
269
549
                                "different from zero"));
270
549
            return destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
271
549
        }
272
273
5.68k
        Q->lamp = atan2(cos(phi1) * sin(phi2) * cos(lam1) -
274
5.68k
                            sin(phi1) * cos(phi2) * cos(lam2),
275
5.68k
                        sin(phi1) * cos(phi2) * sin(lam2) -
276
5.68k
                            cos(phi1) * sin(phi2) * sin(lam1));
277
5.68k
        phip = atan(-cos(Q->lamp - lam1) / tan(phi1));
278
5.68k
    }
279
280
96.8k
    if (fabs(phip) > TOL) { /* oblique */
281
10.9k
        Q->cphip = cos(phip);
282
10.9k
        Q->sphip = sin(phip);
283
10.9k
        P->fwd = Q->link->fwd ? o_forward : nullptr;
284
10.9k
        P->inv = Q->link->inv ? o_inverse : nullptr;
285
85.8k
    } else { /* transverse */
286
85.8k
        P->fwd = Q->link->fwd ? t_forward : nullptr;
287
85.8k
        P->inv = Q->link->inv ? t_inverse : nullptr;
288
85.8k
    }
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
96.8k
    if (Q->link->right == PJ_IO_UNITS_RADIANS)
294
80.2k
        P->right = PJ_IO_UNITS_WHATEVER;
295
296
96.8k
    return P;
297
104k
}
298
299
#undef TOL