Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

fixed a herp derp in getGrade

  • Loading branch information...
commit ee91749e1e1211a37caf41a45a62c1e2e5cf305e 1 parent f9cb54b
@regality regality authored
Showing with 2 additions and 2 deletions.
  1. +1 −1  geolib.js
  2. +1 −1  geolib.min.js
View
2  geolib.js
@@ -729,7 +729,7 @@
getGrade: function(coords, digits) {
var keys = geolib.getKeys(coords[0]);
var elevation = keys.elevation;
- var rise = coords[0][elevation] - coords[coords.length-1][elevation];
+ var rise = coords[coords.length-1][elevation] - coords[0][elevation];
var run = geolib.getPathLength(coords);
var grade = (rise/run)*100;
if (typeof digits === "number") {
View
2  geolib.min.js
@@ -8,4 +8,4 @@
* @version 1.1.7
* @license http://www.gnu.org/licenses/lgpl-3.0.txt LGPL
*
- */(function(a,b){var c=6378137,d=/^([0-9]{1,3})°\s*([0-9]{1,3})'\s*(([0-9]{1,3}(\.([0-9]{1,2}))?)"\s*)?([NEOSW]?)$/,e,f,g={decimal:{},sexagesimal:{},distance:0,getKeys:function(a){var b=a.hasOwnProperty("lat")?"lat":"latitude",c=(a.hasOwnProperty("lng")?"lng":!1)||(a.hasOwnProperty("long")?"long":!1)||"longitude",d=(a.hasOwnProperty("alt")?"alt":!1)||(a.hasOwnProperty("altitude")?"altitude":!1)||(a.hasOwnProperty("elev")?"elev":!1)||"elevation";return{latitude:b,longitude:c,elevation:d}},getDistance:function(a,b,c){var d=g.getKeys(a),e=d.latitude,f=d.longitude,h=d.elevation;c=Math.floor(c)||1;var i={},j={};i[e]=g.useDecimal(a[e]),i[f]=g.useDecimal(a[f]),j[e]=g.useDecimal(b[e]),j[f]=g.useDecimal(b[f]);var k=6378137,l=6356752.314245,m=1/298.257223563,n=(j[f]-i[f]).toRad(),o=Math.atan((1-m)*Math.tan(parseFloat(i[e]).toRad())),p=Math.atan((1-m)*Math.tan(parseFloat(j[e]).toRad())),q=Math.sin(o),r=Math.cos(o),s=Math.sin(p),t=Math.cos(p),u=n,v,w=100;do{var x=Math.sin(u),y=Math.cos(u),z=Math.sqrt(t*x*t*x+(r*s-q*t*y)*(r*s-q*t*y));if(z==0)return g.distance=0;var A=q*s+r*t*y,B=Math.atan2(z,A),C=r*t*x/z,D=1-C*C,E=A-2*q*s/D;isNaN(E)&&(E=0);var F=m/16*D*(4+m*(4-3*D));v=u,u=n+(1-F)*m*C*(B+F*z*(E+F*A*(-1+2*E*E)))}while(Math.abs(u-v)>1e-12&&--w>0);if(w==0)return NaN;var G=D*(k*k-l*l)/(l*l),H=1+G/16384*(4096+G*(-768+G*(320-175*G))),I=G/1024*(256+G*(-128+G*(74-47*G))),J=I*z*(E+I/4*(A*(-1+2*E*E)-I/6*E*(-3+4*z*z)*(-3+4*E*E))),K=l*H*(B-J);K=K.toFixed(3);if(a.hasOwnProperty(h)&&b.hasOwnProperty(h)){var L=Math.abs(a[h]-b[h]);K=Math.sqrt(K*K+L*L)}return g.distance=Math.floor(Math.round(K/c)*c)},getDistanceSimple:function(a,b,d){var e=g.getKeys(a),f=e.latitude,h=e.longitude;d=Math.floor(d)||1;var i={},j={};i[f]=parseFloat(g.useDecimal(a[f])).toRad(),i[h]=parseFloat(g.useDecimal(a[h])).toRad(),j[f]=parseFloat(g.useDecimal(b[f])).toRad(),j[h]=parseFloat(g.useDecimal(b[h])).toRad();var k=Math.round(Math.acos(Math.sin(j[f])*Math.sin(i[f])+Math.cos(j[f])*Math.cos(i[f])*Math.cos(i[h]-j[h]))*c);return g.distance=Math.floor(Math.round(k/d)*d)},getCenter:function(a){if(!a.length)return!1;var b=g.getKeys(a[0]),c=b.latitude,d=b.longitude,e=function(a){return Math.max.apply(Math,a)},f=function(a){return Math.min.apply(Math,a)},h,i,j={lat:[],lng:[]};for(var k in a)j.lat.push(g.useDecimal(a[k][c])),j.lng.push(g.useDecimal(a[k][d]));var l=f(j.lat),m=f(j.lng),n=e(j.lat),o=e(j.lng);h=((l+n)/2).toFixed(6),i=((m+o)/2).toFixed(6);var p=g.convertUnit("km",g.getDistance({lat:l,lng:m},{lat:n,lng:o}));return{latitude:h,longitude:i,distance:p}},getBounds:function(a){if(!a.length)return!1;var b=g.getKeys(a[0]),c=b.latitude,d=b.longitude,e=b.elevation,f=a[0].hasOwnProperty(e),h={maxLat:Infinity*-1,minLat:Infinity,maxLng:Infinity*-1,minLng:Infinity};f&&(h.maxElev=Infinity*-1,h.minElev=Infinity);for(var i=0,j=a.length;i<j;++i)h.maxLat=Math.max(a[i][c],h.maxLat),h.minLat=Math.min(a[i][c],h.minLat),h.maxLng=Math.max(a[i][d],h.maxLng),h.minLng=Math.min(a[i][d],h.minLng),f&&(h.maxElev=Math.max(a[i][e],h.maxElev),h.minElev=Math.min(a[i][e],h.minElev));return h},isPointInside:function(a,b){var c=g.getKeys(a),d=c.latitude,e=c.longitude;for(var f=!1,h=-1,i=b.length,j=i-1;++h<i;j=h)(b[h][e]<=a[e]&&a[e]<b[j][e]||b[j][e]<=a[e]&&a[e]<b[h][e])&&a[d]<(b[j][d]-b[h][d])*(a[e]-b[h][e])/(b[j][e]-b[h][e])+b[h][d]&&(f=!f);return f},isPointInCircle:function(a,b,c){return g.getDistance(a,b)<c},getRhumbLineBearing:function(a,b){var c=g.getKeys(a),d=c.latitude,e=c.longitude,f=g.useDecimal(b[e]).toRad()-g.useDecimal(a[e]).toRad(),h=Math.log(Math.tan(g.useDecimal(b[d]).toRad()/2+Math.PI/4)/Math.tan(g.useDecimal(a[d]).toRad()/2+Math.PI/4));return Math.abs(f)>Math.PI&&(f>0?f=(2*Math.PI-f)*-1:f=2*Math.PI+f),(Math.atan2(f,h).toDeg()+360)%360},getBearing:function(a,b){var c=g.getKeys(a),d=c.latitude,e=c.longitude;b[d]=g.useDecimal(b[d]),b[e]=g.useDecimal(b[e]),a[d]=g.useDecimal(a[d]),a[e]=g.useDecimal(a[e]);var f=(Math.atan2(Math.sin(b[e].toRad()-a[e].toRad())*Math.cos(b[d].toRad()),Math.cos(a[d].toRad())*Math.sin(b[d].toRad())-Math.sin(a[d].toRad())*Math.cos(b[d].toRad())*Math.cos(b[e].toRad()-a[e].toRad())).toDeg()+360)%360;return f},getCompassDirection:function(a,b,c){var d;if(c=="circle")var e=g.getBearing(a,b);else var e=g.getRhumbLineBearing(a,b);console.log(Math.round(e/22.5));switch(Math.round(e/22.5)){case 1:d={exact:"NNE",rough:"N"};break;case 2:d={exact:"NE",rough:"N"};break;case 3:d={exact:"ENE",rough:"E"};break;case 4:d={exact:"E",rough:"E"};break;case 5:d={exact:"ESE",rough:"E"};break;case 6:d={exact:"SE",rough:"E"};break;case 7:d={exact:"SSE",rough:"S"};break;case 8:d={exact:"S",rough:"S"};break;case 9:d={exact:"SSW",rough:"S"};break;case 10:d={exact:"SW",rough:"S"};break;case 11:d={exact:"WSW",rough:"W"};break;case 12:d={exact:"W",rough:"W"};break;case 13:d={exact:"WNW",rough:"W"};break;case 14:d={exact:"NW",rough:"W"};break;case 15:d={exact:"NNW",rough:"N"};break;default:d={exact:"N",rough:"N"}}return d},orderByDistance:function(a,b){var c=g.getKeys(a),d=c.latitude,e=c.longitude,f=[];for(var h in b){var i=g.getDistance(a,b[h]);f.push({key:h,latitude:b[h][d],longitude:b[h][e],distance:i})}return f.sort(function(a,b){return a.distance-b.distance})},findNearest:function(a,b,c){c=c||0;var d=g.orderByDistance(a,b);return d[c]},getPathLength:function(a){var b=0,c;for(var d=0,e=a.length;d<e;++d)c&&(b+=g.getDistance(a[d],c)),c=a[d];return b},setBusinessSpecificParameters:function(a,b){e=a,f=b},getElevation:function(){typeof a.navigator!="undefined"?g.getElevationClient.apply(this,arguments):g.getElevationServer.apply(this,arguments)},getElevationClient:function(b,c){try{if(!a.google)return c(new Error("Geolib: Google maps api not loaded"));if(b.length==0)return c(null,null);if(b.length==1)return c(new Error("Geolib: getElevation requires at least 2 points."));var d=[],e=g.getKeys(b[0]),f=e.latitude,h=e.longitude;for(var i=0;i<b.length;i++)d.push(new google.maps.LatLng(g.useDecimal(b[i][f]),g.useDecimal(b[i][h])));var j={path:d,samples:d.length},k=new google.maps.ElevationService;k.getElevationAlongPath(j,function(a,d){g.elevationHandler(a,d,b,e,c)})}catch(l){return c(l)}},getElevationServer:function(a,b){try{if(a.length==0)return b(null,null);if(a.length==1)return b(new Error("Geolib: getElevation requires at least 2 points."));var c=require("googlemaps"),d=[],h=g.getKeys(a[0]);a[0];var i=h.latitude,j=h.longitude;for(var k=0;k<a.length;k++)d.push(g.useDecimal(a[k][i])+","+g.useDecimal(a[k][j]));c.elevationFromPath(d.join("|"),d.length,function(c,d){g.elevationHandler(d.results,d.status,a,h,b)})}catch(l){return b(l)}var c=require("googlemaps");e&&f&&c.setBusinessSpecificParameters(e,f);var d=[],h=g.getKeys(a[0]),i=h.latitude,j=h.longitude;for(var k=0;k<a.length;k++)d.push(g.useDecimal(a[k][i])+","+g.useDecimal(a[k][j]));c.elevationFromPath(d.join("|"),d.length,function(c,d){g.elevationHandler(d.results,d.status,a,h,b)})},elevationHandler:function(a,b,c,d,e){var f=[],g=d.latitude,h=d.longitude;if(b=="OK"){for(var i=0;i<a.length;i++)f.push({lat:c[i][g],lng:c[i][h],elev:a[i].elevation});e(null,f)}else e(new Error("Geolib: Could not get elevation using Google's API"),elevationResult.status)},getGrade:function(a,b){var c=g.getKeys(a[0]),d=c.elevation,e=Math.abs(a[a.length-1][d]-a[0][d]),f=g.getPathLength(a),h=e/f*100;if(typeof b=="number"){var i=Math.pow(10,b);h=Math.floor(h*i)/i}return h},getTotalElevationGainAndLoss:function(a){var b=g.getKeys(a[0]),c=b.elevation,d=0,e=0;for(var f=0;f<a.length-1;f++){var h=a[f][c]-a[f+1][c];h>0?e+=h:d+=Math.abs(h)}return{gain:d,loss:e}},convertUnit:function(a,b,c){if(b==0||typeof b=="undefined"){if(g.distance==0)return 0;b=g.distance}a=a||"m",c=c||4;switch(a){case"m":return g.round(b,c);case"km":return g.round(b/1e3,c);case"cm":return g.round(b*100,c);case"mm":return g.round(b*1e3,c);case"mi":return g.round(b*(1/1609.344),c);case"sm":return g.round(b*(1/1852.216),c);case"ft":return g.round(b*(100/30.48),c);case"in":return g.round(b*100/2.54,c);case"yd":return g.round(b*(1/.9144),c)}return b},useDecimal:function(a){a=a.toString().replace(/\s*/,"");if(!isNaN(parseFloat(a))&&parseFloat(a).toString()==a)return parseFloat(a);if(g.isSexagesimal(a)==1)return parseFloat(g.sexagesimal2decimal(a));throw new Error("Geolib: Unknown format.")},decimal2sexagesimal:function(a){if(a in g.sexagesimal)return g.sexagesimal[a];var b=a.toString().split("."),c=Math.abs(b[0]),d=("0."+b[1])*60,e=d.toString().split(".");return d=Math.floor(d),e=(("0."+e[1])*60).toFixed(2),g.sexagesimal[a]=c+"° "+d+"' "+e+'"',g.sexagesimal[a]},sexagesimal2decimal:function(a){if(a in g.decimal)return g.decimal[a];var b=new RegExp(d),c=b.exec(a);if(c)var e=parseFloat(c[2]/60),f=parseFloat(c[4]/3600)||0;var h=(parseFloat(c[1])+e+f).toFixed(8);return h=c[7]=="S"||c[7]=="W"?h*-1:h,g.decimal[a]=h,h},isSexagesimal:function(a){return d.test(a)},round:function(a,b){var c=Math.pow(10,b);return Math.round(a*c)/c}};typeof Number.prototype.toRad=="undefined"&&(Number.prototype.toRad=function(){return this*Math.PI/180}),typeof Number.prototype.toDeg=="undefined"&&(Number.prototype.toDeg=function(){return this*180/Math.PI}),typeof a.navigator!="undefined"?a.geolib=g:module.exports=g})(this);
Please sign in to comment.
Something went wrong with that request. Please try again.