UNPKG

mahaseel-gpxparser

Version:
1 lines 2.86 kB
let gpxParser=function(){this.xmlSource="",this.metadata={},this.waypoints=[],this.tracks=[],this.routes=[]};gpxParser.prototype.parse=function(e){let t=this;const{JSDOM:r}=require("jsdom"),{window:s}=new r("");let i=new s.DOMParser;this.xmlSource=i.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={}}this.metadata.author=e}var o=[].slice.call(this.xmlSource.querySelectorAll("wpt"));for(let e in o){var l=o[e];let r={};r.name=t.getElementValue(l,"name"),r.sym=t.getElementValue(l,"sym"),r.lat=parseFloat(l.getAttribute("lat")),r.lon=parseFloat(l.getAttribute("lon"));let s=parseFloat(t.getElementValue(l,"ele"));r.ele=isNaN(s)?null:s,r.cmt=t.getElementValue(l,"cmt"),r.desc=t.getElementValue(l,"desc");let i=t.getElementValue(l,"time");r.time=null==i?null:new Date(i),t.waypoints.push(r)}},gpxParser.prototype.getElementValue=function(e,t){let r=e.querySelector(t);return null!=r?null!=r.innerHTML?r.innerHTML:r.childNodes[0].data:r},gpxParser.prototype.queryDirectSelector=function(e,t){let r=e.querySelectorAll(t),s=r[0];if(r.length>1){let r=e.childNodes;for(idx in r)elem=r[idx],elem.tagName===t&&(s=elem)}return s},gpxParser.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(idx in this.tracks){let s=this.tracks[idx];var t={type:"Feature",geometry:{type:"LineString",coordinates:[]},properties:{}};for(idx in t.properties.name=s.name,t.properties.cmt=s.cmt,t.properties.desc=s.desc,t.properties.src=s.src,t.properties.number=s.number,t.properties.link=s.link,t.properties.type=s.type,s.points){let e=s.points[idx];(r=[]).push(e.lon),r.push(e.lat),r.push(e.ele),t.geometry.coordinates.push(r)}e.features.push(t)}for(idx in this.routes){let s=this.routes[idx];t={type:"Feature",geometry:{type:"LineString",coordinates:[]},properties:{}};for(idx in t.properties.name=s.name,t.properties.cmt=s.cmt,t.properties.desc=s.desc,t.properties.src=s.src,t.properties.number=s.number,t.properties.link=s.link,t.properties.type=s.type,s.points){let e=s.points[idx];var r;(r=[]).push(e.lon),r.push(e.lat),r.push(e.ele),t.geometry.coordinates.push(r)}e.features.push(t)}for(idx in this.waypoints){let r=this.waypoints[idx];(t={type:"Feature",geometry:{type:"Point",coordinates:[]},properties:{}}).properties.name=r.name,t.properties.sym=r.sym,t.properties.cmt=r.cmt,t.properties.desc=r.desc,t.geometry.coordinates=[r.lon,r.lat,r.ele],e.features.push(t)}return e},"undefined"!=typeof module&&(module.exports=gpxParser);