proj4phpDatum.php
16 KB
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
<?php
/**
* Author : Julien Moquet
*
* Inspired by Proj4js from Mike Adair madairATdmsolutions.ca
* and Richard Greenwood rich@greenwoodma$p->com
* License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
*/
/** datum object
*/
class proj4phpDatum {
public $datum_type;
public $datum_params;
/**
*
* @param type $proj
*/
public function __construct( $proj ) {
$this->datum_type = Proj4php::$common->PJD_WGS84; //default setting
if( isset($proj->datumCode) && $proj->datumCode == 'none' ) {
$this->datum_type = Proj4php::$common->PJD_NODATUM;
}
if( isset( $proj->datum_params ) ) {
for( $i = 0; $i < sizeof( $proj->datum_params ); $i++ ) {
$proj->datum_params[$i] = floatval( $proj->datum_params[$i] );
}
if( $proj->datum_params[0] != 0 || $proj->datum_params[1] != 0 || $proj->datum_params[2] != 0 ) {
$this->datum_type = Proj4php::$common->PJD_3PARAM;
}
if( sizeof( $proj->datum_params ) > 3 ) {
if( $proj->datum_params[3] != 0 || $proj->datum_params[4] != 0 ||
$proj->datum_params[5] != 0 || $proj->datum_params[6] != 0 ) {
$this->datum_type = Proj4php::$common->PJD_7PARAM;
$proj->datum_params[3] *= Proj4php::$common->SEC_TO_RAD;
$proj->datum_params[4] *= Proj4php::$common->SEC_TO_RAD;
$proj->datum_params[5] *= Proj4php::$common->SEC_TO_RAD;
$proj->datum_params[6] = ($proj->datum_params[6] / 1000000.0) + 1.0;
}
}
$this->datum_params = $proj->datum_params;
}
if( isset( $proj ) ) {
$this->a = $proj->a; //datum object also uses these values
$this->b = $proj->b;
$this->es = $proj->es;
$this->ep2 = $proj->ep2;
#$this->datum_params = $proj->datum_params;
}
}
/**
*
* @param type $dest
* @return boolean Returns TRUE if the two datums match, otherwise FALSE.
* @throws type
*/
public function compare_datums( $dest ) {
if( $this->datum_type != $dest->datum_type ) {
return false; // false, datums are not equal
} else if( $this->a != $dest->a || abs( $this->es - $dest->es ) > 0.000000000050 ) {
// the tolerence for es is to ensure that GRS80 and WGS84
// are considered identical
return false;
} else if( $this->datum_type == Proj4php::$common->PJD_3PARAM ) {
return ($this->datum_params[0] == $dest->datum_params[0]
&& $this->datum_params[1] == $dest->datum_params[1]
&& $this->datum_params[2] == $dest->datum_params[2]);
} else if( $this->datum_type == Proj4php::$common->PJD_7PARAM ) {
return ($this->datum_params[0] == $dest->datum_params[0]
&& $this->datum_params[1] == $dest->datum_params[1]
&& $this->datum_params[2] == $dest->datum_params[2]
&& $this->datum_params[3] == $dest->datum_params[3]
&& $this->datum_params[4] == $dest->datum_params[4]
&& $this->datum_params[5] == $dest->datum_params[5]
&& $this->datum_params[6] == $dest->datum_params[6]);
} else if( $this->datum_type == Proj4php::$common->PJD_GRIDSHIFT ||
$dest->datum_type == Proj4php::$common->PJD_GRIDSHIFT ) {
throw(new Exception( "ERROR: Grid shift transformations are not implemented." ));
return false;
}
return true; // datums are equal
}
/*
* The function Convert_Geodetic_To_Geocentric converts geodetic coordinates
* (latitude, longitude, and height) to geocentric coordinates (X, Y, Z),
* according to the current ellipsoid parameters.
*
* Latitude : Geodetic latitude in radians (input)
* Longitude : Geodetic longitude in radians (input)
* Height : Geodetic height, in meters (input)
* X : Calculated Geocentric X coordinate, in meters (output)
* Y : Calculated Geocentric Y coordinate, in meters (output)
* Z : Calculated Geocentric Z coordinate, in meters (output)
*
*/
public function geodetic_to_geocentric( $p ) {
$Longitude = $p->x;
$Latitude = $p->y;
$Height = isset( $p->z ) ? $p->z : 0; //Z value not always supplied
$Error_Code = 0; // GEOCENT_NO_ERROR;
/*
* * Don't blow up if Latitude is just a little out of the value
* * range as it may just be a rounding issue. Also removed longitude
* * test, it should be wrapped by cos() and sin(). NFW for PROJ.4, Sep/2001.
*/
if( $Latitude < -Proj4php::$common->HALF_PI && $Latitude > -1.001 * Proj4php::$common->HALF_PI ) {
$Latitude = -Proj4php::$common->HALF_PI;
} else if( $Latitude > Proj4php::$common->HALF_PI && $Latitude < 1.001 * Proj4php::$common->HALF_PI ) {
$Latitude = Proj4php::$common->HALF_PI;
} else if( ($Latitude < -Proj4php::$common->HALF_PI) || ($Latitude > Proj4php::$common->HALF_PI) ) {
/* Latitude out of range */
Proj4php::reportError( 'geocent:lat out of range:' . $Latitude );
return null;
}
if( $Longitude > Proj4php::$common->PI )
$Longitude -= (2 * Proj4php::$common->PI);
$Sin_Lat = sin( $Latitude ); /* sin(Latitude) */
$Cos_Lat = cos( $Latitude ); /* cos(Latitude) */
$Sin2_Lat = $Sin_Lat * $Sin_Lat; /* Square of sin(Latitude) */
$Rn = $this->a / (sqrt( 1.0e0 - $this->es * $Sin2_Lat )); /* Earth radius at location */
$p->x = ($Rn + $Height) * $Cos_Lat * cos( $Longitude );
$p->y = ($Rn + $Height) * $Cos_Lat * sin( $Longitude );
$p->z = (($Rn * (1 - $this->es)) + $Height) * $Sin_Lat;
return $Error_Code;
}
/**
*
* @param object $p
* @return type
*/
public function geocentric_to_geodetic( $p ) {
/* local defintions and variables */
/* end-criterium of loop, accuracy of sin(Latitude) */
$genau = 1.E-12;
$genau2 = ($genau * $genau);
$maxiter = 30;
$X = $p->x;
$Y = $p->y;
$Z = $p->z ? $p->z : 0.0; //Z value not always supplied
/*
$P; // distance between semi-minor axis and location
$RR; // distance between center and location
$CT; // sin of geocentric latitude
$ST; // cos of geocentric latitude
$RX;
$RK;
$RN; // Earth radius at location
$CPHI0; // cos of start or old geodetic latitude in iterations
$SPHI0; // sin of start or old geodetic latitude in iterations
$CPHI; // cos of searched geodetic latitude
$SPHI; // sin of searched geodetic latitude
$SDPHI; // end-criterium: addition-theorem of sin(Latitude(iter)-Latitude(iter-1))
$At_Pole; // indicates location is in polar region
$iter; // of continous iteration, max. 30 is always enough (s.a.)
$Longitude;
$Latitude;
$Height;
*/
$At_Pole = false;
$P = sqrt( $X * $X + $Y * $Y );
$RR = sqrt( $X * $X + $Y * $Y + $Z * $Z );
/* special cases for latitude and longitude */
if( $P / $this->a < $genau ) {
/* special case, if P=0. (X=0., Y=0.) */
$At_Pole = true;
$Longitude = 0.0;
/* if (X,Y,Z)=(0.,0.,0.) then Height becomes semi-minor axis
* of ellipsoid (=center of mass), Latitude becomes PI/2 */
if( $RR / $this->a < $genau ) {
$Latitude = Proj4php::$common->HALF_PI;
$Height = -$this->b;
return;
}
} else {
/* ellipsoidal (geodetic) longitude
* interval: -PI < Longitude <= +PI */
$Longitude = atan2( $Y, $X );
}
/* --------------------------------------------------------------
* Following iterative algorithm was developped by
* "Institut für Erdmessung", University of Hannover, July 1988.
* Internet: www.ife.uni-hannover.de
* Iterative computation of CPHI,SPHI and Height.
* Iteration of CPHI and SPHI to 10**-12 radian res$p->
* 2*10**-7 arcsec.
* --------------------------------------------------------------
*/
$CT = $Z / $RR;
$ST = $P / $RR;
$RX = 1.0 / sqrt( 1.0 - $this->es * (2.0 - $this->es) * $ST * $ST );
$CPHI0 = $ST * (1.0 - $this->es) * $RX;
$SPHI0 = $CT * $RX;
$iter = 0;
/* loop to find sin(Latitude) res$p-> Latitude
* until |sin(Latitude(iter)-Latitude(iter-1))| < genau */
do {
++$iter;
$RN = $this->a / sqrt( 1.0 - $this->es * $SPHI0 * $SPHI0 );
/* ellipsoidal (geodetic) height */
$Height = $P * $CPHI0 + $Z * $SPHI0 - $RN * (1.0 - $this->es * $SPHI0 * $SPHI0);
$RK = $this->es * $RN / ($RN + $Height);
$RX = 1.0 / sqrt( 1.0 - $RK * (2.0 - $RK) * $ST * $ST );
$CPHI = $ST * (1.0 - $RK) * $RX;
$SPHI = $CT * $RX;
$SDPHI = $SPHI * $CPHI0 - $CPHI * $SPHI0;
$CPHI0 = $CPHI;
$SPHI0 = $SPHI;
} while( $SDPHI * $SDPHI > $genau2 && $iter < $maxiter );
/* ellipsoidal (geodetic) latitude */
$Latitude = atan( $SPHI / abs( $CPHI ) );
$p->x = $Longitude;
$p->y = $Latitude;
$p->z = $Height;
return $p;
}
/**
* Convert_Geocentric_To_Geodetic
* The method used here is derived from 'An Improved Algorithm for
* Geocentric to Geodetic Coordinate Conversion', by Ralph Toms, Feb 1996
*
* @param object Point $p
* @return object Point $p
*/
public function geocentric_to_geodetic_noniter( $p ) {
/*
$Longitude;
$Latitude;
$Height;
$W; // distance from Z axis
$W2; // square of distance from Z axis
$T0; // initial estimate of vertical component
$T1; // corrected estimate of vertical component
$S0; // initial estimate of horizontal component
$S1; // corrected estimate of horizontal component
$Sin_B0; // sin(B0), B0 is estimate of Bowring aux variable
$Sin3_B0; // cube of sin(B0)
$Cos_B0; // cos(B0)
$Sin_p1; // sin(phi1), phi1 is estimated latitude
$Cos_p1; // cos(phi1)
$Rn; // Earth radius at location
$Sum; // numerator of cos(phi1)
$At_Pole; // indicates location is in polar region
*/
$X = floatval( $p->x ); // cast from string to float
$Y = floatval( $p->y );
$Z = floatval( $p->z ? $p->z : 0 );
$At_Pole = false;
if( $X <> 0.0 ) {
$Longitude = atan2( $Y, $X );
} else {
if( $Y > 0 ) {
$Longitude = Proj4php::$common->HALF_PI;
} else if( Y < 0 ) {
$Longitude = -Proj4php::$common->HALF_PI;
} else {
$At_Pole = true;
$Longitude = 0.0;
if( $Z > 0.0 ) { /* north pole */
$Latitude = Proj4php::$common->HALF_PI;
} else if( Z < 0.0 ) { /* south pole */
$Latitude = -Proj4php::$common->HALF_PI;
} else { /* center of earth */
$Latitude = Proj4php::$common->HALF_PI;
$Height = -$this->b;
return;
}
}
}
$W2 = $X * $X + $Y * $Y;
$W = sqrt( $W2 );
$T0 = $Z * Proj4php::$common->AD_C;
$S0 = sqrt( $T0 * $T0 + $W2 );
$Sin_B0 = $T0 / $S0;
$Cos_B0 = $W / $S0;
$Sin3_B0 = $Sin_B0 * $Sin_B0 * $Sin_B0;
$T1 = $Z + $this->b * $this->ep2 * $Sin3_B0;
$Sum = $W - $this->a * $this->es * $Cos_B0 * $Cos_B0 * $Cos_B0;
$S1 = sqrt( $T1 * $T1 + $Sum * $Sum );
$Sin_p1 = $T1 / $S1;
$Cos_p1 = $Sum / $S1;
$Rn = $this->a / sqrt( 1.0 - $this->es * $Sin_p1 * $Sin_p1 );
if( $Cos_p1 >= Proj4php::$common->COS_67P5 ) {
$Height = $W / $Cos_p1 - $Rn;
} else if( $Cos_p1 <= -Proj4php::$common->COS_67P5 ) {
$Height = $W / -$Cos_p1 - $Rn;
} else {
$Height = $Z / $Sin_p1 + $Rn * ($this->es - 1.0);
}
if( $At_Pole == false ) {
$Latitude = atan( $Sin_p1 / $Cos_p1 );
}
$p->x = $Longitude;
$p->y = $Latitude;
$p->z = $Height;
return $p;
}
/************************************************************** */
// pj_geocentic_to_wgs84( p )
// p = point to transform in geocentric coordinates (x,y,z)
public function geocentric_to_wgs84( $p ) {
if( $this->datum_type == Proj4php::$common->PJD_3PARAM ) {
// if( x[io] == HUGE_VAL )
// continue;
$p->x += $this->datum_params[0];
$p->y += $this->datum_params[1];
$p->z += $this->datum_params[2];
} else if( $this->datum_type == Proj4php::$common->PJD_7PARAM ) {
$Dx_BF = $this->datum_params[0];
$Dy_BF = $this->datum_params[1];
$Dz_BF = $this->datum_params[2];
$Rx_BF = $this->datum_params[3];
$Ry_BF = $this->datum_params[4];
$Rz_BF = $this->datum_params[5];
$M_BF = $this->datum_params[6];
// if( x[io] == HUGE_VAL )
// continue;
$p->x = $M_BF * ( $p->x - $Rz_BF * $p->y + $Ry_BF * $p->z) + $Dx_BF;
$p->y = $M_BF * ( $Rz_BF * $p->x + $p->y - $Rx_BF * $p->z) + $Dy_BF;
$p->z = $M_BF * (-$Ry_BF * $p->x + $Rx_BF * $p->y + $p->z) + $Dz_BF;
}
}
/*************************************************************** */
// pj_geocentic_from_wgs84()
// coordinate system definition,
// point to transform in geocentric coordinates (x,y,z)
public function geocentric_from_wgs84( $p ) {
if( $this->datum_type == Proj4php::$common->PJD_3PARAM ) {
//if( x[io] == HUGE_VAL )
// continue;
$p->x -= $this->datum_params[0];
$p->y -= $this->datum_params[1];
$p->z -= $this->datum_params[2];
} else if( $this->datum_type == Proj4php::$common->PJD_7PARAM ) {
$Dx_BF = $this->datum_params[0];
$Dy_BF = $this->datum_params[1];
$Dz_BF = $this->datum_params[2];
$Rx_BF = $this->datum_params[3];
$Ry_BF = $this->datum_params[4];
$Rz_BF = $this->datum_params[5];
$M_BF = $this->datum_params[6];
$x_tmp = ($p->x - $Dx_BF) / $M_BF;
$y_tmp = ($p->y - $Dy_BF) / $M_BF;
$z_tmp = ($p->z - $Dz_BF) / $M_BF;
//if( x[io] == HUGE_VAL )
// continue;
$p->x = $x_tmp + $Rz_BF * $y_tmp - $Ry_BF * $z_tmp;
$p->y = -$Rz_BF * $x_tmp + $y_tmp + $Rx_BF * $z_tmp;
$p->z = $Ry_BF * $x_tmp - $Rx_BF * $y_tmp + $z_tmp;
} //cs_geocentric_from_wgs84()
}
}