UNPKG

gpx-js

Version:
1 lines 6.62 kB
let GpxJs=function(){this.xmlSource="",this.metadata={},this.waypoints=[],this.tracks=[],this.routes=[]};GpxJs.prototype.parse=function(e){let t=this,l=new window.DOMParser;this.xmlSource=l.parseFromString(e,"text/xml");let a=this.xmlSource.querySelector("metadata");if(null!=a){this.metadata.name=this.getElementValue(a,"name"),this.metadata.desc=this.getElementValue(a,"desc"),this.metadata.time=this.getElementValue(a,"time");let e={},t=a.querySelector("author");if(null!=t){e.name=this.getElementValue(t,"name"),e.email={};let l=t.querySelector("email");null!=l&&(e.email.id=l.getAttribute("id"),e.email.domain=l.getAttribute("domain"));let a={},r=t.querySelector("link");null!=r&&(a.href=r.getAttribute("href"),a.text=this.getElementValue(r,"text"),a.type=this.getElementValue(r,"type")),e.link=a}this.metadata.author=e;let l={},r=this.queryDirectSelector(a,"link");null!=r&&(l.href=r.getAttribute("href"),l.text=this.getElementValue(r,"text"),l.type=this.getElementValue(r,"type"),this.metadata.link=l)}var r=[].slice.call(this.xmlSource.querySelectorAll("wpt"));for(let e in r){var n=r[e];let l={};l.name=t.getElementValue(n,"name"),l.sym=t.getElementValue(n,"sym"),l.lat=parseFloat(n.getAttribute("lat")),l.lon=parseFloat(n.getAttribute("lon"));let a=parseFloat(t.getElementValue(n,"ele"));l.ele=isNaN(a)?null:a,l.cmt=t.getElementValue(n,"cmt"),l.desc=t.getElementValue(n,"desc");let s=t.getElementValue(n,"time");l.time=null==s?null:new Date(s),t.waypoints.push(l)}var s=[].slice.call(this.xmlSource.querySelectorAll("rte"));for(let e in s){let l=s[e],a={};a.name=t.getElementValue(l,"name"),a.cmt=t.getElementValue(l,"cmt"),a.desc=t.getElementValue(l,"desc"),a.src=t.getElementValue(l,"src"),a.number=t.getElementValue(l,"number");let r=t.queryDirectSelector(l,"type");a.type=null!=r?r.innerHTML:null;let n={},o=l.querySelector("link");null!=o&&(n.href=o.getAttribute("href"),n.text=t.getElementValue(o,"text"),n.type=t.getElementValue(o,"type")),a.link=n;let u=[];var i=[].slice.call(l.querySelectorAll("rtept"));for(let e in i){let l=i[e],a={};a.lat=parseFloat(l.getAttribute("lat")),a.lon=parseFloat(l.getAttribute("lon"));let r=parseFloat(t.getElementValue(l,"ele"));a.ele=isNaN(r)?null:r;let n=t.getElementValue(l,"time");a.time=null==n?null:new Date(n),u.push(a)}a.distance=t.calculDistance(u),a.elevation=t.calcElevation(u),a.slopes=t.calculSlope(u,a.distance.cumul),a.points=u,t.routes.push(a)}var o=[].slice.call(this.xmlSource.querySelectorAll("trk"));for(let e in o){let l=o[e],a={};a.name=t.getElementValue(l,"name"),a.cmt=t.getElementValue(l,"cmt"),a.desc=t.getElementValue(l,"desc"),a.src=t.getElementValue(l,"src"),a.number=t.getElementValue(l,"number");let r=t.queryDirectSelector(l,"type");a.type=null!=r?r.innerHTML:null;let n={},s=l.querySelector("link");null!=s&&(n.href=s.getAttribute("href"),n.text=t.getElementValue(s,"text"),n.type=t.getElementValue(s,"type")),a.link=n;let i=[],p=[].slice.call(l.querySelectorAll("trkpt"));for(let e in p){var u=p[e];let l={};l.lat=parseFloat(u.getAttribute("lat")),l.lon=parseFloat(u.getAttribute("lon"));let a=parseFloat(t.getElementValue(u,"ele"));l.ele=isNaN(a)?null:a;let r=t.getElementValue(u,"time");l.time=null==r?null:new Date(r);let n=u.getElementsByTagName("extensions");if(n.length){let e=n[0];if(e){let t=e.getElementsByTagName("gpxtpx:TrackPointExtension")[0];if(t){let e=t.getElementsByTagName("gpxtpx:atemp")[0].innerHTML,a=t.getElementsByTagName("gpxtpx:hr")[0].innerHTML,r=t.getElementsByTagName("gpxtpx:cad")[0].innerHTML,n=t.getElementsByTagName("gpxtpx:power")[0].innerHTML;l.atemp=e,l.hr=a,l.cad=r,l.power=n}}}i.push(l)}a.distance=t.calculDistance(i),a.elevation=t.calcElevation(i),a.slopes=t.calculSlope(i,a.distance.cumul),a.points=i,t.tracks.push(a)}},GpxJs.prototype.getElementValue=function(e,t){let l=e.querySelector(t);return null!=l?null!=l.innerHTML?l.innerHTML:l.childNodes[0].data:l},GpxJs.prototype.queryDirectSelector=function(e,t){let l=e.querySelectorAll(t),a=l[0];if(l.length>1){let l=e.childNodes;for(let e in l){let r=l[e];r.tagName===t&&(a=r)}}return a},GpxJs.prototype.calculDistance=function(e){let t={},l=0,a=[];for(var r=0;r<e.length-1;r++)l+=this.calcDistanceBetween(e[r],e[r+1]),a[r]=l;return a[e.length-1]=l,t.total=l,t.cumul=a,t},GpxJs.prototype.calcDistanceBetween=function(e,t){let l={};l.lat=e.lat,l.lon=e.lon;let a={};a.lat=t.lat,a.lon=t.lon;var r=Math.PI/180,n=l.lat*r,s=a.lat*r,i=Math.sin((a.lat-l.lat)*r/2),o=Math.sin((a.lon-l.lon)*r/2),u=i*i+Math.cos(n)*Math.cos(s)*o*o;return 6371e3*(2*Math.atan2(Math.sqrt(u),Math.sqrt(1-u)))},GpxJs.prototype.calcElevation=function(e){for(var t=0,l=0,a={},r=0;r<e.length-1;r++){let a=e[r+1].ele,n=e[r].ele;if(null!==a&&null!==n){let e=parseFloat(a)-parseFloat(n);e<0?l+=e:e>0&&(t+=e)}}for(var n=[],s=0,i=0,o=e.length;i<o;i++){if(null!==e[i].ele){var u=parseFloat(e[i].ele);n.push(u),s+=u}}return a.max=Math.max.apply(null,n)||null,a.min=Math.min.apply(null,n)||null,a.pos=Math.abs(t)||null,a.neg=Math.abs(l)||null,a.avg=s/n.length||null,a},GpxJs.prototype.calculSlope=function(e,t){let l=[];for(var a=0;a<e.length-1;a++){let r=e[a],n=100*(e[a+1].ele-r.ele)/(t[a+1]-t[a]);l.push(n)}return l},GpxJs.prototype.toGeoJSON=function(){var e={type:"FeatureCollection",features:[],properties:{name:this.metadata.name,desc:this.metadata.desc,time:this.metadata.time,author:this.metadata.author,link:this.metadata.link}};for(let a in this.tracks){let r=this.tracks[a];var t={type:"Feature",geometry:{type:"LineString",coordinates:[]},properties:{}};t.properties.name=r.name,t.properties.cmt=r.cmt,t.properties.desc=r.desc,t.properties.src=r.src,t.properties.number=r.number,t.properties.link=r.link,t.properties.type=r.type;for(let e in r.points){let a=r.points[e];var l=[];l.push(a.lon),l.push(a.lat),l.push(a.ele),t.geometry.coordinates.push(l)}e.features.push(t)}for(let a in this.routes){let r=this.routes[a];(t={type:"Feature",geometry:{type:"LineString",coordinates:[]},properties:{}}).properties.name=r.name,t.properties.cmt=r.cmt,t.properties.desc=r.desc,t.properties.src=r.src,t.properties.number=r.number,t.properties.link=r.link,t.properties.type=r.type;for(let e in r.points){let a=r.points[e];(l=[]).push(a.lon),l.push(a.lat),l.push(a.ele),t.geometry.coordinates.push(l)}e.features.push(t)}for(let l in this.waypoints){let a=this.waypoints[l];(t={type:"Feature",geometry:{type:"Point",coordinates:[]},properties:{}}).properties.name=a.name,t.properties.sym=a.sym,t.properties.cmt=a.cmt,t.properties.desc=a.desc,t.geometry.coordinates=[a.lon,a.lat,a.ele],e.features.push(t)}return e},"undefined"!=typeof module&&(require("jsdom-global")(),module.exports=GpxJs);