var toGeoJSON=function(){"use strict";var e,t=/\s*/g,r=/^\s*|\s*$/g,n=/\s+/;function i(e){if(!e||!e.length)return 0;for(var t=0,r=0;t<e.length;t++)r=(r<<5)-r+e.charCodeAt(t)|0;return r}function o(e,t){return e.getElementsByTagName(t)}function a(e,t){return e.getAttribute(t)}function s(e,t){return parseFloat(a(e,t))}function l(e,t){var r=o(e,t);return r.length?r[0]:null}function u(e){for(var t=0,r=[];t<e.length;t++)r[t]=parseFloat(e[t]);return r}function f(e){var t;return e&&(t=e).normalize&&t.normalize(),e&&e.textContent||""}function h(e,t){var r,n,i={};for(n=0;n<t.length;n++)(r=l(e,t[n]))&&(i[t[n]]=f(r));return i}function c(e,t){for(var r in t)e[r]=t[r]}function p(e){return u(e.replace(t,"").split(","))}function g(e){for(var t=e.replace(r,"").split(n),i=[],o=0;o<t.length;o++)i.push(p(t[o]));return i}function m(e){var t,r=[s(e,"lon"),s(e,"lat")],n=l(e,"ele"),i=l(e,"gpxtpx:hr")||l(e,"hr"),o=l(e,"time");return n&&(t=parseFloat(f(n)),isNaN(t)||r.push(t)),{coordinates:r,time:o?f(o):null,heartRate:i?parseFloat(f(i)):null}}if("undefined"!=typeof XMLSerializer)e=new XMLSerializer;else{var y="object"==typeof process&&!process.browser,d="object"==typeof Titanium;if("object"!=typeof exports||!y&&!d)throw new Error("Unable to initialize serializer");e=new(require("@xmldom/xmldom").XMLSerializer)}function v(t){return void 0!==t.xml?t.xml:e.serializeToString(t)}return{kml:function(e){for(var t={type:"FeatureCollection",features:[]},r={},n={},s={},h=["Polygon","LineString","Point","Track","gx:Track"],c=o(e,"Placemark"),m=o(e,"Style"),y=o(e,"StyleMap"),d=0;d<m.length;d++){var k=i(v(m[d])).toString(16);r["#"+a(m[d],"id")]=k,n[k]=m[d]}for(var S=0;S<y.length;S++){r["#"+a(y[S],"id")]=i(v(y[S])).toString(16);for(var T=o(y[S],"Pair"),x={},b=0;b<T.length;b++)x[f(l(T[b],"key"))]=f(l(T[b],"styleUrl"));s["#"+a(y[S],"id")]=x}for(var N=0;N<c.length;N++)t.features=t.features.concat(M(c[N]));function w(e){var t,r;return"#"===(e=e||"").substr(0,1)&&(e=e.substr(1)),6!==e.length&&3!==e.length||(t=e),8===e.length&&(r=parseInt(e.substr(0,2),16)/255,t="#"+e.substr(6,2)+e.substr(4,2)+e.substr(2,2)),[t,isNaN(r)?void 0:r]}function F(e){var t=o(e,"coord"),r=[],n=[];0===t.length&&(t=o(e,"gx:coord"));for(var i=0;i<t.length;i++)r.push(u(f(t[i]).split(" ")));for(var a=o(e,"when"),s=0;s<a.length;s++)n.push(f(a[s]));return{coords:r,times:n}}function L(e){var t,r,n,i,a,s=[],u=[];if(l(e,"MultiGeometry"))return L(l(e,"MultiGeometry"));if(l(e,"MultiTrack"))return L(l(e,"MultiTrack"));if(l(e,"gx:MultiTrack"))return L(l(e,"gx:MultiTrack"));for(n=0;n<h.length;n++)if(r=o(e,h[n]))for(i=0;i<r.length;i++)if(t=r[i],"Point"===h[n])s.push({type:"Point",coordinates:p(f(l(t,"coordinates")))});else if("LineString"===h[n])s.push({type:"LineString",coordinates:g(f(l(t,"coordinates")))});else if("Polygon"===h[n]){var c=o(t,"LinearRing"),m=[];for(a=0;a<c.length;a++)m.push(g(f(l(c[a],"coordinates"))));s.push({type:"Polygon",coordinates:m})}else if("Track"===h[n]||"gx:Track"===h[n]){var y=F(t);s.push({type:"LineString",coordinates:y.coords}),y.times.length&&u.push(y.times)}return{geoms:s,coordTimes:u}}function M(e){var t,i=L(e),u={},h=f(l(e,"name")),c=f(l(e,"address")),p=f(l(e,"styleUrl")),g=f(l(e,"description")),m=l(e,"TimeSpan"),y=l(e,"TimeStamp"),d=l(e,"ExtendedData"),v=l(e,"LineStyle"),k=l(e,"PolyStyle"),S=l(e,"visibility");if(!i.geoms.length)return[];if(h&&(u.name=h),c&&(u.address=c),p){"#"!==p[0]&&(p="#"+p),u.styleUrl=p,r[p]&&(u.styleHash=r[p]),s[p]&&(u.styleMapHash=s[p],u.styleHash=r[s[p].normal]);var T=n[u.styleHash];if(T){v||(v=l(T,"LineStyle")),k||(k=l(T,"PolyStyle"));var x=l(T,"IconStyle");if(x){var b=l(x,"Icon");if(b){var N=f(l(b,"href"));N&&(u.icon=N)}}}}if(g&&(u.description=g),m){var F=f(l(m,"begin")),M=f(l(m,"end"));u.timespan={begin:F,end:M}}if(y&&(u.timestamp=f(l(y,"when"))),v){var P=w(f(l(v,"color"))),R=P[0],z=P[1],C=parseFloat(f(l(v,"width")));R&&(u.stroke=R),isNaN(z)||(u["stroke-opacity"]=z),isNaN(C)||(u["stroke-width"]=C)}if(k){var G=w(f(l(k,"color"))),A=G[0],H=G[1],U=f(l(k,"fill")),j=f(l(k,"outline"));A&&(u.fill=A),isNaN(H)||(u["fill-opacity"]=H),U&&(u["fill-opacity"]="1"===U?u["fill-opacity"]||1:0),j&&(u["stroke-opacity"]="1"===j?u["stroke-opacity"]||1:0)}if(d){var D=o(d,"Data"),E=o(d,"SimpleData");for(t=0;t<D.length;t++)u[D[t].getAttribute("name")]=f(l(D[t],"value"));for(t=0;t<E.length;t++)u[E[t].getAttribute("name")]=f(E[t])}S&&(u.visibility=f(S)),i.coordTimes.length&&(u.coordTimes=1===i.coordTimes.length?i.coordTimes[0]:i.coordTimes);var I={type:"Feature",geometry:1===i.geoms.length?i.geoms[0]:{type:"GeometryCollection",geometries:i.geoms},properties:u};return a(e,"id")&&(I.id=a(e,"id")),[I]}return t},gpx:function(e){var t,r,n=o(e,"trk"),i=o(e,"rte"),s=o(e,"wpt"),u={type:"FeatureCollection",features:[]};for(t=0;t<n.length;t++)(r=y(n[t]))&&u.features.push(r);for(t=0;t<i.length;t++)(r=d(i[t]))&&u.features.push(r);for(t=0;t<s.length;t++)u.features.push(v(s[t]));function p(e,t){for(var r=0;r<t;r++)e.push(null);return e}function g(e,t){var r=o(e,t),n=[],i=[],a=[],s=r.length;if(s<2)return{};for(var l=0;l<s;l++){var u=m(r[l]);n.push(u.coordinates),u.time&&i.push(u.time),(u.heartRate||a.length)&&(a.length||p(a,l),a.push(u.heartRate||null))}return{line:n,times:i,heartRates:a}}function y(e){for(var t,r=o(e,"trkseg"),n=[],i=[],a=[],s=0;s<r.length;s++)if((t=g(r[s],"trkpt"))&&(t.line&&n.push(t.line),t.times&&t.times.length&&i.push(t.times),a.length||t.heartRates&&t.heartRates.length)){if(!a.length)for(var u=0;u<s;u++)a.push(p([],n[u].length));t.heartRates&&t.heartRates.length?a.push(t.heartRates):a.push(p([],t.line.length||0))}if(0!==n.length){var f=S(e);return c(f,k(l(e,"extensions"))),i.length&&(f.coordTimes=1===n.length?i[0]:i),a.length&&(f.heartRates=1===n.length?a[0]:a),{type:"Feature",properties:f,geometry:{type:1===n.length?"LineString":"MultiLineString",coordinates:1===n.length?n[0]:n}}}}function d(e){var t=g(e,"rtept");if(t.line){var r=S(e);return c(r,k(l(e,"extensions"))),{type:"Feature",properties:r,geometry:{type:"LineString",coordinates:t.line}}}}function v(e){var t=S(e);return c(t,h(e,["sym"])),{type:"Feature",properties:t,geometry:{type:"Point",coordinates:m(e).coordinates}}}function k(e){var t={};if(e){var r=l(e,"line");if(r){var n=f(l(r,"color")),i=parseFloat(f(l(r,"opacity"))),o=parseFloat(f(l(r,"width")));n&&(t.stroke=n),isNaN(i)||(t["stroke-opacity"]=i),isNaN(o)||(t["stroke-width"]=96*o/25.4)}}return t}function S(e){var t=h(e,["name","cmt","desc","type","time","keywords"]),r=o(e,"link");r.length&&(t.links=[]);for(var n,i=0;i<r.length;i++)c(n={href:a(r[i],"href")},h(r[i],["text","type"])),t.links.push(n);return t}return u}}}();"undefined"!=typeof module&&(module.exports=toGeoJSON);