Pastebin launched a little side project called HostCabi.net, check it out ;-)Don't like ads? PRO users don't see any ads ;-)
Guest

gargantuan

By: a guest on Jun 29th, 2009  |  syntax: JavaScript  |  size: 10.15 KB  |  hits: 470  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. // -------------------------------------------------------------------------------------
  2.  
  3. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
  4. /*  Latitude/longitude spherical geodesy formulae & scripts (c) Chris Veness 2002-2009            */
  5. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
  6. /* Pasted from the original locationhttp://www.movable-type.co.uk/scripts/latlong.html */
  7.  
  8. /*
  9.  * Use Haversine formula to Calculate distance (in km) between two points specified by
  10.  * latitude/longitude (in numeric degrees)
  11.  *
  12.  * from: Haversine formula - R. W. Sinnott, "Virtues of the Haversine",
  13.  *       Sky and Telescope, vol 68, no 2, 1984
  14.  *       http://www.census.gov/cgi-bin/geo/gisfaq?Q5.1
  15.  *
  16.  * example usage from form:
  17.  *   result.value = LatLon.distHaversine(lat1.value.parseDeg(), long1.value.parseDeg(),
  18.  *                                       lat2.value.parseDeg(), long2.value.parseDeg());
  19.  * where lat1, long1, lat2, long2, and result are form fields
  20.  */
  21. LatLon.distHaversine = function(lat1, lon1, lat2, lon2) {
  22.   var R = 6371; // earth's mean radius in km
  23.   var dLat = (lat2-lat1).toRad();
  24.   var dLon = (lon2-lon1).toRad();
  25.   lat1 = lat1.toRad(), lat2 = lat2.toRad();
  26.  
  27.   var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
  28.           Math.cos(lat1) * Math.cos(lat2) *
  29.           Math.sin(dLon/2) * Math.sin(dLon/2);
  30.   var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
  31.   var d = R * c;
  32.   return d;
  33. }
  34.  
  35.  
  36. /*
  37.  * Use Law of Cosines to calculate distance (in km) between two points specified by latitude/longitude
  38.  * (in numeric degrees).
  39.  */
  40. LatLon.distCosineLaw = function(lat1, lon1, lat2, lon2) {
  41.   var R = 6371; // earth's mean radius in km
  42.   var d = Math.acos(Math.sin(lat1.toRad())*Math.sin(lat2.toRad()) +
  43.                     Math.cos(lat1.toRad())*Math.cos(lat2.toRad())*Math.cos((lon2-lon1).toRad())) * R;
  44.   return d;
  45. }
  46.  
  47.  
  48. /*
  49.  * calculate (initial) bearing between two points
  50.  *   see http://williams.best.vwh.net/avform.htm#Crs
  51.  */
  52. LatLon.bearing = function(lat1, lon1, lat2, lon2) {
  53.   lat1 = lat1.toRad(); lat2 = lat2.toRad();
  54.   var dLon = (lon2-lon1).toRad();
  55.  
  56.   var y = Math.sin(dLon) * Math.cos(lat2);
  57.   var x = Math.cos(lat1)*Math.sin(lat2) -
  58.           Math.sin(lat1)*Math.cos(lat2)*Math.cos(dLon);
  59.   return Math.atan2(y, x).toBrng();
  60. }
  61.  
  62.  
  63. /*
  64.  * calculate midpoint of great circle line between p1 & p2.
  65.  *   see http://mathforum.org/library/drmath/view/51822.html for derivation
  66.  */
  67. LatLon.midPoint = function(lat1, lon1, lat2, lon2) {
  68.   lat1 = lat1.toRad();
  69.   lat2 = lat2.toRad();
  70.   var dLon = (lon2-lon1).toRad();
  71.  
  72.   var Bx = Math.cos(lat2) * Math.cos(dLon);
  73.   var By = Math.cos(lat2) * Math.sin(dLon);
  74.  
  75.   lat3 = Math.atan2(Math.sin(lat1)+Math.sin(lat2),
  76.                     Math.sqrt((Math.cos(lat1)+Bx)*(Math.cos(lat1)+Bx) + By*By ) );
  77.   lon3 = lon1.toRad() + Math.atan2(By, Math.cos(lat1) + Bx);
  78.  
  79.   if (isNaN(lat3) || isNaN(lon3)) return null;
  80.   return new LatLon(lat3.toDeg(), lon3.toDeg());
  81. }
  82.  
  83.  
  84. /*
  85.  * calculate destination point given start point, initial bearing (deg) and distance (km)
  86.  *   see http://williams.best.vwh.net/avform.htm#LL
  87.  */
  88. LatLon.prototype.destPoint = function(brng, d) {
  89.   var R = 6371; // earth's mean radius in km
  90.   var lat1 = this.lat.toRad(), lon1 = this.lon.toRad();
  91.   brng = brng.toRad();
  92.  
  93.   var lat2 = Math.asin( Math.sin(lat1)*Math.cos(d/R) +
  94.                         Math.cos(lat1)*Math.sin(d/R)*Math.cos(brng) );
  95.   var lon2 = lon1 + Math.atan2(Math.sin(brng)*Math.sin(d/R)*Math.cos(lat1),
  96.                                Math.cos(d/R)-Math.sin(lat1)*Math.sin(lat2));
  97.   lon2 = (lon2+Math.PI)%(2*Math.PI) - Math.PI;  // normalise to -180...+180
  98.  
  99.   if (isNaN(lat2) || isNaN(lon2)) return null;
  100.   return new LatLon(lat2.toDeg(), lon2.toDeg());
  101. }
  102.  
  103.  
  104. /*
  105.  * calculate final bearing arriving at destination point given start point, initial bearing and distance
  106.  */
  107. LatLon.prototype.finalBrng = function(brng, d) {
  108.   var p1 = this, p2 = p1.destPoint(brng, d);
  109.   // get reverse bearing point 2 to point 1
  110.   var rev = LatLon.bearing(p2.lat, p2.lon, p1.lat, p1.lon);
  111.   // & reverse it by adding 180
  112.   var brng = (rev + 180) % 360;
  113.   return brng;
  114. }
  115.  
  116.  
  117. /*
  118.  * calculate distance, bearing, destination point on rhumb line
  119.  *   see http://williams.best.vwh.net/avform.htm#Rhumb
  120.  */
  121. LatLon.distRhumb = function(lat1, lon1, lat2, lon2) {
  122.   var R = 6371; // earth's mean radius in km
  123.   var dLat = (lat2-lat1).toRad(), dLon = Math.abs(lon2-lon1).toRad();
  124.   var dPhi = Math.log(Math.tan(lat2.toRad()/2+Math.PI/4)/Math.tan(lat1.toRad()/2+Math.PI/4));
  125.   var q = (Math.abs(dLat) > 1e-10) ? dLat/dPhi : Math.cos(lat1.toRad());
  126.   // if dLon over 180 take shorter rhumb across 180 meridian:
  127.   if (dLon > Math.PI) dLon = 2*Math.PI - dLon;
  128.   var d = Math.sqrt(dLat*dLat + q*q*dLon*dLon);
  129.   return d * R;
  130. }
  131.  
  132.  
  133. LatLon.brngRhumb = function(lat1, lon1, lat2, lon2) {
  134.   var dLon = (lon2-lon1).toRad();
  135.   var dPhi = Math.log(Math.tan(lat2.toRad()/2+Math.PI/4)/Math.tan(lat1.toRad()/2+Math.PI/4));
  136.   if (Math.abs(dLon) > Math.PI) dLon = dLon>0 ? -(2*Math.PI-dLon) : (2*Math.PI+dLon);
  137.   return Math.atan2(dLon, dPhi).toBrng();
  138. }
  139.  
  140.  
  141. LatLon.prototype.destPointRhumb = function(brng, dist) {
  142.   var R = 6371; // earth's mean radius in km
  143.   var d = parseFloat(dist)/R;  
  144.   // d = angular distance covered on earth's surface
  145.   var lat1 = this.lat.toRad(), lon1 = this.lon.toRad();
  146.   brng = brng.toRad();
  147.  
  148.   var lat2 = lat1 + d*Math.cos(brng);
  149.   var dLat = lat2-lat1;
  150.   var dPhi = Math.log(Math.tan(lat2/2+Math.PI/4)/Math.tan(lat1/2+Math.PI/4));
  151.   var q = (Math.abs(dLat) > 1e-10) ? dLat/dPhi : Math.cos(lat1);
  152.   var dLon = d*Math.sin(brng)/q;
  153.   // check for some daft bugger going past the pole
  154.   if (Math.abs(lat2) > Math.PI/2) lat2 = lat2>0 ? Math.PI-lat2 : -(Math.PI-lat2);
  155.   lon2 = (lon1+dLon+Math.PI)%(2*Math.PI) - Math.PI;
  156.  
  157.   if (isNaN(lat2) || isNaN(lon2)) return null;
  158.   return new LatLon(lat2.toDeg(), lon2.toDeg());
  159. }
  160.  
  161.  
  162. /*
  163.  * construct a LatLon object: arguments in numeric degrees
  164.  *
  165.  * note all LatLong methods expect & return numeric degrees (for lat/long & for bearings)
  166.  */
  167. function LatLon(lat, lon) {
  168.   this.lat = lat;
  169.   this.lon = lon;
  170. }
  171.  
  172.  
  173. /*
  174.  * represent point {lat, lon} in standard representation
  175.  */
  176. LatLon.prototype.toString = function() {
  177.   return this.lat.toLat() + ', ' + this.lon.toLon();
  178. }
  179.  
  180. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
  181.  
  182. // extend String object with method for parsing degrees or lat/long values to numeric degrees
  183. //
  184. // this is very flexible on formats, allowing signed decimal degrees, or deg-min-sec suffixed by
  185. // compass direction (NSEW). A variety of separators are accepted (eg 3 37' 09"W) or fixed-width
  186. // format without separators (eg 0033709W). Seconds and minutes may be omitted. (Minimal validation
  187. // is done).
  188.  
  189. String.prototype.parseDeg = function() {
  190.   if (!isNaN(this)) return Number(this);                 // signed decimal degrees without NSEW
  191.  
  192.   var degLL = this.replace(/^-/,'').replace(/[NSEW]/i,'');  // strip off any sign or compass dir'n
  193.   var dms = degLL.split(/[^0-9.,]+/);                     // split out separate d/m/s
  194.   for (var i in dms) if (dms[i]=='') dms.splice(i,1);    // remove empty elements (see note below)
  195.   switch (dms.length) {                                  // convert to decimal degrees...
  196.     case 3:                                              // interpret 3-part result as d/m/s
  197.       var deg = dms[0]/1 + dms[1]/60 + dms[2]/3600; break;
  198.     case 2:                                              // interpret 2-part result as d/m
  199.       var deg = dms[0]/1 + dms[1]/60; break;
  200.     case 1:                                              // decimal or non-separated dddmmss
  201.       if (/[NS]/i.test(this)) degLL = '0' + degLL;       // - normalise N/S to 3-digit degrees
  202.       var deg = dms[0].slice(0,3)/1 + dms[0].slice(3,5)/60 + dms[0].slice(5)/3600; break;
  203.     default: return NaN;
  204.   }
  205.   if (/^-/.test(this) || /[WS]/i.test(this)) deg = -deg; // take '-', west and south as -ve
  206.   return deg;
  207. }
  208. // note: whitespace at start/end will split() into empty elements (except in IE)
  209.  
  210.  
  211. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
  212.  
  213. // extend Number object with methods for converting degrees/radians
  214.  
  215. Number.prototype.toRad = function() {  // convert degrees to radians
  216.   return this * Math.PI / 180;
  217. }
  218.  
  219. Number.prototype.toDeg = function() {  // convert radians to degrees (signed)
  220.   return this * 180 / Math.PI;
  221. }
  222.  
  223. Number.prototype.toBrng = function() {  // convert radians to degrees (as bearing: 0...360)
  224.   return (this.toDeg()+360) % 360;
  225. }
  226.  
  227.  
  228. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */
  229.  
  230. // extend Number object with methods for presenting bearings & lat/longs
  231.  
  232. Number.prototype.toDMS = function() {  // convert numeric degrees to deg/min/sec
  233.   var d = Math.abs(this);  // (unsigned result ready for appending compass dir'n)
  234.   d += 1/7200;  // add second for rounding
  235.   var deg = Math.floor(d);
  236.   var min = Math.floor((d-deg)*60);
  237.   var sec = Math.floor((d-deg-min/60)*3600);
  238.   // add leading zeros if required
  239.   if (deg<100) deg = '0' + deg; if (deg<10) deg = '0' + deg;
  240.   if (min<10) min = '0' + min;
  241.   if (sec<10) sec = '0' + sec;
  242.   return deg + '\u00B0' + min + '\u2032' + sec + '\u2033';
  243. }
  244.  
  245. Number.prototype.toLat = function() {  // convert numeric degrees to deg/min/sec latitude
  246.   return this.toDMS().slice(1) + (this<0 ? 'S' : 'N');  // knock off initial '0' for lat!
  247. }
  248.  
  249. Number.prototype.toLon = function() {  // convert numeric degrees to deg/min/sec longitude
  250.   return this.toDMS() + (this>0 ? 'E' : 'W');
  251. }
  252.  
  253. Number.prototype.toPrecision = function(fig) {  // override toPrecision method with one which displays
  254.   if (this == 0) return 0;                      // trailing zeros in place of exponential notation
  255.   var scale = Math.ceil(Math.log(this)*Math.LOG10E);
  256.   var mult = Math.pow(10, fig-scale);
  257.   return Math.round(this*mult)/mult;
  258. }
  259.  
  260. /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -  */