Coverage Report

Created: 2026-12-01 07:24

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PROJ/src/conversions/axisswap.cpp
Line
Count
Source
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
17.8k
static int sign(int x) { return (x > 0) - (x < 0); }
74
75
66.0k
static PJ_XY pj_axisswap_forward_2d(PJ_LP lp, PJ *P) {
76
66.0k
    struct pj_axisswap_data *Q = (struct pj_axisswap_data *)P->opaque;
77
66.0k
    PJ_XY xy;
78
79
66.0k
    double in[2] = {lp.lam, lp.phi};
80
66.0k
    xy.x = in[Q->axis[0]] * Q->sign[0];
81
66.0k
    xy.y = in[Q->axis[1]] * Q->sign[1];
82
66.0k
    return xy;
83
66.0k
}
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
26.7k
static PJ_XYZ pj_axisswap_forward_3d(PJ_LPZ lpz, PJ *P) {
101
26.7k
    struct pj_axisswap_data *Q = (struct pj_axisswap_data *)P->opaque;
102
26.7k
    unsigned int i;
103
26.7k
    PJ_COORD out, in;
104
105
26.7k
    in.v[0] = lpz.lam;
106
26.7k
    in.v[1] = lpz.phi;
107
26.7k
    in.v[2] = lpz.z;
108
26.7k
    out = proj_coord_error();
109
110
106k
    for (i = 0; i < 3; i++)
111
80.1k
        out.v[i] = in.v[Q->axis[i]] * Q->sign[i];
112
113
26.7k
    return out.xyz;
114
26.7k
}
115
116
2.52k
static PJ_LPZ pj_axisswap_reverse_3d(PJ_XYZ xyz, PJ *P) {
117
2.52k
    struct pj_axisswap_data *Q = (struct pj_axisswap_data *)P->opaque;
118
2.52k
    unsigned int i;
119
2.52k
    PJ_COORD in, out;
120
121
2.52k
    out = proj_coord_error();
122
2.52k
    in.v[0] = xyz.x;
123
2.52k
    in.v[1] = xyz.y;
124
2.52k
    in.v[2] = xyz.z;
125
126
10.0k
    for (i = 0; i < 3; i++)
127
7.56k
        out.v[Q->axis[i]] = in.v[i] * Q->sign[i];
128
129
2.52k
    return out.lpz;
130
2.52k
}
131
132
481k
static void swap_xy_4d(PJ_COORD &coo, PJ *) {
133
481k
    std::swap(coo.xyzt.x, coo.xyzt.y);
134
481k
}
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
11.5k
PJ *PJ_CONVERSION(axisswap, 0) {
159
    /***********************************************************************/
160
11.5k
    struct pj_axisswap_data *Q = static_cast<struct pj_axisswap_data *>(
161
11.5k
        calloc(1, sizeof(struct pj_axisswap_data)));
162
11.5k
    char *s;
163
11.5k
    unsigned int i, j, n = 0;
164
165
11.5k
    if (nullptr == Q)
166
0
        return pj_default_destructor(P, PROJ_ERR_OTHER /*ENOMEM*/);
167
11.5k
    P->opaque = (void *)Q;
168
169
    /* +order and +axis are mutually exclusive */
170
11.5k
    if (!pj_param_exists(P->params, "order") ==
171
11.5k
        !pj_param_exists(P->params, "axis")) {
172
44
        proj_log_error(P,
173
44
                       _("must provide EITHER 'order' OR 'axis' parameter."));
174
44
        return pj_default_destructor(
175
44
            P, PROJ_ERR_INVALID_OP_MUTUALLY_EXCLUSIVE_ARGS);
176
44
    }
177
178
    /* fill axis list with indices from 4-7 to simplify duplicate search further
179
     * down */
180
57.7k
    for (i = 0; i < 4; i++) {
181
46.2k
        Q->axis[i] = i + 4;
182
46.2k
        Q->sign[i] = 1;
183
46.2k
    }
184
185
    /* if the "order" parameter is used */
186
11.5k
    if (pj_param_exists(P->params, "order")) {
187
        /* read axis order */
188
8.92k
        char *order = pj_param(P->ctx, P->params, "sorder").s;
189
190
        /* check that all characters are valid */
191
39.5k
        for (i = 0; i < strlen(order); i++)
192
30.6k
            if (strchr("1234-,", order[i]) == nullptr) {
193
7
                proj_log_error(P, _("unknown axis '%c'"), order[i]);
194
7
                return pj_default_destructor(
195
7
                    P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
196
7
            }
197
198
        /* read axes numbers and signs */
199
8.91k
        s = order;
200
8.91k
        n = 0;
201
26.7k
        while (*s != '\0' && n < 4) {
202
17.8k
            Q->axis[n] = abs(atoi(s)) - 1;
203
17.8k
            if (Q->axis[n] > 3) {
204
10
                proj_log_error(P, _("invalid axis '%d'"), Q->axis[n]);
205
10
                return pj_default_destructor(
206
10
                    P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
207
10
            }
208
17.8k
            Q->sign[n++] = sign(atoi(s));
209
39.4k
            while (*s != '\0' && *s != ',')
210
21.6k
                s++;
211
17.8k
            if (*s == ',')
212
8.94k
                s++;
213
17.8k
        }
214
8.91k
    }
215
216
    /* if the "axis" parameter is used */
217
11.5k
    if (pj_param_exists(P->params, "axis")) {
218
        /* parse the classic PROJ.4 enu axis specification */
219
10.5k
        for (i = 0; i < 3; i++) {
220
7.89k
            switch (P->axis[i]) {
221
2.47k
            case 'w':
222
2.47k
                Q->sign[i] = -1;
223
2.47k
                Q->axis[i] = 0;
224
2.47k
                break;
225
158
            case 'e':
226
158
                Q->sign[i] = 1;
227
158
                Q->axis[i] = 0;
228
158
                break;
229
1.93k
            case 's':
230
1.93k
                Q->sign[i] = -1;
231
1.93k
                Q->axis[i] = 1;
232
1.93k
                break;
233
693
            case 'n':
234
693
                Q->sign[i] = 1;
235
693
                Q->axis[i] = 1;
236
693
                break;
237
778
            case 'd':
238
778
                Q->sign[i] = -1;
239
778
                Q->axis[i] = 2;
240
778
                break;
241
1.85k
            case 'u':
242
1.85k
                Q->sign[i] = 1;
243
1.85k
                Q->axis[i] = 2;
244
1.85k
                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
7.89k
            }
250
7.89k
        }
251
2.63k
        n = 3;
252
2.63k
    }
253
254
    /* check for duplicate axes */
255
57.5k
    for (i = 0; i < 4; i++)
256
230k
        for (j = 0; j < 4; j++) {
257
184k
            if (i == j)
258
46.0k
                continue;
259
138k
            if (Q->axis[i] == Q->axis[j]) {
260
26
                proj_log_error(P, _("axisswap: duplicate axes specified"));
261
26
                return pj_default_destructor(
262
26
                    P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
263
26
            }
264
138k
        }
265
266
    /* only map fwd/inv functions that are possible with the given axis setup */
267
11.5k
    if (n == 4) {
268
0
        P->fwd4d = pj_axisswap_forward_4d;
269
0
        P->inv4d = pj_axisswap_reverse_4d;
270
0
    }
271
11.5k
    if (n == 3 && Q->axis[0] < 3 && Q->axis[1] < 3 && Q->axis[2] < 3) {
272
2.62k
        P->fwd3d = pj_axisswap_forward_3d;
273
2.62k
        P->inv3d = pj_axisswap_reverse_3d;
274
2.62k
    }
275
11.5k
    if (n == 2) {
276
8.87k
        if (Q->axis[0] == 1 && Q->sign[0] == 1 && Q->axis[1] == 0 &&
277
6.86k
            Q->sign[1] == 1) {
278
6.71k
            P->fwd4d = swap_xy_4d;
279
6.71k
            P->inv4d = swap_xy_4d;
280
6.71k
        } else if (Q->axis[0] < 2 && Q->axis[1] < 2) {
281
2.15k
            P->fwd = pj_axisswap_forward_2d;
282
2.15k
            P->inv = pj_axisswap_reverse_2d;
283
2.15k
        }
284
8.87k
    }
285
286
11.5k
    if (P->fwd4d == nullptr && P->fwd3d == nullptr && P->fwd == nullptr) {
287
17
        proj_log_error(P, _("axisswap: bad axis order"));
288
17
        return pj_default_destructor(P, PROJ_ERR_INVALID_OP_ILLEGAL_ARG_VALUE);
289
17
    }
290
291
11.4k
    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
11.4k
    } else {
295
11.4k
        P->left = PJ_IO_UNITS_WHATEVER;
296
11.4k
        P->right = PJ_IO_UNITS_WHATEVER;
297
11.4k
    }
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
11.4k
    P->skip_fwd_prepare = 1;
304
11.4k
    P->skip_fwd_finalize = 1;
305
11.4k
    P->skip_inv_prepare = 1;
306
11.4k
    P->skip_inv_finalize = 1;
307
308
11.4k
    return P;
309
11.5k
}