forked from egaxegax/dbcartajs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproj4js-combined.js
5525 lines (4795 loc) · 185 KB
/
proj4js-combined.js
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
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
proj4js.js -- Javascript reprojection library.
Authors: Mike Adair madairATdmsolutions.ca
Richard Greenwood richATgreenwoodmap.com
Didier Richard didier.richardATign.fr
Stephen Irons stephen.ironsATclear.net.nz
Olivier Terral oterralATgmail.com
License:
Copyright (c) 2012, Mike Adair, Richard Greenwood, Didier Richard,
Stephen Irons and Olivier Terral
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.
Note: This program is an almost direct port of the C library PROJ.4.
*/
/* ======================================================================
proj4js.js
====================================================================== */
/*
Author: Mike Adair madairATdmsolutions.ca
Richard Greenwood [email protected]
License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
$Id: Proj.js 2956 2007-07-09 12:17:52Z steven $
*/
/**
* Namespace: Proj4js
*
* Proj4js is a JavaScript library to transform point coordinates from one
* coordinate system to another, including datum transformations.
*
* This library is a port of both the Proj.4 and GCTCP C libraries to JavaScript.
* Enabling these transformations in the browser allows geographic data stored
* in different projections to be combined in browser-based web mapping
* applications.
*
* Proj4js must have access to coordinate system initialization strings (which
* are the same as for PROJ.4 command line). Thes can be included in your
* application using a <script> tag or Proj4js can load CS initialization
* strings from a local directory or a web service such as spatialreference.org.
*
* Similarly, Proj4js must have access to projection transform code. These can
* be included individually using a <script> tag in your page, built into a
* custom build of Proj4js or loaded dynamically at run-time. Using the
* -combined and -compressed versions of Proj4js includes all projection class
* code by default.
*
* Note that dynamic loading of defs and code happens ascynchrously, check the
* Proj.readyToUse flag before using the Proj object. If the defs and code
* required by your application are loaded through script tags, dynamic loading
* is not required and the Proj object will be readyToUse on return from the
* constructor.
*
* All coordinates are handled as points which have a .x and a .y property
* which will be modified in place.
*
* Override Proj4js.reportError for output of alerts and warnings.
*
* See http://trac.osgeo.org/proj4js/wiki/UserGuide for full details.
*/
/**
* Global namespace object for Proj4js library
*/
var Proj4js = {
/**
* Property: defaultDatum
* The datum to use when no others a specified
*/
defaultDatum: 'WGS84', //default datum
/**
* Method: transform(source, dest, point)
* Transform a point coordinate from one map projection to another. This is
* really the only public method you should need to use.
*
* Parameters:
* source - {Proj4js.Proj} source map projection for the transformation
* dest - {Proj4js.Proj} destination map projection for the transformation
* point - {Object} point to transform, may be geodetic (long, lat) or
* projected Cartesian (x,y), but should always have x,y properties.
*/
transform: function(source, dest, point) {
if (!source.readyToUse) {
this.reportError("Proj4js initialization for:"+source.srsCode+" not yet complete");
return point;
}
if (!dest.readyToUse) {
this.reportError("Proj4js initialization for:"+dest.srsCode+" not yet complete");
return point;
}
// Workaround for datum shifts towgs84, if either source or destination projection is not wgs84
if (source.datum && dest.datum && (
((source.datum.datum_type == Proj4js.common.PJD_3PARAM || source.datum.datum_type == Proj4js.common.PJD_7PARAM) && dest.datumCode != "WGS84") ||
((dest.datum.datum_type == Proj4js.common.PJD_3PARAM || dest.datum.datum_type == Proj4js.common.PJD_7PARAM) && source.datumCode != "WGS84"))) {
var wgs84 = Proj4js.WGS84;
this.transform(source, wgs84, point);
source = wgs84;
}
// DGR, 2010/11/12
if (source.axis!="enu") {
this.adjust_axis(source,false,point);
}
// Transform source points to long/lat, if they aren't already.
if ( source.projName=="longlat") {
point.x *= Proj4js.common.D2R; // convert degrees to radians
point.y *= Proj4js.common.D2R;
} else {
if (source.to_meter) {
point.x *= source.to_meter;
point.y *= source.to_meter;
}
source.inverse(point); // Convert Cartesian to longlat
}
// Adjust for the prime meridian if necessary
if (source.from_greenwich) {
point.x += source.from_greenwich;
}
// Convert datums if needed, and if possible.
point = this.datum_transform( source.datum, dest.datum, point );
// Adjust for the prime meridian if necessary
if (dest.from_greenwich) {
point.x -= dest.from_greenwich;
}
if( dest.projName=="longlat" ) {
// convert radians to decimal degrees
point.x *= Proj4js.common.R2D;
point.y *= Proj4js.common.R2D;
} else { // else project
dest.forward(point);
if (dest.to_meter) {
point.x /= dest.to_meter;
point.y /= dest.to_meter;
}
}
// DGR, 2010/11/12
if (dest.axis!="enu") {
this.adjust_axis(dest,true,point);
}
return point;
}, // transform()
/** datum_transform()
source coordinate system definition,
destination coordinate system definition,
point to transform in geodetic coordinates (long, lat, height)
*/
datum_transform : function( source, dest, point ) {
// Short cut if the datums are identical.
if( source.compare_datums( dest ) ) {
return point; // in this case, zero is sucess,
// whereas cs_compare_datums returns 1 to indicate TRUE
// confusing, should fix this
}
// Explicitly skip datum transform by setting 'datum=none' as parameter for either source or dest
if( source.datum_type == Proj4js.common.PJD_NODATUM
|| dest.datum_type == Proj4js.common.PJD_NODATUM) {
return point;
}
// Do we need to go through geocentric coordinates?
if( source.es != dest.es || source.a != dest.a
|| source.datum_type == Proj4js.common.PJD_3PARAM
|| source.datum_type == Proj4js.common.PJD_7PARAM
|| dest.datum_type == Proj4js.common.PJD_3PARAM
|| dest.datum_type == Proj4js.common.PJD_7PARAM)
{
// Convert to geocentric coordinates.
source.geodetic_to_geocentric( point );
// CHECK_RETURN;
// Convert between datums
if( source.datum_type == Proj4js.common.PJD_3PARAM || source.datum_type == Proj4js.common.PJD_7PARAM ) {
source.geocentric_to_wgs84(point);
// CHECK_RETURN;
}
if( dest.datum_type == Proj4js.common.PJD_3PARAM || dest.datum_type == Proj4js.common.PJD_7PARAM ) {
dest.geocentric_from_wgs84(point);
// CHECK_RETURN;
}
// Convert back to geodetic coordinates
dest.geocentric_to_geodetic( point );
// CHECK_RETURN;
}
return point;
}, // cs_datum_transform
/**
* Function: adjust_axis
* Normalize or de-normalized the x/y/z axes. The normal form is "enu"
* (easting, northing, up).
* Parameters:
* crs {Proj4js.Proj} the coordinate reference system
* denorm {Boolean} when false, normalize
* point {Object} the coordinates to adjust
*/
adjust_axis: function(crs, denorm, point) {
var xin= point.x, yin= point.y, zin= point.z || 0.0;
var v, t;
for (var i= 0; i<3; i++) {
if (denorm && i==2 && point.z===undefined) { continue; }
if (i==0) { v= xin; t= 'x'; }
else if (i==1) { v= yin; t= 'y'; }
else { v= zin; t= 'z'; }
switch(crs.axis[i]) {
case 'e':
point[t]= v;
break;
case 'w':
point[t]= -v;
break;
case 'n':
point[t]= v;
break;
case 's':
point[t]= -v;
break;
case 'u':
if (point[t]!==undefined) { point.z= v; }
break;
case 'd':
if (point[t]!==undefined) { point.z= -v; }
break;
default :
alert("ERROR: unknow axis ("+crs.axis[i]+") - check definition of "+crs.projName);
return null;
}
}
return point;
},
/**
* Function: reportError
* An internal method to report errors back to user.
* Override this in applications to report error messages or throw exceptions.
*/
reportError: function(msg) {
//console.log(msg);
},
/**
*
* Title: Private Methods
* The following properties and methods are intended for internal use only.
*
* This is a minimal implementation of JavaScript inheritance methods so that
* Proj4js can be used as a stand-alone library.
* These are copies of the equivalent OpenLayers methods at v2.7
*/
/**
* Function: extend
* Copy all properties of a source object to a destination object. Modifies
* the passed in destination object. Any properties on the source object
* that are set to undefined will not be (re)set on the destination object.
*
* Parameters:
* destination - {Object} The object that will be modified
* source - {Object} The object with properties to be set on the destination
*
* Returns:
* {Object} The destination object.
*/
extend: function(destination, source) {
destination = destination || {};
if(source) {
for(var property in source) {
var value = source[property];
if(value !== undefined) {
destination[property] = value;
}
}
}
return destination;
},
/**
* Constructor: Class
* Base class used to construct all other classes. Includes support for
* multiple inheritance.
*
*/
Class: function() {
var Class = function() {
this.initialize.apply(this, arguments);
};
var extended = {};
var parent;
for(var i=0; i<arguments.length; ++i) {
if(typeof arguments[i] == "function") {
// get the prototype of the superclass
parent = arguments[i].prototype;
} else {
// in this case we're extending with the prototype
parent = arguments[i];
}
Proj4js.extend(extended, parent);
}
Class.prototype = extended;
return Class;
},
/**
* Function: bind
* Bind a function to an object. Method to easily create closures with
* 'this' altered.
*
* Parameters:
* func - {Function} Input function.
* object - {Object} The object to bind to the input function (as this).
*
* Returns:
* {Function} A closure with 'this' set to the passed in object.
*/
bind: function(func, object) {
// create a reference to all arguments past the second one
var args = Array.prototype.slice.apply(arguments, [2]);
return function() {
// Push on any additional arguments from the actual function call.
// These will come after those sent to the bind call.
var newArgs = args.concat(
Array.prototype.slice.apply(arguments, [0])
);
return func.apply(object, newArgs);
};
},
/**
* The following properties and methods handle dynamic loading of JSON objects.
*/
/**
* Property: scriptName
* {String} The filename of this script without any path.
*/
scriptName: "proj4js-combined.js",
/**
* Property: defsLookupService
* AJAX service to retreive projection definition parameters from
*/
defsLookupService: 'http://spatialreference.org/ref',
/**
* Property: libPath
* internal: http server path to library code.
*/
libPath: null,
/**
* Function: getScriptLocation
* Return the path to this script.
*
* Returns:
* Path to this script
*/
getScriptLocation: function () {
if (this.libPath) return this.libPath;
var scriptName = this.scriptName;
var scriptNameLen = scriptName.length;
var scripts = document.getElementsByTagName('script');
for (var i = 0; i < scripts.length; i++) {
var src = scripts[i].getAttribute('src');
if (src) {
var index = src.lastIndexOf(scriptName);
// is it found, at the end of the URL?
if ((index > -1) && (index + scriptNameLen == src.length)) {
this.libPath = src.slice(0, -scriptNameLen);
break;
}
}
}
return this.libPath||"";
},
/**
* Function: loadScript
* Load a JS file from a URL into a <script> tag in the page.
*
* Parameters:
* url - {String} The URL containing the script to load
* onload - {Function} A method to be executed when the script loads successfully
* onfail - {Function} A method to be executed when there is an error loading the script
* loadCheck - {Function} A boolean method that checks to see if the script
* has loaded. Typically this just checks for the existance of
* an object in the file just loaded.
*/
loadScript: function(url, onload, onfail, loadCheck) {
var script = document.createElement('script');
script.defer = false;
script.type = "text/javascript";
script.id = url;
script.src = url;
script.onload = onload;
script.onerror = onfail;
script.loadCheck = loadCheck;
if (/MSIE/.test(navigator.userAgent)) {
script.onreadystatechange = this.checkReadyState;
}
document.getElementsByTagName('head')[0].appendChild(script);
},
/**
* Function: checkReadyState
* IE workaround since there is no onerror handler. Calls the user defined
* loadCheck method to determine if the script is loaded.
*
*/
checkReadyState: function() {
if (this.readyState == 'loaded') {
if (!this.loadCheck()) {
this.onerror();
} else {
this.onload();
}
}
}
};
/**
* Class: Proj4js.Proj
*
* Proj objects provide transformation methods for point coordinates
* between geodetic latitude/longitude and a projected coordinate system.
* once they have been initialized with a projection code.
*
* Initialization of Proj objects is with a projection code, usually EPSG codes,
* which is the key that will be used with the Proj4js.defs array.
*
* The code passed in will be stripped of colons and converted to uppercase
* to locate projection definition files.
*
* A projection object has properties for units and title strings.
*/
Proj4js.Proj = Proj4js.Class({
/**
* Property: readyToUse
* Flag to indicate if initialization is complete for this Proj object
*/
readyToUse: false,
/**
* Property: title
* The title to describe the projection
*/
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.
*/
projName: null,
/**
* Property: units
* The units of the projection. Values include 'm' and 'degrees'
*/
units: null,
/**
* Property: datum
* The datum specified for the projection
*/
datum: null,
/**
* Property: x0
* The x coordinate origin
*/
x0: 0,
/**
* Property: y0
* The y coordinate origin
*/
y0: 0,
/**
* Property: localCS
* Flag to indicate if the projection is a local one in which no transforms
* are required.
*/
localCS: false,
/**
* Property: queue
* Buffer (FIFO) to hold callbacks waiting to be called when projection loaded.
*/
queue: null,
/**
* Constructor: initialize
* Constructor for Proj4js.Proj objects
*
* Parameters:
* srsCode - a code for map projection definition parameters. These are usually
* (but not always) EPSG codes.
*/
initialize: function(srsCode, callback) {
this.srsCodeInput = srsCode;
//Register callbacks prior to attempting to process definition
this.queue = [];
if( callback ){
this.queue.push( callback );
}
//check to see if this is a WKT string
if ((srsCode.indexOf('GEOGCS') >= 0) ||
(srsCode.indexOf('GEOCCS') >= 0) ||
(srsCode.indexOf('PROJCS') >= 0) ||
(srsCode.indexOf('LOCAL_CS') >= 0)) {
this.parseWKT(srsCode);
this.deriveConstants();
this.loadProjCode(this.projName);
return;
}
// DGR 2008-08-03 : support urn and url
if (srsCode.indexOf('urn:') == 0) {
//urn:ORIGINATOR:def:crs:CODESPACE:VERSION:ID
var urn = srsCode.split(':');
if ((urn[1] == 'ogc' || urn[1] =='x-ogc') &&
(urn[2] =='def') &&
(urn[3] =='crs')) {
srsCode = urn[4]+':'+urn[urn.length-1];
}
} else if (srsCode.indexOf('http://') == 0) {
//url#ID
var url = srsCode.split('#');
if (url[0].match(/epsg.org/)) {
// http://www.epsg.org/#
srsCode = 'EPSG:'+url[1];
} else if (url[0].match(/RIG.xml/)) {
//http://librairies.ign.fr/geoportail/resources/RIG.xml#
//http://interop.ign.fr/registers/ign/RIG.xml#
srsCode = 'IGNF:'+url[1];
}
}
this.srsCode = srsCode.toUpperCase();
if (this.srsCode.indexOf("EPSG") == 0) {
this.srsCode = this.srsCode;
this.srsAuth = 'epsg';
this.srsProjNumber = this.srsCode.substring(5);
// DGR 2007-11-20 : authority IGNF
} else if (this.srsCode.indexOf("IGNF") == 0) {
this.srsCode = this.srsCode;
this.srsAuth = 'IGNF';
this.srsProjNumber = this.srsCode.substring(5);
// DGR 2008-06-19 : pseudo-authority CRS for WMS
} else if (this.srsCode.indexOf("CRS") == 0) {
this.srsCode = this.srsCode;
this.srsAuth = 'CRS';
this.srsProjNumber = this.srsCode.substring(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.
*
*/
loadProjDefinition: function() {
//check in memory
if (Proj4js.defs[this.srsCode]) {
this.defsLoaded();
return;
}
//else check for def on the server
var url = Proj4js.getScriptLocation() + 'defs/' + this.srsAuth.toUpperCase() + this.srsProjNumber + '.js';
Proj4js.loadScript(url,
Proj4js.bind(this.defsLoaded, this),
Proj4js.bind(this.loadFromService, this),
Proj4js.bind(this.checkDefsLoaded, this) );
},
/**
* Function: loadFromService
* Creates the REST URL for loading the definition from a web service and
* loads it.
*
*/
loadFromService: function() {
//else load from web service
var url = Proj4js.defsLookupService +'/' + this.srsAuth +'/'+ this.srsProjNumber + '/proj4js/';
Proj4js.loadScript(url,
Proj4js.bind(this.defsLoaded, this),
Proj4js.bind(this.defsFailed, this),
Proj4js.bind(this.checkDefsLoaded, this) );
},
/**
* Function: defsLoaded
* Continues the Proj object initilization once the def file is loaded
*
*/
defsLoaded: function() {
this.parseDefs();
this.loadProjCode(this.projName);
},
/**
* Function: checkDefsLoaded
* This is the loadCheck method to see if the def object exists
*
*/
checkDefsLoaded: function() {
if (Proj4js.defs[this.srsCode]) {
return true;
} else {
return false;
}
},
/**
* Function: defsFailed
* Report an error in loading the defs file, but continue on using WGS84
*
*/
defsFailed: function() {
Proj4js.reportError('failed to load projection definition for: '+this.srsCode);
Proj4js.defs[this.srsCode] = Proj4js.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 proj4js
*
*/
loadProjCode: function(projName) {
if (Proj4js.Proj[projName]) {
this.initTransforms();
return;
}
//the URL for the projection code
var url = Proj4js.getScriptLocation() + 'projCode/' + projName + '.js';
Proj4js.loadScript(url,
Proj4js.bind(this.loadProjCodeSuccess, this, projName),
Proj4js.bind(this.loadProjCodeFailure, this, projName),
Proj4js.bind(this.checkCodeLoaded, this, projName) );
},
/**
* Function: loadProjCodeSuccess
* Loads any proj dependencies or continue on to final initialization.
*
*/
loadProjCodeSuccess: function(projName) {
if (Proj4js.Proj[projName].dependsOn){
this.loadProjCode(Proj4js.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.
*
*/
loadProjCodeFailure: function(projName) {
Proj4js.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
*
*/
checkCodeLoaded: function(projName) {
if (Proj4js.Proj[projName]) {
return true;
} else {
return false;
}
},
/**
* Function: initTransforms
* Finalize the initialization of the Proj object
*
*/
initTransforms: function() {
Proj4js.extend(this, Proj4js.Proj[this.projName]);
this.init();
this.readyToUse = true;
if( this.queue ) {
var item;
while( (item = this.queue.shift()) ) {
item.call( this, this );
}
}
},
/**
* Function: parseWKT
* Parses a WKT string to get initialization parameters
*
*/
wktRE: /^(\w+)\[(.*)\]$/,
parseWKT: function(wkt) {
var wktMatch = wkt.match(this.wktRE);
if (!wktMatch) return;
var wktObject = wktMatch[1];
var wktContent = wktMatch[2];
var wktTemp = wktContent.split(",");
var wktName;
if (wktObject.toUpperCase() == "TOWGS84") {
wktName = wktObject; //no name supplied for the TOWGS84 array
} else {
wktName = wktTemp.shift();
}
wktName = wktName.replace(/^\"/,"");
wktName = wktName.replace(/\"$/,"");
/*
wktContent = wktTemp.join(",");
var wktArray = wktContent.split("],");
for (var i=0; i<wktArray.length-1; ++i) {
wktArray[i] += "]";
}
*/
var wktArray = new Array();
var bkCount = 0;
var obj = "";
for (var i=0; i<wktTemp.length; ++i) {
var token = wktTemp[i];
for (var j=0; j<token.length; ++j) {
if (token.charAt(j) == "[") ++bkCount;
if (token.charAt(j) == "]") --bkCount;
}
obj += token;
if (bkCount === 0) {
wktArray.push(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 = Proj4js.wktProjections[wktName]
break;
case 'DATUM':
this.datumName = wktName;
break;
case 'LOCAL_DATUM':
this.datumCode = 'none';
break;
case 'SPHEROID':
this.ellps = wktName;
this.a = parseFloat(wktArray.shift());
this.rf = parseFloat(wktArray.shift());
break;
case 'PRIMEM':
this.from_greenwich = parseFloat(wktArray.shift()); //to radians?
break;
case 'UNIT':
this.units = wktName;
this.unitsPerMeter = parseFloat(wktArray.shift());
break;
case 'PARAMETER':
var name = wktName.toLowerCase();
var value = parseFloat(wktArray.shift());
//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*Proj4js.common.D2R;
break;
case 'latitude_of_origin':
this.lat0 = value*Proj4js.common.D2R;
break;
case 'more_here':
break;
default:
break;
}
break;
case 'TOWGS84':
this.datum_params = wktArray;
break;
//DGR 2010-11-12: AXIS
case 'AXIS':
var name= wktName.toLowerCase();
var value= wktArray.shift();
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 + this.axis.substr(1,2); break;
case 'y': this.axis= this.axis.substr(0,1) + value + this.axis.substr(2,1); break;
case 'z': this.axis= this.axis.substr(0,2) + value ; break;
default : break;
}
case 'MORE_HERE':
break;
default:
break;
}
for (var i=0; i<wktArray.length; ++i) {
this.parseWKT(wktArray[i]);
}
},
/**
* Function: parseDefs
* Parses the PROJ.4 initialization string and sets the associated properties.
*
*/
parseDefs: function() {
this.defData = Proj4js.defs[this.srsCode];
var paramName, paramVal;
if (!this.defData) {
return;
}
var paramArray=this.defData.split("+");
for (var prop=0; prop<paramArray.length; prop++) {
var property = paramArray[prop].split("=");
paramName = property[0].toLowerCase();
paramVal = property[1];
switch (paramName.replace(/\s/gi,"")) { // trim out spaces
case "": break; // throw away nameless parameter
case "title": this.title = paramVal; break;
case "proj": this.projName = paramVal.replace(/\s/gi,""); break;
case "units": this.units = paramVal.replace(/\s/gi,""); break;
case "datum": this.datumCode = paramVal.replace(/\s/gi,""); break;
case "nadgrids": this.nagrids = paramVal.replace(/\s/gi,""); break;
case "ellps": this.ellps = paramVal.replace(/\s/gi,""); break;
case "a": this.a = parseFloat(paramVal); break; // semi-major radius
case "b": this.b = parseFloat(paramVal); break; // semi-minor radius
// DGR 2007-11-20
case "rf": this.rf = parseFloat(paramVal); break; // inverse flattening rf= a/(a-b)
case "lat_0": this.lat0 = paramVal*Proj4js.common.D2R; break; // phi0, central latitude
case "lat_1": this.lat1 = paramVal*Proj4js.common.D2R; break; //standard parallel 1
case "lat_2": this.lat2 = paramVal*Proj4js.common.D2R; break; //standard parallel 2
case "lat_ts": this.lat_ts = paramVal*Proj4js.common.D2R; break; // used in merc and eqc
case "lon_0": this.long0 = paramVal*Proj4js.common.D2R; break; // lam0, central longitude
case "alpha": this.alpha = parseFloat(paramVal)*Proj4js.common.D2R; break; //for somerc projection
case "lonc": this.longc = paramVal*Proj4js.common.D2R; break; //for somerc projection
case "x_0": this.x0 = parseFloat(paramVal); break; // false easting
case "y_0": this.y0 = parseFloat(paramVal); break; // false northing
case "k_0": this.k0 = parseFloat(paramVal); break; // projection scale factor
case "k": this.k0 = parseFloat(paramVal); break; // both forms returned
case "r_a": this.R_A = true; break; // sphere--area of ellipsoid
case "zone": this.zone = parseInt(paramVal,10); break; // UTM Zone
case "south": this.utmSouth = true; break; // UTM north/south
case "towgs84":this.datum_params = paramVal.split(","); break;
case "to_meter": this.to_meter = parseFloat(paramVal); break; // cartesian scaling
case "from_greenwich": this.from_greenwich = paramVal*Proj4js.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 = paramVal.replace(/\s/gi,"");
this.from_greenwich = Proj4js.PrimeMeridian[paramVal] ?
Proj4js.PrimeMeridian[paramVal] : parseFloat(paramVal);
this.from_greenwich *= Proj4js.common.D2R;
break;
// DGR 2010-11-12: axis
case "axis": paramVal = paramVal.replace(/\s/gi,"");
var legalAxis= "ewnsud";
if (paramVal.length==3 &&
legalAxis.indexOf(paramVal.substr(0,1))!=-1 &&
legalAxis.indexOf(paramVal.substr(1,1))!=-1 &&
legalAxis.indexOf(paramVal.substr(2,1))!=-1) {
this.axis= paramVal;
} //FIXME: be silent ?
break
case "h":this.h = parseFloat(paramVal); break; // height above sphere
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.
*
*/
deriveConstants: function() {
if (this.nagrids == '@null') this.datumCode = 'none';
if (this.datumCode && this.datumCode != 'none') {
var datumDef = Proj4js.Datum[this.datumCode];
if (datumDef) {
this.datum_params = datumDef.towgs84 ? datumDef.towgs84.split(',') : null;
this.ellps = datumDef.ellipse;
this.datumName = datumDef.datumName ? datumDef.datumName : this.datumCode;
}
}
if (!this.a) { // do we have an ellipsoid?
var ellipse = Proj4js.Ellipsoid[this.ellps] ? Proj4js.Ellipsoid[this.ellps] : Proj4js.Ellipsoid['WGS84'];
Proj4js.extend(this, ellipse);
}
if (this.rf && !this.b) this.b = (1.0 - 1.0/this.rf) * this.a;
if (this.rf === 0 || Math.abs(this.a - this.b)<Proj4js.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 = Math.sqrt(this.es); // eccentricity
if (this.R_A) {
this.a *= 1. - this.es * (Proj4js.common.SIXTH + this.es * (Proj4js.common.RA4 + this.es * Proj4js.common.RA6));
this.a2 = this.a * this.a;
this.b2 = this.b * this.b;
this.es = 0.;