/webportal/src/main/webapp/scripts/proj4js-combined.js

http://alageospatialportal.googlecode.com/ · JavaScript · 4987 lines · 2984 code · 537 blank · 1466 comment · 547 complexity · 4f9fc4de15ecf4ff75223155d283a4c8 MD5 · raw file

Large files are truncated click here to view the full file

  1. /*
  2. proj4js.js -- Javascript reprojection library.
  3. Authors: Mike Adair madairATdmsolutions.ca
  4. Richard Greenwood richATgreenwoodmap.com
  5. Didier Richard didier.richardATign.fr
  6. Stephen Irons
  7. License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
  8. Note: This program is an almost direct port of the C library
  9. Proj4.
  10. */
  11. /* ======================================================================
  12. proj4js.js
  13. ====================================================================== */
  14. /*
  15. Author: Mike Adair madairATdmsolutions.ca
  16. Richard Greenwood rich@greenwoodmap.com
  17. License: LGPL as per: http://www.gnu.org/copyleft/lesser.html
  18. $Id: Proj.js 2956 2007-07-09 12:17:52Z steven $
  19. */
  20. /**
  21. * Namespace: Proj4js
  22. *
  23. * Proj4js is a JavaScript library to transform point coordinates from one
  24. * coordinate system to another, including datum transformations.
  25. *
  26. * This library is a port of both the Proj.4 and GCTCP C libraries to JavaScript.
  27. * Enabling these transformations in the browser allows geographic data stored
  28. * in different projections to be combined in browser-based web mapping
  29. * applications.
  30. *
  31. * Proj4js must have access to coordinate system initialization strings (which
  32. * are the same as for PROJ.4 command line). Thes can be included in your
  33. * application using a <script> tag or Proj4js can load CS initialization
  34. * strings from a local directory or a web service such as spatialreference.org.
  35. *
  36. * Similarly, Proj4js must have access to projection transform code. These can
  37. * be included individually using a <script> tag in your page, built into a
  38. * custom build of Proj4js or loaded dynamically at run-time. Using the
  39. * -combined and -compressed versions of Proj4js includes all projection class
  40. * code by default.
  41. *
  42. * Note that dynamic loading of defs and code happens ascynchrously, check the
  43. * Proj.readyToUse flag before using the Proj object. If the defs and code
  44. * required by your application are loaded through script tags, dynamic loading
  45. * is not required and the Proj object will be readyToUse on return from the
  46. * constructor.
  47. *
  48. * All coordinates are handled as points which have a .x and a .y property
  49. * which will be modified in place.
  50. *
  51. * Override Proj4js.reportError for output of alerts and warnings.
  52. *
  53. * See http://trac.osgeo.org/proj4js/wiki/UserGuide for full details.
  54. */
  55. /**
  56. * Global namespace object for Proj4js library
  57. */
  58. Proj4js = {
  59. /**
  60. * Property: defaultDatum
  61. * The datum to use when no others a specified
  62. */
  63. defaultDatum: 'WGS84', //default datum
  64. /**
  65. * Method: transform(source, dest, point)
  66. * Transform a point coordinate from one map projection to another. This is
  67. * really the only public method you should need to use.
  68. *
  69. * Parameters:
  70. * source - {Proj4js.Proj} source map projection for the transformation
  71. * dest - {Proj4js.Proj} destination map projection for the transformation
  72. * point - {Object} point to transform, may be geodetic (long, lat) or
  73. * projected Cartesian (x,y), but should always have x,y properties.
  74. */
  75. transform: function(source, dest, point) {
  76. if (!source.readyToUse || !dest.readyToUse) {
  77. this.reportError("Proj4js initialization for "+source.srsCode+" not yet complete");
  78. return point;
  79. }
  80. // Workaround for Spherical Mercator
  81. if ((source.srsProjNumber =="900913" && dest.datumCode != "WGS84") ||
  82. (dest.srsProjNumber == "900913" && source.datumCode != "WGS84")) {
  83. var wgs84 = Proj4js.WGS84;
  84. this.transform(source, wgs84, point);
  85. source = wgs84;
  86. }
  87. // Transform source points to long/lat, if they aren't already.
  88. if ( source.projName=="longlat") {
  89. point.x *= Proj4js.common.D2R; // convert degrees to radians
  90. point.y *= Proj4js.common.D2R;
  91. } else {
  92. if (source.to_meter) {
  93. point.x *= source.to_meter;
  94. point.y *= source.to_meter;
  95. }
  96. source.inverse(point); // Convert Cartesian to longlat
  97. }
  98. // Adjust for the prime meridian if necessary
  99. if (source.from_greenwich) {
  100. point.x += source.from_greenwich;
  101. }
  102. // Convert datums if needed, and if possible.
  103. point = this.datum_transform( source.datum, dest.datum, point );
  104. // Adjust for the prime meridian if necessary
  105. if (dest.from_greenwich) {
  106. point.x -= dest.from_greenwich;
  107. }
  108. if( dest.projName=="longlat" ) {
  109. // convert radians to decimal degrees
  110. point.x *= Proj4js.common.R2D;
  111. point.y *= Proj4js.common.R2D;
  112. } else { // else project
  113. dest.forward(point);
  114. if (dest.to_meter) {
  115. point.x /= dest.to_meter;
  116. point.y /= dest.to_meter;
  117. }
  118. }
  119. return point;
  120. }, // transform()
  121. /** datum_transform()
  122. source coordinate system definition,
  123. destination coordinate system definition,
  124. point to transform in geodetic coordinates (long, lat, height)
  125. */
  126. datum_transform : function( source, dest, point ) {
  127. // Short cut if the datums are identical.
  128. if( source.compare_datums( dest ) ) {
  129. return point; // in this case, zero is sucess,
  130. // whereas cs_compare_datums returns 1 to indicate TRUE
  131. // confusing, should fix this
  132. }
  133. // Explicitly skip datum transform by setting 'datum=none' as parameter for either source or dest
  134. if( source.datum_type == Proj4js.common.PJD_NODATUM
  135. || dest.datum_type == Proj4js.common.PJD_NODATUM) {
  136. return point;
  137. }
  138. // If this datum requires grid shifts, then apply it to geodetic coordinates.
  139. if( source.datum_type == Proj4js.common.PJD_GRIDSHIFT )
  140. {
  141. alert("ERROR: Grid shift transformations are not implemented yet.");
  142. /*
  143. pj_apply_gridshift( pj_param(source.params,"snadgrids").s, 0,
  144. point_count, point_offset, x, y, z );
  145. CHECK_RETURN;
  146. src_a = SRS_WGS84_SEMIMAJOR;
  147. src_es = 0.006694379990;
  148. */
  149. }
  150. if( dest.datum_type == Proj4js.common.PJD_GRIDSHIFT )
  151. {
  152. alert("ERROR: Grid shift transformations are not implemented yet.");
  153. /*
  154. dst_a = ;
  155. dst_es = 0.006694379990;
  156. */
  157. }
  158. // Do we need to go through geocentric coordinates?
  159. if( source.es != dest.es || source.a != dest.a
  160. || source.datum_type == Proj4js.common.PJD_3PARAM
  161. || source.datum_type == Proj4js.common.PJD_7PARAM
  162. || dest.datum_type == Proj4js.common.PJD_3PARAM
  163. || dest.datum_type == Proj4js.common.PJD_7PARAM)
  164. {
  165. // Convert to geocentric coordinates.
  166. source.geodetic_to_geocentric( point );
  167. // CHECK_RETURN;
  168. // Convert between datums
  169. if( source.datum_type == Proj4js.common.PJD_3PARAM || source.datum_type == Proj4js.common.PJD_7PARAM ) {
  170. source.geocentric_to_wgs84(point);
  171. // CHECK_RETURN;
  172. }
  173. if( dest.datum_type == Proj4js.common.PJD_3PARAM || dest.datum_type == Proj4js.common.PJD_7PARAM ) {
  174. dest.geocentric_from_wgs84(point);
  175. // CHECK_RETURN;
  176. }
  177. // Convert back to geodetic coordinates
  178. dest.geocentric_to_geodetic( point );
  179. // CHECK_RETURN;
  180. }
  181. // Apply grid shift to destination if required
  182. if( dest.datum_type == Proj4js.common.PJD_GRIDSHIFT )
  183. {
  184. alert("ERROR: Grid shift transformations are not implemented yet.");
  185. // pj_apply_gridshift( pj_param(dest.params,"snadgrids").s, 1, point);
  186. // CHECK_RETURN;
  187. }
  188. return point;
  189. }, // cs_datum_transform
  190. /**
  191. * Function: reportError
  192. * An internal method to report errors back to user.
  193. * Override this in applications to report error messages or throw exceptions.
  194. */
  195. reportError: function(msg) {
  196. //console.log(msg);
  197. },
  198. /**
  199. *
  200. * Title: Private Methods
  201. * The following properties and methods are intended for internal use only.
  202. *
  203. * This is a minimal implementation of JavaScript inheritance methods so that
  204. * Proj4js can be used as a stand-alone library.
  205. * These are copies of the equivalent OpenLayers methods at v2.7
  206. */
  207. /**
  208. * Function: extend
  209. * Copy all properties of a source object to a destination object. Modifies
  210. * the passed in destination object. Any properties on the source object
  211. * that are set to undefined will not be (re)set on the destination object.
  212. *
  213. * Parameters:
  214. * destination - {Object} The object that will be modified
  215. * source - {Object} The object with properties to be set on the destination
  216. *
  217. * Returns:
  218. * {Object} The destination object.
  219. */
  220. extend: function(destination, source) {
  221. destination = destination || {};
  222. if(source) {
  223. for(var property in source) {
  224. var value = source[property];
  225. if(value !== undefined) {
  226. destination[property] = value;
  227. }
  228. }
  229. }
  230. return destination;
  231. },
  232. /**
  233. * Constructor: Class
  234. * Base class used to construct all other classes. Includes support for
  235. * multiple inheritance.
  236. *
  237. */
  238. Class: function() {
  239. var Class = function() {
  240. this.initialize.apply(this, arguments);
  241. };
  242. var extended = {};
  243. var parent;
  244. for(var i=0; i<arguments.length; ++i) {
  245. if(typeof arguments[i] == "function") {
  246. // get the prototype of the superclass
  247. parent = arguments[i].prototype;
  248. } else {
  249. // in this case we're extending with the prototype
  250. parent = arguments[i];
  251. }
  252. Proj4js.extend(extended, parent);
  253. }
  254. Class.prototype = extended;
  255. return Class;
  256. },
  257. /**
  258. * Function: bind
  259. * Bind a function to an object. Method to easily create closures with
  260. * 'this' altered.
  261. *
  262. * Parameters:
  263. * func - {Function} Input function.
  264. * object - {Object} The object to bind to the input function (as this).
  265. *
  266. * Returns:
  267. * {Function} A closure with 'this' set to the passed in object.
  268. */
  269. bind: function(func, object) {
  270. // create a reference to all arguments past the second one
  271. var args = Array.prototype.slice.apply(arguments, [2]);
  272. return function() {
  273. // Push on any additional arguments from the actual function call.
  274. // These will come after those sent to the bind call.
  275. var newArgs = args.concat(
  276. Array.prototype.slice.apply(arguments, [0])
  277. );
  278. return func.apply(object, newArgs);
  279. };
  280. },
  281. /**
  282. * The following properties and methods handle dynamic loading of JSON objects.
  283. *
  284. /**
  285. * Property: scriptName
  286. * {String} The filename of this script without any path.
  287. */
  288. scriptName: "proj4js-combined.js",
  289. /**
  290. * Property: defsLookupService
  291. * AJAX service to retreive projection definition parameters from
  292. */
  293. defsLookupService: 'http://spatialreference.org/ref',
  294. /**
  295. * Property: libPath
  296. * internal: http server path to library code.
  297. */
  298. libPath: null,
  299. /**
  300. * Function: getScriptLocation
  301. * Return the path to this script.
  302. *
  303. * Returns:
  304. * Path to this script
  305. */
  306. getScriptLocation: function () {
  307. if (this.libPath) return this.libPath;
  308. var scriptName = this.scriptName;
  309. var scriptNameLen = scriptName.length;
  310. var scripts = document.getElementsByTagName('script');
  311. for (var i = 0; i < scripts.length; i++) {
  312. var src = scripts[i].getAttribute('src');
  313. if (src) {
  314. var index = src.lastIndexOf(scriptName);
  315. // is it found, at the end of the URL?
  316. if ((index > -1) && (index + scriptNameLen == src.length)) {
  317. this.libPath = src.slice(0, -scriptNameLen);
  318. break;
  319. }
  320. }
  321. }
  322. return this.libPath||"";
  323. },
  324. /**
  325. * Function: loadScript
  326. * Load a JS file from a URL into a <script> tag in the page.
  327. *
  328. * Parameters:
  329. * url - {String} The URL containing the script to load
  330. * onload - {Function} A method to be executed when the script loads successfully
  331. * onfail - {Function} A method to be executed when there is an error loading the script
  332. * loadCheck - {Function} A boolean method that checks to see if the script
  333. * has loaded. Typically this just checks for the existance of
  334. * an object in the file just loaded.
  335. */
  336. loadScript: function(url, onload, onfail, loadCheck) {
  337. var script = document.createElement('script');
  338. script.defer = false;
  339. script.type = "text/javascript";
  340. script.id = url;
  341. script.src = url;
  342. script.onload = onload;
  343. script.onerror = onfail;
  344. script.loadCheck = loadCheck;
  345. if (/MSIE/.test(navigator.userAgent)) {
  346. script.onreadystatechange = this.checkReadyState;
  347. }
  348. document.getElementsByTagName('head')[0].appendChild(script);
  349. },
  350. /**
  351. * Function: checkReadyState
  352. * IE workaround since there is no onerror handler. Calls the user defined
  353. * loadCheck method to determine if the script is loaded.
  354. *
  355. */
  356. checkReadyState: function() {
  357. if (this.readyState == 'loaded') {
  358. if (!this.loadCheck()) {
  359. this.onerror();
  360. } else {
  361. this.onload();
  362. }
  363. }
  364. }
  365. };
  366. /**
  367. * Class: Proj4js.Proj
  368. *
  369. * Proj objects provide transformation methods for point coordinates
  370. * between geodetic latitude/longitude and a projected coordinate system.
  371. * once they have been initialized with a projection code.
  372. *
  373. * Initialization of Proj objects is with a projection code, usually EPSG codes,
  374. * which is the key that will be used with the Proj4js.defs array.
  375. *
  376. * The code passed in will be stripped of colons and converted to uppercase
  377. * to locate projection definition files.
  378. *
  379. * A projection object has properties for units and title strings.
  380. */
  381. Proj4js.Proj = Proj4js.Class({
  382. /**
  383. * Property: readyToUse
  384. * Flag to indicate if initialization is complete for this Proj object
  385. */
  386. readyToUse: false,
  387. /**
  388. * Property: title
  389. * The title to describe the projection
  390. */
  391. title: null,
  392. /**
  393. * Property: projName
  394. * The projection class for this projection, e.g. lcc (lambert conformal conic,
  395. * or merc for mercator). These are exactly equivalent to their Proj4
  396. * counterparts.
  397. */
  398. projName: null,
  399. /**
  400. * Property: units
  401. * The units of the projection. Values include 'm' and 'degrees'
  402. */
  403. units: null,
  404. /**
  405. * Property: datum
  406. * The datum specified for the projection
  407. */
  408. datum: null,
  409. /**
  410. * Property: x0
  411. * The x coordinate origin
  412. */
  413. x0: 0,
  414. /**
  415. * Property: y0
  416. * The y coordinate origin
  417. */
  418. y0: 0,
  419. /**
  420. * Constructor: initialize
  421. * Constructor for Proj4js.Proj objects
  422. *
  423. * Parameters:
  424. * srsCode - a code for map projection definition parameters. These are usually
  425. * (but not always) EPSG codes.
  426. */
  427. initialize: function(srsCode) {
  428. this.srsCodeInput = srsCode;
  429. // DGR 2008-08-03 : support urn and url
  430. if (srsCode.indexOf('urn:') == 0) {
  431. //urn:ORIGINATOR:def:crs:CODESPACE:VERSION:ID
  432. var urn = srsCode.split(':');
  433. if ((urn[1] == 'ogc' || urn[1] =='x-ogc') &&
  434. (urn[2] =='def') &&
  435. (urn[3] =='crs')) {
  436. srsCode = urn[4]+':'+urn[urn.length-1];
  437. }
  438. } else if (srsCode.indexOf('http://') == 0) {
  439. //url#ID
  440. var url = srsCode.split('#');
  441. if (url[0].match(/epsg.org/)) {
  442. // http://www.epsg.org/#
  443. srsCode = 'EPSG:'+url[1];
  444. } else if (url[0].match(/RIG.xml/)) {
  445. //http://librairies.ign.fr/geoportail/resources/RIG.xml#
  446. //http://interop.ign.fr/registers/ign/RIG.xml#
  447. srsCode = 'IGNF:'+url[1];
  448. }
  449. }
  450. this.srsCode = srsCode.toUpperCase();
  451. if (this.srsCode.indexOf("EPSG") == 0) {
  452. this.srsCode = this.srsCode;
  453. this.srsAuth = 'epsg';
  454. this.srsProjNumber = this.srsCode.substring(5);
  455. // DGR 2007-11-20 : authority IGNF
  456. } else if (this.srsCode.indexOf("IGNF") == 0) {
  457. this.srsCode = this.srsCode;
  458. this.srsAuth = 'IGNF';
  459. this.srsProjNumber = this.srsCode.substring(5);
  460. // DGR 2008-06-19 : pseudo-authority CRS for WMS
  461. } else if (this.srsCode.indexOf("CRS") == 0) {
  462. this.srsCode = this.srsCode;
  463. this.srsAuth = 'CRS';
  464. this.srsProjNumber = this.srsCode.substring(4);
  465. } else {
  466. this.srsAuth = '';
  467. this.srsProjNumber = this.srsCode;
  468. }
  469. this.loadProjDefinition();
  470. },
  471. /**
  472. * Function: loadProjDefinition
  473. * Loads the coordinate system initialization string if required.
  474. * Note that dynamic loading happens asynchronously so an application must
  475. * wait for the readyToUse property is set to true.
  476. * To prevent dynamic loading, include the defs through a script tag in
  477. * your application.
  478. *
  479. */
  480. loadProjDefinition: function() {
  481. //check in memory
  482. if (Proj4js.defs[this.srsCode]) {
  483. this.defsLoaded();
  484. return;
  485. }
  486. //else check for def on the server
  487. var url = Proj4js.getScriptLocation() + 'defs/' + this.srsAuth.toUpperCase() + this.srsProjNumber + '.js';
  488. Proj4js.loadScript(url,
  489. Proj4js.bind(this.defsLoaded, this),
  490. Proj4js.bind(this.loadFromService, this),
  491. Proj4js.bind(this.checkDefsLoaded, this) );
  492. },
  493. /**
  494. * Function: loadFromService
  495. * Creates the REST URL for loading the definition from a web service and
  496. * loads it.
  497. *
  498. */
  499. loadFromService: function() {
  500. //else load from web service
  501. var url = Proj4js.defsLookupService +'/' + this.srsAuth +'/'+ this.srsProjNumber + '/proj4js/';
  502. Proj4js.loadScript(url,
  503. Proj4js.bind(this.defsLoaded, this),
  504. Proj4js.bind(this.defsFailed, this),
  505. Proj4js.bind(this.checkDefsLoaded, this) );
  506. },
  507. /**
  508. * Function: defsLoaded
  509. * Continues the Proj object initilization once the def file is loaded
  510. *
  511. */
  512. defsLoaded: function() {
  513. this.parseDefs();
  514. this.loadProjCode(this.projName);
  515. },
  516. /**
  517. * Function: checkDefsLoaded
  518. * This is the loadCheck method to see if the def object exists
  519. *
  520. */
  521. checkDefsLoaded: function() {
  522. if (Proj4js.defs[this.srsCode]) {
  523. return true;
  524. } else {
  525. return false;
  526. }
  527. },
  528. /**
  529. * Function: defsFailed
  530. * Report an error in loading the defs file, but continue on using WGS84
  531. *
  532. */
  533. defsFailed: function() {
  534. Proj4js.reportError('failed to load projection definition for: '+this.srsCode);
  535. Proj4js.defs[this.srsCode] = Proj4js.defs['WGS84']; //set it to something so it can at least continue
  536. this.defsLoaded();
  537. },
  538. /**
  539. * Function: loadProjCode
  540. * Loads projection class code dynamically if required.
  541. * Projection code may be included either through a script tag or in
  542. * a built version of proj4js
  543. *
  544. */
  545. loadProjCode: function(projName) {
  546. if (Proj4js.Proj[projName]) {
  547. this.initTransforms();
  548. return;
  549. }
  550. //the URL for the projection code
  551. var url = Proj4js.getScriptLocation() + 'projCode/' + projName + '.js';
  552. Proj4js.loadScript(url,
  553. Proj4js.bind(this.loadProjCodeSuccess, this, projName),
  554. Proj4js.bind(this.loadProjCodeFailure, this, projName),
  555. Proj4js.bind(this.checkCodeLoaded, this, projName) );
  556. },
  557. /**
  558. * Function: loadProjCodeSuccess
  559. * Loads any proj dependencies or continue on to final initialization.
  560. *
  561. */
  562. loadProjCodeSuccess: function(projName) {
  563. if (Proj4js.Proj[projName].dependsOn){
  564. this.loadProjCode(Proj4js.Proj[projName].dependsOn);
  565. } else {
  566. this.initTransforms();
  567. }
  568. },
  569. /**
  570. * Function: defsFailed
  571. * Report an error in loading the proj file. Initialization of the Proj
  572. * object has failed and the readyToUse flag will never be set.
  573. *
  574. */
  575. loadProjCodeFailure: function(projName) {
  576. Proj4js.reportError("failed to find projection file for: " + projName);
  577. //TBD initialize with identity transforms so proj will still work?
  578. },
  579. /**
  580. * Function: checkCodeLoaded
  581. * This is the loadCheck method to see if the projection code is loaded
  582. *
  583. */
  584. checkCodeLoaded: function(projName) {
  585. if (Proj4js.Proj[projName]) {
  586. return true;
  587. } else {
  588. return false;
  589. }
  590. },
  591. /**
  592. * Function: initTransforms
  593. * Finalize the initialization of the Proj object
  594. *
  595. */
  596. initTransforms: function() {
  597. Proj4js.extend(this, Proj4js.Proj[this.projName]);
  598. this.init();
  599. this.readyToUse = true;
  600. },
  601. /**
  602. * Function: parseDefs
  603. * Parses the PROJ.4 initialization string and sets the associated properties.
  604. *
  605. */
  606. parseDefs: function() {
  607. this.defData = Proj4js.defs[this.srsCode];
  608. var paramName, paramVal;
  609. if (!this.defData) {
  610. return;
  611. }
  612. var paramArray=this.defData.split("+");
  613. for (var prop=0; prop<paramArray.length; prop++) {
  614. var property = paramArray[prop].split("=");
  615. paramName = property[0].toLowerCase();
  616. paramVal = property[1];
  617. switch (paramName.replace(/\s/gi,"")) { // trim out spaces
  618. case "": break; // throw away nameless parameter
  619. case "title": this.title = paramVal; break;
  620. case "proj": this.projName = paramVal.replace(/\s/gi,""); break;
  621. case "units": this.units = paramVal.replace(/\s/gi,""); break;
  622. case "datum": this.datumCode = paramVal.replace(/\s/gi,""); break;
  623. case "nadgrids": this.nagrids = paramVal.replace(/\s/gi,""); break;
  624. case "ellps": this.ellps = paramVal.replace(/\s/gi,""); break;
  625. case "a": this.a = parseFloat(paramVal); break; // semi-major radius
  626. case "b": this.b = parseFloat(paramVal); break; // semi-minor radius
  627. // DGR 2007-11-20
  628. case "rf": this.rf = parseFloat(paramVal); break; // inverse flattening rf= a/(a-b)
  629. case "lat_0": this.lat0 = paramVal*Proj4js.common.D2R; break; // phi0, central latitude
  630. case "lat_1": this.lat1 = paramVal*Proj4js.common.D2R; break; //standard parallel 1
  631. case "lat_2": this.lat2 = paramVal*Proj4js.common.D2R; break; //standard parallel 2
  632. case "lat_ts": this.lat_ts = paramVal*Proj4js.common.D2R; break; // used in merc and eqc
  633. case "lon_0": this.long0 = paramVal*Proj4js.common.D2R; break; // lam0, central longitude
  634. case "alpha": this.alpha = parseFloat(paramVal)*Proj4js.common.D2R; break; //for somerc projection
  635. case "lonc": this.longc = paramVal*Proj4js.common.D2R; break; //for somerc projection
  636. case "x_0": this.x0 = parseFloat(paramVal); break; // false easting
  637. case "y_0": this.y0 = parseFloat(paramVal); break; // false northing
  638. case "k_0": this.k0 = parseFloat(paramVal); break; // projection scale factor
  639. case "k": this.k0 = parseFloat(paramVal); break; // both forms returned
  640. case "r_a": this.R_A = true; break; // sphere--area of ellipsoid
  641. case "zone": this.zone = parseInt(paramVal); break; // UTM Zone
  642. case "south": this.utmSouth = true; break; // UTM north/south
  643. case "towgs84":this.datum_params = paramVal.split(","); break;
  644. case "to_meter": this.to_meter = parseFloat(paramVal); break; // cartesian scaling
  645. case "from_greenwich": this.from_greenwich = paramVal*Proj4js.common.D2R; break;
  646. // DGR 2008-07-09 : if pm is not a well-known prime meridian take
  647. // the value instead of 0.0, then convert to radians
  648. case "pm": paramVal = paramVal.replace(/\s/gi,"");
  649. this.from_greenwich = Proj4js.PrimeMeridian[paramVal] ?
  650. Proj4js.PrimeMeridian[paramVal] : parseFloat(paramVal);
  651. this.from_greenwich *= Proj4js.common.D2R;
  652. break;
  653. case "no_defs": break;
  654. default: //alert("Unrecognized parameter: " + paramName);
  655. } // switch()
  656. } // for paramArray
  657. this.deriveConstants();
  658. },
  659. /**
  660. * Function: deriveConstants
  661. * Sets several derived constant values and initialization of datum and ellipse
  662. * parameters.
  663. *
  664. */
  665. deriveConstants: function() {
  666. if (this.nagrids == '@null') this.datumCode = 'none';
  667. if (this.datumCode && this.datumCode != 'none') {
  668. var datumDef = Proj4js.Datum[this.datumCode];
  669. if (datumDef) {
  670. this.datum_params = datumDef.towgs84 ? datumDef.towgs84.split(',') : null;
  671. this.ellps = datumDef.ellipse;
  672. this.datumName = datumDef.datumName ? datumDef.datumName : this.datumCode;
  673. }
  674. }
  675. if (!this.a) { // do we have an ellipsoid?
  676. var ellipse = Proj4js.Ellipsoid[this.ellps] ? Proj4js.Ellipsoid[this.ellps] : Proj4js.Ellipsoid['WGS84'];
  677. Proj4js.extend(this, ellipse);
  678. }
  679. if (this.rf && !this.b) this.b = (1.0 - 1.0/this.rf) * this.a;
  680. if (Math.abs(this.a - this.b)<Proj4js.common.EPSLN) {
  681. this.sphere = true;
  682. this.b= this.a;
  683. }
  684. this.a2 = this.a * this.a; // used in geocentric
  685. this.b2 = this.b * this.b; // used in geocentric
  686. this.es = (this.a2-this.b2)/this.a2; // e ^ 2
  687. this.e = Math.sqrt(this.es); // eccentricity
  688. if (this.R_A) {
  689. this.a *= 1. - this.es * (Proj4js.common.SIXTH + this.es * (Proj4js.common.RA4 + this.es * Proj4js.common.RA6));
  690. this.a2 = this.a * this.a;
  691. this.b2 = this.b * this.b;
  692. this.es = 0.;
  693. }
  694. this.ep2=(this.a2-this.b2)/this.b2; // used in geocentric
  695. if (!this.k0) this.k0 = 1.0; //default value
  696. this.datum = new Proj4js.datum(this);
  697. }
  698. });
  699. Proj4js.Proj.longlat = {
  700. init: function() {
  701. //no-op for longlat
  702. },
  703. forward: function(pt) {
  704. //identity transform
  705. return pt;
  706. },
  707. inverse: function(pt) {
  708. //identity transform
  709. return pt;
  710. }
  711. };
  712. /**
  713. Proj4js.defs is a collection of coordinate system definition objects in the
  714. PROJ.4 command line format.
  715. Generally a def is added by means of a separate .js file for example:
  716. <SCRIPT type="text/javascript" src="defs/EPSG26912.js"></SCRIPT>
  717. def is a CS definition in PROJ.4 WKT format, for example:
  718. +proj="tmerc" //longlat, etc.
  719. +a=majorRadius
  720. +b=minorRadius
  721. +lat0=somenumber
  722. +long=somenumber
  723. */
  724. Proj4js.defs = {
  725. // These are so widely used, we'll go ahead and throw them in
  726. // without requiring a separate .js file
  727. 'WGS84': "+title=long/lat:WGS84 +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees",
  728. 'EPSG:4326': "+title=long/lat:WGS84 +proj=longlat +a=6378137.0 +b=6356752.31424518 +ellps=WGS84 +datum=WGS84 +units=degrees",
  729. 'EPSG:4269': "+title=long/lat:NAD83 +proj=longlat +a=6378137.0 +b=6356752.31414036 +ellps=GRS80 +datum=NAD83 +units=degrees",
  730. 'EPSG:3785': "+title= Google Mercator +proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +no_defs"
  731. };
  732. Proj4js.defs['GOOGLE'] = Proj4js.defs['EPSG:3785'];
  733. Proj4js.defs['EPSG:900913'] = Proj4js.defs['EPSG:3785'];
  734. Proj4js.defs['EPSG:102113'] = Proj4js.defs['EPSG:3785'];
  735. Proj4js.common = {
  736. PI : 3.141592653589793238, //Math.PI,
  737. HALF_PI : 1.570796326794896619, //Math.PI*0.5,
  738. TWO_PI : 6.283185307179586477, //Math.PI*2,
  739. FORTPI : 0.78539816339744833,
  740. R2D : 57.29577951308232088,
  741. D2R : 0.01745329251994329577,
  742. SEC_TO_RAD : 4.84813681109535993589914102357e-6, /* SEC_TO_RAD = Pi/180/3600 */
  743. EPSLN : 1.0e-10,
  744. MAX_ITER : 20,
  745. // following constants from geocent.c
  746. COS_67P5 : 0.38268343236508977, /* cosine of 67.5 degrees */
  747. AD_C : 1.0026000, /* Toms region 1 constant */
  748. /* datum_type values */
  749. PJD_UNKNOWN : 0,
  750. PJD_3PARAM : 1,
  751. PJD_7PARAM : 2,
  752. PJD_GRIDSHIFT: 3,
  753. PJD_WGS84 : 4, // WGS84 or equivalent
  754. PJD_NODATUM : 5, // WGS84 or equivalent
  755. SRS_WGS84_SEMIMAJOR : 6378137.0, // only used in grid shift transforms
  756. // ellipoid pj_set_ell.c
  757. SIXTH : .1666666666666666667, /* 1/6 */
  758. RA4 : .04722222222222222222, /* 17/360 */
  759. RA6 : .02215608465608465608, /* 67/3024 */
  760. RV4 : .06944444444444444444, /* 5/72 */
  761. RV6 : .04243827160493827160, /* 55/1296 */
  762. // Function to compute the constant small m which is the radius of
  763. // a parallel of latitude, phi, divided by the semimajor axis.
  764. // -----------------------------------------------------------------
  765. msfnz : function(eccent, sinphi, cosphi) {
  766. var con = eccent * sinphi;
  767. return cosphi/(Math.sqrt(1.0 - con * con));
  768. },
  769. // Function to compute the constant small t for use in the forward
  770. // computations in the Lambert Conformal Conic and the Polar
  771. // Stereographic projections.
  772. // -----------------------------------------------------------------
  773. tsfnz : function(eccent, phi, sinphi) {
  774. var con = eccent * sinphi;
  775. var com = .5 * eccent;
  776. con = Math.pow(((1.0 - con) / (1.0 + con)), com);
  777. return (Math.tan(.5 * (this.HALF_PI - phi))/con);
  778. },
  779. // Function to compute the latitude angle, phi2, for the inverse of the
  780. // Lambert Conformal Conic and Polar Stereographic projections.
  781. // ----------------------------------------------------------------
  782. phi2z : function(eccent, ts) {
  783. var eccnth = .5 * eccent;
  784. var con, dphi;
  785. var phi = this.HALF_PI - 2 * Math.atan(ts);
  786. for (i = 0; i <= 15; i++) {
  787. con = eccent * Math.sin(phi);
  788. dphi = this.HALF_PI - 2 * Math.atan(ts *(Math.pow(((1.0 - con)/(1.0 + con)),eccnth))) - phi;
  789. phi += dphi;
  790. if (Math.abs(dphi) <= .0000000001) return phi;
  791. }
  792. alert("phi2z has NoConvergence");
  793. return (-9999);
  794. },
  795. /* Function to compute constant small q which is the radius of a
  796. parallel of latitude, phi, divided by the semimajor axis.
  797. ------------------------------------------------------------*/
  798. qsfnz : function(eccent,sinphi) {
  799. var con;
  800. if (eccent > 1.0e-7) {
  801. con = eccent * sinphi;
  802. return (( 1.0- eccent * eccent) * (sinphi /(1.0 - con * con) - (.5/eccent)*Math.log((1.0 - con)/(1.0 + con))));
  803. } else {
  804. return(2.0 * sinphi);
  805. }
  806. },
  807. /* Function to eliminate roundoff errors in asin
  808. ----------------------------------------------*/
  809. asinz : function(x) {
  810. if (Math.abs(x)>1.0) {
  811. x=(x>1.0)?1.0:-1.0;
  812. }
  813. return Math.asin(x);
  814. },
  815. // following functions from gctpc cproj.c for transverse mercator projections
  816. e0fn : function(x) {return(1.0-0.25*x*(1.0+x/16.0*(3.0+1.25*x)));},
  817. e1fn : function(x) {return(0.375*x*(1.0+0.25*x*(1.0+0.46875*x)));},
  818. e2fn : function(x) {return(0.05859375*x*x*(1.0+0.75*x));},
  819. e3fn : function(x) {return(x*x*x*(35.0/3072.0));},
  820. mlfn : function(e0,e1,e2,e3,phi) {return(e0*phi-e1*Math.sin(2.0*phi)+e2*Math.sin(4.0*phi)-e3*Math.sin(6.0*phi));},
  821. srat : function(esinp, exp) {
  822. return(Math.pow((1.0-esinp)/(1.0+esinp), exp));
  823. },
  824. // Function to return the sign of an argument
  825. sign : function(x) { if (x < 0.0) return(-1); else return(1);},
  826. // Function to adjust longitude to -180 to 180; input in radians
  827. adjust_lon : function(x) {
  828. x = (Math.abs(x) < this.PI) ? x: (x - (this.sign(x)*this.TWO_PI) );
  829. return x;
  830. },
  831. // IGNF - DGR : algorithms used by IGN France
  832. // Function to adjust latitude to -90 to 90; input in radians
  833. adjust_lat : function(x) {
  834. x= (Math.abs(x) < this.HALF_PI) ? x: (x - (this.sign(x)*this.PI) );
  835. return x;
  836. },
  837. // Latitude Isometrique - close to tsfnz ...
  838. latiso : function(eccent, phi, sinphi) {
  839. if (Math.abs(phi) > this.HALF_PI) return +Number.NaN;
  840. if (phi==this.HALF_PI) return Number.POSITIVE_INFINITY;
  841. if (phi==-1.0*this.HALF_PI) return -1.0*Number.POSITIVE_INFINITY;
  842. var con= eccent*sinphi;
  843. return Math.log(Math.tan((this.HALF_PI+phi)/2.0))+eccent*Math.log((1.0-con)/(1.0+con))/2.0;
  844. },
  845. fL : function(x,L) {
  846. return 2.0*Math.atan(x*Math.exp(L)) - this.HALF_PI;
  847. },
  848. // Inverse Latitude Isometrique - close to ph2z
  849. invlatiso : function(eccent, ts) {
  850. var phi= this.fL(1.0,ts);
  851. var Iphi= 0.0;
  852. var con= 0.0;
  853. do {
  854. Iphi= phi;
  855. con= eccent*Math.sin(Iphi);
  856. phi= this.fL(Math.exp(eccent*Math.log((1.0+con)/(1.0-con))/2.0),ts)
  857. } while (Math.abs(phi-Iphi)>1.0e-12);
  858. return phi;
  859. },
  860. // Needed for Gauss Schreiber
  861. // Original: Denis Makarov (info@binarythings.com)
  862. // Web Site: http://www.binarythings.com
  863. sinh : function(x)
  864. {
  865. var r= Math.exp(x);
  866. r= (r-1.0/r)/2.0;
  867. return r;
  868. },
  869. cosh : function(x)
  870. {
  871. var r= Math.exp(x);
  872. r= (r+1.0/r)/2.0;
  873. return r;
  874. },
  875. tanh : function(x)
  876. {
  877. var r= Math.exp(x);
  878. r= (r-1.0/r)/(r+1.0/r);
  879. return r;
  880. },
  881. asinh : function(x)
  882. {
  883. var s= (x>= 0? 1.0:-1.0);
  884. return s*(Math.log( Math.abs(x) + Math.sqrt(x*x+1.0) ));
  885. },
  886. acosh : function(x)
  887. {
  888. return 2.0*Math.log(Math.sqrt((x+1.0)/2.0) + Math.sqrt((x-1.0)/2.0));
  889. },
  890. atanh : function(x)
  891. {
  892. return Math.log((x-1.0)/(x+1.0))/2.0;
  893. },
  894. // Grande Normale
  895. gN : function(a,e,sinphi)
  896. {
  897. var temp= e*sinphi;
  898. return a/Math.sqrt(1.0 - temp*temp);
  899. }
  900. };
  901. /** datum object
  902. */
  903. Proj4js.datum = Proj4js.Class({
  904. initialize : function(proj) {
  905. this.datum_type = Proj4js.common.PJD_WGS84; //default setting
  906. if (proj.datumCode && proj.datumCode == 'none') {
  907. this.datum_type = Proj4js.common.PJD_NODATUM;
  908. }
  909. if (proj && proj.datum_params) {
  910. for (var i=0; i<proj.datum_params.length; i++) {
  911. proj.datum_params[i]=parseFloat(proj.datum_params[i]);
  912. }
  913. if (proj.datum_params[0] != 0 || proj.datum_params[1] != 0 || proj.datum_params[2] != 0 ) {
  914. this.datum_type = Proj4js.common.PJD_3PARAM;
  915. }
  916. if (proj.datum_params.length > 3) {
  917. if (proj.datum_params[3] != 0 || proj.datum_params[4] != 0 ||
  918. proj.datum_params[5] != 0 || proj.datum_params[6] != 0 ) {
  919. this.datum_type = Proj4js.common.PJD_7PARAM;
  920. proj.datum_params[3] *= Proj4js.common.SEC_TO_RAD;
  921. proj.datum_params[4] *= Proj4js.common.SEC_TO_RAD;
  922. proj.datum_params[5] *= Proj4js.common.SEC_TO_RAD;
  923. proj.datum_params[6] = (proj.datum_params[6]/1000000.0) + 1.0;
  924. }
  925. }
  926. }
  927. if (proj) {
  928. this.a = proj.a; //datum object also uses these values
  929. this.b = proj.b;
  930. this.es = proj.es;
  931. this.ep2 = proj.ep2;
  932. this.datum_params = proj.datum_params;
  933. }
  934. },
  935. /****************************************************************/
  936. // cs_compare_datums()
  937. // Returns 1 (TRUE) if the two datums match, otherwise 0 (FALSE).
  938. compare_datums : function( dest ) {
  939. if( this.datum_type != dest.datum_type ) {
  940. return false; // false, datums are not equal
  941. } else if( this.a != dest.a || Math.abs(this.es-dest.es) > 0.000000000050 ) {
  942. // the tolerence for es is to ensure that GRS80 and WGS84
  943. // are considered identical
  944. return false;
  945. } else if( this.datum_type == Proj4js.common.PJD_3PARAM ) {
  946. return (this.datum_params[0] == dest.datum_params[0]
  947. && this.datum_params[1] == dest.datum_params[1]
  948. && this.datum_params[2] == dest.datum_params[2]);
  949. } else if( this.datum_type == Proj4js.common.PJD_7PARAM ) {
  950. return (this.datum_params[0] == dest.datum_params[0]
  951. && this.datum_params[1] == dest.datum_params[1]
  952. && this.datum_params[2] == dest.datum_params[2]
  953. && this.datum_params[3] == dest.datum_params[3]
  954. && this.datum_params[4] == dest.datum_params[4]
  955. && this.datum_params[5] == dest.datum_params[5]
  956. && this.datum_params[6] == dest.datum_params[6]);
  957. } else if( this.datum_type == Proj4js.common.PJD_GRIDSHIFT ) {
  958. return strcmp( pj_param(this.params,"snadgrids").s,
  959. pj_param(dest.params,"snadgrids").s ) == 0;
  960. } else {
  961. return true; // datums are equal
  962. }
  963. }, // cs_compare_datums()
  964. /*
  965. * The function Convert_Geodetic_To_Geocentric converts geodetic coordinates
  966. * (latitude, longitude, and height) to geocentric coordinates (X, Y, Z),
  967. * according to the current ellipsoid parameters.
  968. *
  969. * Latitude : Geodetic latitude in radians (input)
  970. * Longitude : Geodetic longitude in radians (input)
  971. * Height : Geodetic height, in meters (input)
  972. * X : Calculated Geocentric X coordinate, in meters (output)
  973. * Y : Calculated Geocentric Y coordinate, in meters (output)
  974. * Z : Calculated Geocentric Z coordinate, in meters (output)
  975. *
  976. */
  977. geodetic_to_geocentric : function(p) {
  978. var Longitude = p.x;
  979. var Latitude = p.y;
  980. var Height = p.z ? p.z : 0; //Z value not always supplied
  981. var X; // output
  982. var Y;
  983. var Z;
  984. var Error_Code=0; // GEOCENT_NO_ERROR;
  985. var Rn; /* Earth radius at location */
  986. var Sin_Lat; /* Math.sin(Latitude) */
  987. var Sin2_Lat; /* Square of Math.sin(Latitude) */
  988. var Cos_Lat; /* Math.cos(Latitude) */
  989. /*
  990. ** Don't blow up if Latitude is just a little out of the value
  991. ** range as it may just be a rounding issue. Also removed longitude
  992. ** test, it should be wrapped by Math.cos() and Math.sin(). NFW for PROJ.4, Sep/2001.
  993. */
  994. if( Latitude < -Proj4js.common.HALF_PI && Latitude > -1.001 * Proj4js.common.HALF_PI ) {
  995. Latitude = -Proj4js.common.HALF_PI;
  996. } else if( Latitude > Proj4js.common.HALF_PI && Latitude < 1.001 * Proj4js.common.HALF_PI ) {
  997. Latitude = Proj4js.common.HALF_PI;
  998. } else if ((Latitude < -Proj4js.common.HALF_PI) || (Latitude > Proj4js.common.HALF_PI)) {
  999. /* Latitude out of range */
  1000. Proj4js.reportError('geocent:lat out of range:'+Latitude);
  1001. return null;
  1002. }
  1003. if (Longitude > Proj4js.common.PI) Longitude -= (2*Proj4js.common.PI);
  1004. Sin_Lat = Math.sin(Latitude);
  1005. Cos_Lat = Math.cos(Latitude);
  1006. Sin2_Lat = Sin_Lat * Sin_Lat;
  1007. Rn = this.a / (Math.sqrt(1.0e0 - this.es * Sin2_Lat));
  1008. X = (Rn + Height) * Cos_Lat * Math.cos(Longitude);
  1009. Y = (Rn + Height) * Cos_Lat * Math.sin(Longitude);
  1010. Z = ((Rn * (1 - this.es)) + Height) * Sin_Lat;
  1011. p.x = X;
  1012. p.y = Y;
  1013. p.z = Z;
  1014. return Error_Code;
  1015. }, // cs_geodetic_to_geocentric()
  1016. geocentric_to_geodetic : function (p) {
  1017. /* local defintions and variables */
  1018. /* end-criterium of loop, accuracy of sin(Latitude) */
  1019. var genau = 1.E-12;
  1020. var genau2 = (genau*genau);
  1021. var maxiter = 30;
  1022. var P; /* distance between semi-minor axis and location */
  1023. var RR; /* distance between center and location */
  1024. var CT; /* sin of geocentric latitude */
  1025. var ST; /* cos of geocentric latitude */
  1026. var RX;
  1027. var RK;
  1028. var RN; /* Earth radius at location */
  1029. var CPHI0; /* cos of start or old geodetic latitude in iterations */
  1030. var SPHI0; /* sin of start or old geodetic latitude in iterations */
  1031. var CPHI; /* cos of searched geodetic latitude */
  1032. var SPHI; /* sin of searched geodetic latitude */
  1033. var SDPHI; /* end-criterium: addition-theorem of sin(Latitude(iter)-Latitude(iter-1)) */
  1034. var At_Pole; /* indicates location is in polar region */
  1035. var iter; /* # of continous iteration, max. 30 is always enough (s.a.) */
  1036. var X = p.x;
  1037. var Y = p.y;
  1038. var Z = p.z ? p.z : 0.0; //Z value not always supplied
  1039. var Longitude;
  1040. var Latitude;
  1041. var Height;
  1042. At_Pole = false;
  1043. P = Math.sqrt(X*X+Y*Y);
  1044. RR = Math.sqrt(X*X+Y*Y+Z*Z);
  1045. /* special cases for latitude and longitude */
  1046. if (P/this.a < genau) {
  1047. /* special case, if P=0. (X=0., Y=0.) */
  1048. At_Pole = true;
  1049. Longitude = 0.0;
  1050. /* if (X,Y,Z)=(0.,0.,0.) then Height becomes semi-minor axis
  1051. * of ellipsoid (=center of mass), Latitude becomes PI/2 */
  1052. if (RR/this.a < genau) {
  1053. Latitude = Proj4js.common.HALF_PI;
  1054. Height = -this.b;
  1055. return;
  1056. }
  1057. } else {
  1058. /* ellipsoidal (geodetic) longitude
  1059. * interval: -PI < Longitude <= +PI */
  1060. Longitude=Math.atan2(Y,X);
  1061. }
  1062. /* --------------------------------------------------------------
  1063. * Following iterative algorithm was developped by
  1064. * "Institut für Erdmessung", University of Hannover, July 1988.
  1065. * Internet: www.ife.uni-hannover.de
  1066. * Iterative computation of CPHI,SPHI and Height.
  1067. * Iteration of CPHI and SPHI to 10**-12 radian resp.
  1068. * 2*10**-7 arcsec.
  1069. * --------------------------------------------------------------
  1070. */
  1071. CT = Z/RR;
  1072. ST = P/RR;
  1073. RX = 1.0/Math.sqrt(1.0-this.es*(2.0-this.es)*ST*ST);
  1074. CPHI0 = ST*(1.0-this.es)*RX;
  1075. SPHI0 = CT*RX;
  1076. iter = 0;
  1077. /* loop to find sin(Latitude) resp. Latitude
  1078. * until |sin(Latitude(iter)-Latitude(iter-1))| < genau */
  1079. do
  1080. {
  1081. iter++;
  1082. RN = this.a/Math.sqrt(1.0-this.es*SPHI0*SPHI0);
  1083. /* ellipsoidal (geodetic) height */
  1084. Height = P*CPHI0+Z*SPHI0-RN*(1.0-this.es*SPHI0*SPHI0);
  1085. RK = this.es*RN/(RN+Height);
  1086. RX = 1.0/Math.sqrt(1.0-RK*(2.0-RK)*ST*ST);
  1087. CPHI = ST*(1.0-RK)*RX;
  1088. SPHI = CT*RX;
  1089. SDPHI = SPHI*CPHI0-CPHI*SPHI0;
  1090. CPHI0 = CPHI;
  1091. SPHI0 = SPHI;
  1092. }
  1093. while (SDPHI*SDPHI > genau2 && iter < maxiter);
  1094. /* ellipsoidal (geodetic) latitude */
  1095. Latitude=Math.atan(SPHI/Math.abs(CPHI));
  1096. p.x = Longitude;
  1097. p.y = Latitude;
  1098. p.z = Height;
  1099. return p;
  1100. }, // cs_geocentric_to_geodetic()
  1101. /** Convert_Geocentric_To_Geodetic
  1102. * The method used here is derived from 'An Improved Algorithm for
  1103. * Geocentric to Geodetic Coordinate Conversion', by Ralph Toms, Feb 1996
  1104. */
  1105. geocentric_to_geodetic_noniter : function (p) {
  1106. var X = p.x;
  1107. var Y = p.y;
  1108. var Z = p.z ? p.z : 0; //Z value not always supplied
  1109. var Longitude;
  1110. var Latitude;
  1111. var Height;
  1112. var W; /* distance from Z axis */
  1113. var W2; /* square of distance from Z axis */
  1114. var T0; /* initial estimate of vertical component */
  1115. var T1; /* corrected estimate of vertical component */
  1116. var S0; /* initial estimate of horizontal component */
  1117. var S1; /* corrected estimate of horizontal component */
  1118. var Sin_B0; /* Math.sin(B0), B0 is estimate of Bowring aux variable */
  1119. var Sin3_B0; /* cube of Math.sin(B0) */
  1120. var Cos_B0; /* Math.cos(B0) */
  1121. var Sin_p1; /* Math.sin(phi1), phi1 is estimated latitude */
  1122. var Cos_p1; /* Math.cos(phi1) */
  1123. var Rn; /* Earth radius at location */
  1124. var Sum; /* numerator of Math.cos(phi1) */
  1125. var At_Pole; /* indicates location is in polar region */
  1126. X = parseFloat(X); // cast from string to float
  1127. Y = parseFloat(Y);
  1128. Z = parseFloat(Z);
  1129. At_Pole = false;
  1130. if (X != 0.0)
  1131. {
  1132. Longitude = Math.atan2(Y,X);
  1133. }
  1134. else
  1135. {
  1136. if (Y > 0)
  1137. {
  1138. Longitude = Proj4js.common.HALF_PI;
  1139. }
  1140. else if (Y < 0)
  1141. {
  1142. Longitude = -Proj4js.common.HALF_PI;
  1143. }
  1144. else
  1145. {
  1146. At_Pole = true;
  1147. Longitude = 0.0;
  1148. if (Z > 0.0)
  1149. { /* north pole */
  1150. Latitude = Proj4js.common.HALF_PI;
  1151. }
  1152. else if (Z < 0.0)
  1153. { /* south pole */
  1154. Latitude = -Proj4js.common.HALF_PI;
  1155. }
  1156. else
  1157. { /* center of earth */
  1158. Latitude = Proj4js.common.HALF_PI;
  1159. Height = -this.b;
  1160. return;
  1161. }
  1162. }
  1163. }
  1164. W2 = X*X + Y*Y;
  1165. W = Math.sqrt(W2);
  1166. T0 = Z * Proj4js.common.AD_C;
  1167. S0 = Math.sqrt(T0 * T0 + W2);
  1168. Sin_B0 = T0 / S0;
  1169. Cos_B0 = W / S0;
  1170. Sin3_B0 = Sin_B0 * Sin_B0 * Sin_B0;
  1171. T1 = Z + this.b * this.ep2 * Sin3_B0;
  1172. Sum = W - this.a * this.es * Cos_B0 * Cos_B0 * Cos_B0;
  1173. S1 = Math.sqrt(T1*T1 + Sum * Sum);
  1174. Sin_p1 = T1 / S1;
  1175. Cos_p1 = Sum / S1;
  1176. Rn = this.a / Math.sqrt(1.0 - this.es * Sin_p1 * Sin_p1);
  1177. if (Cos_p1 >= Proj4js.common.COS_67P5)
  1178. {
  1179. Height = W / Cos_p1 - Rn;
  1180. }
  1181. else if (Cos_p1 <= -Proj4js.common.COS_67P5)
  1182. {
  1183. Height = W / -Cos_p1 - Rn;
  1184. }
  1185. else
  1186. {
  1187. Height = Z / Sin_p1 + Rn * (this.es - 1.0);
  1188. }
  1189. if (At_Pole == false)
  1190. {
  1191. Latitude = Math.atan(Sin_p1 / Cos_p1);
  1192. }
  1193. p.x = Longitude;
  1194. p.y = Latitude;
  1195. p.z = Height;
  1196. return p;
  1197. }, // geocentric_to_geodetic_noniter()
  1198. /****************************************************************/
  1199. // pj_geocentic_to_wgs84( p )
  1200. // p = point to transform in geocentric coordinates (x,y,z)
  1201. geocentric_to_wgs84 : function ( p ) {
  1202. if( this.datum_type == Proj4js.common.PJD_3PARAM )
  1203. {
  1204. // if( x[io] == HUGE_VAL )
  1205. // continue;
  1206. p.x += this.datum_params[0];
  1207. p.y += this.datum_params[1];
  1208. p.z += this.datum_params[2];
  1209. }
  1210. else if (this.datum_type == Proj4js.common.PJD_7PARAM)
  1211. {
  1212. var Dx_BF =this.datum_params[0];
  1213. var Dy_BF =this.datum_params[1];
  1214. var Dz_BF =this.datum_params[2];
  1215. var Rx_BF =this.datum_params[3];
  1216. var Ry_BF =this.datum_params[4];
  1217. var Rz_BF =this.datum_params[5];
  1218. var M_BF =this.datum_params[6];
  1219. // if( x[io] == HUGE_VAL )
  1220. // continue;
  1221. var x_out = M_BF*( p.x - Rz_BF*p.y + Ry_BF*p.z) + Dx_BF;
  1222. var y_out = M_BF*( Rz_BF*p.x + p.y - Rx_BF*p.z) + Dy_BF;
  1223. var z_out = M_BF*(-Ry_BF*p.x + Rx_BF*p.y + p.z) + Dz_BF;
  1224. p.x = x_out;
  1225. p.y = y_out;
  1226. p.z = z_out;
  1227. }
  1228. }, // cs_geocentric_to_wgs84
  1229. /****************************************************************/
  1230. // pj_geocentic_from_wgs84()
  1231. // coordinate system definition,
  1232. // point to transform in geocentric coordinates (x,y,z)
  1233. geocentric_from_wgs84 : function( p ) {
  1234. if( this.datum_type == Proj4js.common.PJD_3PARAM )
  1235. {
  1236. //if( x[io] == HUGE_VAL )
  1237. // continue;
  1238. p.x -= this.datum_params[0];
  1239. p.y -= this.datum_params[1];
  1240. p.z -= this.datum_params[2];
  1241. }
  1242. else if (this.datum_type == Proj4js.common.PJD_7PARAM)
  1243. {
  1244. var Dx_BF =this.datum_params[0];
  1245. var Dy_BF =this.datum_params[1];
  1246. var Dz_BF =this.datum_params[2];
  1247. var Rx_BF =this.datum_params[3];
  1248. var Ry_BF =this.datum_params[4];
  1249. var Rz_BF =this.datum_params[5];
  1250. var M_BF =this.datum_params[6];
  1251. var x_tmp = (p.x - Dx_BF) / M_BF;
  1252. var y_tmp = (p.y - Dy_BF) / M_BF;
  1253. var z_tmp = (p.z - Dz_BF) / M_BF;
  1254. //if( x[io] == HUGE_VAL )
  1255. // continue;
  1256. p.x = x_tmp + Rz_BF*y_tmp - Ry_BF*z_tmp;
  1257. p.y = -Rz_BF*x_tmp + y_tmp + Rx_BF*z_tmp;
  1258. p.z = Ry_BF*x_tmp - Rx_BF*y_tmp + z_tmp;
  1259. } //cs_geocentric_from_wgs84()
  1260. }
  1261. });
  1262. /** point object, nothing fancy, just allows values to be
  1263. passed back and forth by reference rather than by value.
  1264. Other point classes may be used as long as they have
  1265. x and y properties, which will get modified in the transform method.
  1266. */
  1267. Proj4js.Point = Proj4js.Class({
  1268. /**
  1269. * Constructor: Proj4js.Point
  1270. *
  1271. * Parameters:
  1272. * - x {float} or {Array} either the first coordinates component or
  1273. * the full coordinates
  1274. * - y {float} the second component
  1275. * - z {float} the third component, optional.
  1276. */
  1277. initialize : function(x,y,z) {
  1278. if (typeof x == 'object') {
  1279. this.x = x[0];
  1280. this.y = x[1];
  1281. this.z = x[2] || 0.0;
  1282. } else if (typeof x == 'string') {
  1283. var coords = x.split(',');
  1284. this.x = parseFloat(coords[0]);
  1285. this.y = parseFloat(coords[1]);
  1286. this.z = parseFloat(coords[2]) || 0.0;
  1287. } else {
  1288. this.x = x;
  1289. this.y = y;
  1290. this.z = z || 0.0;
  1291. }
  1292. },
  1293. /**
  1294. * APIMethod: clone
  1295. * Build a copy of a Proj4js.Point object.
  1296. *
  1297. * Return:
  1298. * {Proj4js}.Point the cloned point.
  1299. */
  1300. clone : function() {
  1301. return new Proj4js.Point(this.x, this.y, this.z);
  1302. },
  1303. /**
  1304. * APIMethod: toString
  1305. * Return a readable string version of the point
  1306. *
  1307. * Return:
  1308. * {String} String representation of Proj4js.Point object.
  1309. * (ex. <i>"x=5,y=42"</i>)
  1310. */
  1311. toString : function() {
  1312. return ("x=" + this.x + ",y=" + this.y);
  1313. },
  1314. /**
  1315. * APIMethod: toShortString
  1316. * Return a short string version of the point.
  1317. *
  1318. * Return:
  1319. * {String} Shortened String representation of Proj4js.Point object.
  1320. * (ex. <i>"5, 42"</i>)
  1321. */
  1322. toShortString : function() {
  1323. return (this.x + ", " + this.y);
  1324. }
  1325. });
  1326. Proj4js.PrimeMeridian = {
  1327. "greenwich": 0.0, //"0dE",
  1328. "lisbon": -9.131906111111, //"9d07'54.862\"W",
  1329. "paris": 2.337229166667, //"2d20'14.025\"E"…