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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
|
/***********************************************************************
3-, 4-and 7-parameter shifts, and their 6-, 8-
and 14-parameter kinematic counterparts.
Thomas Knudsen, 2016-05-24
************************************************************************
Implements 3(6)-, 4(8) and 7(14)-parameter Helmert transformations for
3D data.
Primarily useful for implementation of datum shifts in transformation
pipelines.
************************************************************************
Thomas Knudsen, thokn@sdfe.dk, 2016-05-24/06-05
Kristian Evers, kreve@sdfe.dk, 2017-05-01
Last update: 2017-05-15
************************************************************************
* Copyright (c) 2016, Thomas Knudsen / SDFE
*
* 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 <assert.h>
#include <stddef.h>
#include <errno.h>
#include "proj_internal.h"
#include "projects.h"
#include "geocent.h"
PROJ_HEAD(helmert, "3(6)-, 4(8)- and 7(14)-parameter Helmert shift");
static XYZ helmert_forward_3d (LPZ lpz, PJ *P);
static LPZ helmert_reverse_3d (XYZ xyz, PJ *P);
/***********************************************************************/
struct pj_opaque_helmert {
/************************************************************************
Projection specific elements for the "helmert" PJ object
************************************************************************/
XYZ xyz;
XYZ xyz_0;
XYZ dxyz;
PJ_OPK opk;
PJ_OPK opk_0;
PJ_OPK dopk;
double scale;
double scale_0;
double dscale;
double theta;
double theta_0;
double dtheta;
double R[3][3];
double epoch, t_obs;
int no_rotation, approximate, transpose, fourparam;
};
/* Make the maths of the rotation operations somewhat more readable and textbook like */
#define R00 (Q->R[0][0])
#define R01 (Q->R[0][1])
#define R02 (Q->R[0][2])
#define R10 (Q->R[1][0])
#define R11 (Q->R[1][1])
#define R12 (Q->R[1][2])
#define R20 (Q->R[2][0])
#define R21 (Q->R[2][1])
#define R22 (Q->R[2][2])
/**************************************************************************/
static void update_parameters(PJ *P) {
/***************************************************************************
Update transformation parameters.
---------------------------------
The 14-parameter Helmert transformation is at it's core the same as the
7-parameter transformation, since the transformation parameters are
projected forward or backwards in time via the rate of changes of the
parameters. The transformation parameters are calculated for a specific
epoch before the actual Helmert transformation is carried out.
The transformation parameters are updated with the following equation [0]:
.
P(t) = P(EPOCH) + P * (t - EPOCH)
.
where EPOCH is the epoch indicated in the above table and P is the rate
of that parameter.
[0] http://itrf.ign.fr/doc_ITRF/Transfo-ITRF2008_ITRFs.txt
*******************************************************************************/
struct pj_opaque_helmert *Q = (struct pj_opaque_helmert *) P->opaque;
double dt = Q->t_obs - Q->epoch;
Q->xyz.x = Q->xyz_0.x + Q->dxyz.x * dt;
Q->xyz.y = Q->xyz_0.y + Q->dxyz.y * dt;
Q->xyz.z = Q->xyz_0.z + Q->dxyz.z * dt;
Q->opk.o = Q->opk_0.o + Q->dopk.o * dt;
Q->opk.p = Q->opk_0.p + Q->dopk.p * dt;
Q->opk.k = Q->opk_0.k + Q->dopk.k * dt;
Q->scale = Q->scale_0 + Q->dscale * dt;
Q->theta = Q->theta_0 + Q->dtheta * dt;
/* debugging output */
if (proj_log_level(P->ctx, PJ_LOG_TELL) >= PJ_LOG_TRACE) {
proj_log_trace(P, "Transformation parameters for observation epoch %g:", Q->t_obs);
proj_log_trace(P, "x: %g", Q->xyz.x);
proj_log_trace(P, "y: %g", Q->xyz.y);
proj_log_trace(P, "z: %g", Q->xyz.z);
proj_log_trace(P, "s: %g", Q->scale*1e-6);
proj_log_trace(P, "rx: %g", Q->opk.o);
proj_log_trace(P, "ry: %g", Q->opk.p);
proj_log_trace(P, "rz: %g", Q->opk.k);
proj_log_trace(P, "theta: %g", Q->theta);
}
return;
}
/**************************************************************************/
static void build_rot_matrix(PJ *P) {
/***************************************************************************
Build rotation matrix.
----------------------
Here we rename rotation indices from omega, phi, kappa (opk), to
fi (i.e. phi), theta, psi (ftp), in order to reduce the mental agility
needed to implement the expression for the rotation matrix derived over
at https://en.wikipedia.org/wiki/Rotation_formalisms_in_three_dimensions
The relevant section is Euler angles ( z-’-x" intrinsic) -> Rotation matrix
If the option "approximate" is set, small angle approximations are used:
The matrix elements are approximated by expanding the trigonometric
functions to linear order (i.e. cos(x) = 1, sin(x) = x), and discarding
products of second order.
This was a useful hack when calculating by hand was the only option,
but in general, today, should be avoided because:
1. It does not save much computation time, as the rotation matrix
is built only once and probably used many times (except when
transforming spatio-temporal coordinates).
2. The error induced may be too large for ultra high accuracy
applications: the Earth is huge and the linear error is
approximately the angular error multiplied by the Earth radius.
However, in many cases the approximation is necessary, since it has
been used historically: Rotation angles from older published datum
shifts may actually be a least squares fit to the linearized rotation
approximation, hence not being strictly valid for deriving the full
rotation matrix.
So in order to fit historically derived coordinates, the access to
the approximate rotation matrix is necessary - at least in principle.
Also, when using any published datum transformation information, one
should always check which convention (exact or approximate rotation
matrix) is expected, and whether the induced error for selecting
the opposite convention is acceptable (which it often is).
Sign conventions
----------------
Take care: Two different sign conventions exist.
Conceptually they relate to whether we rotate the coordinate system
or the "position vector" (the vector going from the coordinate system
origin to the point being transformed, i.e. the point coordinates
interpreted as vector coordinates).
Switching between the "position vector" and "coordinate system"
conventions is simply a matter of switching the sign of the rotation
angles, which algebraically also translates into a transposition of
the rotation matrix.
Hence, as geodetic constants should preferably be referred to exactly
as published, the "transpose" option provides the ability to switch
between the conventions.
***************************************************************************/
struct pj_opaque_helmert *Q = (struct pj_opaque_helmert *) P->opaque;
double f, t, p; /* phi/fi , theta, psi */
double cf, ct, cp; /* cos (fi, theta, psi) */
double sf, st, sp; /* sin (fi, theta, psi) */
/* rename (omega, phi, kappa) to (fi, theta, psi) */
f = Q->opk.o;
t = Q->opk.p;
p = Q->opk.k;
if (Q->approximate) {
R00 = 1;
R01 = p;
R02 = -t;
R10 = -p;
R11 = 1;
R12 = f;
R20 = t;
R21 = -f;
R22 = 1;
}
else {
cf = cos(f);
sf = sin(f);
ct = cos(t);
st = sin(t);
cp = cos(p);
sp = sin(p);
R00 = ct*cp;
R01 = cf*sp + sf*st*cp;
R02 = sf*sp - cf*st*cp;
R10 = -ct*sp;
R11 = cf*cp - sf*st*sp;
R12 = sf*cp + cf*st*sp;
R20 = st;
R21 = -sf*ct;
R22 = cf*ct;
}
/*
For comparison: Description from Engsager/Poder implementation
in set_dtm_1.c (trlib)
DATUM SHIFT:
TO = scale * ROTZ * ROTY * ROTX * FROM + TRANSLA
( cz sz 0) (cy 0 -sy) (1 0 0)
ROTZ=(-sz cz 0), ROTY=(0 1 0), ROTX=(0 cx sx)
( 0 0 1) (sy 0 cy) (0 -sx cx)
trp->r11 = cos_ry*cos_rz;
trp->r12 = cos_rx*sin_rz + sin_rx*sin_ry*cos_rz;
trp->r13 = sin_rx*sin_rz - cos_rx*sin_ry*cos_rz;
trp->r21 = -cos_ry*sin_rz;
trp->r22 = cos_rx*cos_rz - sin_rx*sin_ry*sin_rz;
trp->r23 = sin_rx*cos_rz + cos_rx*sin_ry*sin_rz;
trp->r31 = sin_ry;
trp->r32 = -sin_rx*cos_ry;
trp->r33 = cos_rx*cos_ry;
trp->scale = 1.0 + scale;
*/
if (Q->transpose) {
double r;
r = R01; R01 = R10; R10 = r;
r = R02; R02 = R20; R20 = r;
r = R12; R12 = R21; R21 = r;
}
/* some debugging output */
if (proj_log_level(P->ctx, PJ_LOG_TELL) >= PJ_LOG_TRACE) {
proj_log_trace(P, "Rotation Matrix:");
proj_log_trace(P, " | % 6.6g % 6.6g % 6.6g |", R00, R01, R02);
proj_log_trace(P, " | % 6.6g % 6.6g % 6.6g |", R10, R11, R12);
proj_log_trace(P, " | % 6.6g % 6.6g % 6.6g |", R20, R21, R22);
}
return;
}
/***********************************************************************/
static XY helmert_forward (LP lp, PJ *P) {
/***********************************************************************/
struct pj_opaque_helmert *Q = (struct pj_opaque_helmert *) P->opaque;
PJ_TRIPLET point;
double x, y, cr, sr;
point.lp = lp;
cr = cos(Q->theta) * Q->scale;
sr = sin(Q->theta) * Q->scale;
x = point.xy.x;
y = point.xy.y;
point.xy.x = cr*x + sr*y + Q->xyz_0.x;
point.xy.y = -sr*x + cr*y + Q->xyz_0.y;
return point.xy;
}
/***********************************************************************/
static LP helmert_reverse (XY xy, PJ *P) {
/***********************************************************************/
struct pj_opaque_helmert *Q = (struct pj_opaque_helmert *) P->opaque;
PJ_TRIPLET point;
double x, y, sr, cr;
point.xy = xy;
cr = cos(Q->theta) / Q->scale;
sr = sin(Q->theta) / Q->scale;
x = point.xy.x - Q->xyz_0.x;
y = point.xy.y - Q->xyz_0.y;
point.xy.x = x*cr - y*sr;
point.xy.y = x*sr + y*cr;
return point.lp;
}
/***********************************************************************/
static XYZ helmert_forward_3d (LPZ lpz, PJ *P) {
/***********************************************************************/
struct pj_opaque_helmert *Q = (struct pj_opaque_helmert *) P->opaque;
PJ_TRIPLET point;
double X, Y, Z, scale;
point.lpz = lpz;
if (Q->fourparam) {
point.xy = helmert_forward(point.lp, P);
return point.xyz;
}
if (Q->no_rotation) {
point.xyz.x = lpz.lam + Q->xyz.x;
point.xyz.y = lpz.phi + Q->xyz.y;
point.xyz.z = lpz.z + Q->xyz.z;
return point.xyz;
}
scale = 1 + Q->scale * 1e-6;
X = lpz.lam;
Y = lpz.phi;
Z = lpz.z;
point.xyz.x = scale * ( R00 * X + R01 * Y + R02 * Z);
point.xyz.y = scale * ( R10 * X + R11 * Y + R12 * Z);
point.xyz.z = scale * ( R20 * X + R21 * Y + R22 * Z);
point.xyz.x += Q->xyz.x;
point.xyz.y += Q->xyz.y;
point.xyz.z += Q->xyz.z;
return point.xyz;
}
/***********************************************************************/
static LPZ helmert_reverse_3d (XYZ xyz, PJ *P) {
/***********************************************************************/
struct pj_opaque_helmert *Q = (struct pj_opaque_helmert *) P->opaque;
PJ_TRIPLET point;
double X, Y, Z, scale;
point.xyz = xyz;
if (Q->fourparam) {
point.lp = helmert_reverse(point.xy, P);
return point.lpz;
}
if (Q->no_rotation) {
point.xyz.x = xyz.x - Q->xyz.x;
point.xyz.y = xyz.y - Q->xyz.y;
point.xyz.z = xyz.z - Q->xyz.z;
return point.lpz;
}
scale = 1 + Q->scale * 1e-6;
/* Unscale and deoffset */
X = (xyz.x - Q->xyz.x) / scale;
Y = (xyz.y - Q->xyz.y) / scale;
Z = (xyz.z - Q->xyz.z) / scale;
/* Inverse rotation through transpose multiplication */
point.xyz.x = ( R00 * X + R10 * Y + R20 * Z);
point.xyz.y = ( R01 * X + R11 * Y + R21 * Z);
point.xyz.z = ( R02 * X + R12 * Y + R22 * Z);
return point.lpz;
}
static PJ_COORD helmert_forward_4d (PJ_COORD point, PJ *P) {
struct pj_opaque_helmert *Q = (struct pj_opaque_helmert *) P->opaque;
/* We only need to rebuild the rotation matrix if the
* observation time is different from the last call */
if (point.xyzt.t != Q->t_obs) {
Q->t_obs = point.xyzt.t;
update_parameters(P);
build_rot_matrix(P);
}
point.xyz = helmert_forward_3d (point.lpz, P);
return point;
}
static PJ_COORD helmert_reverse_4d (PJ_COORD point, PJ *P) {
struct pj_opaque_helmert *Q = (struct pj_opaque_helmert *) P->opaque;
/* We only need to rebuild the rotation matrix if the
* observation time is different from the last call */
if (point.xyzt.t != Q->t_obs) {
Q->t_obs = point.xyzt.t;
update_parameters(P);
build_rot_matrix(P);
}
point.lpz = helmert_reverse_3d (point.xyz, P);
return point;
}
/* Arcsecond to radians */
#define ARCSEC_TO_RAD (DEG_TO_RAD / 3600.0)
/***********************************************************************/
PJ *TRANSFORMATION(helmert, 0) {
/***********************************************************************/
struct pj_opaque_helmert *Q = pj_calloc (1, sizeof (struct pj_opaque_helmert));
if (0==Q)
return pj_default_destructor (P, ENOMEM);
P->opaque = (void *) Q;
P->fwd4d = helmert_forward_4d;
P->inv4d = helmert_reverse_4d;
P->fwd3d = helmert_forward_3d;
P->inv3d = helmert_reverse_3d;
P->fwd = helmert_forward;
P->inv = helmert_reverse;
P->left = PJ_IO_UNITS_METERS;
P->right = PJ_IO_UNITS_METERS;
/* Translations */
if (pj_param (P->ctx, P->params, "tx").i)
Q->xyz_0.x = pj_param (P->ctx, P->params, "dx").f;
if (pj_param (P->ctx, P->params, "ty").i)
Q->xyz_0.y = pj_param (P->ctx, P->params, "dy").f;
if (pj_param (P->ctx, P->params, "tz").i)
Q->xyz_0.z = pj_param (P->ctx, P->params, "dz").f;
/* Rotations */
if (pj_param (P->ctx, P->params, "trx").i)
Q->opk_0.o = pj_param (P->ctx, P->params, "drx").f * ARCSEC_TO_RAD;
if (pj_param (P->ctx, P->params, "try").i)
Q->opk_0.p = pj_param (P->ctx, P->params, "dry").f * ARCSEC_TO_RAD;
if (pj_param (P->ctx, P->params, "trz").i)
Q->opk_0.k = pj_param (P->ctx, P->params, "drz").f * ARCSEC_TO_RAD;
if (pj_param (P->ctx, P->params, "ttheta").i) {
Q->theta_0 = pj_param (P->ctx, P->params, "dtheta").f * ARCSEC_TO_RAD;
Q->fourparam = 1;
Q->scale_0 = 1.0; /* default scale for the 4-param shift */
}
/* Scale */
if (pj_param (P->ctx, P->params, "ts").i) {
Q->scale_0 = pj_param (P->ctx, P->params, "ds").f;
if (pj_param (P->ctx, P->params, "ttheta").i && Q->scale_0 == 0.0)
return pj_default_destructor (P, PJD_ERR_INVALID_SCALE);
}
/* Translation rates */
if (pj_param(P->ctx, P->params, "tdx").i)
Q->dxyz.x = pj_param (P->ctx, P->params, "ddx").f;
if (pj_param(P->ctx, P->params, "tdy").i)
Q->dxyz.y = pj_param (P->ctx, P->params, "ddy").f;
if (pj_param(P->ctx, P->params, "tdz").i)
Q->dxyz.z = pj_param (P->ctx, P->params, "ddz").f;
/* Rotations rates */
if (pj_param (P->ctx, P->params, "tdrx").i)
Q->dopk.o = pj_param (P->ctx, P->params, "ddrx").f * ARCSEC_TO_RAD;
if (pj_param (P->ctx, P->params, "tdry").i)
Q->dopk.p = pj_param (P->ctx, P->params, "ddry").f * ARCSEC_TO_RAD;
if (pj_param (P->ctx, P->params, "tdrz").i)
Q->dopk.k = pj_param (P->ctx, P->params, "ddrz").f * ARCSEC_TO_RAD;
if (pj_param (P->ctx, P->params, "tdtheta").i)
Q->dtheta = pj_param (P->ctx, P->params, "ddtheta").f * ARCSEC_TO_RAD;
/* Scale rate */
if (pj_param (P->ctx, P->params, "tds").i)
Q->dscale = pj_param (P->ctx, P->params, "dds").f;
/* Epoch */
if (pj_param(P->ctx, P->params, "tepoch").i)
Q->epoch = pj_param (P->ctx, P->params, "depoch").f;
if (pj_param(P->ctx, P->params, "ttobs").i)
Q->t_obs = pj_param (P->ctx, P->params, "dtobs").f;
/* Use small angle approximations? */
if (pj_param (P->ctx, P->params, "bapprox").i)
Q->approximate = 1;
/* Use "other" rotation sign convention? */
if (pj_param (P->ctx, P->params, "ttranspose").i)
Q->transpose = 1;
Q->xyz = Q->xyz_0;
Q->opk = Q->opk_0;
Q->scale = Q->scale_0;
Q->theta = Q->theta_0;
/* Let's help with debugging */
if (proj_log_level(P->ctx, PJ_LOG_TELL) >= PJ_LOG_DEBUG) {
proj_log_debug(P, "Helmert parameters:");
proj_log_debug(P, "x= % 3.5f y= % 3.5f z= % 3.5f", Q->xyz.x, Q->xyz.y, Q->xyz.z);
proj_log_debug(P, "rx= % 3.5f ry= % 3.5f rz= % 3.5f",
Q->opk.o / ARCSEC_TO_RAD, Q->opk.p / ARCSEC_TO_RAD, Q->opk.k / ARCSEC_TO_RAD);
proj_log_debug(P, "s=% 3.5f approximate=% d transpose=% d",
Q->scale, Q->approximate, Q->transpose);
proj_log_debug(P, "dx= % 3.5f dy= % 3.5f dz= % 3.5f", Q->dxyz.x, Q->dxyz.y, Q->dxyz.z);
proj_log_debug(P, "drx=% 3.5f dry=% 3.5f drz=% 3.5f", Q->dopk.o, Q->dopk.p, Q->dopk.k);
proj_log_debug(P, "ds=% 3.5f epoch=% 5.5f tobs=% 5.5f", Q->dscale, Q->epoch, Q->t_obs);
}
if ((Q->opk.o==0) && (Q->opk.p==0) && (Q->opk.k==0) && (Q->scale==0)) {
Q->no_rotation = 1;
return P;
}
update_parameters(P);
build_rot_matrix(P);
return P;
}
|