Coverage Report

Created: 2026-04-10 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/proj/src/transformations/hgridshift.cpp
Line
Count
Source
1
2
3
#include <errno.h>
4
#include <mutex>
5
#include <stddef.h>
6
#include <string.h>
7
8
#include "grids.hpp"
9
#include "proj_internal.h"
10
11
PROJ_HEAD(hgridshift, "Horizontal grid shift");
12
13
static std::mutex gMutexHGridShift{};
14
static std::set<std::string> gKnownGridsHGridShift{};
15
16
using namespace NS_PROJ;
17
18
namespace { // anonymous namespace
19
struct hgridshiftData {
20
    double t_final = 0;
21
    double t_epoch = 0;
22
    ListOfHGrids grids{};
23
    bool defer_grid_opening = false;
24
    int error_code_in_defer_grid_opening = 0;
25
};
26
} // anonymous namespace
27
28
0
static PJ_XYZ pj_hgridshift_forward_3d(PJ_LPZ lpz, PJ *P) {
29
0
    auto Q = static_cast<hgridshiftData *>(P->opaque);
30
0
    PJ_COORD point = {{0, 0, 0, 0}};
31
0
    point.lpz = lpz;
32
33
0
    if (Q->defer_grid_opening) {
34
0
        Q->defer_grid_opening = false;
35
0
        Q->grids = pj_hgrid_init(P, "grids");
36
0
        Q->error_code_in_defer_grid_opening = proj_errno(P);
37
0
    }
38
0
    if (Q->error_code_in_defer_grid_opening) {
39
0
        proj_errno_set(P, Q->error_code_in_defer_grid_opening);
40
0
        return proj_coord_error().xyz;
41
0
    }
42
43
0
    if (!Q->grids.empty()) {
44
        /* Only try the gridshift if at least one grid is loaded,
45
         * otherwise just pass the coordinate through unchanged. */
46
0
        point.lp = pj_hgrid_apply(P->ctx, Q->grids, point.lp, PJ_FWD);
47
0
    }
48
49
0
    return point.xyz;
50
0
}
51
52
0
static PJ_LPZ pj_hgridshift_reverse_3d(PJ_XYZ xyz, PJ *P) {
53
0
    auto Q = static_cast<hgridshiftData *>(P->opaque);
54
0
    PJ_COORD point = {{0, 0, 0, 0}};
55
0
    point.xyz = xyz;
56
57
0
    if (Q->defer_grid_opening) {
58
0
        Q->defer_grid_opening = false;
59
0
        Q->grids = pj_hgrid_init(P, "grids");
60
0
        Q->error_code_in_defer_grid_opening = proj_errno(P);
61
0
    }
62
0
    if (Q->error_code_in_defer_grid_opening) {
63
0
        proj_errno_set(P, Q->error_code_in_defer_grid_opening);
64
0
        return proj_coord_error().lpz;
65
0
    }
66
67
0
    if (!Q->grids.empty()) {
68
        /* Only try the gridshift if at least one grid is loaded,
69
         * otherwise just pass the coordinate through unchanged. */
70
0
        point.lp = pj_hgrid_apply(P->ctx, Q->grids, point.lp, PJ_INV);
71
0
    }
72
73
0
    return point.lpz;
74
0
}
75
76
0
static void pj_hgridshift_forward_4d(PJ_COORD &coo, PJ *P) {
77
0
    struct hgridshiftData *Q = (struct hgridshiftData *)P->opaque;
78
79
    /* If transformation is not time restricted, we always call it */
80
0
    if (Q->t_final == 0 || Q->t_epoch == 0) {
81
        // Assigning in 2 steps avoids cppcheck warning
82
        // "Overlapping read/write of union is undefined behavior"
83
        // Cf
84
        // https://github.com/OSGeo/PROJ/pull/3527#pullrequestreview-1233332710
85
0
        const auto xyz = pj_hgridshift_forward_3d(coo.lpz, P);
86
0
        coo.xyz = xyz;
87
0
        return;
88
0
    }
89
90
    /* Time restricted - only apply transform if within time bracket */
91
0
    if (coo.lpzt.t < Q->t_epoch && Q->t_final > Q->t_epoch) {
92
        // Assigning in 2 steps avoids cppcheck warning
93
        // "Overlapping read/write of union is undefined behavior"
94
        // Cf
95
        // https://github.com/OSGeo/PROJ/pull/3527#pullrequestreview-1233332710
96
0
        const auto xyz = pj_hgridshift_forward_3d(coo.lpz, P);
97
0
        coo.xyz = xyz;
98
0
    }
99
0
}
100
101
0
static void pj_hgridshift_reverse_4d(PJ_COORD &coo, PJ *P) {
102
0
    struct hgridshiftData *Q = (struct hgridshiftData *)P->opaque;
103
104
    /* If transformation is not time restricted, we always call it */
105
0
    if (Q->t_final == 0 || Q->t_epoch == 0) {
106
        // Assigning in 2 steps avoids cppcheck warning
107
        // "Overlapping read/write of union is undefined behavior"
108
        // Cf
109
        // https://github.com/OSGeo/PROJ/pull/3527#pullrequestreview-1233332710
110
0
        const auto lpz = pj_hgridshift_reverse_3d(coo.xyz, P);
111
0
        coo.lpz = lpz;
112
0
        return;
113
0
    }
114
115
    /* Time restricted - only apply transform if within time bracket */
116
0
    if (coo.lpzt.t < Q->t_epoch && Q->t_final > Q->t_epoch) {
117
        // Assigning in 2 steps avoids cppcheck warning
118
        // "Overlapping read/write of union is undefined behavior"
119
        // Cf
120
        // https://github.com/OSGeo/PROJ/pull/3527#pullrequestreview-1233332710
121
0
        const auto lpz = pj_hgridshift_reverse_3d(coo.xyz, P);
122
0
        coo.lpz = lpz;
123
0
    }
124
0
}
125
126
3.27k
static PJ *pj_hgridshift_destructor(PJ *P, int errlev) {
127
3.27k
    if (nullptr == P)
128
0
        return nullptr;
129
130
3.27k
    delete static_cast<struct hgridshiftData *>(P->opaque);
131
3.27k
    P->opaque = nullptr;
132
133
3.27k
    return pj_default_destructor(P, errlev);
134
3.27k
}
135
136
0
static void pj_hgridshift_reassign_context(PJ *P, PJ_CONTEXT *ctx) {
137
0
    auto Q = (struct hgridshiftData *)P->opaque;
138
0
    for (auto &grid : Q->grids) {
139
0
        grid->reassign_context(ctx);
140
0
    }
141
0
}
142
143
3.27k
PJ *PJ_TRANSFORMATION(hgridshift, 0) {
144
3.27k
    auto Q = new hgridshiftData;
145
3.27k
    P->opaque = (void *)Q;
146
3.27k
    P->destructor = pj_hgridshift_destructor;
147
3.27k
    P->reassign_context = pj_hgridshift_reassign_context;
148
149
3.27k
    P->fwd4d = pj_hgridshift_forward_4d;
150
3.27k
    P->inv4d = pj_hgridshift_reverse_4d;
151
3.27k
    P->fwd3d = pj_hgridshift_forward_3d;
152
3.27k
    P->inv3d = pj_hgridshift_reverse_3d;
153
3.27k
    P->fwd = nullptr;
154
3.27k
    P->inv = nullptr;
155
156
3.27k
    P->left = PJ_IO_UNITS_RADIANS;
157
3.27k
    P->right = PJ_IO_UNITS_RADIANS;
158
159
3.27k
    if (0 == pj_param(P->ctx, P->params, "tgrids").i) {
160
7
        proj_log_error(P, _("+grids parameter missing."));
161
7
        return pj_hgridshift_destructor(P, PROJ_ERR_INVALID_OP_MISSING_ARG);
162
7
    }
163
164
3.26k
    Q->t_final = pj_parse_t_final(P);
165
166
3.26k
    if (pj_param(P->ctx, P->params, "tt_epoch").i)
167
14
        Q->t_epoch = pj_param(P->ctx, P->params, "dt_epoch").f;
168
169
3.26k
    if (P->ctx->defer_grid_opening) {
170
0
        Q->defer_grid_opening = true;
171
3.26k
    } else {
172
3.26k
        const char *gridnames = pj_param(P->ctx, P->params, "sgrids").s;
173
3.26k
        gMutexHGridShift.lock();
174
3.26k
        const bool isKnownGrid = gKnownGridsHGridShift.find(gridnames) !=
175
3.26k
                                 gKnownGridsHGridShift.end();
176
3.26k
        gMutexHGridShift.unlock();
177
3.26k
        if (isKnownGrid) {
178
2.85k
            Q->defer_grid_opening = true;
179
2.85k
        } else {
180
409
            Q->grids = pj_hgrid_init(P, "grids");
181
            /* Was gridlist compiled properly? */
182
409
            if (proj_errno(P)) {
183
275
                proj_log_error(P, _("could not find required grid(s)."));
184
275
                return pj_hgridshift_destructor(
185
275
                    P, PROJ_ERR_INVALID_OP_FILE_NOT_FOUND_OR_INVALID);
186
275
            }
187
188
134
            gMutexHGridShift.lock();
189
134
            gKnownGridsHGridShift.insert(gridnames);
190
134
            gMutexHGridShift.unlock();
191
134
        }
192
3.26k
    }
193
194
2.99k
    return P;
195
3.26k
}
196
197
0
void pj_clear_hgridshift_knowngrids_cache() {
198
0
    std::lock_guard<std::mutex> lock(gMutexHGridShift);
199
0
    gKnownGridsHGridShift.clear();
200
0
}