Proj4js

reprojection library

目前为 2015-10-16 提交的版本。查看 最新版本

此脚本不应直接安装,它是一个供其他脚本使用的外部库。如果您需要使用该库,请在脚本元属性加入:// @require https://update.cn-greasyfork.org/scripts/13097/80447/Proj4js.js

  1. /*
  2. Author: Mike Adair madairATdmsolutions.ca
  3. Richard Greenwood rich@greenwoodmap.com
  4. License: MIT as per: ../LICENSE
  5.  
  6. $Id: Proj.js 2956 2007-07-09 12:17:52Z steven $
  7. */
  8.  
  9. /**
  10. * Namespace: Proj4js
  11. *
  12. * Proj4js is a JavaScript library to transform point coordinates from one
  13. * coordinate system to another, including datum transformations.
  14. *
  15. * This library is a port of both the Proj.4 and GCTCP C libraries to JavaScript.
  16. * Enabling these transformations in the browser allows geographic data stored
  17. * in different projections to be combined in browser-based web mapping
  18. * applications.
  19. *
  20. * Proj4js must have access to coordinate system initialization strings (which
  21. * are the same as for PROJ.4 command line). Thes can be included in your
  22. * application using a <script> tag or Proj4js can load CS initialization
  23. * strings from a local directory or a web service such as spatialreference.org.
  24. *
  25. * Similarly, Proj4js must have access to projection transform code. These can
  26. * be included individually using a <script> tag in your page, built into a
  27. * custom build of Proj4js or loaded dynamically at run-time. Using the
  28. * -combined and -compressed versions of Proj4js includes all projection class
  29. * code by default.
  30. *
  31. * Note that dynamic loading of defs and code happens ascynchrously, check the
  32. * Proj.readyToUse flag before using the Proj object. If the defs and code
  33. * required by your application are loaded through script tags, dynamic loading
  34. * is not required and the Proj object will be readyToUse on return from the
  35. * constructor.
  36. *
  37. * All coordinates are handled as points which have a .x and a .y property
  38. * which will be modified in place.
  39. *
  40. * Override Proj4js.reportError for output of alerts and warnings.
  41. *
  42. * See http://trac.osgeo.org/proj4js/wiki/UserGuide for full details.
  43. */
  44.  
  45. /**
  46. * Global namespace object for Proj4js library
  47. */
  48. var Proj4js = {
  49.  
  50. /**
  51. * Property: defaultDatum
  52. * The datum to use when no others a specified
  53. */
  54. defaultDatum: 'WGS84', //default datum
  55.  
  56. /**
  57. * Method: transform(source, dest, point)
  58. * Transform a point coordinate from one map projection to another. This is
  59. * really the only public method you should need to use.
  60. *
  61. * Parameters:
  62. * source - {Proj4js.Proj} source map projection for the transformation
  63. * dest - {Proj4js.Proj} destination map projection for the transformation
  64. * point - {Object} point to transform, may be geodetic (long, lat) or
  65. * projected Cartesian (x,y), but should always have x,y properties.
  66. */
  67. transform: function(source, dest, point) {
  68. if (!source.readyToUse) {
  69. this.reportError("Proj4js initialization for:"+source.srsCode+" not yet complete");
  70. return point;
  71. }
  72. if (!dest.readyToUse) {
  73. this.reportError("Proj4js initialization for:"+dest.srsCode+" not yet complete");
  74. return point;
  75. }
  76. // Workaround for datum shifts towgs84, if either source or destination projection is not wgs84
  77. if (source.datum && dest.datum && (
  78. ((source.datum.datum_type == Proj4js.common.PJD_3PARAM || source.datum.datum_type == Proj4js.common.PJD_7PARAM) && dest.datumCode != "WGS84") ||
  79. ((dest.datum.datum_type == Proj4js.common.PJD_3PARAM || dest.datum.datum_type == Proj4js.common.PJD_7PARAM) && source.datumCode != "WGS84"))) {
  80. var wgs84 = Proj4js.WGS84;
  81. this.transform(source, wgs84, point);
  82. source = wgs84;
  83. }
  84.  
  85. // DGR, 2010/11/12
  86. if (source.axis!="enu") {
  87. this.adjust_axis(source,false,point);
  88. }
  89.  
  90. // Transform source points to long/lat, if they aren't already.
  91. if (source.projName=="longlat") {
  92. point.x *= Proj4js.common.D2R; // convert degrees to radians
  93. point.y *= Proj4js.common.D2R;
  94. } else {
  95. if (source.to_meter) {
  96. point.x *= source.to_meter;
  97. point.y *= source.to_meter;
  98. }
  99. source.inverse(point); // Convert Cartesian to longlat
  100. }
  101.  
  102. // Adjust for the prime meridian if necessary
  103. if (source.from_greenwich) {
  104. point.x += source.from_greenwich;
  105. }
  106.  
  107. // Convert datums if needed, and if possible.
  108. point = this.datum_transform( source.datum, dest.datum, point );
  109.  
  110. // Adjust for the prime meridian if necessary
  111. if (dest.from_greenwich) {
  112. point.x -= dest.from_greenwich;
  113. }
  114.  
  115. if (dest.projName=="longlat") {
  116. // convert radians to decimal degrees
  117. point.x *= Proj4js.common.R2D;
  118. point.y *= Proj4js.common.R2D;
  119. } else { // else project
  120. dest.forward(point);
  121. if (dest.to_meter) {
  122. point.x /= dest.to_meter;
  123. point.y /= dest.to_meter;
  124. }
  125. }
  126.  
  127. // DGR, 2010/11/12
  128. if (dest.axis!="enu") {
  129. this.adjust_axis(dest,true,point);
  130. }
  131.  
  132. return point;
  133. }, // transform()
  134.  
  135. /** datum_transform()
  136. source coordinate system definition,
  137. destination coordinate system definition,
  138. point to transform in geodetic coordinates (long, lat, height)
  139. */
  140. datum_transform : function( source, dest, point ) {
  141.  
  142. // Short cut if the datums are identical.
  143. if( source.compare_datums( dest ) ) {
  144. return point; // in this case, zero is sucess,
  145. // whereas cs_compare_datums returns 1 to indicate TRUE
  146. // confusing, should fix this
  147. }
  148.  
  149. // Explicitly skip datum transform by setting 'datum=none' as parameter for either source or dest
  150. if( source.datum_type == Proj4js.common.PJD_NODATUM
  151. || dest.datum_type == Proj4js.common.PJD_NODATUM) {
  152. return point;
  153. }
  154.  
  155. //DGR: 2012-07-29 : add nadgrids support (begin)
  156. var src_a = source.a;
  157. var src_es = source.es;
  158.  
  159. var dst_a = dest.a;
  160. var dst_es = dest.es;
  161.  
  162. var fallback= source.datum_type;
  163. // If this datum requires grid shifts, then apply it to geodetic coordinates.
  164. if( fallback == Proj4js.common.PJD_GRIDSHIFT )
  165. {
  166. if (this.apply_gridshift( source, 0, point )==0) {
  167. source.a = Proj4js.common.SRS_WGS84_SEMIMAJOR;
  168. source.es = Proj4js.common.SRS_WGS84_ESQUARED;
  169. } else {
  170.  
  171. // try 3 or 7 params transformation or nothing ?
  172. if (!source.datum_params) {
  173. source.a = src_a;
  174. source.es = source.es;
  175. return point;
  176. }
  177. var wp= 1.0;
  178. for (var i= 0, l= source.datum_params.length; i<l; i++) {
  179. wp*= source.datum_params[i];
  180. }
  181. if (wp==0.0) {
  182. source.a = src_a;
  183. source.es = source.es;
  184. return point;
  185. }
  186. fallback= source.datum_params.length>3?
  187. Proj4js.common.PJD_7PARAM
  188. : Proj4js.common.PJD_3PARAM;
  189. // CHECK_RETURN;
  190. }
  191. }
  192.  
  193. if( dest.datum_type == Proj4js.common.PJD_GRIDSHIFT )
  194. {
  195. dest.a = Proj4js.common.SRS_WGS84_SEMIMAJOR;
  196. dest.es = Proj4js.common.SRS_WGS84_ESQUARED;
  197. }
  198. // Do we need to go through geocentric coordinates?
  199. if (source.es != dest.es || source.a != dest.a
  200. || fallback == Proj4js.common.PJD_3PARAM
  201. || fallback == Proj4js.common.PJD_7PARAM
  202. || dest.datum_type == Proj4js.common.PJD_3PARAM
  203. || dest.datum_type == Proj4js.common.PJD_7PARAM)
  204. {
  205. //DGR: 2012-07-29 : add nadgrids support (end)
  206.  
  207. // Convert to geocentric coordinates.
  208. source.geodetic_to_geocentric( point );
  209. // CHECK_RETURN;
  210.  
  211. // Convert between datums
  212. if( source.datum_type == Proj4js.common.PJD_3PARAM || source.datum_type == Proj4js.common.PJD_7PARAM ) {
  213. source.geocentric_to_wgs84(point);
  214. // CHECK_RETURN;
  215. }
  216.  
  217. if( dest.datum_type == Proj4js.common.PJD_3PARAM || dest.datum_type == Proj4js.common.PJD_7PARAM ) {
  218. dest.geocentric_from_wgs84(point);
  219. // CHECK_RETURN;
  220. }
  221.  
  222. // Convert back to geodetic coordinates
  223. dest.geocentric_to_geodetic( point );
  224. // CHECK_RETURN;
  225. }
  226.  
  227. // Apply grid shift to destination if required
  228. if( dest.datum_type == Proj4js.common.PJD_GRIDSHIFT )
  229. {
  230. this.apply_gridshift( dest, 1, point);
  231. // CHECK_RETURN;
  232. }
  233.  
  234. source.a = src_a;
  235. source.es = src_es;
  236. dest.a = dst_a;
  237. dest.es = dst_es;
  238.  
  239. return point;
  240. }, // cs_datum_transform
  241.  
  242. /**
  243. * This is the real workhorse, given a gridlist
  244. * DGR: 2012-07-29 addition based on proj4 trunk
  245. */
  246. apply_gridshift : function(srs,inverse,point) {
  247. if (srs.grids==null || srs.grids.length==0) {
  248. return -38;
  249. }
  250. var input= {"x":point.x, "y":point.y};
  251. var output= {"x":Number.NaN, "y":Number.NaN};
  252. /* keep trying till we find a table that works */
  253. var onlyMandatoryGrids= false;
  254. for (var i= 0, l= srs.grids.length; i<l; i++) {
  255. var gi= srs.grids[i];
  256. onlyMandatoryGrids= gi.mandatory;
  257. var ct= gi.grid;
  258. if (ct==null) {
  259. if (gi.mandatory) {
  260. this.reportError("unable to find '"+gi.name+"' grid.");
  261. return -48;
  262. }
  263. continue;//optional grid
  264. }
  265. /* skip tables that don't match our point at all. */
  266. var epsilon= (Math.abs(ct.del[1])+Math.abs(ct.del[0]))/10000.0;
  267. if( ct.ll[1]-epsilon>input.y || ct.ll[0]-epsilon>input.x ||
  268. ct.ll[1]+(ct.lim[1]-1)*ct.del[1]+epsilon<input.y ||
  269. ct.ll[0]+(ct.lim[0]-1)*ct.del[0]+epsilon<input.x ) {
  270. continue;
  271. }
  272. /* If we have child nodes, check to see if any of them apply. */
  273. /* TODO : only plain grid has been implemented ... */
  274. /* we found a more refined child node to use */
  275. /* load the grid shift info if we don't have it. */
  276. /* TODO : Proj4js.grids pre-loaded (as they can be huge ...) */
  277. /* skip numerical computing error when "null" grid (identity grid): */
  278. if (gi.name=="null") {
  279. output.x= input.x;
  280. output.y= input.y;
  281. } else {
  282. output= Proj4js.common.nad_cvt(input, inverse, ct);
  283. }
  284. if (!isNaN(output.x)) {
  285. break;
  286. }
  287. }
  288. if (isNaN(output.x)) {
  289. if (!onlyMandatoryGrids) {
  290. this.reportError("failed to find a grid shift table for location '"+
  291. input.x*Proj4js.common.R2D+" "+input.y*Proj4js.common.R2D+
  292. " tried: '"+srs.nadgrids+"'");
  293. return -48;
  294. }
  295. return -1;//FIXME: no shift applied ...
  296. }
  297. point.x= output.x;
  298. point.y= output.y;
  299. return 0;
  300. },
  301.  
  302. /**
  303. * Function: adjust_axis
  304. * Normalize or de-normalized the x/y/z axes. The normal form is "enu"
  305. * (easting, northing, up).
  306. * Parameters:
  307. * crs {Proj4js.Proj} the coordinate reference system
  308. * denorm {Boolean} when false, normalize
  309. * point {Object} the coordinates to adjust
  310. */
  311. adjust_axis: function(crs, denorm, point) {
  312. var xin= point.x, yin= point.y, zin= point.z || 0.0;
  313. var v, t;
  314. for (var i= 0; i<3; i++) {
  315. if (denorm && i==2 && point.z===undefined) { continue; }
  316. if (i==0) { v= xin; t= 'x'; }
  317. else if (i==1) { v= yin; t= 'y'; }
  318. else { v= zin; t= 'z'; }
  319. switch(crs.axis[i]) {
  320. case 'e':
  321. point[t]= v;
  322. break;
  323. case 'w':
  324. point[t]= -v;
  325. break;
  326. case 'n':
  327. point[t]= v;
  328. break;
  329. case 's':
  330. point[t]= -v;
  331. break;
  332. case 'u':
  333. if (point[t]!==undefined) { point.z= v; }
  334. break;
  335. case 'd':
  336. if (point[t]!==undefined) { point.z= -v; }
  337. break;
  338. default :
  339. alert("ERROR: unknow axis ("+crs.axis[i]+") - check definition of "+crs.projName);
  340. return null;
  341. }
  342. }
  343. return point;
  344. },
  345.  
  346. /**
  347. * Function: reportError
  348. * An internal method to report errors back to user.
  349. * Override this in applications to report error messages or throw exceptions.
  350. */
  351. reportError: function(msg) {
  352. //console.log(msg);
  353. },
  354.  
  355. /**
  356. *
  357. * Title: Private Methods
  358. * The following properties and methods are intended for internal use only.
  359. *
  360. * This is a minimal implementation of JavaScript inheritance methods so that
  361. * Proj4js can be used as a stand-alone library.
  362. * These are copies of the equivalent OpenLayers methods at v2.7
  363. */
  364. /**
  365. * Function: extend
  366. * Copy all properties of a source object to a destination object. Modifies
  367. * the passed in destination object. Any properties on the source object
  368. * that are set to undefined will not be (re)set on the destination object.
  369. *
  370. * Parameters:
  371. * destination - {Object} The object that will be modified
  372. * source - {Object} The object with properties to be set on the destination
  373. *
  374. * Returns:
  375. * {Object} The destination object.
  376. */
  377. extend: function(destination, source) {
  378. destination = destination || {};
  379. if(source) {
  380. for(var property in source) {
  381. var value = source[property];
  382. if(value !== undefined) {
  383. destination[property] = value;
  384. }
  385. }
  386. }
  387. return destination;
  388. },
  389.  
  390. /**
  391. * Constructor: Class
  392. * Base class used to construct all other classes. Includes support for
  393. * multiple inheritance.
  394. *
  395. */
  396. Class: function() {
  397. var Class = function() {
  398. this.initialize.apply(this, arguments);
  399. };
  400. var extended = {};
  401. var parent;
  402. for(var i=0; i<arguments.length; ++i) {
  403. if(typeof arguments[i] == "function") {
  404. // get the prototype of the superclass
  405. parent = arguments[i].prototype;
  406. } else {
  407. // in this case we're extending with the prototype
  408. parent = arguments[i];
  409. }
  410. Proj4js.extend(extended, parent);
  411. }
  412. Class.prototype = extended;
  413. return Class;
  414. },
  415.  
  416. /**
  417. * Function: bind
  418. * Bind a function to an object. Method to easily create closures with
  419. * 'this' altered.
  420. *
  421. * Parameters:
  422. * func - {Function} Input function.
  423. * object - {Object} The object to bind to the input function (as this).
  424. *
  425. * Returns:
  426. * {Function} A closure with 'this' set to the passed in object.
  427. */
  428. bind: function(func, object) {
  429. // create a reference to all arguments past the second one
  430. var args = Array.prototype.slice.apply(arguments, [2]);
  431. return function() {
  432. // Push on any additional arguments from the actual function call.
  433. // These will come after those sent to the bind call.
  434. var newArgs = args.concat(
  435. Array.prototype.slice.apply(arguments, [0])
  436. );
  437. return func.apply(object, newArgs);
  438. };
  439. },
  440. /**
  441. * The following properties and methods handle dynamic loading of JSON objects.
  442. */
  443. /**
  444. * Property: scriptName
  445. * {String} The filename of this script without any path.
  446. */
  447. scriptName: "proj4js.js",
  448.  
  449. /**
  450. * Property: defsLookupService
  451. * AJAX service to retreive projection definition parameters from
  452. */
  453. defsLookupService: 'http://spatialreference.org/ref',
  454.  
  455. /**
  456. * Property: libPath
  457. * internal: http server path to library code.
  458. */
  459. libPath: null,
  460.  
  461. /**
  462. * Function: getScriptLocation
  463. * Return the path to this script.
  464. *
  465. * Returns:
  466. * Path to this script
  467. */
  468. getScriptLocation: function () {
  469. if (this.libPath) return this.libPath;
  470. var scriptName = this.scriptName;
  471. var scriptNameLen = scriptName.length;
  472.  
  473. var scripts = document.getElementsByTagName('script');
  474. for (var i = 0; i < scripts.length; i++) {
  475. var src = scripts[i].getAttribute('src');
  476. if (src) {
  477. var index = src.lastIndexOf(scriptName);
  478. // is it found, at the end of the URL?
  479. if ((index > -1) && (index + scriptNameLen == src.length)) {
  480. this.libPath = src.slice(0, -scriptNameLen);
  481. break;
  482. }
  483. }
  484. }
  485. return this.libPath||"";
  486. },
  487.  
  488. /**
  489. * Function: loadScript
  490. * Load a JS file from a URL into a <script> tag in the page.
  491. *
  492. * Parameters:
  493. * url - {String} The URL containing the script to load
  494. * onload - {Function} A method to be executed when the script loads successfully
  495. * onfail - {Function} A method to be executed when there is an error loading the script
  496. * loadCheck - {Function} A boolean method that checks to see if the script
  497. * has loaded. Typically this just checks for the existance of
  498. * an object in the file just loaded.
  499. */
  500. loadScript: function(url, onload, onfail, loadCheck) {
  501. var script = document.createElement('script');
  502. script.defer = false;
  503. script.type = "text/javascript";
  504. script.id = url;
  505. script.onload = onload;
  506. script.onerror = onfail;
  507. script.loadCheck = loadCheck;
  508. if (/MSIE/.test(navigator.userAgent)) {
  509. script.onreadystatechange = this.checkReadyState;
  510. }
  511. document.getElementsByTagName('head')[0].appendChild(script);
  512. script.src = url;
  513. },
  514. /**
  515. * Function: checkReadyState
  516. * IE workaround since there is no onerror handler. Calls the user defined
  517. * loadCheck method to determine if the script is loaded.
  518. *
  519. */
  520. checkReadyState: function() {
  521. if (this.readyState == 'loaded') {
  522. if (!this.loadCheck()) {
  523. this.onerror();
  524. } else {
  525. this.onload();
  526. }
  527. }
  528. }
  529. };
  530.  
  531. /**
  532. * Class: Proj4js.Proj
  533. *
  534. * Proj objects provide transformation methods for point coordinates
  535. * between geodetic latitude/longitude and a projected coordinate system.
  536. * once they have been initialized with a projection code.
  537. *
  538. * Initialization of Proj objects is with a projection code, usually EPSG codes,
  539. * which is the key that will be used with the Proj4js.defs array.
  540. *
  541. * The code passed in will be stripped of colons and converted to uppercase
  542. * to locate projection definition files.
  543. *
  544. * A projection object has properties for units and title strings.
  545. */
  546. Proj4js.Proj = Proj4js.Class({
  547.  
  548. /**
  549. * Property: readyToUse
  550. * Flag to indicate if initialization is complete for this Proj object
  551. */
  552. readyToUse: false,
  553. /**
  554. * Property: title
  555. * The title to describe the projection
  556. */
  557. title: null,
  558. /**
  559. * Property: projName
  560. * The projection class for this projection, e.g. lcc (lambert conformal conic,
  561. * or merc for mercator). These are exactly equivalent to their Proj4
  562. * counterparts.
  563. */
  564. projName: null,
  565. /**
  566. * Property: units
  567. * The units of the projection. Values include 'm' and 'degrees'
  568. */
  569. units: null,
  570. /**
  571. * Property: datum
  572. * The datum specified for the projection
  573. */
  574. datum: null,
  575. /**
  576. * Property: x0
  577. * The x coordinate origin
  578. */
  579. x0: 0,
  580. /**
  581. * Property: y0
  582. * The y coordinate origin
  583. */
  584. y0: 0,
  585. /**
  586. * Property: localCS
  587. * Flag to indicate if the projection is a local one in which no transforms
  588. * are required.
  589. */
  590. localCS: false,
  591.  
  592. /**
  593. * Property: queue
  594. * Buffer (FIFO) to hold callbacks waiting to be called when projection loaded.
  595. */
  596. queue: null,
  597.  
  598. /**
  599. * Constructor: initialize
  600. * Constructor for Proj4js.Proj objects
  601. *
  602. * Parameters:
  603. * srsCode - a code for map projection definition parameters. These are usually
  604. * (but not always) EPSG codes.
  605. */
  606. initialize: function(srsCode, callback) {
  607. this.srsCodeInput = srsCode;
  608. //Register callbacks prior to attempting to process definition
  609. this.queue = [];
  610. if( callback ){
  611. this.queue.push( callback );
  612. }
  613. //check to see if this is a WKT string
  614. if ((srsCode.indexOf('GEOGCS') >= 0) ||
  615. (srsCode.indexOf('GEOCCS') >= 0) ||
  616. (srsCode.indexOf('PROJCS') >= 0) ||
  617. (srsCode.indexOf('LOCAL_CS') >= 0)) {
  618. this.parseWKT(srsCode);
  619. this.deriveConstants();
  620. this.loadProjCode(this.projName);
  621. return;
  622. }
  623. // DGR 2008-08-03 : support urn and url
  624. if (srsCode.indexOf('urn:') == 0) {
  625. //urn:ORIGINATOR:def:crs:CODESPACE:VERSION:ID
  626. var urn = srsCode.split(':');
  627. if ((urn[1] == 'ogc' || urn[1] =='x-ogc') &&
  628. (urn[2] =='def') &&
  629. (urn[3] =='crs')) {
  630. srsCode = urn[4]+':'+urn[urn.length-1];
  631. }
  632. } else if (srsCode.indexOf('http://') == 0) {
  633. //url#ID
  634. var url = srsCode.split('#');
  635. if (url[0].match(/epsg.org/)) {
  636. // http://www.epsg.org/#
  637. srsCode = 'EPSG:'+url[1];
  638. } else if (url[0].match(/RIG.xml/)) {
  639. //http://librairies.ign.fr/geoportail/resources/RIG.xml#
  640. //http://interop.ign.fr/registers/ign/RIG.xml#
  641. srsCode = 'IGNF:'+url[1];
  642. } else if (url[0].indexOf('/def/crs/')!=-1) {
  643. // http://www.opengis.net/def/crs/EPSG/0/code
  644. url= srsCode.split('/');
  645. srsCode = url.pop();//code
  646. url.pop();//version FIXME
  647. srsCode = url.pop()+':'+srsCode;//authority
  648. }
  649. }
  650. this.srsCode = srsCode.toUpperCase();
  651. if (this.srsCode.indexOf("EPSG") == 0) {
  652. this.srsCode = this.srsCode;
  653. this.srsAuth = 'epsg';
  654. this.srsProjNumber = this.srsCode.substring(5);
  655. // DGR 2007-11-20 : authority IGNF
  656. } else if (this.srsCode.indexOf("IGNF") == 0) {
  657. this.srsCode = this.srsCode;
  658. this.srsAuth = 'IGNF';
  659. this.srsProjNumber = this.srsCode.substring(5);
  660. // DGR 2008-06-19 : pseudo-authority CRS for WMS
  661. } else if (this.srsCode.indexOf("CRS") == 0) {
  662. this.srsCode = this.srsCode;
  663. this.srsAuth = 'CRS';
  664. this.srsProjNumber = this.srsCode.substring(4);
  665. } else {
  666. this.srsAuth = '';
  667. this.srsProjNumber = this.srsCode;
  668. }
  669. this.loadProjDefinition();
  670. },
  671. /**
  672. * Function: loadProjDefinition
  673. * Loads the coordinate system initialization string if required.
  674. * Note that dynamic loading happens asynchronously so an application must
  675. * wait for the readyToUse property is set to true.
  676. * To prevent dynamic loading, include the defs through a script tag in
  677. * your application.
  678. *
  679. */
  680. loadProjDefinition: function() {
  681. //check in memory
  682. if (Proj4js.defs[this.srsCode]) {
  683. this.defsLoaded();
  684. return;
  685. }
  686.  
  687. //else check for def on the server
  688. var url = Proj4js.getScriptLocation() + 'defs/' + this.srsAuth.toUpperCase() + this.srsProjNumber + '.js';
  689. Proj4js.loadScript(url,
  690. Proj4js.bind(this.defsLoaded, this),
  691. Proj4js.bind(this.loadFromService, this),
  692. Proj4js.bind(this.checkDefsLoaded, this) );
  693. },
  694.  
  695. /**
  696. * Function: loadFromService
  697. * Creates the REST URL for loading the definition from a web service and
  698. * loads it.
  699. *
  700. */
  701. loadFromService: function() {
  702. //else load from web service
  703. var url = Proj4js.defsLookupService +'/' + this.srsAuth +'/'+ this.srsProjNumber + '/proj4js/';
  704. Proj4js.loadScript(url,
  705. Proj4js.bind(this.defsLoaded, this),
  706. Proj4js.bind(this.defsFailed, this),
  707. Proj4js.bind(this.checkDefsLoaded, this) );
  708. },
  709.  
  710. /**
  711. * Function: defsLoaded
  712. * Continues the Proj object initilization once the def file is loaded
  713. *
  714. */
  715. defsLoaded: function() {
  716. this.parseDefs();
  717. this.loadProjCode(this.projName);
  718. },
  719. /**
  720. * Function: checkDefsLoaded
  721. * This is the loadCheck method to see if the def object exists
  722. *
  723. */
  724. checkDefsLoaded: function() {
  725. if (Proj4js.defs[this.srsCode]) {
  726. return true;
  727. } else {
  728. return false;
  729. }
  730. },
  731.  
  732. /**
  733. * Function: defsFailed
  734. * Report an error in loading the defs file, but continue on using WGS84
  735. *
  736. */
  737. defsFailed: function() {
  738. Proj4js.reportError('failed to load projection definition for: '+this.srsCode);
  739. Proj4js.defs[this.srsCode] = Proj4js.defs['WGS84']; //set it to something so it can at least continue
  740. this.defsLoaded();
  741. },
  742.  
  743. /**
  744. * Function: loadProjCode
  745. * Loads projection class code dynamically if required.
  746. * Projection code may be included either through a script tag or in
  747. * a built version of proj4js
  748. *
  749. */
  750. loadProjCode: function(projName) {
  751. if (Proj4js.Proj[projName]) {
  752. this.initTransforms();
  753. return;
  754. }
  755.  
  756. //the URL for the projection code
  757. var url = Proj4js.getScriptLocation() + 'projCode/' + projName + '.js';
  758. Proj4js.loadScript(url,
  759. Proj4js.bind(this.loadProjCodeSuccess, this, projName),
  760. Proj4js.bind(this.loadProjCodeFailure, this, projName),
  761. Proj4js.bind(this.checkCodeLoaded, this, projName) );
  762. },
  763.  
  764. /**
  765. * Function: loadProjCodeSuccess
  766. * Loads any proj dependencies or continue on to final initialization.
  767. *
  768. */
  769. loadProjCodeSuccess: function(projName) {
  770. if (Proj4js.Proj[projName].dependsOn){
  771. this.loadProjCode(Proj4js.Proj[projName].dependsOn);
  772. } else {
  773. this.initTransforms();
  774. }
  775. },
  776.  
  777. /**
  778. * Function: defsFailed
  779. * Report an error in loading the proj file. Initialization of the Proj
  780. * object has failed and the readyToUse flag will never be set.
  781. *
  782. */
  783. loadProjCodeFailure: function(projName) {
  784. Proj4js.reportError("failed to find projection file for: " + projName);
  785. //TBD initialize with identity transforms so proj will still work?
  786. },
  787. /**
  788. * Function: checkCodeLoaded
  789. * This is the loadCheck method to see if the projection code is loaded
  790. *
  791. */
  792. checkCodeLoaded: function(projName) {
  793. if (Proj4js.Proj[projName]) {
  794. return true;
  795. } else {
  796. return false;
  797. }
  798. },
  799.  
  800. /**
  801. * Function: initTransforms
  802. * Finalize the initialization of the Proj object
  803. *
  804. */
  805. initTransforms: function() {
  806. Proj4js.extend(this, Proj4js.Proj[this.projName]);
  807. this.init();
  808. this.readyToUse = true;
  809. if( this.queue ) {
  810. var item;
  811. while( (item = this.queue.shift()) ) {
  812. item.call( this, this );
  813. }
  814. }
  815. },
  816.  
  817. /**
  818. * Function: parseWKT
  819. * Parses a WKT string to get initialization parameters
  820. *
  821. */
  822. wktRE: /^(\w+)\[(.*)\]$/,
  823. parseWKT: function(wkt) {
  824. var wktMatch = wkt.match(this.wktRE);
  825. if (!wktMatch) return;
  826. var wktObject = wktMatch[1];
  827. var wktContent = wktMatch[2];
  828. var wktTemp = wktContent.split(",");
  829. var wktName;
  830. if (wktObject.toUpperCase() == "TOWGS84") {
  831. wktName = wktObject; //no name supplied for the TOWGS84 array
  832. } else {
  833. wktName = wktTemp.shift();
  834. }
  835. wktName = wktName.replace(/^\"/,"");
  836. wktName = wktName.replace(/\"$/,"");
  837. /*
  838. wktContent = wktTemp.join(",");
  839. var wktArray = wktContent.split("],");
  840. for (var i=0; i<wktArray.length-1; ++i) {
  841. wktArray[i] += "]";
  842. }
  843. */
  844. var wktArray = new Array();
  845. var bkCount = 0;
  846. var obj = "";
  847. for (var i=0; i<wktTemp.length; ++i) {
  848. var token = wktTemp[i];
  849. for (var j=0; j<token.length; ++j) {
  850. if (token.charAt(j) == "[") ++bkCount;
  851. if (token.charAt(j) == "]") --bkCount;
  852. }
  853. obj += token;
  854. if (bkCount === 0) {
  855. wktArray.push(obj);
  856. obj = "";
  857. } else {
  858. obj += ",";
  859. }
  860. }
  861. //do something based on the type of the wktObject being parsed
  862. //add in variations in the spelling as required
  863. switch (wktObject) {
  864. case 'LOCAL_CS':
  865. this.projName = 'identity';
  866. this.localCS = true;
  867. this.srsCode = wktName;
  868. break;
  869. case 'GEOGCS':
  870. this.projName = 'longlat';
  871. this.geocsCode = wktName;
  872. if (!this.srsCode) this.srsCode = wktName;
  873. break;
  874. case 'PROJCS':
  875. this.srsCode = wktName;
  876. break;
  877. case 'GEOCCS':
  878. break;
  879. case 'PROJECTION':
  880. this.projName = Proj4js.wktProjections[wktName];
  881. break;
  882. case 'DATUM':
  883. this.datumName = wktName;
  884. break;
  885. case 'LOCAL_DATUM':
  886. this.datumCode = 'none';
  887. break;
  888. case 'SPHEROID':
  889. this.ellps = wktName;
  890. this.a = parseFloat(wktArray.shift());
  891. this.rf = parseFloat(wktArray.shift());
  892. break;
  893. case 'PRIMEM':
  894. this.from_greenwich = parseFloat(wktArray.shift()); //to radians?
  895. break;
  896. case 'UNIT':
  897. this.units = wktName;
  898. this.unitsPerMeter = parseFloat(wktArray.shift());
  899. break;
  900. case 'PARAMETER':
  901. var name = wktName.toLowerCase();
  902. var value = parseFloat(wktArray.shift());
  903. //there may be many variations on the wktName values, add in case
  904. //statements as required
  905. switch (name) {
  906. case 'false_easting':
  907. this.x0 = value;
  908. break;
  909. case 'false_northing':
  910. this.y0 = value;
  911. break;
  912. case 'scale_factor':
  913. this.k0 = value;
  914. break;
  915. case 'central_meridian':
  916. this.long0 = value*Proj4js.common.D2R;
  917. break;
  918. case 'latitude_of_origin':
  919. this.lat0 = value*Proj4js.common.D2R;
  920. break;
  921. case 'more_here':
  922. break;
  923. default:
  924. break;
  925. }
  926. break;
  927. case 'TOWGS84':
  928. this.datum_params = wktArray;
  929. break;
  930. //DGR 2010-11-12: AXIS
  931. case 'AXIS':
  932. var name= wktName.toLowerCase();
  933. var value= wktArray.shift();
  934. switch (value) {
  935. case 'EAST' : value= 'e'; break;
  936. case 'WEST' : value= 'w'; break;
  937. case 'NORTH': value= 'n'; break;
  938. case 'SOUTH': value= 's'; break;
  939. case 'UP' : value= 'u'; break;
  940. case 'DOWN' : value= 'd'; break;
  941. case 'OTHER':
  942. default : value= ' '; break;//FIXME
  943. }
  944. if (!this.axis) { this.axis= "enu"; }
  945. switch(name) {
  946. case 'x': this.axis= value + this.axis.substr(1,2); break;
  947. case 'y': this.axis= this.axis.substr(0,1) + value + this.axis.substr(2,1); break;
  948. case 'z': this.axis= this.axis.substr(0,2) + value ; break;
  949. default : break;
  950. }
  951. case 'MORE_HERE':
  952. break;
  953. default:
  954. break;
  955. }
  956. for (var i=0; i<wktArray.length; ++i) {
  957. this.parseWKT(wktArray[i]);
  958. }
  959. },
  960.  
  961. /**
  962. * Function: parseDefs
  963. * Parses the PROJ.4 initialization string and sets the associated properties.
  964. *
  965. */
  966. parseDefs: function() {
  967. var re= new RegExp('(title|proj|units|datum|nadgrids|'+
  968. 'ellps|a|b|rf|'+
  969. 'lat_0|lat_1|lat_2|lat_ts|lon_0|lon_1|lon_2|alpha|lonc|'+
  970. 'x_0|y_0|k_0|k|r_a|zone|south|'+
  971. 'towgs84|to_meter|from_greenwich|pm|axis|czech|'+
  972. 'wktext|no_rot|no_off|no_defs)');
  973. this.defData = Proj4js.defs[this.srsCode];
  974. var paramName, paramVal;
  975. if (!this.defData) {
  976. return;
  977. }
  978. var paramArray=this.defData.split("+");
  979.  
  980. for (var prop=0; prop<paramArray.length; prop++) {
  981. var property = paramArray[prop].split("=");
  982. paramName = property[0].toLowerCase();
  983. paramVal = property[1];
  984.  
  985. switch (paramName.replace(/\s/gi,"")) { // trim out spaces
  986. case "": break; // throw away nameless parameter
  987. // DGR 2012-10-13 : + in title (EPSG:2056: CH1903+ / LV95)
  988. case "title": this.title = paramVal;
  989. while (!paramArray[prop+1].match(re)) {
  990. this.title+= '+'+paramArray[++prop];
  991. }
  992. break;
  993. case "proj": this.projName = paramVal.replace(/\s/gi,""); break;
  994. case "units": this.units = paramVal.replace(/\s/gi,""); break;
  995. case "datum": this.datumCode = paramVal.replace(/\s/gi,""); break;
  996. // DGR 2011-03-20 : nagrids -> nadgrids
  997. case "nadgrids": this.nadgrids = paramVal.replace(/\s/gi,""); break;// DGR 2012-07-29
  998. case "ellps": this.ellps = paramVal.replace(/\s/gi,""); break;
  999. case "a": this.a = parseFloat(paramVal); break; // semi-major radius
  1000. case "b": this.b = parseFloat(paramVal); break; // semi-minor radius
  1001. // DGR 2007-11-20
  1002. case "rf": this.rf = parseFloat(paramVal); break; // inverse flattening rf= a/(a-b)
  1003. case "lat_0": this.lat0 = paramVal*Proj4js.common.D2R; break; // phi0, central latitude
  1004. case "lat_1": this.lat1 = paramVal*Proj4js.common.D2R; break; //standard parallel 1
  1005. case "lat_2": this.lat2 = paramVal*Proj4js.common.D2R; break; //standard parallel 2
  1006. case "lat_ts": this.lat_ts = paramVal*Proj4js.common.D2R; break; // used in merc and eqc
  1007. case "lon_0": this.long0 = paramVal*Proj4js.common.D2R; break; // lam0, central longitude
  1008. case "lon_1": this.long1 = paramVal*Proj4js.common.D2R; break;
  1009. case "lon_2": this.long2 = paramVal*Proj4js.common.D2R; break;
  1010. case "no_rot": this.no_rot = true; break;
  1011. case "no_off": this.no_off = true; break;
  1012. case "alpha": this.alpha = parseFloat(paramVal)*Proj4js.common.D2R; break; //for somerc projection
  1013. case "lonc": this.longc = paramVal*Proj4js.common.D2R; break; //for somerc projection
  1014. case "x_0": this.x0 = parseFloat(paramVal); break; // false easting
  1015. case "y_0": this.y0 = parseFloat(paramVal); break; // false northing
  1016. case "k_0": this.k0 = parseFloat(paramVal); break; // projection scale factor
  1017. case "k": this.k0 = parseFloat(paramVal); break; // both forms returned
  1018. case "r_a": this.R_A = true; break; // sphere--area of ellipsoid
  1019. case "zone": this.zone = parseInt(paramVal,10); break; // UTM Zone
  1020. case "south": this.utmSouth = true; break; // UTM north/south
  1021. case "towgs84":this.datum_params = paramVal.split(","); break;
  1022. case "to_meter": this.to_meter = parseFloat(paramVal); break; // cartesian scaling
  1023. case "from_greenwich": this.from_greenwich = paramVal*Proj4js.common.D2R; break;
  1024. case "czech": this.czech = true; break;
  1025. // DGR 2008-07-09 : if pm is not a well-known prime meridian take
  1026. // the value instead of 0.0, then convert to radians
  1027. case "pm": paramVal = paramVal.replace(/\s/gi,"");
  1028. this.from_greenwich = Proj4js.PrimeMeridian[paramVal] ?
  1029. Proj4js.PrimeMeridian[paramVal] : parseFloat(paramVal);
  1030. this.from_greenwich *= Proj4js.common.D2R;
  1031. break;
  1032. // DGR 2010-11-12: axis
  1033. case "axis": paramVal = paramVal.replace(/\s/gi,"");
  1034. var legalAxis= "ewnsud";
  1035. if (paramVal.length==3 &&
  1036. legalAxis.indexOf(paramVal.substr(0,1))!=-1 &&
  1037. legalAxis.indexOf(paramVal.substr(1,1))!=-1 &&
  1038. legalAxis.indexOf(paramVal.substr(2,1))!=-1) {
  1039. this.axis= paramVal;
  1040. } //FIXME: be silent ?
  1041. break;
  1042. case "wktext": break;//DGR 2012-07-29
  1043. case "no_defs": break;
  1044. default: //alert("Unrecognized parameter: " + paramName);
  1045. } // switch()
  1046. } // for paramArray
  1047. this.deriveConstants();
  1048. },
  1049.  
  1050. /**
  1051. * Function: deriveConstants
  1052. * Sets several derived constant values and initialization of datum and ellipse
  1053. * parameters.
  1054. *
  1055. */
  1056. deriveConstants: function() {
  1057. // DGR 2011-03-20 : nagrids -> nadgrids
  1058. if (this.nadgrids && this.nadgrids.length==0) {
  1059. this.nadgrids= null;
  1060. }
  1061. if (this.nadgrids) {
  1062. this.grids= this.nadgrids.split(",");
  1063. var g= null, l= this.grids.length;
  1064. if (l>0) {
  1065. for (var i= 0; i<l; i++) {
  1066. g= this.grids[i];
  1067. var fg= g.split("@");
  1068. if (fg[fg.length-1]=="") {
  1069. Proj4js.reportError("nadgrids syntax error '"+this.nadgrids+"' : empty grid found");
  1070. continue;
  1071. }
  1072. this.grids[i]= {
  1073. mandatory: fg.length==1,//@=> optional grid (no error if not found)
  1074. name:fg[fg.length-1],
  1075. grid: Proj4js.grids[fg[fg.length-1]]//FIXME: grids loading ...
  1076. };
  1077. if (this.grids[i].mandatory && !this.grids[i].grid) {
  1078. Proj4js.reportError("Missing '"+this.grids[i].name+"'");
  1079. }
  1080. }
  1081. }
  1082. // DGR, 2011-03-20: grids is an array of objects that hold
  1083. // the loaded grids, its name and the mandatory informations of it.
  1084. }
  1085. if (this.datumCode && this.datumCode != 'none') {
  1086. var datumDef = Proj4js.Datum[this.datumCode];
  1087. if (datumDef) {
  1088. this.datum_params = datumDef.towgs84 ? datumDef.towgs84.split(',') : null;
  1089. this.ellps = datumDef.ellipse;
  1090. this.datumName = datumDef.datumName ? datumDef.datumName : this.datumCode;
  1091. }
  1092. }
  1093. if (!this.a) { // do we have an ellipsoid?
  1094. var ellipse = Proj4js.Ellipsoid[this.ellps] ? Proj4js.Ellipsoid[this.ellps] : Proj4js.Ellipsoid['WGS84'];
  1095. Proj4js.extend(this, ellipse);
  1096. }
  1097. if (this.rf && !this.b) this.b = (1.0 - 1.0/this.rf) * this.a;
  1098. if (this.rf === 0 || Math.abs(this.a - this.b)<Proj4js.common.EPSLN) {
  1099. this.sphere = true;
  1100. this.b= this.a;
  1101. }
  1102. this.a2 = this.a * this.a; // used in geocentric
  1103. this.b2 = this.b * this.b; // used in geocentric
  1104. this.es = (this.a2-this.b2)/this.a2; // e ^ 2
  1105. this.e = Math.sqrt(this.es); // eccentricity
  1106. if (this.R_A) {
  1107. this.a *= 1. - this.es * (Proj4js.common.SIXTH + this.es * (Proj4js.common.RA4 + this.es * Proj4js.common.RA6));
  1108. this.a2 = this.a * this.a;
  1109. this.b2 = this.b * this.b;
  1110. this.es = 0.;
  1111. }
  1112. this.ep2=(this.a2-this.b2)/this.b2; // used in geocentric
  1113. if (!this.k0) this.k0 = 1.0; //default value
  1114. //DGR 2010-11-12: axis
  1115. if (!this.axis) { this.axis= "enu"; }
  1116.  
  1117. this.datum = new Proj4js.datum(this);
  1118. }
  1119. });
  1120.  
  1121. Proj4js.Proj.longlat = {
  1122. init: function() {
  1123. //no-op for longlat
  1124. },
  1125. forward: function(pt) {
  1126. //identity transform
  1127. return pt;
  1128. },
  1129. inverse: function(pt) {
  1130. //identity transform
  1131. return pt;
  1132. }
  1133. };
  1134. Proj4js.Proj.identity = Proj4js.Proj.longlat;
  1135.  
  1136. /**
  1137. Proj4js.defs is a collection of coordinate system definition objects in the
  1138. PROJ.4 command line format.
  1139. Generally a def is added by means of a separate .js file for example:
  1140.  
  1141. <SCRIPT type="text/javascript" src="defs/EPSG26912.js"></SCRIPT>
  1142.  
  1143. def is a CS definition in PROJ.4 WKT format, for example:
  1144. +proj="tmerc" //longlat, etc.
  1145. +a=majorRadius
  1146. +b=minorRadius
  1147. +lat0=somenumber
  1148. +long=somenumber
  1149. */
  1150. Proj4js.defs = {
  1151. // These are so widely used, we'll go ahead and throw them in
  1152. // without requiring a separate .js file
  1153. 'WGS84': "+title=WGS 84 (long/lat) +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees",
  1154. 'EPSG:4326': "+title=WGS 84 (long/lat) +proj=longlat +ellps=WGS84 +datum=WGS84 +units=degrees",
  1155. 'EPSG:4269': "+title=NAD83 (long/lat) +proj=longlat +a=6378137.0 +b=6356752.31414036 +ellps=GRS80 +datum=NAD83 +units=degrees",
  1156. 'EPSG:3857': "+title=WGS 84 / Pseudo-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"
  1157. };
  1158. Proj4js.defs['EPSG:3785'] = Proj4js.defs['EPSG:3857']; //maintain backward compat, official code is 3857
  1159. Proj4js.defs['GOOGLE'] = Proj4js.defs['EPSG:3857'];
  1160. Proj4js.defs['EPSG:900913'] = Proj4js.defs['EPSG:3857'];
  1161. Proj4js.defs['EPSG:102113'] = Proj4js.defs['EPSG:3857'];
  1162.  
  1163. Proj4js.common = {
  1164. PI : 3.141592653589793238, //Math.PI,
  1165. HALF_PI : 1.570796326794896619, //Math.PI*0.5,
  1166. TWO_PI : 6.283185307179586477, //Math.PI*2,
  1167. FORTPI : 0.78539816339744833,
  1168. R2D : 57.29577951308232088,
  1169. D2R : 0.01745329251994329577,
  1170. SEC_TO_RAD : 4.84813681109535993589914102357e-6, /* SEC_TO_RAD = Pi/180/3600 */
  1171. EPSLN : 1.0e-10,
  1172. MAX_ITER : 20,
  1173. // following constants from geocent.c
  1174. COS_67P5 : 0.38268343236508977, /* cosine of 67.5 degrees */
  1175. AD_C : 1.0026000, /* Toms region 1 constant */
  1176.  
  1177. /* datum_type values */
  1178. PJD_UNKNOWN : 0,
  1179. PJD_3PARAM : 1,
  1180. PJD_7PARAM : 2,
  1181. PJD_GRIDSHIFT: 3,
  1182. PJD_WGS84 : 4, // WGS84 or equivalent
  1183. PJD_NODATUM : 5, // WGS84 or equivalent
  1184. SRS_WGS84_SEMIMAJOR : 6378137.0, // only used in grid shift transforms
  1185. SRS_WGS84_ESQUARED : 0.006694379990141316, //DGR: 2012-07-29
  1186.  
  1187. // ellipoid pj_set_ell.c
  1188. SIXTH : .1666666666666666667, /* 1/6 */
  1189. RA4 : .04722222222222222222, /* 17/360 */
  1190. RA6 : .02215608465608465608, /* 67/3024 */
  1191. RV4 : .06944444444444444444, /* 5/72 */
  1192. RV6 : .04243827160493827160, /* 55/1296 */
  1193.  
  1194. // Function to compute the constant small m which is the radius of
  1195. // a parallel of latitude, phi, divided by the semimajor axis.
  1196. // -----------------------------------------------------------------
  1197. msfnz : function(eccent, sinphi, cosphi) {
  1198. var con = eccent * sinphi;
  1199. return cosphi/(Math.sqrt(1.0 - con * con));
  1200. },
  1201.  
  1202. // Function to compute the constant small t for use in the forward
  1203. // computations in the Lambert Conformal Conic and the Polar
  1204. // Stereographic projections.
  1205. // -----------------------------------------------------------------
  1206. tsfnz : function(eccent, phi, sinphi) {
  1207. var con = eccent * sinphi;
  1208. var com = .5 * eccent;
  1209. con = Math.pow(((1.0 - con) / (1.0 + con)), com);
  1210. return (Math.tan(.5 * (this.HALF_PI - phi))/con);
  1211. },
  1212.  
  1213. // Function to compute the latitude angle, phi2, for the inverse of the
  1214. // Lambert Conformal Conic and Polar Stereographic projections.
  1215. // ----------------------------------------------------------------
  1216. phi2z : function(eccent, ts) {
  1217. var eccnth = .5 * eccent;
  1218. var con, dphi;
  1219. var phi = this.HALF_PI - 2 * Math.atan(ts);
  1220. for (var i = 0; i <= 15; i++) {
  1221. con = eccent * Math.sin(phi);
  1222. dphi = this.HALF_PI - 2 * Math.atan(ts *(Math.pow(((1.0 - con)/(1.0 + con)),eccnth))) - phi;
  1223. phi += dphi;
  1224. if (Math.abs(dphi) <= .0000000001) return phi;
  1225. }
  1226. alert("phi2z has NoConvergence");
  1227. return (-9999);
  1228. },
  1229.  
  1230. /* Function to compute constant small q which is the radius of a
  1231. parallel of latitude, phi, divided by the semimajor axis.
  1232. ------------------------------------------------------------*/
  1233. qsfnz : function(eccent,sinphi) {
  1234. var con;
  1235. if (eccent > 1.0e-7) {
  1236. con = eccent * sinphi;
  1237. return (( 1.0- eccent * eccent) * (sinphi /(1.0 - con * con) - (.5/eccent)*Math.log((1.0 - con)/(1.0 + con))));
  1238. } else {
  1239. return(2.0 * sinphi);
  1240. }
  1241. },
  1242.  
  1243. /* Function to compute the inverse of qsfnz
  1244. ------------------------------------------------------------*/
  1245. iqsfnz : function (eccent, q) {
  1246. var temp = 1.0-(1.0-eccent*eccent)/(2.0*eccent)*Math.log((1-eccent)/(1+eccent));
  1247. if (Math.abs(Math.abs(q)-temp)<1.0E-6) {
  1248. if (q<0.0) {
  1249. return (-1.0*Proj4js.common.HALF_PI);
  1250. } else {
  1251. return Proj4js.common.HALF_PI;
  1252. }
  1253. }
  1254. //var phi = 0.5* q/(1-eccent*eccent);
  1255. var phi = Math.asin(0.5*q);
  1256. var dphi;
  1257. var sin_phi;
  1258. var cos_phi;
  1259. var con;
  1260. for (var i=0;i<30;i++){
  1261. sin_phi = Math.sin(phi);
  1262. cos_phi = Math.cos(phi);
  1263. con = eccent*sin_phi;
  1264. dphi=Math.pow(1.0-con*con,2.0)/(2.0*cos_phi)*(q/(1-eccent*eccent)-sin_phi/(1.0-con*con)+0.5/eccent*Math.log((1.0-con)/(1.0+con)));
  1265. phi+=dphi;
  1266. if (Math.abs(dphi) <= .0000000001) {
  1267. return phi;
  1268. }
  1269. }
  1270.  
  1271. alert("IQSFN-CONV:Latitude failed to converge after 30 iterations");
  1272. return (NaN);
  1273. },
  1274.  
  1275. /* Function to eliminate roundoff errors in asin
  1276. ----------------------------------------------*/
  1277. asinz : function(x) {
  1278. if (Math.abs(x)>1.0) {
  1279. x=(x>1.0)?1.0:-1.0;
  1280. }
  1281. return Math.asin(x);
  1282. },
  1283.  
  1284. // following functions from gctpc cproj.c for transverse mercator projections
  1285. e0fn : function(x) {return(1.0-0.25*x*(1.0+x/16.0*(3.0+1.25*x)));},
  1286. e1fn : function(x) {return(0.375*x*(1.0+0.25*x*(1.0+0.46875*x)));},
  1287. e2fn : function(x) {return(0.05859375*x*x*(1.0+0.75*x));},
  1288. e3fn : function(x) {return(x*x*x*(35.0/3072.0));},
  1289. 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));},
  1290. imlfn : function(ml, e0, e1, e2, e3) {
  1291. var phi;
  1292. var dphi;
  1293.  
  1294. phi=ml/e0;
  1295. for (var i=0;i<15;i++){
  1296. dphi=(ml-(e0*phi-e1*Math.sin(2.0*phi)+e2*Math.sin(4.0*phi)-e3*Math.sin(6.0*phi)))/(e0-2.0*e1*Math.cos(2.0*phi)+4.0*e2*Math.cos(4.0*phi)-6.0*e3*Math.cos(6.0*phi));
  1297. phi+=dphi;
  1298. if (Math.abs(dphi) <= .0000000001) {
  1299. return phi;
  1300. }
  1301. }
  1302.  
  1303. Proj4js.reportError("IMLFN-CONV:Latitude failed to converge after 15 iterations");
  1304. return NaN;
  1305. },
  1306.  
  1307. srat : function(esinp, exp) {
  1308. return(Math.pow((1.0-esinp)/(1.0+esinp), exp));
  1309. },
  1310.  
  1311. // Function to return the sign of an argument
  1312. sign : function(x) { if (x < 0.0) return(-1); else return(1);},
  1313.  
  1314. // Function to adjust longitude to -180 to 180; input in radians
  1315. adjust_lon : function(x) {
  1316. x = (Math.abs(x) < this.PI) ? x: (x - (this.sign(x)*this.TWO_PI) );
  1317. return x;
  1318. },
  1319.  
  1320. // IGNF - DGR : algorithms used by IGN France
  1321.  
  1322. // Function to adjust latitude to -90 to 90; input in radians
  1323. adjust_lat : function(x) {
  1324. x= (Math.abs(x) < this.HALF_PI) ? x: (x - (this.sign(x)*this.PI) );
  1325. return x;
  1326. },
  1327.  
  1328. // Latitude Isometrique - close to tsfnz ...
  1329. latiso : function(eccent, phi, sinphi) {
  1330. if (Math.abs(phi) > this.HALF_PI) return +Number.NaN;
  1331. if (phi==this.HALF_PI) return Number.POSITIVE_INFINITY;
  1332. if (phi==-1.0*this.HALF_PI) return -1.0*Number.POSITIVE_INFINITY;
  1333.  
  1334. var con= eccent*sinphi;
  1335. return Math.log(Math.tan((this.HALF_PI+phi)/2.0))+eccent*Math.log((1.0-con)/(1.0+con))/2.0;
  1336. },
  1337.  
  1338. fL : function(x,L) {
  1339. return 2.0*Math.atan(x*Math.exp(L)) - this.HALF_PI;
  1340. },
  1341.  
  1342. // Inverse Latitude Isometrique - close to ph2z
  1343. invlatiso : function(eccent, ts) {
  1344. var phi= this.fL(1.0,ts);
  1345. var Iphi= 0.0;
  1346. var con= 0.0;
  1347. do {
  1348. Iphi= phi;
  1349. con= eccent*Math.sin(Iphi);
  1350. phi= this.fL(Math.exp(eccent*Math.log((1.0+con)/(1.0-con))/2.0),ts);
  1351. } while (Math.abs(phi-Iphi)>1.0e-12);
  1352. return phi;
  1353. },
  1354.  
  1355. // Needed for Gauss Schreiber
  1356. // Original: Denis Makarov (info@binarythings.com)
  1357. // Web Site: http://www.binarythings.com
  1358. sinh : function(x)
  1359. {
  1360. var r= Math.exp(x);
  1361. r= (r-1.0/r)/2.0;
  1362. return r;
  1363. },
  1364.  
  1365. cosh : function(x)
  1366. {
  1367. var r= Math.exp(x);
  1368. r= (r+1.0/r)/2.0;
  1369. return r;
  1370. },
  1371.  
  1372. tanh : function(x)
  1373. {
  1374. var r= Math.exp(x);
  1375. r= (r-1.0/r)/(r+1.0/r);
  1376. return r;
  1377. },
  1378.  
  1379. asinh : function(x)
  1380. {
  1381. var s= (x>= 0? 1.0:-1.0);
  1382. return s*(Math.log( Math.abs(x) + Math.sqrt(x*x+1.0) ));
  1383. },
  1384.  
  1385. acosh : function(x)
  1386. {
  1387. return 2.0*Math.log(Math.sqrt((x+1.0)/2.0) + Math.sqrt((x-1.0)/2.0));
  1388. },
  1389.  
  1390. atanh : function(x)
  1391. {
  1392. return Math.log((x-1.0)/(x+1.0))/2.0;
  1393. },
  1394.  
  1395. // Grande Normale
  1396. gN : function(a,e,sinphi)
  1397. {
  1398. var temp= e*sinphi;
  1399. return a/Math.sqrt(1.0 - temp*temp);
  1400. },
  1401. //code from the PROJ.4 pj_mlfn.c file; this may be useful for other projections
  1402. pj_enfn: function(es) {
  1403. var en = new Array();
  1404. en[0] = this.C00 - es * (this.C02 + es * (this.C04 + es * (this.C06 + es * this.C08)));
  1405. en[1] = es * (this.C22 - es * (this.C04 + es * (this.C06 + es * this.C08)));
  1406. var t = es * es;
  1407. en[2] = t * (this.C44 - es * (this.C46 + es * this.C48));
  1408. t *= es;
  1409. en[3] = t * (this.C66 - es * this.C68);
  1410. en[4] = t * es * this.C88;
  1411. return en;
  1412. },
  1413. pj_mlfn: function(phi, sphi, cphi, en) {
  1414. cphi *= sphi;
  1415. sphi *= sphi;
  1416. return(en[0] * phi - cphi * (en[1] + sphi*(en[2]+ sphi*(en[3] + sphi*en[4]))));
  1417. },
  1418. pj_inv_mlfn: function(arg, es, en) {
  1419. var k = 1./(1.-es);
  1420. var phi = arg;
  1421. for (var i = Proj4js.common.MAX_ITER; i ; --i) { /* rarely goes over 2 iterations */
  1422. var s = Math.sin(phi);
  1423. var t = 1. - es * s * s;
  1424. //t = this.pj_mlfn(phi, s, Math.cos(phi), en) - arg;
  1425. //phi -= t * (t * Math.sqrt(t)) * k;
  1426. t = (this.pj_mlfn(phi, s, Math.cos(phi), en) - arg) * (t * Math.sqrt(t)) * k;
  1427. phi -= t;
  1428. if (Math.abs(t) < Proj4js.common.EPSLN)
  1429. return phi;
  1430. }
  1431. Proj4js.reportError("cass:pj_inv_mlfn: Convergence error");
  1432. return phi;
  1433. },
  1434.  
  1435. /**
  1436. * Determine correction values
  1437. * source: nad_intr.c (DGR: 2012-07-29)
  1438. */
  1439. nad_intr: function(pin,ct) {
  1440. // force computation by decreasing by 1e-7 to be as closed as possible
  1441. // from computation under C:C++ by leveraging rounding problems ...
  1442. var t= {"x":(pin.x-1.e-7)/ct.del[0],"y":(pin.y-1e-7)/ct.del[1]};
  1443. var indx= {"x":Math.floor(t.x),"y":Math.floor(t.y)};
  1444. var frct= {"x":t.x-1.0*indx.x,"y":t.y-1.0*indx.y};
  1445. var val= {"x":Number.NaN,"y":Number.NaN};
  1446. var inx;
  1447. if (indx.x<0) {
  1448. if (!(indx.x==-1 && frct.x>0.99999999999)) {
  1449. return val;
  1450. }
  1451. ++indx.x;
  1452. frct.x= 0.0;
  1453. } else {
  1454. inx= indx.x+1;
  1455. if (inx>=ct.lim[0]) {
  1456. if (!(inx==ct.lim[0] && frct.x<1e-11)) {
  1457. return val;
  1458. }
  1459. --indx.x;
  1460. frct.x= 1.0;
  1461. }
  1462. }
  1463. if (indx.y<0) {
  1464. if (!(indx.y==-1 && frct.y>0.99999999999)) {
  1465. return val;
  1466. }
  1467. ++indx.y;
  1468. frct.y= 0.0;
  1469. } else {
  1470. inx= indx.y+1;
  1471. if (inx>=ct.lim[1]) {
  1472. if (!(inx==ct.lim[1] && frct.y<1e-11)) {
  1473. return val;
  1474. }
  1475. --indx.y;
  1476. frct.y= 1.0;
  1477. }
  1478. }
  1479. inx= (indx.y*ct.lim[0])+indx.x;
  1480. var f00= {"x":ct.cvs[inx][0], "y":ct.cvs[inx][1]};
  1481. inx++;
  1482. var f10= {"x":ct.cvs[inx][0], "y":ct.cvs[inx][1]};
  1483. inx+= ct.lim[0];
  1484. var f11= {"x":ct.cvs[inx][0], "y":ct.cvs[inx][1]};
  1485. inx--;
  1486. var f01= {"x":ct.cvs[inx][0], "y":ct.cvs[inx][1]};
  1487. var m11= frct.x*frct.y, m10= frct.x*(1.0-frct.y),
  1488. m00= (1.0-frct.x)*(1.0-frct.y), m01= (1.0-frct.x)*frct.y;
  1489. val.x= (m00*f00.x + m10*f10.x + m01*f01.x + m11*f11.x);
  1490. val.y= (m00*f00.y + m10*f10.y + m01*f01.y + m11*f11.y);
  1491. return val;
  1492. },
  1493.  
  1494. /**
  1495. * Correct value
  1496. * source: nad_cvt.c (DGR: 2012-07-29)
  1497. */
  1498. nad_cvt: function(pin,inverse,ct) {
  1499. var val= {"x":Number.NaN, "y":Number.NaN};
  1500. if (isNaN(pin.x)) { return val; }
  1501. var tb= {"x":pin.x, "y":pin.y};
  1502. tb.x-= ct.ll[0];
  1503. tb.y-= ct.ll[1];
  1504. tb.x= Proj4js.common.adjust_lon(tb.x - Proj4js.common.PI) + Proj4js.common.PI;
  1505. var t= Proj4js.common.nad_intr(tb,ct);
  1506. if (inverse) {
  1507. if (isNaN(t.x)) {
  1508. return val;
  1509. }
  1510. t.x= tb.x + t.x;
  1511. t.y= tb.y - t.y;
  1512. var i= 9, tol= 1e-12;
  1513. var dif, del;
  1514. do {
  1515. del= Proj4js.common.nad_intr(t,ct);
  1516. if (isNaN(del.x)) {
  1517. this.reportError("Inverse grid shift iteration failed, presumably at grid edge. Using first approximation.");
  1518. break;
  1519. }
  1520. dif= {"x":t.x-del.x-tb.x, "y":t.y+del.y-tb.y};
  1521. t.x-= dif.x;
  1522. t.y-= dif.y;
  1523. } while (i-- && Math.abs(dif.x)>tol && Math.abs(dif.y)>tol);
  1524. if (i<0) {
  1525. this.reportError("Inverse grid shift iterator failed to converge.");
  1526. return val;
  1527. }
  1528. val.x= Proj4js.common.adjust_lon(t.x+ct.ll[0]);
  1529. val.y= t.y+ct.ll[1];
  1530. } else {
  1531. if (!isNaN(t.x)) {
  1532. val.x= pin.x - t.x;
  1533. val.y= pin.y + t.y;
  1534. }
  1535. }
  1536. return val;
  1537. },
  1538.  
  1539. /* meridinal distance for ellipsoid and inverse
  1540. ** 8th degree - accurate to < 1e-5 meters when used in conjuction
  1541. ** with typical major axis values.
  1542. ** Inverse determines phi to EPS (1e-11) radians, about 1e-6 seconds.
  1543. */
  1544. C00: 1.0,
  1545. C02: .25,
  1546. C04: .046875,
  1547. C06: .01953125,
  1548. C08: .01068115234375,
  1549. C22: .75,
  1550. C44: .46875,
  1551. C46: .01302083333333333333,
  1552. C48: .00712076822916666666,
  1553. C66: .36458333333333333333,
  1554. C68: .00569661458333333333,
  1555. C88: .3076171875
  1556.  
  1557. };
  1558.  
  1559. /** datum object
  1560. */
  1561. Proj4js.datum = Proj4js.Class({
  1562.  
  1563. initialize : function(proj) {
  1564. this.datum_type = Proj4js.common.PJD_WGS84; //default setting
  1565. if (!proj) { return; }
  1566. if (proj.datumCode && proj.datumCode == 'none') {
  1567. this.datum_type = Proj4js.common.PJD_NODATUM;
  1568. }
  1569. if (proj.datum_params) {
  1570. for (var i=0; i<proj.datum_params.length; i++) {
  1571. proj.datum_params[i]=parseFloat(proj.datum_params[i]);
  1572. }
  1573. if (proj.datum_params[0] != 0 || proj.datum_params[1] != 0 || proj.datum_params[2] != 0 ) {
  1574. this.datum_type = Proj4js.common.PJD_3PARAM;
  1575. }
  1576. if (proj.datum_params.length > 3) {
  1577. if (proj.datum_params[3] != 0 || proj.datum_params[4] != 0 ||
  1578. proj.datum_params[5] != 0 || proj.datum_params[6] != 0 ) {
  1579. this.datum_type = Proj4js.common.PJD_7PARAM;
  1580. proj.datum_params[3] *= Proj4js.common.SEC_TO_RAD;
  1581. proj.datum_params[4] *= Proj4js.common.SEC_TO_RAD;
  1582. proj.datum_params[5] *= Proj4js.common.SEC_TO_RAD;
  1583. proj.datum_params[6] = (proj.datum_params[6]/1000000.0) + 1.0;
  1584. }
  1585. }
  1586. }
  1587. // DGR 2011-03-21 : nadgrids support
  1588. this.datum_type = proj.grids?
  1589. Proj4js.common.PJD_GRIDSHIFT
  1590. : this.datum_type;
  1591.  
  1592. this.a = proj.a; //datum object also uses these values
  1593. this.b = proj.b;
  1594. this.es = proj.es;
  1595. this.ep2 = proj.ep2;
  1596. this.datum_params = proj.datum_params;
  1597. if (this.datum_type==Proj4js.common.PJD_GRIDSHIFT) {
  1598. this.grids= proj.grids;
  1599. }
  1600. },
  1601.  
  1602. /****************************************************************/
  1603. // cs_compare_datums()
  1604. // Returns TRUE if the two datums match, otherwise FALSE.
  1605. compare_datums : function( dest ) {
  1606. if( this.datum_type != dest.datum_type ) {
  1607. return false; // false, datums are not equal
  1608. } else if( this.a != dest.a || Math.abs(this.es-dest.es) > 0.000000000050 ) {
  1609. // the tolerence for es is to ensure that GRS80 and WGS84
  1610. // are considered identical
  1611. return false;
  1612. } else if( this.datum_type == Proj4js.common.PJD_3PARAM ) {
  1613. return (this.datum_params[0] == dest.datum_params[0]
  1614. && this.datum_params[1] == dest.datum_params[1]
  1615. && this.datum_params[2] == dest.datum_params[2]);
  1616. } else if( this.datum_type == Proj4js.common.PJD_7PARAM ) {
  1617. return (this.datum_params[0] == dest.datum_params[0]
  1618. && this.datum_params[1] == dest.datum_params[1]
  1619. && this.datum_params[2] == dest.datum_params[2]
  1620. && this.datum_params[3] == dest.datum_params[3]
  1621. && this.datum_params[4] == dest.datum_params[4]
  1622. && this.datum_params[5] == dest.datum_params[5]
  1623. && this.datum_params[6] == dest.datum_params[6]);
  1624. } else if ( this.datum_type == Proj4js.common.PJD_GRIDSHIFT ||
  1625. dest.datum_type == Proj4js.common.PJD_GRIDSHIFT ) {
  1626. //alert("ERROR: Grid shift transformations are not implemented.");
  1627. //return false
  1628. //DGR 2012-07-29 lazy ...
  1629. return this.nadgrids == dest.nadgrids;
  1630. } else {
  1631. return true; // datums are equal
  1632. }
  1633. }, // cs_compare_datums()
  1634.  
  1635. /*
  1636. * The function Convert_Geodetic_To_Geocentric converts geodetic coordinates
  1637. * (latitude, longitude, and height) to geocentric coordinates (X, Y, Z),
  1638. * according to the current ellipsoid parameters.
  1639. *
  1640. * Latitude : Geodetic latitude in radians (input)
  1641. * Longitude : Geodetic longitude in radians (input)
  1642. * Height : Geodetic height, in meters (input)
  1643. * X : Calculated Geocentric X coordinate, in meters (output)
  1644. * Y : Calculated Geocentric Y coordinate, in meters (output)
  1645. * Z : Calculated Geocentric Z coordinate, in meters (output)
  1646. *
  1647. */
  1648. geodetic_to_geocentric : function(p) {
  1649. var Longitude = p.x;
  1650. var Latitude = p.y;
  1651. var Height = p.z ? p.z : 0; //Z value not always supplied
  1652. var X; // output
  1653. var Y;
  1654. var Z;
  1655.  
  1656. var Error_Code=0; // GEOCENT_NO_ERROR;
  1657. var Rn; /* Earth radius at location */
  1658. var Sin_Lat; /* Math.sin(Latitude) */
  1659. var Sin2_Lat; /* Square of Math.sin(Latitude) */
  1660. var Cos_Lat; /* Math.cos(Latitude) */
  1661.  
  1662. /*
  1663. ** Don't blow up if Latitude is just a little out of the value
  1664. ** range as it may just be a rounding issue. Also removed longitude
  1665. ** test, it should be wrapped by Math.cos() and Math.sin(). NFW for PROJ.4, Sep/2001.
  1666. */
  1667. if( Latitude < -Proj4js.common.HALF_PI && Latitude > -1.001 * Proj4js.common.HALF_PI ) {
  1668. Latitude = -Proj4js.common.HALF_PI;
  1669. } else if( Latitude > Proj4js.common.HALF_PI && Latitude < 1.001 * Proj4js.common.HALF_PI ) {
  1670. Latitude = Proj4js.common.HALF_PI;
  1671. } else if ((Latitude < -Proj4js.common.HALF_PI) || (Latitude > Proj4js.common.HALF_PI)) {
  1672. /* Latitude out of range */
  1673. Proj4js.reportError('geocent:lat out of range:'+Latitude);
  1674. return null;
  1675. }
  1676.  
  1677. if (Longitude > Proj4js.common.PI) Longitude -= (2*Proj4js.common.PI);
  1678. Sin_Lat = Math.sin(Latitude);
  1679. Cos_Lat = Math.cos(Latitude);
  1680. Sin2_Lat = Sin_Lat * Sin_Lat;
  1681. Rn = this.a / (Math.sqrt(1.0e0 - this.es * Sin2_Lat));
  1682. X = (Rn + Height) * Cos_Lat * Math.cos(Longitude);
  1683. Y = (Rn + Height) * Cos_Lat * Math.sin(Longitude);
  1684. Z = ((Rn * (1 - this.es)) + Height) * Sin_Lat;
  1685.  
  1686. p.x = X;
  1687. p.y = Y;
  1688. p.z = Z;
  1689. return Error_Code;
  1690. }, // cs_geodetic_to_geocentric()
  1691.  
  1692.  
  1693. geocentric_to_geodetic : function (p) {
  1694. /* local defintions and variables */
  1695. /* end-criterium of loop, accuracy of sin(Latitude) */
  1696. var genau = 1.E-12;
  1697. var genau2 = (genau*genau);
  1698. var maxiter = 30;
  1699.  
  1700. var P; /* distance between semi-minor axis and location */
  1701. var RR; /* distance between center and location */
  1702. var CT; /* sin of geocentric latitude */
  1703. var ST; /* cos of geocentric latitude */
  1704. var RX;
  1705. var RK;
  1706. var RN; /* Earth radius at location */
  1707. var CPHI0; /* cos of start or old geodetic latitude in iterations */
  1708. var SPHI0; /* sin of start or old geodetic latitude in iterations */
  1709. var CPHI; /* cos of searched geodetic latitude */
  1710. var SPHI; /* sin of searched geodetic latitude */
  1711. var SDPHI; /* end-criterium: addition-theorem of sin(Latitude(iter)-Latitude(iter-1)) */
  1712. var At_Pole; /* indicates location is in polar region */
  1713. var iter; /* # of continous iteration, max. 30 is always enough (s.a.) */
  1714.  
  1715. var X = p.x;
  1716. var Y = p.y;
  1717. var Z = p.z ? p.z : 0.0; //Z value not always supplied
  1718. var Longitude;
  1719. var Latitude;
  1720. var Height;
  1721.  
  1722. At_Pole = false;
  1723. P = Math.sqrt(X*X+Y*Y);
  1724. RR = Math.sqrt(X*X+Y*Y+Z*Z);
  1725.  
  1726. /* special cases for latitude and longitude */
  1727. if (P/this.a < genau) {
  1728.  
  1729. /* special case, if P=0. (X=0., Y=0.) */
  1730. At_Pole = true;
  1731. Longitude = 0.0;
  1732.  
  1733. /* if (X,Y,Z)=(0.,0.,0.) then Height becomes semi-minor axis
  1734. * of ellipsoid (=center of mass), Latitude becomes PI/2 */
  1735. if (RR/this.a < genau) {
  1736. Latitude = Proj4js.common.HALF_PI;
  1737. Height = -this.b;
  1738. return;
  1739. }
  1740. } else {
  1741. /* ellipsoidal (geodetic) longitude
  1742. * interval: -PI < Longitude <= +PI */
  1743. Longitude=Math.atan2(Y,X);
  1744. }
  1745.  
  1746. /* --------------------------------------------------------------
  1747. * Following iterative algorithm was developped by
  1748. * "Institut f�r Erdmessung", University of Hannover, July 1988.
  1749. * Internet: www.ife.uni-hannover.de
  1750. * Iterative computation of CPHI,SPHI and Height.
  1751. * Iteration of CPHI and SPHI to 10**-12 radian resp.
  1752. * 2*10**-7 arcsec.
  1753. * --------------------------------------------------------------
  1754. */
  1755. CT = Z/RR;
  1756. ST = P/RR;
  1757. RX = 1.0/Math.sqrt(1.0-this.es*(2.0-this.es)*ST*ST);
  1758. CPHI0 = ST*(1.0-this.es)*RX;
  1759. SPHI0 = CT*RX;
  1760. iter = 0;
  1761.  
  1762. /* loop to find sin(Latitude) resp. Latitude
  1763. * until |sin(Latitude(iter)-Latitude(iter-1))| < genau */
  1764. do
  1765. {
  1766. iter++;
  1767. RN = this.a/Math.sqrt(1.0-this.es*SPHI0*SPHI0);
  1768.  
  1769. /* ellipsoidal (geodetic) height */
  1770. Height = P*CPHI0+Z*SPHI0-RN*(1.0-this.es*SPHI0*SPHI0);
  1771.  
  1772. RK = this.es*RN/(RN+Height);
  1773. RX = 1.0/Math.sqrt(1.0-RK*(2.0-RK)*ST*ST);
  1774. CPHI = ST*(1.0-RK)*RX;
  1775. SPHI = CT*RX;
  1776. SDPHI = SPHI*CPHI0-CPHI*SPHI0;
  1777. CPHI0 = CPHI;
  1778. SPHI0 = SPHI;
  1779. }
  1780. while (SDPHI*SDPHI > genau2 && iter < maxiter);
  1781.  
  1782. /* ellipsoidal (geodetic) latitude */
  1783. Latitude=Math.atan(SPHI/Math.abs(CPHI));
  1784.  
  1785. p.x = Longitude;
  1786. p.y = Latitude;
  1787. p.z = Height;
  1788. return p;
  1789. }, // cs_geocentric_to_geodetic()
  1790.  
  1791. /** Convert_Geocentric_To_Geodetic
  1792. * The method used here is derived from 'An Improved Algorithm for
  1793. * Geocentric to Geodetic Coordinate Conversion', by Ralph Toms, Feb 1996
  1794. */
  1795. geocentric_to_geodetic_noniter : function (p) {
  1796. var X = p.x;
  1797. var Y = p.y;
  1798. var Z = p.z ? p.z : 0; //Z value not always supplied
  1799. var Longitude;
  1800. var Latitude;
  1801. var Height;
  1802.  
  1803. var W; /* distance from Z axis */
  1804. var W2; /* square of distance from Z axis */
  1805. var T0; /* initial estimate of vertical component */
  1806. var T1; /* corrected estimate of vertical component */
  1807. var S0; /* initial estimate of horizontal component */
  1808. var S1; /* corrected estimate of horizontal component */
  1809. var Sin_B0; /* Math.sin(B0), B0 is estimate of Bowring aux variable */
  1810. var Sin3_B0; /* cube of Math.sin(B0) */
  1811. var Cos_B0; /* Math.cos(B0) */
  1812. var Sin_p1; /* Math.sin(phi1), phi1 is estimated latitude */
  1813. var Cos_p1; /* Math.cos(phi1) */
  1814. var Rn; /* Earth radius at location */
  1815. var Sum; /* numerator of Math.cos(phi1) */
  1816. var At_Pole; /* indicates location is in polar region */
  1817.  
  1818. X = parseFloat(X); // cast from string to float
  1819. Y = parseFloat(Y);
  1820. Z = parseFloat(Z);
  1821.  
  1822. At_Pole = false;
  1823. if (X != 0.0)
  1824. {
  1825. Longitude = Math.atan2(Y,X);
  1826. }
  1827. else
  1828. {
  1829. if (Y > 0)
  1830. {
  1831. Longitude = Proj4js.common.HALF_PI;
  1832. }
  1833. else if (Y < 0)
  1834. {
  1835. Longitude = -Proj4js.common.HALF_PI;
  1836. }
  1837. else
  1838. {
  1839. At_Pole = true;
  1840. Longitude = 0.0;
  1841. if (Z > 0.0)
  1842. { /* north pole */
  1843. Latitude = Proj4js.common.HALF_PI;
  1844. }
  1845. else if (Z < 0.0)
  1846. { /* south pole */
  1847. Latitude = -Proj4js.common.HALF_PI;
  1848. }
  1849. else
  1850. { /* center of earth */
  1851. Latitude = Proj4js.common.HALF_PI;
  1852. Height = -this.b;
  1853. return;
  1854. }
  1855. }
  1856. }
  1857. W2 = X*X + Y*Y;
  1858. W = Math.sqrt(W2);
  1859. T0 = Z * Proj4js.common.AD_C;
  1860. S0 = Math.sqrt(T0 * T0 + W2);
  1861. Sin_B0 = T0 / S0;
  1862. Cos_B0 = W / S0;
  1863. Sin3_B0 = Sin_B0 * Sin_B0 * Sin_B0;
  1864. T1 = Z + this.b * this.ep2 * Sin3_B0;
  1865. Sum = W - this.a * this.es * Cos_B0 * Cos_B0 * Cos_B0;
  1866. S1 = Math.sqrt(T1*T1 + Sum * Sum);
  1867. Sin_p1 = T1 / S1;
  1868. Cos_p1 = Sum / S1;
  1869. Rn = this.a / Math.sqrt(1.0 - this.es * Sin_p1 * Sin_p1);
  1870. if (Cos_p1 >= Proj4js.common.COS_67P5)
  1871. {
  1872. Height = W / Cos_p1 - Rn;
  1873. }
  1874. else if (Cos_p1 <= -Proj4js.common.COS_67P5)
  1875. {
  1876. Height = W / -Cos_p1 - Rn;
  1877. }
  1878. else
  1879. {
  1880. Height = Z / Sin_p1 + Rn * (this.es - 1.0);
  1881. }
  1882. if (At_Pole == false)
  1883. {
  1884. Latitude = Math.atan(Sin_p1 / Cos_p1);
  1885. }
  1886.  
  1887. p.x = Longitude;
  1888. p.y = Latitude;
  1889. p.z = Height;
  1890. return p;
  1891. }, // geocentric_to_geodetic_noniter()
  1892.  
  1893. /****************************************************************/
  1894. // pj_geocentic_to_wgs84( p )
  1895. // p = point to transform in geocentric coordinates (x,y,z)
  1896. geocentric_to_wgs84 : function ( p ) {
  1897.  
  1898. if( this.datum_type == Proj4js.common.PJD_3PARAM )
  1899. {
  1900. // if( x[io] == HUGE_VAL )
  1901. // continue;
  1902. p.x += this.datum_params[0];
  1903. p.y += this.datum_params[1];
  1904. p.z += this.datum_params[2];
  1905.  
  1906. }
  1907. else if (this.datum_type == Proj4js.common.PJD_7PARAM)
  1908. {
  1909. var Dx_BF =this.datum_params[0];
  1910. var Dy_BF =this.datum_params[1];
  1911. var Dz_BF =this.datum_params[2];
  1912. var Rx_BF =this.datum_params[3];
  1913. var Ry_BF =this.datum_params[4];
  1914. var Rz_BF =this.datum_params[5];
  1915. var M_BF =this.datum_params[6];
  1916. // if( x[io] == HUGE_VAL )
  1917. // continue;
  1918. var x_out = M_BF*( p.x - Rz_BF*p.y + Ry_BF*p.z) + Dx_BF;
  1919. var y_out = M_BF*( Rz_BF*p.x + p.y - Rx_BF*p.z) + Dy_BF;
  1920. var z_out = M_BF*(-Ry_BF*p.x + Rx_BF*p.y + p.z) + Dz_BF;
  1921. p.x = x_out;
  1922. p.y = y_out;
  1923. p.z = z_out;
  1924. }
  1925. }, // cs_geocentric_to_wgs84
  1926.  
  1927. /****************************************************************/
  1928. // pj_geocentic_from_wgs84()
  1929. // coordinate system definition,
  1930. // point to transform in geocentric coordinates (x,y,z)
  1931. geocentric_from_wgs84 : function( p ) {
  1932.  
  1933. if( this.datum_type == Proj4js.common.PJD_3PARAM )
  1934. {
  1935. //if( x[io] == HUGE_VAL )
  1936. // continue;
  1937. p.x -= this.datum_params[0];
  1938. p.y -= this.datum_params[1];
  1939. p.z -= this.datum_params[2];
  1940.  
  1941. }
  1942. else if (this.datum_type == Proj4js.common.PJD_7PARAM)
  1943. {
  1944. var Dx_BF =this.datum_params[0];
  1945. var Dy_BF =this.datum_params[1];
  1946. var Dz_BF =this.datum_params[2];
  1947. var Rx_BF =this.datum_params[3];
  1948. var Ry_BF =this.datum_params[4];
  1949. var Rz_BF =this.datum_params[5];
  1950. var M_BF =this.datum_params[6];
  1951. var x_tmp = (p.x - Dx_BF) / M_BF;
  1952. var y_tmp = (p.y - Dy_BF) / M_BF;
  1953. var z_tmp = (p.z - Dz_BF) / M_BF;
  1954. //if( x[io] == HUGE_VAL )
  1955. // continue;
  1956.  
  1957. p.x = x_tmp + Rz_BF*y_tmp - Ry_BF*z_tmp;
  1958. p.y = -Rz_BF*x_tmp + y_tmp + Rx_BF*z_tmp;
  1959. p.z = Ry_BF*x_tmp - Rx_BF*y_tmp + z_tmp;
  1960. } //cs_geocentric_from_wgs84()
  1961. }
  1962. });
  1963.  
  1964. /** point object, nothing fancy, just allows values to be
  1965. passed back and forth by reference rather than by value.
  1966. Other point classes may be used as long as they have
  1967. x and y properties, which will get modified in the transform method.
  1968. */
  1969. Proj4js.Point = Proj4js.Class({
  1970.  
  1971. /**
  1972. * Constructor: Proj4js.Point
  1973. *
  1974. * Parameters:
  1975. * - x {float} or {Array} either the first coordinates component or
  1976. * the full coordinates
  1977. * - y {float} the second component
  1978. * - z {float} the third component, optional.
  1979. */
  1980. initialize : function(x,y,z) {
  1981. if (typeof x == 'object') {
  1982. this.x = x[0];
  1983. this.y = x[1];
  1984. this.z = x[2] || 0.0;
  1985. } else if (typeof x == 'string' && typeof y == 'undefined') {
  1986. var coords = x.split(',');
  1987. this.x = parseFloat(coords[0]);
  1988. this.y = parseFloat(coords[1]);
  1989. this.z = parseFloat(coords[2]) || 0.0;
  1990. } else {
  1991. this.x = x;
  1992. this.y = y;
  1993. this.z = z || 0.0;
  1994. }
  1995. },
  1996.  
  1997. /**
  1998. * APIMethod: clone
  1999. * Build a copy of a Proj4js.Point object.
  2000. *
  2001. * Return:
  2002. * {Proj4js}.Point the cloned point.
  2003. */
  2004. clone : function() {
  2005. return new Proj4js.Point(this.x, this.y, this.z);
  2006. },
  2007.  
  2008. /**
  2009. * APIMethod: toString
  2010. * Return a readable string version of the point
  2011. *
  2012. * Return:
  2013. * {String} String representation of Proj4js.Point object.
  2014. * (ex. <i>"x=5,y=42"</i>)
  2015. */
  2016. toString : function() {
  2017. return ("x=" + this.x + ",y=" + this.y);
  2018. },
  2019.  
  2020. /**
  2021. * APIMethod: toShortString
  2022. * Return a short string version of the point.
  2023. *
  2024. * Return:
  2025. * {String} Shortened String representation of Proj4js.Point object.
  2026. * (ex. <i>"5, 42"</i>)
  2027. */
  2028. toShortString : function() {
  2029. return (this.x + ", " + this.y);
  2030. }
  2031. });
  2032.  
  2033. Proj4js.PrimeMeridian = {
  2034. "greenwich": 0.0, //"0dE",
  2035. "lisbon": -9.131906111111, //"9d07'54.862\"W",
  2036. "paris": 2.337229166667, //"2d20'14.025\"E",
  2037. "bogota": -74.080916666667, //"74d04'51.3\"W",
  2038. "madrid": -3.687938888889, //"3d41'16.58\"W",
  2039. "rome": 12.452333333333, //"12d27'8.4\"E",
  2040. "bern": 7.439583333333, //"7d26'22.5\"E",
  2041. "jakarta": 106.807719444444, //"106d48'27.79\"E",
  2042. "ferro": -17.666666666667, //"17d40'W",
  2043. "brussels": 4.367975, //"4d22'4.71\"E",
  2044. "stockholm": 18.058277777778, //"18d3'29.8\"E",
  2045. "athens": 23.7163375, //"23d42'58.815\"E",
  2046. "oslo": 10.722916666667 //"10d43'22.5\"E"
  2047. };
  2048.  
  2049. Proj4js.Ellipsoid = {
  2050. "MERIT": {a:6378137.0, rf:298.257, ellipseName:"MERIT 1983"},
  2051. "SGS85": {a:6378136.0, rf:298.257, ellipseName:"Soviet Geodetic System 85"},
  2052. "GRS80": {a:6378137.0, rf:298.257222101, ellipseName:"GRS 1980(IUGG, 1980)"},
  2053. "IAU76": {a:6378140.0, rf:298.257, ellipseName:"IAU 1976"},
  2054. "airy": {a:6377563.396, b:6356256.910, ellipseName:"Airy 1830"},
  2055. "APL4.": {a:6378137, rf:298.25, ellipseName:"Appl. Physics. 1965"},
  2056. "NWL9D": {a:6378145.0, rf:298.25, ellipseName:"Naval Weapons Lab., 1965"},
  2057. "mod_airy": {a:6377340.189, b:6356034.446, ellipseName:"Modified Airy"},
  2058. "andrae": {a:6377104.43, rf:300.0, ellipseName:"Andrae 1876 (Den., Iclnd.)"},
  2059. "aust_SA": {a:6378160.0, rf:298.25, ellipseName:"Australian Natl & S. Amer. 1969"},
  2060. "GRS67": {a:6378160.0, rf:298.2471674270, ellipseName:"GRS 67(IUGG 1967)"},
  2061. "bessel": {a:6377397.155, rf:299.1528128, ellipseName:"Bessel 1841"},
  2062. "bess_nam": {a:6377483.865, rf:299.1528128, ellipseName:"Bessel 1841 (Namibia)"},
  2063. "clrk66": {a:6378206.4, b:6356583.8, ellipseName:"Clarke 1866"},
  2064. "clrk80": {a:6378249.145, rf:293.4663, ellipseName:"Clarke 1880 mod."},
  2065. "CPM": {a:6375738.7, rf:334.29, ellipseName:"Comm. des Poids et Mesures 1799"},
  2066. "delmbr": {a:6376428.0, rf:311.5, ellipseName:"Delambre 1810 (Belgium)"},
  2067. "engelis": {a:6378136.05, rf:298.2566, ellipseName:"Engelis 1985"},
  2068. "evrst30": {a:6377276.345, rf:300.8017, ellipseName:"Everest 1830"},
  2069. "evrst48": {a:6377304.063, rf:300.8017, ellipseName:"Everest 1948"},
  2070. "evrst56": {a:6377301.243, rf:300.8017, ellipseName:"Everest 1956"},
  2071. "evrst69": {a:6377295.664, rf:300.8017, ellipseName:"Everest 1969"},
  2072. "evrstSS": {a:6377298.556, rf:300.8017, ellipseName:"Everest (Sabah & Sarawak)"},
  2073. "fschr60": {a:6378166.0, rf:298.3, ellipseName:"Fischer (Mercury Datum) 1960"},
  2074. "fschr60m": {a:6378155.0, rf:298.3, ellipseName:"Fischer 1960"},
  2075. "fschr68": {a:6378150.0, rf:298.3, ellipseName:"Fischer 1968"},
  2076. "helmert": {a:6378200.0, rf:298.3, ellipseName:"Helmert 1906"},
  2077. "hough": {a:6378270.0, rf:297.0, ellipseName:"Hough"},
  2078. "intl": {a:6378388.0, rf:297.0, ellipseName:"International 1909 (Hayford)"},
  2079. "kaula": {a:6378163.0, rf:298.24, ellipseName:"Kaula 1961"},
  2080. "lerch": {a:6378139.0, rf:298.257, ellipseName:"Lerch 1979"},
  2081. "mprts": {a:6397300.0, rf:191.0, ellipseName:"Maupertius 1738"},
  2082. "new_intl": {a:6378157.5, b:6356772.2, ellipseName:"New International 1967"},
  2083. "plessis": {a:6376523.0, rf:6355863.0, ellipseName:"Plessis 1817 (France)"},
  2084. "krass": {a:6378245.0, rf:298.3, ellipseName:"Krassovsky, 1942"},
  2085. "SEasia": {a:6378155.0, b:6356773.3205, ellipseName:"Southeast Asia"},
  2086. "walbeck": {a:6376896.0, b:6355834.8467, ellipseName:"Walbeck"},
  2087. "WGS60": {a:6378165.0, rf:298.3, ellipseName:"WGS 60"},
  2088. "WGS66": {a:6378145.0, rf:298.25, ellipseName:"WGS 66"},
  2089. "WGS72": {a:6378135.0, rf:298.26, ellipseName:"WGS 72"},
  2090. "WGS84": {a:6378137.0, rf:298.257223563, ellipseName:"WGS 84"},
  2091. "sphere": {a:6370997.0, b:6370997.0, ellipseName:"Normal Sphere (r=6370997)"}
  2092. };
  2093.  
  2094. Proj4js.Datum = {
  2095. "WGS84": {towgs84: "0,0,0", ellipse: "WGS84", datumName: "WGS84"},
  2096. "GGRS87": {towgs84: "-199.87,74.79,246.62", ellipse: "GRS80", datumName: "Greek_Geodetic_Reference_System_1987"},
  2097. "NAD83": {towgs84: "0,0,0", ellipse: "GRS80", datumName: "North_American_Datum_1983"},
  2098. "NAD27": {nadgrids: "@conus,@alaska,@ntv2_0.gsb,@ntv1_can.dat", ellipse: "clrk66", datumName: "North_American_Datum_1927"},
  2099. "potsdam": {towgs84: "606.0,23.0,413.0", ellipse: "bessel", datumName: "Potsdam Rauenberg 1950 DHDN"},
  2100. "carthage": {towgs84: "-263.0,6.0,431.0", ellipse: "clark80", datumName: "Carthage 1934 Tunisia"},
  2101. "hermannskogel": {towgs84: "653.0,-212.0,449.0", ellipse: "bessel", datumName: "Hermannskogel"},
  2102. "ire65": {towgs84: "482.530,-130.596,564.557,-1.042,-0.214,-0.631,8.15", ellipse: "mod_airy", datumName: "Ireland 1965"},
  2103. "nzgd49": {towgs84: "59.47,-5.04,187.44,0.47,-0.1,1.024,-4.5993", ellipse: "intl", datumName: "New Zealand Geodetic Datum 1949"},
  2104. "OSGB36": {towgs84: "446.448,-125.157,542.060,0.1502,0.2470,0.8421,-20.4894", ellipse: "airy", datumName: "Airy 1830"}
  2105. };
  2106.  
  2107. Proj4js.WGS84 = new Proj4js.Proj('WGS84');
  2108. Proj4js.Datum['OSB36'] = Proj4js.Datum['OSGB36']; //as returned from spatialreference.org
  2109.  
  2110. //lookup table to go from the projection name in WKT to the Proj4js projection name
  2111. //build this out as required
  2112. Proj4js.wktProjections = {
  2113. "Lambert Tangential Conformal Conic Projection": "lcc",
  2114. "Mercator": "merc",
  2115. "Popular Visualisation Pseudo Mercator": "merc",
  2116. "Mercator_1SP": "merc",
  2117. "Transverse_Mercator": "tmerc",
  2118. "Transverse Mercator": "tmerc",
  2119. "Lambert Azimuthal Equal Area": "laea",
  2120. "Universal Transverse Mercator System": "utm"
  2121. };
  2122.  
  2123. // Based on proj4 CTABLE structure :
  2124. // FIXME: better to have array instead of object holding longitudes, latitudes members
  2125. // In the former case, one has to document index 0 is longitude and
  2126. // 1 is latitude ...
  2127. // In the later case, grid object gets bigger !!!!
  2128. // Solution 1 is chosen based on pj_gridinfo.c
  2129. Proj4js.grids= {
  2130. "null":{ // name of grid's file
  2131. "ll": [-3.14159265, -1.57079633], // lower-left coordinates in radians (longitude, latitude):
  2132. "del":[ 3.14159265, 1.57079633], // cell's size in radians (longitude, latitude):
  2133. "lim":[ 3, 3], // number of nodes in longitude, latitude (including edges):
  2134. "count":9, // total number of nodes
  2135. "cvs":[ // shifts : in ntv2 reverse order : lon, lat in radians ...
  2136. [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], // for (lon= 0; lon<lim[0]; lon++) {
  2137. [0.0, 0.0], [0.0, 0.0], [0.0, 0.0], // for (lat= 0; lat<lim[1]; lat++) { p= cvs[lat*lim[0]+lon]; }
  2138. [0.0, 0.0], [0.0, 0.0], [0.0, 0.0] // }
  2139. ]
  2140. }
  2141. };