proj4phpProj.php
23.3 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
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
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
<?php
/**
* Author : Julien Moquet
*
* Inspired by Proj4js from Mike Adair madairATdmsolutions.ca
* and Richard Greenwood rich@greenwoodmap.com
* License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
*/
class Proj4phpProj {
/**
* Property: readyToUse
* Flag to indicate if initialization is complete for $this Proj object
*/
public $readyToUse = false;
/**
* Property: title
* The title to describe the projection
*/
public $title = null;
/**
* Property: projName
* The projection class for $this projection, e.g. lcc (lambert conformal conic,
* or merc for mercator). These are exactly equivalent to their Proj4
* counterparts.
*/
public $projName = null;
/**
* Property: projection
* The projection object for $this projection. */
public $projection = null;
/**
* Property: units
* The units of the projection. Values include 'm' and 'degrees'
*/
public $units = null;
/**
* Property: datum
* The datum specified for the projection
*/
public $datum = null;
/**
* Property: x0
* The x coordinate origin
*/
public $x0 = 0;
/**
* Property: y0
* The y coordinate origin
*/
public $y0 = 0;
/**
* Property: localCS
* Flag to indicate if the projection is a local one in which no transforms
* are required.
*/
public $localCS = false;
/**
*
* @var type
*/
protected $wktRE = '/^(\w+)\[(.*)\]$/';
/**
* Constructor: initialize
* Constructor for Proj4php::Proj objects
*
* Parameters:
* $srsCode - a code for map projection definition parameters. These are usually
* (but not always) EPSG codes.
*/
public function __construct( $srsCode ) {
$this->srsCodeInput = $srsCode;
//check to see if $this is a WKT string
if( (strpos( $srsCode, 'GEOGCS' ) !== false) ||
(strpos( $srsCode, 'GEOCCS' ) !== false) ||
(strpos( $srsCode, 'PROJCS' ) !== false) ||
(strpos( $srsCode, 'LOCAL_CS' ) !== false) ) {
$this->parseWKT( $srsCode );
$this->deriveConstants();
$this->loadProjCode( $this->projName );
return;
}
// DGR 2008-08-03 : support urn and url
if( strpos( $srsCode, 'urn:' ) === 0 ) {
//urn:ORIGINATOR:def:crs:CODESPACE:VERSION:ID
$urn = explode( ':', $srsCode );
if( ($urn[1] == 'ogc' || $urn[1] == 'x-ogc') &&
($urn[2] == 'def') &&
($urn[3] == 'crs') ) {
$srsCode = $urn[4] . ':' . $urn[strlen( $urn ) - 1];
}
} else if( strpos( $srsCode, 'http://' ) === 0 ) {
//url#ID
$url = explode( '#', $srsCode );
if( preg_match( "/epsg.org/", $url[0] ) ) {
// http://www.epsg.org/#
$srsCode = 'EPSG:' . $url[1];
} else if( preg_match( "/RIG.xml/", $url[0] ) ) {
//http://librairies.ign.fr/geoportail/resources/RIG.xml#
//http://interop.ign.fr/registers/ign/RIG.xml#
$srsCode = 'IGNF:' . $url[1];
}
}
$this->srsCode = strtoupper( $srsCode );
if( strpos( $this->srsCode, "EPSG" ) === 0 ) {
$this->srsCode = $this->srsCode;
$this->srsAuth = 'epsg';
$this->srsProjNumber = substr( $this->srsCode, 5 );
// DGR 2007-11-20 : authority IGNF
} else if( strpos( $this->srsCode, "IGNF" ) === 0 ) {
$this->srsCode = $this->srsCode;
$this->srsAuth = 'IGNF';
$this->srsProjNumber = substr( $this->srsCode, 5 );
// DGR 2008-06-19 : pseudo-authority CRS for WMS
} else if( strpos( $this->srsCode, "CRS" ) === 0 ) {
$this->srsCode = $this->srsCode;
$this->srsAuth = 'CRS';
$this->srsProjNumber = substr( $this->srsCode, 4 );
} else {
$this->srsAuth = '';
$this->srsProjNumber = $this->srsCode;
}
$this->loadProjDefinition();
}
/**
* Function: loadProjDefinition
* Loads the coordinate system initialization string if required.
* Note that dynamic loading happens asynchronously so an application must
* wait for the readyToUse property is set to true.
* To prevent dynamic loading, include the defs through a script tag in
* your application.
*
*/
public function loadProjDefinition() {
//check in memory
if( array_key_exists( $this->srsCode, Proj4php::$defs ) ) {
$this->defsLoaded();
return;
}
//else check for def on the server
$filename = dirname( __FILE__ ) . '/defs/' . strtoupper( $this->srsAuth ) . $this->srsProjNumber . '.php';
try {
Proj4php::loadScript( $filename );
$this->defsLoaded(); // succes
} catch ( Exception $e ) {
$this->loadFromService(); // fail
}
}
/**
* Function: loadFromService
* Creates the REST URL for loading the definition from a web service and
* loads it.
*
*
* DO IT AGAIN. : SHOULD PHP CODE BE GET BY WEBSERVICES ?
*/
public function loadFromService() {
//else load from web service
$url = Proj4php::$defsLookupService . '/' . $this->srsAuth . '/' . $this->srsProjNumber . '/proj4/';
try {
Proj4php::$defs[strtoupper($this->srsAuth) . ":" . $this->srsProjNumber] = Proj4php::loadScript( $url );
} catch ( Exception $e ) {
$this->defsFailed();
}
}
/**
* Function: defsLoaded
* Continues the Proj object initilization once the def file is loaded
*
*/
public function defsLoaded() {
$this->parseDefs();
$this->loadProjCode( $this->projName );
}
/**
* Function: checkDefsLoaded
* $this is the loadCheck method to see if the def object exists
*
*/
public function checkDefsLoaded() {
return isset(Proj4php::$defs[$this->srsCode]) && !empty(Proj4php::$defs[$this->srsCode]);
}
/**
* Function: defsFailed
* Report an error in loading the defs file, but continue on using WGS84
*
*/
public function defsFailed() {
Proj4php::reportError( 'failed to load projection definition for: ' . $this->srsCode );
Proj4php::$defs[$this->srsCode] = Proj4php::$defs['WGS84']; //set it to something so it can at least continue
$this->defsLoaded();
}
/**
* Function: loadProjCode
* Loads projection class code dynamically if required.
* Projection code may be included either through a script tag or in
* a built version of proj4php
*
* An exception occurs if the projection is not found.
*/
public function loadProjCode( $projName ) {
if( array_key_exists( $projName, Proj4php::$proj )) {
$this->initTransforms();
return;
}
//the filename for the projection code
$filename = dirname( __FILE__ ) . '/projCode/' . $projName . '.php';
try {
Proj4php::loadScript( $filename );
$this->loadProjCodeSuccess( $projName );
} catch ( Exception $e ) {
$this->loadProjCodeFailure( $projName );
}
}
/**
* Function: loadProjCodeSuccess
* Loads any proj dependencies or continue on to final initialization.
*
*/
public function loadProjCodeSuccess( $projName ) {
if( isset(Proj4php::$proj[$projName]->dependsOn) && !empty(Proj4php::$proj[$projName]->dependsOn)) {
$this->loadProjCode( Proj4php::$proj[$projName]->dependsOn );
} else {
$this->initTransforms();
}
}
/**
* Function: defsFailed
* Report an error in loading the proj file. Initialization of the Proj
* object has failed and the readyToUse flag will never be set.
*
*/
public function loadProjCodeFailure( $projName ) {
Proj4php::reportError( "failed to find projection file for: " . $projName );
//TBD initialize with identity transforms so proj will still work?
}
/**
* Function: checkCodeLoaded
* $this is the loadCheck method to see if the projection code is loaded
*
*/
public function checkCodeLoaded( $projName ) {
return isset(Proj4php::$proj[$projName]) && !empty(Proj4php::$proj[$projName]);
}
/**
* Function: initTransforms
* Finalize the initialization of the Proj object
*
*/
public function initTransforms() {
$this->projection = new Proj4php::$proj[$this->projName];
Proj4php::extend( $this->projection, $this );
// initiate depending class
if( false !== ($dependsOn = isset($this->projection->dependsOn) && !empty($this->projection->dependsOn) ? $this->projection->dependsOn : false) )
{
Proj4php::extend( Proj4php::$proj[$dependsOn], $this->projection);
Proj4php::$proj[$dependsOn]->init();
Proj4php::extend( $this->projection, Proj4php::$proj[$dependsOn] );
}
$this->init();
$this->readyToUse = true;
}
/**
*
*/
public function init() {
$this->projection->init();
}
/**
*
* @param type $pt
* @return type
*/
public function forward( $pt ) {
return $this->projection->forward( $pt );
}
/**
*
* @param type $pt
* @return type
*/
public function inverse( $pt ) {
return $this->projection->inverse( $pt );
}
/**
* Function: parseWKT
* Parses a WKT string to get initialization parameters
*
*/
public function parseWKT( $wkt ) {
if( false === ($match = preg_match( $this->wktRE, $wkt, $wktMatch )) )
return;
$wktObject = $wktMatch[1];
$wktContent = $wktMatch[2];
$wktTemp = explode( ",", $wktContent );
$wktName = (strtoupper($wktObject) == "TOWGS84") ? "TOWGS84" : array_shift( $wktTemp );
$wktName = preg_replace( '/^\"/', "", $wktName );
$wktName = preg_replace( '/\"$/', "", $wktName );
/*
$wktContent = implode(",",$wktTemp);
$wktArray = explode("],",$wktContent);
for ($i=0; i<sizeof($wktArray)-1; ++$i) {
$wktArray[$i] .= "]";
}
*/
$wktArray = array();
$bkCount = 0;
$obj = "";
foreach( $wktTemp as $token ) {
$bkCount = substr_count($token, "[") - substr_count($token, "]");
// ???
$obj .= $token;
if( $bkCount === 0 ) {
array_push( $wktArray, $obj );
$obj = "";
} else {
$obj .= ",";
}
}
//do something based on the type of the wktObject being parsed
//add in variations in the spelling as required
switch( $wktObject ) {
case 'LOCAL_CS':
$this->projName = 'identity';
$this->localCS = true;
$this->srsCode = $wktName;
break;
case 'GEOGCS':
$this->projName = 'longlat';
$this->geocsCode = $wktName;
if( !$this->srsCode )
$this->srsCode = $wktName;
break;
case 'PROJCS':
$$this->srsCode = $wktName;
break;
case 'GEOCCS':
break;
case 'PROJECTION':
$this->projName = Proj4php::$wktProjections[$wktName];
break;
case 'DATUM':
$this->datumName = $wktName;
break;
case 'LOCAL_DATUM':
$this->datumCode = 'none';
break;
case 'SPHEROID':
$this->ellps = $wktName;
$this->a = floatval( array_shift( $wktArray ) );
$this->rf = floatval( array_shift( $wktArray ) );
break;
case 'PRIMEM':
$this->from_greenwich = floatval( array_shift( $wktArray ) ); //to radians?
break;
case 'UNIT':
$this->units = $wktName;
$this->unitsPerMeter = floatval( array_shift( $wktArray ) );
break;
case 'PARAMETER':
$name = strtolower( $wktName );
$value = floatval( array_shift( $wktArray ) );
//there may be many variations on the wktName values, add in case
//statements as required
switch( $name ) {
case 'false_easting':
$this->x0 = $value;
break;
case 'false_northing':
$this->y0 = $value;
break;
case 'scale_factor':
$this->k0 = $value;
break;
case 'central_meridian':
$this->long0 = $value * Proj4php::$common->D2R;
break;
case 'latitude_of_origin':
$this->lat0 = $value * Proj4php::$common->D2R;
break;
case 'more_here':
break;
default:
break;
}
break;
case 'TOWGS84':
$this->datum_params = $wktArray;
break;
//DGR 2010-11-12: AXIS
case 'AXIS':
$name = strtolower( $wktName );
$value = array_shift( $wktArray );
switch( $value ) {
case 'EAST' : $value = 'e';
break;
case 'WEST' : $value = 'w';
break;
case 'NORTH': $value = 'n';
break;
case 'SOUTH': $value = 's';
break;
case 'UP' : $value = 'u';
break;
case 'DOWN' : $value = 'd';
break;
case 'OTHER':
default : $value = ' ';
break; //FIXME
}
if( !$this->axis ) {
$this->axis = "enu";
}
switch( $name ) {
case 'X': $this->axis = $value . substr( $this->axis, 1, 2 );
break;
case 'Y': $this->axis = substr( $this->axis, 0, 1 ) . $value . substr( $this->axis, 2, 1 );
break;
case 'Z': $this->axis = substr( $this->axis, 0, 2 ) . $value;
break;
default : break;
}
case 'MORE_HERE':
break;
default:
break;
}
foreach( $wktArray as $wktArrayContent )
$this->parseWKT( $wktArrayContent );
}
/**
* Function: parseDefs
* Parses the PROJ.4 initialization string and sets the associated properties.
*
*/
public function parseDefs() {
$this->defData = Proj4php::$defs[$this->srsCode];
#$paramName;
#$paramVal;
if( !$this->defData ) {
return;
}
$paramArray = explode( "+", $this->defData );
for( $prop = 0; $prop < sizeof( $paramArray ); $prop++ ) {
if( strlen( $paramArray[$prop] ) == 0 )
continue;
$property = explode( "=", $paramArray[$prop] );
$paramName = strtolower( $property[0] );
if( sizeof( $property ) >= 2 ) {
$paramVal = $property[1];
}
switch( trim( $paramName ) ) { // trim out spaces
case "": break; // throw away nameless parameter
case "title": $this->title = $paramVal;
break;
case "proj": $this->projName = trim( $paramVal );
break;
case "units": $this->units = trim( $paramVal );
break;
case "datum": $this->datumCode = trim( $paramVal );
break;
case "nadgrids": $this->nagrids = trim( $paramVal );
break;
case "ellps": $this->ellps = trim( $paramVal );
break;
case "a": $this->a = floatval( $paramVal );
break; // semi-major radius
case "b": $this->b = floatval( $paramVal );
break; // semi-minor radius
// DGR 2007-11-20
case "rf": $this->rf = floatval( paramVal );
break; // inverse flattening rf= a/(a-b)
case "lat_0": $this->lat0 = $paramVal * Proj4php::$common->D2R;
break; // phi0, central latitude
case "lat_1": $this->lat1 = $paramVal * Proj4php::$common->D2R;
break; //standard parallel 1
case "lat_2": $this->lat2 = $paramVal * Proj4php::$common->D2R;
break; //standard parallel 2
case "lat_ts": $this->lat_ts = $paramVal * Proj4php::$common->D2R;
break; // used in merc and eqc
case "lon_0": $this->long0 = $paramVal * Proj4php::$common->D2R;
break; // lam0, central longitude
case "alpha": $this->alpha = floatval( $paramVal ) * Proj4php::$common->D2R;
break; //for somerc projection
case "lonc": $this->longc = paramVal * Proj4php::$common->D2R;
break; //for somerc projection
case "x_0": $this->x0 = floatval( $paramVal );
break; // false easting
case "y_0": $this->y0 = floatval( $paramVal );
break; // false northing
case "k_0": $this->k0 = floatval( $paramVal );
break; // projection scale factor
case "k": $this->k0 = floatval( $paramVal );
break; // both forms returned
case "r_a": $this->R_A = true;
break; // sphere--area of ellipsoid
case "zone": $this->zone = intval( $paramVal, 10 );
break; // UTM Zone
case "south": $this->utmSouth = true;
break; // UTM north/south
case "towgs84": $this->datum_params = explode( ",", $paramVal );
break;
case "to_meter": $this->to_meter = floatval( $paramVal );
break; // cartesian scaling
case "from_greenwich": $this->from_greenwich = $paramVal * Proj4php::$common->D2R;
break;
// DGR 2008-07-09 : if pm is not a well-known prime meridian take
// the value instead of 0.0, then convert to radians
case "pm": $paramVal = trim( $paramVal );
$this->from_greenwich = Proj4php::$primeMeridian[$paramVal] ? Proj4php::$primeMeridian[$paramVal] : floatval( $paramVal );
$this->from_greenwich *= Proj4php::$common->D2R;
break;
// DGR 2010-11-12: axis
case "axis": $paramVal = trim( $paramVal );
$legalAxis = "ewnsud";
if( strlen( paramVal ) == 3 &&
strpos( $legalAxis, substr( $paramVal, 0, 1 ) ) !== false &&
strpos( $legalAxis, substr( $paramVal, 1, 1 ) ) !== false &&
strpos( $legalAxis, substr( $paramVal, 2, 1 ) ) !== false ) {
$this->axis = $paramVal;
} //FIXME: be silent ?
break;
case "no_defs": break;
default: //alert("Unrecognized parameter: " . paramName);
} // switch()
} // for paramArray
$this->deriveConstants();
}
/**
* Function: deriveConstants
* Sets several derived constant values and initialization of datum and ellipse parameters.
*
*/
public function deriveConstants() {
if( isset( $this->nagrids ) && $this->nagrids == '@null' )
$this->datumCode = 'none';
if( isset( $this->datumCode ) && $this->datumCode != 'none' ) {
$datumDef = Proj4php::$datum[$this->datumCode];
if( is_array($datumDef ) ) {
$this->datum_params = array_key_exists( 'towgs84', $datumDef ) ? explode( ',', $datumDef['towgs84'] ) : null;
$this->ellps = $datumDef['ellipse'];
$this->datumName = array_key_exists( 'datumName', $datumDef ) ? $datumDef['datumName'] : $this->datumCode;
}
}
if( !isset( $this->a ) ) { // do we have an ellipsoid?
if( !isset( $this->ellps ) || strlen( $this->ellps ) == 0 || !array_key_exists( $this->ellps, Proj4php::$ellipsoid ) )
$ellipse = Proj4php::$ellipsoid['WGS84'];
else {
$ellipse = Proj4php::$ellipsoid[$this->ellps];
}
Proj4php::extend( $this, $ellipse );
}
if( isset( $this->rf ) && !isset( $this->b ) )
$this->b = (1.0 - 1.0 / $this->rf) * $this->a;
if ( (isset($this->rf) && $this->rf === 0) || abs($this->a - $this->b) < Proj4php::$common->EPSLN) {
$this->sphere = true;
$this->b = $this->a;
}
$this->a2 = $this->a * $this->a; // used in geocentric
$this->b2 = $this->b * $this->b; // used in geocentric
$this->es = ($this->a2 - $this->b2) / $this->a2; // e ^ 2
$this->e = sqrt( $this->es ); // eccentricity
if( isset( $this->R_A ) ) {
$this->a *= 1. - $this->es * (Proj4php::$common->SIXTH + $this->es * (Proj4php::$common->RA4 + $this->es * Proj4php::$common->RA6));
$this->a2 = $this->a * $this->a;
$this->b2 = $this->b * $this->b;
$this->es = 0.0;
}
$this->ep2 = ($this->a2 - $this->b2) / $this->b2; // used in geocentric
if( !isset( $this->k0 ) )
$this->k0 = 1.0; //default value
//DGR 2010-11-12: axis
if( !isset( $this->axis ) ) {
$this->axis = "enu";
}
$this->datum = new Proj4phpDatum( $this );
}
}