1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
|
/******************************************************************************
* Project: PROJ
* Purpose: Geocentric translation using a grid
* Author: Even Rouault, <even.rouault at spatialys.com>
*
******************************************************************************
* Copyright (c) 2019, Even Rouault, <even.rouault at spatialys.com>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, copy, modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
* DEALINGS IN THE SOFTWARE.
*****************************************************************************/
#define PJ_LIB__
#include <errno.h>
#include <string.h>
#include <stddef.h>
#include <time.h>
#include "proj_internal.h"
#include "grids.hpp"
#include <algorithm>
PROJ_HEAD(xyzgridshift, "Geocentric grid shift");
using namespace NS_PROJ;
namespace { // anonymous namespace
struct xyzgridshiftData {
PJ *cart = nullptr;
bool grid_ref_is_input = true;
ListOfGenericGrids grids{};
bool defer_grid_opening = false;
double multiplier = 1.0;
};
} // anonymous namespace
// ---------------------------------------------------------------------------
static bool get_grid_values(PJ* P,
xyzgridshiftData* Q,
const PJ_LP& lp,
double& dx,
double& dy,
double& dz)
{
if ( Q->defer_grid_opening ) {
Q->defer_grid_opening = false;
Q->grids = pj_generic_grid_init(P, "grids");
if ( proj_errno(P) ) {
return false;
}
}
GenericShiftGridSet* gridset = nullptr;
auto grid = pj_find_generic_grid(Q->grids, lp, gridset);
if( !grid ) {
return false;
}
if( grid->isNullGrid() ) {
dx = 0;
dy = 0;
dz = 0;
return true;
}
const auto samplesPerPixel = grid->samplesPerPixel();
if( samplesPerPixel < 3 ) {
proj_log_error(P, "xyzgridshift: grid has not enough samples");
return false;
}
int sampleX = 0;
int sampleY = 1;
int sampleZ = 2;
for( int i = 0; i < samplesPerPixel; i++ )
{
const auto desc = grid->description(i);
if( desc == "x_translation") {
sampleX = i;
} else if( desc == "y_translation") {
sampleY = i;
} else if( desc == "z_translation") {
sampleZ = i;
}
}
const auto unit = grid->unit(sampleX);
if( !unit.empty() && unit != "metre" ) {
proj_log_error(P, "xyzgridshift: Only unit=metre currently handled");
return false;
}
bool must_retry = false;
if( !pj_bilinear_interpolation_three_samples(P->ctx, grid, lp,
sampleX, sampleY, sampleZ,
dx, dy, dz,
must_retry) )
{
if( must_retry )
return get_grid_values( P, Q, lp, dx, dy, dz);
return false;
}
dx *= Q->multiplier;
dy *= Q->multiplier;
dz *= Q->multiplier;
return true;
}
// ---------------------------------------------------------------------------
#define SQUARE(x) ((x)*(x))
// ---------------------------------------------------------------------------
static PJ_COORD iterative_adjustment(PJ* P,
xyzgridshiftData* Q,
const PJ_COORD& pointInit,
double factor)
{
PJ_COORD point = pointInit;
for(int i = 0; i < 10; i++) {
PJ_COORD geodetic;
geodetic.lpz = pj_inv3d(point.xyz, Q->cart);
double dx, dy, dz;
if( !get_grid_values(P, Q, geodetic.lp, dx, dy, dz) ) {
return proj_coord_error();
}
dx *= factor;
dy *= factor;
dz *= factor;
const double err = SQUARE((point.xyz.x - pointInit.xyz.x) - dx) +
SQUARE((point.xyz.y - pointInit.xyz.y) - dy) +
SQUARE((point.xyz.z - pointInit.xyz.z) - dz);
point.xyz.x = pointInit.xyz.x + dx;
point.xyz.y = pointInit.xyz.y + dy;
point.xyz.z = pointInit.xyz.z + dz;
if( err < 1e-10 ) {
break;
}
}
return point;
}
// ---------------------------------------------------------------------------
static PJ_COORD direct_adjustment(PJ* P,
xyzgridshiftData* Q,
PJ_COORD point,
double factor)
{
PJ_COORD geodetic;
geodetic.lpz = pj_inv3d(point.xyz, Q->cart);
double dx, dy, dz;
if( !get_grid_values(P, Q, geodetic.lp, dx, dy, dz) ) {
return proj_coord_error();
}
point.xyz.x += factor * dx;
point.xyz.y += factor * dy;
point.xyz.z += factor * dz;
return point;
}
// ---------------------------------------------------------------------------
static PJ_XYZ forward_3d(PJ_LPZ lpz, PJ *P) {
auto Q = static_cast<xyzgridshiftData*>(P->opaque);
PJ_COORD point = {{0,0,0,0}};
point.lpz = lpz;
if( Q->grid_ref_is_input ) {
point = direct_adjustment(P, Q, point, 1.0);
}
else {
point = iterative_adjustment(P, Q, point, 1.0);
}
return point.xyz;
}
static PJ_LPZ reverse_3d(PJ_XYZ xyz, PJ *P) {
auto Q = static_cast<xyzgridshiftData*>(P->opaque);
PJ_COORD point = {{0,0,0,0}};
point.xyz = xyz;
if( Q->grid_ref_is_input ) {
point = iterative_adjustment(P, Q, point, -1.0);
}
else {
point = direct_adjustment(P, Q, point, -1.0);
}
return point.lpz;
}
static PJ *destructor (PJ *P, int errlev) {
if (nullptr==P)
return nullptr;
auto Q = static_cast<struct xyzgridshiftData*>(P->opaque);
if( Q )
{
if (Q->cart)
Q->cart->destructor (Q->cart, errlev);
delete Q;
}
P->opaque = nullptr;
return pj_default_destructor(P, errlev);
}
static void reassign_context( PJ* P, PJ_CONTEXT* ctx )
{
auto Q = (struct xyzgridshiftData *) P->opaque;
for( auto& grid: Q->grids ) {
grid->reassign_context(ctx);
}
}
PJ *TRANSFORMATION(xyzgridshift,0) {
auto Q = new xyzgridshiftData;
P->opaque = (void *) Q;
P->destructor = destructor;
P->reassign_context = reassign_context;
P->fwd4d = nullptr;
P->inv4d = nullptr;
P->fwd3d = forward_3d;
P->inv3d = reverse_3d;
P->fwd = nullptr;
P->inv = nullptr;
P->left = PJ_IO_UNITS_CARTESIAN;
P->right = PJ_IO_UNITS_CARTESIAN;
// Pass a dummy ellipsoid definition that will be overridden just afterwards
Q->cart = proj_create(P->ctx, "+proj=cart +a=1");
if (Q->cart == nullptr)
return destructor(P, ENOMEM);
/* inherit ellipsoid definition from P to Q->cart */
pj_inherit_ellipsoid_def (P, Q->cart);
const char* grid_ref = pj_param (P->ctx, P->params, "sgrid_ref").s;
if( grid_ref ) {
if (strcmp(grid_ref, "input_crs") == 0 ) {
// default
} else if (strcmp(grid_ref, "output_crs") == 0 ) {
// Convention use for example for NTF->RGF93 grid that contains
// delta x,y,z from NTF to RGF93, but the grid itself is referenced
// in RGF93
Q->grid_ref_is_input = false;
} else {
proj_log_error(P, "xyzgridshift: unusupported value for grid_ref");
return destructor (P, PJD_ERR_NO_ARGS);
}
}
if (0==pj_param(P->ctx, P->params, "tgrids").i) {
proj_log_error(P, "xyzgridshift: +grids parameter missing.");
return destructor (P, PJD_ERR_NO_ARGS);
}
/* multiplier for delta x,y,z */
if (pj_param(P->ctx, P->params, "tmultiplier").i) {
Q->multiplier = pj_param(P->ctx, P->params, "dmultiplier").f;
}
if( P->ctx->defer_grid_opening ) {
Q->defer_grid_opening = true;
}
else {
Q->grids = pj_generic_grid_init(P, "grids");
/* Was gridlist compiled properly? */
if ( proj_errno(P) ) {
proj_log_error(P, "xyzgridshift: could not find required grid(s).");
return destructor(P, PJD_ERR_FAILED_TO_LOAD_GRID);
}
}
return P;
}
|