Coverage Report

Created: 2025-07-12 06:32

/src/PROJ/src/conversions/axisswap.cpp
Line
Count
Source (jump to first uncovered line)
1
/***********************************************************************
2
3
        Axis order operation for use with transformation pipelines.
4
5
                Kristian Evers, kreve@sdfe.dk, 2017-10-31
6
7
************************************************************************
8
9
Change the order and sign of 2,3 or 4 axes. Each of the possible four
10
axes are numbered with 1-4, such that the first input axis is 1, the
11
second is 2 and so on. The output ordering is controlled by a list of the
12
input axes re-ordered to the new mapping. Examples:
13
14
Reversing the order of the axes:
15
16
    +proj=axisswap +order=4,3,2,1
17
18
Swapping the first two axes (x and y):
19
20
    +proj=axisswap +order=2,1,3,4
21
22
The direction, or sign, of an axis can be changed by adding a minus in
23
front of the axis-number:
24
25
    +proj=axisswap +order=1,-2,3,4
26
27
It is only necessary to specify the axes that are affected by the swap
28
operation:
29
30
    +proj=axisswap +order=2,1
31
32
************************************************************************
33
* Copyright (c) 2017, Kristian Evers / SDFE
34
*
35
* Permission is hereby granted, free of charge, to any person obtaining a
36
* copy of this software and associated documentation files (the "Software"),
37
* to deal in the Software without restriction, including without limitation
38
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
39
* and/or sell copies of the Software, and to permit persons to whom the
40
* Software is furnished to do so, subject to the following conditions:
41
*
42
* The above copyright notice and this permission notice shall be included
43
* in all copies or substantial portions of the Software.
44
*
45
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
46
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
47
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
48
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
49
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
50
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
51
* DEALINGS IN THE SOFTWARE.
52
*
53
***********************************************************************/
54
55
#include <errno.h>
56
#include <stdlib.h>
57
#include <string.h>
58
59
#include <algorithm>
60
61
#include "proj.h"
62
#include "proj_internal.h"
63
64
PROJ_HEAD(axisswap, "Axis ordering");
65
66
namespace { // anonymous namespace
67
struct pj_axisswap_data {
68
    unsigned int axis[4];
69
    int sign[4];
70
};
71
} // anonymous namespace
72
73
6.53k
static int sign(int x) { return (x > 0) - (x < 0); }
74
75
45.2k
static PJ_XY pj_axisswap_forward_2d(PJ_LP lp, PJ *P) {
76
45.2k
    struct pj_axisswap_data *Q = (struct pj_axisswap_data *)P->opaque;
77
45.2k
    PJ_XY xy;
78
79
45.2k
    double in[2] = {lp.lam, lp.phi};
80
45.2k
    xy.x = in[Q->axis[0]] * Q->sign[0];
81
45.2k
    xy.y = in[Q->axis[1]] * Q->sign[1];
82
45.2k
    return xy;
83
45.2k
}
84
85
0
static PJ_LP pj_axisswap_reverse_2d(PJ_XY xy, PJ *P) {
86
0
    struct pj_axisswap_data *Q = (struct pj_axisswap_data *)P->opaque;
87
0
    unsigned int i;
88
0
    PJ_COORD out, in;
89
90
0
    in.v[0] = xy.x;
91
0
    in.v[1] = xy.y;
92
0
    out = proj_coord_error();
93
94
0
    for (i = 0; i < 2; i++)
95
0
        out.v[Q->axis[i]] = in.v[i] * Q->sign[i];
96
97
0
    return out.lp;
98
0
}
99
100
2.85k
static PJ_XYZ pj_axisswap_forward_3d(PJ_LPZ lpz, PJ *P) {
101
2.85k
    struct pj_axisswap_data *Q = (struct pj_axisswap_data *)P->opaque;
102
2.85k
    unsigned int i;
103
2.85k
    PJ_COORD out, in;
104
105
2.85k
    in.v[0] = lpz.lam;
106
2.85k
    in.v[1] = lpz.phi;
107
2.85k
    in.v[2] = lpz.z;
108
2.85k
    out = proj_coord_error();
109
110
11.4k
    for (i = 0; i < 3; i++)
111
8.56k
        out.v[i] = in.v[Q->axis[i]] * Q->sign[i];
112
113
2.85k
    return out.xyz;
114
2.85k
}
115
116
0
static PJ_LPZ pj_axisswap_reverse_3d(PJ_XYZ xyz, PJ *P) {
117
0
    struct pj_axisswap_data *Q = (struct pj_axisswap_data *)P->opaque;
118
0
    unsigned int i;
119
0
    PJ_COORD in, out;
120
121
0
    out = proj_coord_error();
122
0
    in.v[0] = xyz.x;
123
0
    in.v[1] = xyz.y;
124
0
    in.v[2] = xyz.z;
125
126
0
    for (i = 0; i < 3; i++)
127
0
        out.v[Q->axis[i]] = in.v[i] * Q->sign[i];
128
129
0
    return out.lpz;
130
0
}
131
132
58.4k
static void swap_xy_4d(PJ_COORD &coo, PJ *) {
133
58.4k
    std::swap(coo.xyzt.x, coo.xyzt.y);
134
58.4k
}
135
136
0
static void pj_axisswap_forward_4d(PJ_COORD &coo, PJ *P) {
137
0
    struct pj_axisswap_data *Q = (struct pj_axisswap_data *)P->opaque;
138
0
    unsigned int i;
139
0
    PJ_COORD out;
140
141
0
    for (i = 0; i < 4; i++)
142
0
        out.v[i] = coo.v[Q->axis[i]] * Q->sign[i];
143
0
    coo = out;
144
0
}
145
146
0
static void pj_axisswap_reverse_4d(PJ_COORD &coo, PJ *P) {
147
0
    struct pj_axisswap_data *Q = (struct pj_axisswap_data *)P->opaque;
148
0
    unsigned int i;
149
0
    PJ_COORD out;
150
151
0
    for (i = 0; i < 4; i++)
152
0
        out.v[Q->axis[i]] = coo.v[i] * Q->sign[i];
153
154
0
    coo = out;
155
0
}
156
157
/***********************************************************************/
158
4.36k
PJ *PJ_CONVERSION(axisswap, 0) {
159
    /***********************************************************************/
160
4.36k
    struct pj_axisswap_data *Q = static_cast<struct pj_axisswap_data *>(
161
4.36k
        calloc(1, sizeof(struct pj_axisswap_data)));
162
4.36k
    char *s;
163
4.36k
    unsigned int i, j, n = 0;
164
165
4.36k
    if (nullptr == Q)
166
0
        return pj_default_destructor(P, PROJ_ERR_OTHER /*ENOMEM*/);
167
4.36k
    P->opaque = (void *)Q;
168
169
    /* +order and +axis are mutually exclusive */
170
4.36k
    if (!pj_param_exists(P->params, "order") ==
171
4.36k
        !pj_param_exists(P->params, "axis")) {
172
15
        proj_log_error(P,
173
15
                       _("must provide EITHER 'order' OR 'axis' parameter."));
174
15
        return pj_default_destructor(
175
15
            P, PROJ_ERR_INVALID_OP_MUTUALLY_EXCLUSIVE_ARGS);
176
15
    }
177
178
    /* fill axis list with indices from 4-7 to simplify duplicate search further
179
     * down */
180
21.7k
    for (i = 0; i < 4; i++) {
181
17.4k
        Q->axis[i] = i + 4;
182
17.4k
        Q->sign[i] = 1;
183
17.4k
    }
184
185
    /* if the "order" parameter is used */
186
4.35k
    if (pj_param_exists(P->params, "order")) {
187
        /* read axis order */
188
3.26k
        char *order = pj_param(P->ctx, P->params, "sorder").s;
189
190
        /* check that all characters are valid */
191
16.8k
        for (i = 0; i < strlen(order); i++)
192
13.5k
            if (strchr("1234-,", order[i]) == nullptr) {
193
3
                proj_log_error(P, _("unknown axis '%c'"), order[i]);
194
3
                return pj_default_destructor(
195
3
                    P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
196
3
            }
197
198
        /* read axes numbers and signs */
199
3.26k
        s = order;
200
3.26k
        n = 0;
201
9.79k
        while (*s != '\0' && n < 4) {
202
6.54k
            Q->axis[n] = abs(atoi(s)) - 1;
203
6.54k
            if (Q->axis[n] > 3) {
204
7
                proj_log_error(P, _("invalid axis '%d'"), Q->axis[n]);
205
7
                return pj_default_destructor(
206
7
                    P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
207
7
            }
208
6.53k
            Q->sign[n++] = sign(atoi(s));
209
16.7k
            while (*s != '\0' && *s != ',')
210
10.2k
                s++;
211
6.53k
            if (*s == ',')
212
3.29k
                s++;
213
6.53k
        }
214
3.26k
    }
215
216
    /* if the "axis" parameter is used */
217
4.34k
    if (pj_param_exists(P->params, "axis")) {
218
        /* parse the classic PROJ.4 enu axis specification */
219
4.34k
        for (i = 0; i < 3; i++) {
220
3.25k
            switch (P->axis[i]) {
221
826
            case 'w':
222
826
                Q->sign[i] = -1;
223
826
                Q->axis[i] = 0;
224
826
                break;
225
260
            case 'e':
226
260
                Q->sign[i] = 1;
227
260
                Q->axis[i] = 0;
228
260
                break;
229
832
            case 's':
230
832
                Q->sign[i] = -1;
231
832
                Q->axis[i] = 1;
232
832
                break;
233
252
            case 'n':
234
252
                Q->sign[i] = 1;
235
252
                Q->axis[i] = 1;
236
252
                break;
237
285
            case 'd':
238
285
                Q->sign[i] = -1;
239
285
                Q->axis[i] = 2;
240
285
                break;
241
803
            case 'u':
242
803
                Q->sign[i] = 1;
243
803
                Q->axis[i] = 2;
244
803
                break;
245
0
            default:
246
0
                proj_log_error(P, _("unknown axis '%c'"), P->axis[i]);
247
0
                return pj_default_destructor(
248
0
                    P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
249
3.25k
            }
250
3.25k
        }
251
1.08k
        n = 3;
252
1.08k
    }
253
254
    /* check for duplicate axes */
255
21.6k
    for (i = 0; i < 4; i++)
256
86.6k
        for (j = 0; j < 4; j++) {
257
69.2k
            if (i == j)
258
17.3k
                continue;
259
51.9k
            if (Q->axis[i] == Q->axis[j]) {
260
15
                proj_log_error(P, _("axisswap: duplicate axes specified"));
261
15
                return pj_default_destructor(
262
15
                    P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
263
15
            }
264
51.9k
        }
265
266
    /* only map fwd/inv functions that are possible with the given axis setup */
267
4.32k
    if (n == 4) {
268
5
        P->fwd4d = pj_axisswap_forward_4d;
269
5
        P->inv4d = pj_axisswap_reverse_4d;
270
5
    }
271
4.32k
    if (n == 3 && Q->axis[0] < 3 && Q->axis[1] < 3 && Q->axis[2] < 3) {
272
1.08k
        P->fwd3d = pj_axisswap_forward_3d;
273
1.08k
        P->inv3d = pj_axisswap_reverse_3d;
274
1.08k
    }
275
4.32k
    if (n == 2) {
276
3.23k
        if (Q->axis[0] == 1 && Q->sign[0] == 1 && Q->axis[1] == 0 &&
277
3.23k
            Q->sign[1] == 1) {
278
1.38k
            P->fwd4d = swap_xy_4d;
279
1.38k
            P->inv4d = swap_xy_4d;
280
1.84k
        } else if (Q->axis[0] < 2 && Q->axis[1] < 2) {
281
1.83k
            P->fwd = pj_axisswap_forward_2d;
282
1.83k
            P->inv = pj_axisswap_reverse_2d;
283
1.83k
        }
284
3.23k
    }
285
286
4.32k
    if (P->fwd4d == nullptr && P->fwd3d == nullptr && P->fwd == nullptr) {
287
12
        proj_log_error(P, _("axisswap: bad axis order"));
288
12
        return pj_default_destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
289
12
    }
290
291
4.31k
    if (pj_param(P->ctx, P->params, "tangularunits").i) {
292
0
        P->left = PJ_IO_UNITS_RADIANS;
293
0
        P->right = PJ_IO_UNITS_RADIANS;
294
4.31k
    } else {
295
4.31k
        P->left = PJ_IO_UNITS_WHATEVER;
296
4.31k
        P->right = PJ_IO_UNITS_WHATEVER;
297
4.31k
    }
298
299
    /* Preparation and finalization steps are skipped, since the reason   */
300
    /* d'etre of axisswap is to bring input coordinates in line with the  */
301
    /* the internally expected order (ENU), such that handling of offsets */
302
    /* etc. can be done correctly in a later step of a pipeline */
303
4.31k
    P->skip_fwd_prepare = 1;
304
4.31k
    P->skip_fwd_finalize = 1;
305
4.31k
    P->skip_inv_prepare = 1;
306
4.31k
    P->skip_inv_finalize = 1;
307
308
4.31k
    return P;
309
4.32k
}