@tmcw/togeojson 5.0.0-0 → 5.0.0-3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +11 -0
- package/README.md +2 -0
- package/dist/index.d.ts +97 -3
- package/dist/togeojson.cjs +164 -73
- package/dist/togeojson.cjs.map +1 -1
- package/dist/togeojson.es.js +897 -1
- package/dist/togeojson.es.js.map +1 -1
- package/dist/togeojson.umd.js +1 -1
- package/dist/togeojson.umd.js.map +1 -1
- package/package.json +9 -11
- package/dist/gpx/coord_pair.d.ts +0 -9
- package/dist/gpx/extensions.d.ts +0 -2
- package/dist/gpx/line.d.ts +0 -3
- package/dist/gpx/properties.d.ts +0 -3
- package/dist/gpx.d.ts +0 -19
- package/dist/kml/extractStyle.d.ts +0 -13
- package/dist/kml/fixColor.d.ts +0 -2
- package/dist/kml/geometry.d.ts +0 -16
- package/dist/kml/placemark.d.ts +0 -3
- package/dist/kml.d.ts +0 -61
- package/dist/shared.d.ts +0 -27
- package/dist/tcx.d.ts +0 -12
- package/dist/togeojson.js +0 -2
- package/dist/togeojson.js.map +0 -1
- package/dist/togeojson.modern.js +0 -2
- package/dist/togeojson.modern.js.map +0 -1
package/dist/togeojson.es.js
CHANGED
|
@@ -1,2 +1,898 @@
|
|
|
1
|
-
function e(e,t){(null==t||t>e.length)&&(t=e.length);for(var r=0,n=new Array(t);r<t;r++)n[r]=e[r];return n}function t(t,r){var n="undefined"!=typeof Symbol&&t[Symbol.iterator]||t["@@iterator"];if(n)return(n=n.call(t)).next.bind(n);if(Array.isArray(t)||(n=function(t,r){if(t){if("string"==typeof t)return e(t,r);var n=Object.prototype.toString.call(t).slice(8,-1);return"Object"===n&&t.constructor&&(n=t.constructor.name),"Map"===n||"Set"===n?Array.from(t):"Arguments"===n||/^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)?e(t,r):void 0}}(t))||r&&t&&"number"==typeof t.length){n&&(t=n);var a=0;return function(){return a>=t.length?{done:!0}:{done:!1,value:t[a++]}}}throw new TypeError("Invalid attempt to iterate non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method.")}function r(e,t){return Array.from(e.getElementsByTagName(t))}function n(e){return"#"===e[0]?e:"#"+e}function a(e){return null==e||e.normalize(),e&&e.textContent||""}function i(e,t,r){var n=e.getElementsByTagName(t),a=n.length?n[0]:null;return a&&r&&r(a),a}function o(e,t,r){var n={};if(!e)return n;var a=e.getElementsByTagName(t),i=a.length?a[0]:null;return i&&r?r(i,n):n}function u(e,t,r){var n=a(i(e,t));return n&&r&&r(n)||{}}function s(e,t,r){var n=parseFloat(a(i(e,t)));if(!isNaN(n))return n&&r&&r(n)||{}}function l(e,t,r){var n=parseFloat(a(i(e,t)));if(!isNaN(n))return n&&r&&r(n),n}function c(e,r){for(var n,a={},i=function(){var t=n.value;u(e,t,function(e){a[t]=e})},o=t(r);!(n=o()).done;)i();return a}function f(e){return 1===(null==e?void 0:e.nodeType)}function g(e){return o(e,"line",function(e){return Object.assign({},u(e,"color",function(e){return{stroke:"#"+e}}),s(e,"opacity",function(e){return{"stroke-opacity":e}}),s(e,"width",function(e){return{"stroke-width":96*e/25.4}}))})}function p(e){var t=[];if(null===e)return t;for(var r=0,n=Array.from(e.childNodes);r<n.length;r++){var i=n[r];if(f(i)){var o=d(i.nodeName);if("gpxtpx:TrackPointExtension"===o)t=t.concat(p(i));else{var u=a(i);t.push([o,v(u)])}}}return t}function d(e){return["heart","gpxtpx:hr","hr"].includes(e)?"heart":e}function v(e){var t=parseFloat(e);return isNaN(t)?e:t}function m(e){var t=[parseFloat(e.getAttribute("lon")||""),parseFloat(e.getAttribute("lat")||"")];l(e,"ele",function(e){t.push(e)});var r=i(e,"time");return{coordinates:t,time:r?a(r):null,extendedValues:p(i(e,"extensions"))}}function h(e){for(var t=c(e,["name","cmt","desc","type","time","keywords"]),n=0,i=Array.from(e.getElementsByTagNameNS("http://www.garmin.com/xmlschemas/GpxExtensions/v3","*"));n<i.length;n++){var o,u=i[n];(null==(o=u.parentNode)?void 0:o.parentNode)===e&&(t[u.tagName.replace(":","_")]=a(u))}var s=r(e,"link");return s.length&&(t.links=s.map(function(e){return Object.assign({href:e.getAttribute("href")},c(e,["text","type"]))})),t}var y=/*#__PURE__*/regeneratorRuntime.mark(A);function b(e,n){var a=r(e,n);if(!(a.length<2)){for(var i=[],o=[],u={},s=0;s<a.length;s++){var l=m(a[s]);i.push(l.coordinates),l.time&&o.push(l.time);for(var c,f=t(l.extendedValues);!(c=f()).done;){var g=c.value,p=g[0],d=g[1],v="heart"===p?p:p.replace("gpxtpx:","")+"s";u[v]||(u[v]=Array(a.length).fill(null)),u[v][s]=d}}return{line:i,times:o,extendedValues:u}}}function x(e){var t=b(e,"rtept");if(t)return{type:"Feature",properties:Object.assign({_gpxType:"rte"},h(e),g(i(e,"extensions"))),geometry:{type:"LineString",coordinates:t.line}}}function k(e){for(var n,a=[],o=[],u=[],s=t(r(e,"trkseg"));!(n=s()).done;){var l=b(n.value,"trkpt");l&&(u.push(l),l.times&&l.times.length&&o.push(l.times))}if(0===u.length)return null;for(var c=u.length>1,f=Object.assign({_gpxType:"trk"},h(e),g(i(e,"extensions")),o.length?{coordinateProperties:{times:c?o:o[0]}}:{}),p=0,d=u;p<d.length;p++){var v=d[p];a.push(v.line),f.coordinateProperties||(f.coordinateProperties={});for(var m=f.coordinateProperties,y=Object.entries(v.extendedValues),x=0;x<y.length;x++){var k=y[x],N=k[0],A=k[1];c?(m[N]||(m[N]=u.map(function(e){return new Array(e.line.length).fill(null)})),m[N][x]=A):m[N]=A}}return{type:"Feature",properties:f,geometry:c?{type:"MultiLineString",coordinates:a}:{type:"LineString",coordinates:a[0]}}}function N(e){return{type:"Feature",properties:Object.assign(h(e),c(e,["sym"])),geometry:{type:"Point",coordinates:m(e).coordinates}}}function A(e){var n,a,i,o,u,s,l,c,f;return regeneratorRuntime.wrap(function(g){for(;;)switch(g.prev=g.next){case 0:n=t(r(e,"trk"));case 1:if((a=n()).done){g.next=9;break}if(!(i=k(a.value))){g.next=7;break}return g.next=7,i;case 7:g.next=1;break;case 9:o=t(r(e,"rte"));case 10:if((u=o()).done){g.next=18;break}if(!(s=x(u.value))){g.next=16;break}return g.next=16,s;case 16:g.next=10;break;case 18:l=t(r(e,"wpt"));case 19:if((c=l()).done){g.next=25;break}return f=c.value,g.next=23,N(f);case 23:g.next=19;break;case 25:case"end":return g.stop()}},y)}function S(e){return{type:"FeatureCollection",features:Array.from(A(e))}}var w=/*#__PURE__*/regeneratorRuntime.mark(L),T=[["heartRate","heartRates"],["Cadence","cadences"],["Speed","speeds"],["Watts","watts"]],F=[["TotalTimeSeconds","totalTimeSeconds"],["DistanceMeters","distanceMeters"],["MaximumSpeed","maxSpeed"],["AverageHeartRateBpm","avgHeartRate"],["MaximumHeartRateBpm","maxHeartRate"],["AvgSpeed","avgSpeed"],["AvgWatts","avgWatts"],["MaxWatts","maxWatts"]];function P(e,r){for(var n,o=[],u=t(r);!(n=u()).done;){var s=n.value,l=s[0],c=s[1],f=i(e,l);if(!f){var g=e.getElementsByTagNameNS("http://www.garmin.com/xmlschemas/ActivityExtension/v2",l);g.length&&(f=g[0])}var p=parseFloat(a(f));isNaN(p)||o.push([c,p])}return o}function j(e){var t=[l(e,"LongitudeDegrees"),l(e,"LatitudeDegrees")];if(void 0===t[0]||isNaN(t[0])||void 0===t[1]||isNaN(t[1]))return null;var r=i(e,"HeartRateBpm"),n=a(i(e,"Time"));return i(e,"AltitudeMeters",function(e){var r=parseFloat(a(e));isNaN(r)||t.push(r)}),{coordinates:t,time:n||null,heartRate:r?parseFloat(a(r)):null,extensions:P(e,T)}}function O(e){var n=r(e,"Trackpoint"),a=[],i=[],o=[];if(n.length<2)return null;for(var u={},s={extendedProperties:u},l=0;l<n.length;l++){var c=j(n[l]);if(null!==c){a.push(c.coordinates);var f=c.time,g=c.heartRate,p=c.extensions;f&&i.push(f),g&&o.push(g);for(var d,v=t(p);!(d=v()).done;){var m=d.value,h=m[0],y=m[1];u[h]||(u[h]=Array(n.length).fill(null)),u[h][l]=y}}}return Object.assign(s,{line:a,times:i,heartRates:o})}function R(e){for(var n,i,u=r(e,"Track"),s=[],l=[],c=[],f=[],g=Object.assign(Object.fromEntries(P(e,F)),o(e,"Name",function(e){return{name:a(e)}})),p=t(u);!(i=p()).done;)(n=O(i.value))&&(s.push(n.line),n.times.length&&l.push(n.times),n.heartRates.length&&c.push(n.heartRates),f.push(n.extendedProperties));for(var d=0;d<f.length;d++){var v=f[d];for(var m in v)1===u.length?n&&(g[m]=n.extendedProperties[m]):(g[m]||(g[m]=s.map(function(e){return Array(e.length).fill(null)})),g[m][d]=v[m])}return 0===s.length?null:((l.length||c.length)&&(g.coordinateProperties=Object.assign(l.length?{times:1===s.length?l[0]:l}:{},c.length?{heart:1===s.length?c[0]:c}:{})),{type:"Feature",properties:g,geometry:1===s.length?{type:"LineString",coordinates:s[0]}:{type:"MultiLineString",coordinates:s}})}function L(e){var n,a,i,o,u,s;return regeneratorRuntime.wrap(function(l){for(;;)switch(l.prev=l.next){case 0:n=t(r(e,"Lap"));case 1:if((a=n()).done){l.next=9;break}if(!(i=R(a.value))){l.next=7;break}return l.next=7,i;case 7:l.next=1;break;case 9:o=t(r(e,"Courses"));case 10:if((u=o()).done){l.next=18;break}if(!(s=R(u.value))){l.next=16;break}return l.next=16,s;case 16:l.next=10;break;case 18:case"end":return l.stop()}},w)}function M(e){return{type:"FeatureCollection",features:Array.from(L(e))}}function E(e,t){var r={},n="stroke"==t||"fill"===t?t:t+"-color";return"#"===e[0]&&(e=e.substring(1)),6===e.length||3===e.length?r[n]="#"+e:8===e.length&&(r[t+"-opacity"]=parseInt(e.substring(0,2),16)/255,r[n]="#"+e.substring(6,8)+e.substring(4,6)+e.substring(2,4)),r}function B(e,t,r){var n={};return l(e,t,function(e){n[r]=e}),n}function C(e,t){return o(e,"color",function(e){return E(a(e),t)})}function D(e){return Object.assign({},function(e){return o(e,"PolyStyle",function(e,t){return Object.assign(t,o(e,"color",function(e){return E(a(e),"fill")}),u(e,"fill",function(e){if("0"===e)return{"fill-opacity":0}}),u(e,"outline",function(e){if("0"===e)return{"stroke-opacity":0}}))})}(e),function(e){return o(e,"LineStyle",function(t){return Object.assign(C(e,"stroke"),B(t,"width","stroke-width"))})}(e),function(e){return o(e,"LabelStyle",function(t){return Object.assign(C(e,"label"),B(t,"scale","label-scale"))})}(e),function(e){return o(e,"IconStyle",function(t){return Object.assign(C(e,"icon"),B(t,"scale","icon-scale"),B(t,"heading","icon-heading"),o(t,"hotSpot",function(e){var t=parseFloat(e.getAttribute("x")||""),r=parseFloat(e.getAttribute("y")||"");return isNaN(t)||isNaN(r)?{}:{"icon-offset":[t,r]}}),o(t,"Icon",function(e,t){return u(e,"href",function(e){t.icon=e}),t}))})}(e))}var I=/\s*/g,H=/^\s*|\s*$/g,U=/\s+/;function W(e){return e.replace(I,"").split(",").map(parseFloat).filter(function(e){return!isNaN(e)})}function V(e){return e.replace(H,"").split(U).map(W).filter(function(e){return e.length>=2})}function G(e){var t=r(e,"coord");0===t.length&&(t=Array.from(e.getElementsByTagNameNS("*","coord")));var n=t.map(function(e){return a(e).split(" ").map(parseFloat)});return 0===n.length?null:{geometry:n.length>2?{type:"LineString",coordinates:n}:{type:"Point",coordinates:n[0]},times:r(e,"when").map(function(e){return a(e)})}}function _(e){if(0===e.length)return e;for(var t=e[0],r=e[e.length-1],n=!0,a=0;a<Math.max(t.length,r.length);a++)if(t[a]!==r[a]){n=!1;break}return n?e:e.concat([e[0]])}var $=["Polygon","LineString","Point","Track","gx:Track"];function z(e){return a(i(e,"coordinates"))}function q(e){for(var n=[],a=[],o=0,u=["MultiGeometry","MultiTrack","gx:MultiTrack"];o<u.length;o++){var s=i(e,u[o]);if(s)return q(s)}for(var l,c=t($);!(l=c()).done;)for(var f,g=l.value,p=t(r(e,g));!(f=p()).done;){var d=f.value;switch(g){case"Point":var v=W(z(d));v.length>=2&&n.push({type:"Point",coordinates:v});break;case"LineString":var m=V(z(d));m.length>=2&&n.push({type:"LineString",coordinates:m});break;case"Polygon":for(var h,y=[],b=t(r(d,"LinearRing"));!(h=b()).done;){var x=_(V(z(h.value)));x.length>=4&&y.push(x)}y.length&&n.push({type:"Polygon",coordinates:y});break;case"Track":case"gx:Track":var k=G(d);if(!k)break;var N=k.times;n.push(k.geometry),N.length&&a.push(N)}}return{geometries:n,coordTimes:a}}function J(e){return o(e,"ExtendedData",function(e,n){for(var o,u=t(r(e,"Data"));!(o=u()).done;){var s=o.value;n[s.getAttribute("name")||""]=a(i(s,"value"))}for(var l,c=t(r(e,"SimpleData"));!(l=c()).done;){var f=l.value;n[f.getAttribute("name")||""]=a(f)}return n})}function K(e){return o(e,"TimeSpan",function(e){return{timespan:{begin:a(i(e,"begin")),end:a(i(e,"end"))}}})}function Q(e){return o(e,"TimeStamp",function(e){return{timestamp:a(i(e,"when"))}})}function X(e,t){return u(e,"styleUrl",function(e){return e=n(e),t[e]?Object.assign({styleUrl:e},t[e]):{styleUrl:e}})}function Y(e,t){var r,n=q(e),a=n.coordTimes,i={type:"Feature",geometry:(r=n.geometries,0===r.length?null:1===r.length?r[0]:{type:"GeometryCollection",geometries:r}),properties:Object.assign(c(e,["name","address","visibility","open","phoneNumber","description"]),X(e,t),D(e),J(e),K(e),Q(e),a.length?{coordinateProperties:{times:1===a.length?a[0]:a}}:{})},o=e.getAttribute("id");return null!==o&&""!==o&&(i.id=o),i}var Z=/*#__PURE__*/regeneratorRuntime.mark(ne);function ee(e){var t=e.getAttribute("id"),r=e.parentNode;return!t&&f(r)&&"CascadingStyle"===r.localName&&(t=r.getAttribute("kml:id")||r.getAttribute("id")),n(t||"")}function te(e){for(var a,i={},o=t(r(e,"Style"));!(a=o()).done;){var s=a.value;i[ee(s)]=D(s)}for(var l,c=function(){var e=l.value,t=n(e.getAttribute("id")||"");u(e,"styleUrl",function(e){e=n(e),i[e]&&(i[t]=i[e])})},f=t(r(e,"StyleMap"));!(l=f()).done;)c();return i}function re(e){var t=te(e),r={type:"root",children:[]};return function e(r,n){if(r.childNodes)for(var i=0;i<r.childNodes.length;i++)e(r.childNodes[i],n);if(f(r))switch(r.tagName){case"Placemark":var o=Y(r,t);o&&n.children.push(o);break;case"Folder":var u=function(e){for(var t=["name","visibility","open","address","description","phoneNumber","visibility"],r={},n=0,i=Array.from(e.childNodes);n<i.length;n++){var o=i[n];f(o)&&t.includes(o.tagName)&&(r[o.tagName]=a(o))}return{type:"folder",meta:r,children:[]}}(r);n.children.push(u),n=u}}(e,r),r}function ne(e){var n,a,i,o;return regeneratorRuntime.wrap(function(u){for(;;)switch(u.prev=u.next){case 0:n=te(e),a=t(r(e,"Placemark"));case 2:if((i=a()).done){u.next=10;break}if(!(o=Y(i.value,n))){u.next=8;break}return u.next=8,o;case 8:u.next=2;break;case 10:case"end":return u.stop()}},Z)}function ae(e){return{type:"FeatureCollection",features:Array.from(ne(e))}}export{S as gpx,A as gpxGen,ae as kml,ne as kmlGen,re as kmlWithFolders,M as tcx,L as tcxGen};
|
|
1
|
+
function $(element, tagName) {
|
|
2
|
+
return Array.from(element.getElementsByTagName(tagName));
|
|
3
|
+
}
|
|
4
|
+
function normalizeId(id) {
|
|
5
|
+
return id[0] === "#" ? id : `#${id}`;
|
|
6
|
+
}
|
|
7
|
+
function $ns(element, tagName, ns) {
|
|
8
|
+
return Array.from(element.getElementsByTagNameNS(ns, tagName));
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* get the content of a text node, if any
|
|
12
|
+
*/
|
|
13
|
+
function nodeVal(node) {
|
|
14
|
+
node?.normalize();
|
|
15
|
+
return (node && node.textContent) || "";
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Get one Y child of X, if any, otherwise null
|
|
19
|
+
*/
|
|
20
|
+
function get1(node, tagName, callback) {
|
|
21
|
+
const n = node.getElementsByTagName(tagName);
|
|
22
|
+
const result = n.length ? n[0] : null;
|
|
23
|
+
if (result && callback)
|
|
24
|
+
callback(result);
|
|
25
|
+
return result;
|
|
26
|
+
}
|
|
27
|
+
function get(node, tagName, callback) {
|
|
28
|
+
const properties = {};
|
|
29
|
+
if (!node)
|
|
30
|
+
return properties;
|
|
31
|
+
const n = node.getElementsByTagName(tagName);
|
|
32
|
+
const result = n.length ? n[0] : null;
|
|
33
|
+
if (result && callback) {
|
|
34
|
+
return callback(result, properties);
|
|
35
|
+
}
|
|
36
|
+
return properties;
|
|
37
|
+
}
|
|
38
|
+
function val1(node, tagName, callback) {
|
|
39
|
+
const val = nodeVal(get1(node, tagName));
|
|
40
|
+
if (val && callback)
|
|
41
|
+
return callback(val) || {};
|
|
42
|
+
return {};
|
|
43
|
+
}
|
|
44
|
+
function $num(node, tagName, callback) {
|
|
45
|
+
const val = parseFloat(nodeVal(get1(node, tagName)));
|
|
46
|
+
if (isNaN(val))
|
|
47
|
+
return undefined;
|
|
48
|
+
if (val && callback)
|
|
49
|
+
return callback(val) || {};
|
|
50
|
+
return {};
|
|
51
|
+
}
|
|
52
|
+
function num1(node, tagName, callback) {
|
|
53
|
+
const val = parseFloat(nodeVal(get1(node, tagName)));
|
|
54
|
+
if (isNaN(val))
|
|
55
|
+
return undefined;
|
|
56
|
+
if (val && callback)
|
|
57
|
+
callback(val);
|
|
58
|
+
return val;
|
|
59
|
+
}
|
|
60
|
+
function getMulti(node, propertyNames) {
|
|
61
|
+
const properties = {};
|
|
62
|
+
for (const property of propertyNames) {
|
|
63
|
+
val1(node, property, (val) => {
|
|
64
|
+
properties[property] = val;
|
|
65
|
+
});
|
|
66
|
+
}
|
|
67
|
+
return properties;
|
|
68
|
+
}
|
|
69
|
+
function isElement(node) {
|
|
70
|
+
return node?.nodeType === 1;
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
function getLineStyle(node) {
|
|
74
|
+
return get(node, "line", (lineStyle) => {
|
|
75
|
+
const val = Object.assign({}, val1(lineStyle, "color", (color) => {
|
|
76
|
+
return { stroke: `#${color}` };
|
|
77
|
+
}), $num(lineStyle, "opacity", (opacity) => {
|
|
78
|
+
return { "stroke-opacity": opacity };
|
|
79
|
+
}), $num(lineStyle, "width", (width) => {
|
|
80
|
+
// GPX width is in mm, convert to px with 96 px per inch
|
|
81
|
+
return { "stroke-width": (width * 96) / 25.4 };
|
|
82
|
+
}));
|
|
83
|
+
return val;
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
function getExtensions(node) {
|
|
88
|
+
let values = [];
|
|
89
|
+
if (node === null)
|
|
90
|
+
return values;
|
|
91
|
+
for (const child of Array.from(node.childNodes)) {
|
|
92
|
+
if (!isElement(child))
|
|
93
|
+
continue;
|
|
94
|
+
const name = abbreviateName(child.nodeName);
|
|
95
|
+
if (name === "gpxtpx:TrackPointExtension") {
|
|
96
|
+
// loop again for nested garmin extensions (eg. "gpxtpx:hr")
|
|
97
|
+
values = values.concat(getExtensions(child));
|
|
98
|
+
}
|
|
99
|
+
else {
|
|
100
|
+
// push custom extension (eg. "power")
|
|
101
|
+
const val = nodeVal(child);
|
|
102
|
+
values.push([name, parseNumeric(val)]);
|
|
103
|
+
}
|
|
104
|
+
}
|
|
105
|
+
return values;
|
|
106
|
+
}
|
|
107
|
+
function abbreviateName(name) {
|
|
108
|
+
return ["heart", "gpxtpx:hr", "hr"].includes(name) ? "heart" : name;
|
|
109
|
+
}
|
|
110
|
+
function parseNumeric(val) {
|
|
111
|
+
const num = parseFloat(val);
|
|
112
|
+
return isNaN(num) ? val : num;
|
|
113
|
+
}
|
|
114
|
+
|
|
115
|
+
function coordPair$1(node) {
|
|
116
|
+
const ll = [
|
|
117
|
+
parseFloat(node.getAttribute("lon") || ""),
|
|
118
|
+
parseFloat(node.getAttribute("lat") || ""),
|
|
119
|
+
];
|
|
120
|
+
num1(node, "ele", (val) => {
|
|
121
|
+
ll.push(val);
|
|
122
|
+
});
|
|
123
|
+
const time = get1(node, "time");
|
|
124
|
+
return {
|
|
125
|
+
coordinates: ll,
|
|
126
|
+
time: time ? nodeVal(time) : null,
|
|
127
|
+
extendedValues: getExtensions(get1(node, "extensions")),
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
function extractProperties(node) {
|
|
132
|
+
const properties = getMulti(node, [
|
|
133
|
+
"name",
|
|
134
|
+
"cmt",
|
|
135
|
+
"desc",
|
|
136
|
+
"type",
|
|
137
|
+
"time",
|
|
138
|
+
"keywords",
|
|
139
|
+
]);
|
|
140
|
+
const extensions = Array.from(node.getElementsByTagNameNS("http://www.garmin.com/xmlschemas/GpxExtensions/v3", "*"));
|
|
141
|
+
for (const child of extensions) {
|
|
142
|
+
if (child.parentNode?.parentNode === node) {
|
|
143
|
+
properties[child.tagName.replace(":", "_")] = nodeVal(child);
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
const links = $(node, "link");
|
|
147
|
+
if (links.length) {
|
|
148
|
+
properties.links = links.map((link) => Object.assign({ href: link.getAttribute("href") }, getMulti(link, ["text", "type"])));
|
|
149
|
+
}
|
|
150
|
+
return properties;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
function getPoints$1(node, pointname) {
|
|
154
|
+
const pts = $(node, pointname);
|
|
155
|
+
if (pts.length < 2)
|
|
156
|
+
return; // Invalid line in GeoJSON
|
|
157
|
+
const line = [];
|
|
158
|
+
const times = [];
|
|
159
|
+
const extendedValues = {};
|
|
160
|
+
for (let i = 0; i < pts.length; i++) {
|
|
161
|
+
const c = coordPair$1(pts[i]);
|
|
162
|
+
line.push(c.coordinates);
|
|
163
|
+
if (c.time)
|
|
164
|
+
times.push(c.time);
|
|
165
|
+
for (const [name, val] of c.extendedValues) {
|
|
166
|
+
const plural = name === "heart" ? name : name.replace("gpxtpx:", "") + "s";
|
|
167
|
+
if (!extendedValues[plural]) {
|
|
168
|
+
extendedValues[plural] = Array(pts.length).fill(null);
|
|
169
|
+
}
|
|
170
|
+
extendedValues[plural][i] = val;
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
return {
|
|
174
|
+
line: line,
|
|
175
|
+
times: times,
|
|
176
|
+
extendedValues: extendedValues,
|
|
177
|
+
};
|
|
178
|
+
}
|
|
179
|
+
function getRoute(node) {
|
|
180
|
+
const line = getPoints$1(node, "rtept");
|
|
181
|
+
if (!line)
|
|
182
|
+
return;
|
|
183
|
+
return {
|
|
184
|
+
type: "Feature",
|
|
185
|
+
properties: Object.assign({ _gpxType: "rte" }, extractProperties(node), getLineStyle(get1(node, "extensions"))),
|
|
186
|
+
geometry: {
|
|
187
|
+
type: "LineString",
|
|
188
|
+
coordinates: line.line,
|
|
189
|
+
},
|
|
190
|
+
};
|
|
191
|
+
}
|
|
192
|
+
function getTrack(node) {
|
|
193
|
+
const segments = $(node, "trkseg");
|
|
194
|
+
const track = [];
|
|
195
|
+
const times = [];
|
|
196
|
+
const extractedLines = [];
|
|
197
|
+
for (const segment of segments) {
|
|
198
|
+
const line = getPoints$1(segment, "trkpt");
|
|
199
|
+
if (line) {
|
|
200
|
+
extractedLines.push(line);
|
|
201
|
+
if (line.times && line.times.length)
|
|
202
|
+
times.push(line.times);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
if (extractedLines.length === 0)
|
|
206
|
+
return null;
|
|
207
|
+
const multi = extractedLines.length > 1;
|
|
208
|
+
const properties = Object.assign({ _gpxType: "trk" }, extractProperties(node), getLineStyle(get1(node, "extensions")), times.length
|
|
209
|
+
? {
|
|
210
|
+
coordinateProperties: {
|
|
211
|
+
times: multi ? times : times[0],
|
|
212
|
+
},
|
|
213
|
+
}
|
|
214
|
+
: {});
|
|
215
|
+
for (const line of extractedLines) {
|
|
216
|
+
track.push(line.line);
|
|
217
|
+
if (!properties.coordinateProperties) {
|
|
218
|
+
properties.coordinateProperties = {};
|
|
219
|
+
}
|
|
220
|
+
const props = properties.coordinateProperties;
|
|
221
|
+
const entries = Object.entries(line.extendedValues);
|
|
222
|
+
for (let i = 0; i < entries.length; i++) {
|
|
223
|
+
const [name, val] = entries[i];
|
|
224
|
+
if (multi) {
|
|
225
|
+
if (!props[name]) {
|
|
226
|
+
props[name] = extractedLines.map((line) => new Array(line.line.length).fill(null));
|
|
227
|
+
}
|
|
228
|
+
props[name][i] = val;
|
|
229
|
+
}
|
|
230
|
+
else {
|
|
231
|
+
props[name] = val;
|
|
232
|
+
}
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
return {
|
|
236
|
+
type: "Feature",
|
|
237
|
+
properties: properties,
|
|
238
|
+
geometry: multi
|
|
239
|
+
? {
|
|
240
|
+
type: "MultiLineString",
|
|
241
|
+
coordinates: track,
|
|
242
|
+
}
|
|
243
|
+
: {
|
|
244
|
+
type: "LineString",
|
|
245
|
+
coordinates: track[0],
|
|
246
|
+
},
|
|
247
|
+
};
|
|
248
|
+
}
|
|
249
|
+
function getPoint(node) {
|
|
250
|
+
const properties = Object.assign(extractProperties(node), getMulti(node, ["sym"]));
|
|
251
|
+
return {
|
|
252
|
+
type: "Feature",
|
|
253
|
+
properties,
|
|
254
|
+
geometry: {
|
|
255
|
+
type: "Point",
|
|
256
|
+
coordinates: coordPair$1(node).coordinates,
|
|
257
|
+
},
|
|
258
|
+
};
|
|
259
|
+
}
|
|
260
|
+
/**
|
|
261
|
+
* Convert GPX to GeoJSON incrementally, returning
|
|
262
|
+
* a [Generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators)
|
|
263
|
+
* that yields output feature by feature.
|
|
264
|
+
*/
|
|
265
|
+
function* gpxGen(node) {
|
|
266
|
+
for (const track of $(node, "trk")) {
|
|
267
|
+
const feature = getTrack(track);
|
|
268
|
+
if (feature)
|
|
269
|
+
yield feature;
|
|
270
|
+
}
|
|
271
|
+
for (const route of $(node, "rte")) {
|
|
272
|
+
const feature = getRoute(route);
|
|
273
|
+
if (feature)
|
|
274
|
+
yield feature;
|
|
275
|
+
}
|
|
276
|
+
for (const waypoint of $(node, "wpt")) {
|
|
277
|
+
yield getPoint(waypoint);
|
|
278
|
+
}
|
|
279
|
+
}
|
|
280
|
+
/**
|
|
281
|
+
*
|
|
282
|
+
* Convert a GPX document to GeoJSON. The first argument, `doc`, must be a GPX
|
|
283
|
+
* document as an XML DOM - not as a string. You can get this using jQuery's default
|
|
284
|
+
* `.ajax` function or using a bare XMLHttpRequest with the `.response` property
|
|
285
|
+
* holding an XML DOM.
|
|
286
|
+
*
|
|
287
|
+
* The output is a JavaScript object of GeoJSON data, same as `.kml` outputs, with the
|
|
288
|
+
* addition of a `_gpxType` property on each `LineString` feature that indicates whether
|
|
289
|
+
* the feature was encoded as a route (`rte`) or track (`trk`) in the GPX document.
|
|
290
|
+
*/
|
|
291
|
+
function gpx(node) {
|
|
292
|
+
return {
|
|
293
|
+
type: "FeatureCollection",
|
|
294
|
+
features: Array.from(gpxGen(node)),
|
|
295
|
+
};
|
|
296
|
+
}
|
|
297
|
+
|
|
298
|
+
const EXTENSIONS_NS = "http://www.garmin.com/xmlschemas/ActivityExtension/v2";
|
|
299
|
+
const TRACKPOINT_ATTRIBUTES = [
|
|
300
|
+
["heartRate", "heartRates"],
|
|
301
|
+
["Cadence", "cadences"],
|
|
302
|
+
// Extended Trackpoint attributes
|
|
303
|
+
["Speed", "speeds"],
|
|
304
|
+
["Watts", "watts"],
|
|
305
|
+
];
|
|
306
|
+
const LAP_ATTRIBUTES = [
|
|
307
|
+
["TotalTimeSeconds", "totalTimeSeconds"],
|
|
308
|
+
["DistanceMeters", "distanceMeters"],
|
|
309
|
+
["MaximumSpeed", "maxSpeed"],
|
|
310
|
+
["AverageHeartRateBpm", "avgHeartRate"],
|
|
311
|
+
["MaximumHeartRateBpm", "maxHeartRate"],
|
|
312
|
+
// Extended Lap attributes
|
|
313
|
+
["AvgSpeed", "avgSpeed"],
|
|
314
|
+
["AvgWatts", "avgWatts"],
|
|
315
|
+
["MaxWatts", "maxWatts"],
|
|
316
|
+
];
|
|
317
|
+
function getProperties(node, attributeNames) {
|
|
318
|
+
const properties = [];
|
|
319
|
+
for (const [tag, alias] of attributeNames) {
|
|
320
|
+
let elem = get1(node, tag);
|
|
321
|
+
if (!elem) {
|
|
322
|
+
const elements = node.getElementsByTagNameNS(EXTENSIONS_NS, tag);
|
|
323
|
+
if (elements.length) {
|
|
324
|
+
elem = elements[0];
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
const val = parseFloat(nodeVal(elem));
|
|
328
|
+
if (!isNaN(val)) {
|
|
329
|
+
properties.push([alias, val]);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
return properties;
|
|
333
|
+
}
|
|
334
|
+
function coordPair(node) {
|
|
335
|
+
const ll = [num1(node, "LongitudeDegrees"), num1(node, "LatitudeDegrees")];
|
|
336
|
+
if (ll[0] === undefined ||
|
|
337
|
+
isNaN(ll[0]) ||
|
|
338
|
+
ll[1] === undefined ||
|
|
339
|
+
isNaN(ll[1])) {
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
const heartRate = get1(node, "HeartRateBpm");
|
|
343
|
+
const time = nodeVal(get1(node, "Time"));
|
|
344
|
+
get1(node, "AltitudeMeters", (alt) => {
|
|
345
|
+
const a = parseFloat(nodeVal(alt));
|
|
346
|
+
if (!isNaN(a)) {
|
|
347
|
+
ll.push(a);
|
|
348
|
+
}
|
|
349
|
+
});
|
|
350
|
+
return {
|
|
351
|
+
coordinates: ll,
|
|
352
|
+
time: time || null,
|
|
353
|
+
heartRate: heartRate ? parseFloat(nodeVal(heartRate)) : null,
|
|
354
|
+
extensions: getProperties(node, TRACKPOINT_ATTRIBUTES),
|
|
355
|
+
};
|
|
356
|
+
}
|
|
357
|
+
function getPoints(node) {
|
|
358
|
+
const pts = $(node, "Trackpoint");
|
|
359
|
+
const line = [];
|
|
360
|
+
const times = [];
|
|
361
|
+
const heartRates = [];
|
|
362
|
+
if (pts.length < 2)
|
|
363
|
+
return null; // Invalid line in GeoJSON
|
|
364
|
+
const extendedProperties = {};
|
|
365
|
+
const result = { extendedProperties };
|
|
366
|
+
for (let i = 0; i < pts.length; i++) {
|
|
367
|
+
const c = coordPair(pts[i]);
|
|
368
|
+
if (c === null)
|
|
369
|
+
continue;
|
|
370
|
+
line.push(c.coordinates);
|
|
371
|
+
const { time, heartRate, extensions } = c;
|
|
372
|
+
if (time)
|
|
373
|
+
times.push(time);
|
|
374
|
+
if (heartRate)
|
|
375
|
+
heartRates.push(heartRate);
|
|
376
|
+
for (const [alias, value] of extensions) {
|
|
377
|
+
if (!extendedProperties[alias]) {
|
|
378
|
+
extendedProperties[alias] = Array(pts.length).fill(null);
|
|
379
|
+
}
|
|
380
|
+
extendedProperties[alias][i] = value;
|
|
381
|
+
}
|
|
382
|
+
}
|
|
383
|
+
return Object.assign(result, {
|
|
384
|
+
line: line,
|
|
385
|
+
times: times,
|
|
386
|
+
heartRates: heartRates,
|
|
387
|
+
});
|
|
388
|
+
}
|
|
389
|
+
function getLap(node) {
|
|
390
|
+
const segments = $(node, "Track");
|
|
391
|
+
const track = [];
|
|
392
|
+
const times = [];
|
|
393
|
+
const heartRates = [];
|
|
394
|
+
const allExtendedProperties = [];
|
|
395
|
+
let line;
|
|
396
|
+
const properties = Object.assign(Object.fromEntries(getProperties(node, LAP_ATTRIBUTES)), get(node, "Name", (nameElement) => {
|
|
397
|
+
return { name: nodeVal(nameElement) };
|
|
398
|
+
}));
|
|
399
|
+
for (const segment of segments) {
|
|
400
|
+
line = getPoints(segment);
|
|
401
|
+
if (line) {
|
|
402
|
+
track.push(line.line);
|
|
403
|
+
if (line.times.length)
|
|
404
|
+
times.push(line.times);
|
|
405
|
+
if (line.heartRates.length)
|
|
406
|
+
heartRates.push(line.heartRates);
|
|
407
|
+
allExtendedProperties.push(line.extendedProperties);
|
|
408
|
+
}
|
|
409
|
+
}
|
|
410
|
+
for (let i = 0; i < allExtendedProperties.length; i++) {
|
|
411
|
+
const extendedProperties = allExtendedProperties[i];
|
|
412
|
+
for (const property in extendedProperties) {
|
|
413
|
+
if (segments.length === 1) {
|
|
414
|
+
if (line) {
|
|
415
|
+
properties[property] = line.extendedProperties[property];
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
else {
|
|
419
|
+
if (!properties[property]) {
|
|
420
|
+
properties[property] = track.map((track) => Array(track.length).fill(null));
|
|
421
|
+
}
|
|
422
|
+
properties[property][i] = extendedProperties[property];
|
|
423
|
+
}
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
if (track.length === 0)
|
|
427
|
+
return null;
|
|
428
|
+
if (times.length || heartRates.length) {
|
|
429
|
+
properties.coordinateProperties = Object.assign(times.length
|
|
430
|
+
? {
|
|
431
|
+
times: track.length === 1 ? times[0] : times,
|
|
432
|
+
}
|
|
433
|
+
: {}, heartRates.length
|
|
434
|
+
? {
|
|
435
|
+
heart: track.length === 1 ? heartRates[0] : heartRates,
|
|
436
|
+
}
|
|
437
|
+
: {});
|
|
438
|
+
}
|
|
439
|
+
return {
|
|
440
|
+
type: "Feature",
|
|
441
|
+
properties: properties,
|
|
442
|
+
geometry: track.length === 1
|
|
443
|
+
? {
|
|
444
|
+
type: "LineString",
|
|
445
|
+
coordinates: track[0],
|
|
446
|
+
}
|
|
447
|
+
: {
|
|
448
|
+
type: "MultiLineString",
|
|
449
|
+
coordinates: track,
|
|
450
|
+
},
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
/**
|
|
454
|
+
* Incrementally convert a TCX document to GeoJSON. The
|
|
455
|
+
* first argument, `doc`, must be a TCX
|
|
456
|
+
* document as an XML DOM - not as a string.
|
|
457
|
+
*/
|
|
458
|
+
function* tcxGen(node) {
|
|
459
|
+
for (const lap of $(node, "Lap")) {
|
|
460
|
+
const feature = getLap(lap);
|
|
461
|
+
if (feature)
|
|
462
|
+
yield feature;
|
|
463
|
+
}
|
|
464
|
+
for (const course of $(node, "Courses")) {
|
|
465
|
+
const feature = getLap(course);
|
|
466
|
+
if (feature)
|
|
467
|
+
yield feature;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
/**
|
|
471
|
+
* Convert a TCX document to GeoJSON. The first argument, `doc`, must be a TCX
|
|
472
|
+
* document as an XML DOM - not as a string.
|
|
473
|
+
*/
|
|
474
|
+
function tcx(node) {
|
|
475
|
+
return {
|
|
476
|
+
type: "FeatureCollection",
|
|
477
|
+
features: Array.from(tcxGen(node)),
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
function fixColor(v, prefix) {
|
|
482
|
+
const properties = {};
|
|
483
|
+
const colorProp = prefix == "stroke" || prefix === "fill" ? prefix : prefix + "-color";
|
|
484
|
+
if (v[0] === "#") {
|
|
485
|
+
v = v.substring(1);
|
|
486
|
+
}
|
|
487
|
+
if (v.length === 6 || v.length === 3) {
|
|
488
|
+
properties[colorProp] = "#" + v;
|
|
489
|
+
}
|
|
490
|
+
else if (v.length === 8) {
|
|
491
|
+
properties[prefix + "-opacity"] = parseInt(v.substring(0, 2), 16) / 255;
|
|
492
|
+
properties[colorProp] =
|
|
493
|
+
"#" + v.substring(6, 8) + v.substring(4, 6) + v.substring(2, 4);
|
|
494
|
+
}
|
|
495
|
+
return properties;
|
|
496
|
+
}
|
|
497
|
+
|
|
498
|
+
function numericProperty(node, source, target) {
|
|
499
|
+
const properties = {};
|
|
500
|
+
num1(node, source, (val) => {
|
|
501
|
+
properties[target] = val;
|
|
502
|
+
});
|
|
503
|
+
return properties;
|
|
504
|
+
}
|
|
505
|
+
function getColor(node, output) {
|
|
506
|
+
return get(node, "color", (elem) => fixColor(nodeVal(elem), output));
|
|
507
|
+
}
|
|
508
|
+
function extractIcon(node) {
|
|
509
|
+
return get(node, "IconStyle", (iconStyle) => {
|
|
510
|
+
return Object.assign(getColor(node, "icon"), numericProperty(iconStyle, "scale", "icon-scale"), numericProperty(iconStyle, "heading", "icon-heading"), get(iconStyle, "hotSpot", (hotspot) => {
|
|
511
|
+
const left = parseFloat(hotspot.getAttribute("x") || "");
|
|
512
|
+
const top = parseFloat(hotspot.getAttribute("y") || "");
|
|
513
|
+
if (!isNaN(left) && !isNaN(top))
|
|
514
|
+
return { "icon-offset": [left, top] };
|
|
515
|
+
return {};
|
|
516
|
+
}), get(iconStyle, "Icon", (icon, properties) => {
|
|
517
|
+
val1(icon, "href", (href) => {
|
|
518
|
+
properties.icon = href;
|
|
519
|
+
});
|
|
520
|
+
return properties;
|
|
521
|
+
}));
|
|
522
|
+
});
|
|
523
|
+
}
|
|
524
|
+
function extractLabel(node) {
|
|
525
|
+
return get(node, "LabelStyle", (labelStyle) => {
|
|
526
|
+
return Object.assign(getColor(node, "label"), numericProperty(labelStyle, "scale", "label-scale"));
|
|
527
|
+
});
|
|
528
|
+
}
|
|
529
|
+
function extractLine(node) {
|
|
530
|
+
return get(node, "LineStyle", (lineStyle) => {
|
|
531
|
+
return Object.assign(getColor(node, "stroke"), numericProperty(lineStyle, "width", "stroke-width"));
|
|
532
|
+
});
|
|
533
|
+
}
|
|
534
|
+
function extractPoly(node) {
|
|
535
|
+
return get(node, "PolyStyle", (polyStyle, properties) => {
|
|
536
|
+
return Object.assign(properties, get(polyStyle, "color", (elem) => fixColor(nodeVal(elem), "fill")), val1(polyStyle, "fill", (fill) => {
|
|
537
|
+
if (fill === "0")
|
|
538
|
+
return { "fill-opacity": 0 };
|
|
539
|
+
}), val1(polyStyle, "outline", (outline) => {
|
|
540
|
+
if (outline === "0")
|
|
541
|
+
return { "stroke-opacity": 0 };
|
|
542
|
+
}));
|
|
543
|
+
});
|
|
544
|
+
}
|
|
545
|
+
function extractStyle(node) {
|
|
546
|
+
return Object.assign({}, extractPoly(node), extractLine(node), extractLabel(node), extractIcon(node));
|
|
547
|
+
}
|
|
548
|
+
|
|
549
|
+
const removeSpace = /\s*/g;
|
|
550
|
+
const trimSpace = /^\s*|\s*$/g;
|
|
551
|
+
const splitSpace = /\s+/;
|
|
552
|
+
/**
|
|
553
|
+
* Get one coordinate from a coordinate array, if any
|
|
554
|
+
*/
|
|
555
|
+
function coord1(value) {
|
|
556
|
+
return value
|
|
557
|
+
.replace(removeSpace, "")
|
|
558
|
+
.split(",")
|
|
559
|
+
.map(parseFloat)
|
|
560
|
+
.filter((num) => !isNaN(num));
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Get all coordinates from a coordinate array as [[],[]]
|
|
564
|
+
*/
|
|
565
|
+
function coord(value) {
|
|
566
|
+
return value
|
|
567
|
+
.replace(trimSpace, "")
|
|
568
|
+
.split(splitSpace)
|
|
569
|
+
.map(coord1)
|
|
570
|
+
.filter((coord) => {
|
|
571
|
+
return coord.length >= 2;
|
|
572
|
+
});
|
|
573
|
+
}
|
|
574
|
+
function gxCoords(node) {
|
|
575
|
+
let elems = $(node, "coord");
|
|
576
|
+
if (elems.length === 0) {
|
|
577
|
+
elems = $ns(node, "coord", "*");
|
|
578
|
+
}
|
|
579
|
+
const coordinates = elems.map((elem) => {
|
|
580
|
+
return nodeVal(elem).split(" ").map(parseFloat);
|
|
581
|
+
});
|
|
582
|
+
if (coordinates.length === 0) {
|
|
583
|
+
return null;
|
|
584
|
+
}
|
|
585
|
+
return {
|
|
586
|
+
geometry: coordinates.length > 2
|
|
587
|
+
? {
|
|
588
|
+
type: "LineString",
|
|
589
|
+
coordinates,
|
|
590
|
+
}
|
|
591
|
+
: {
|
|
592
|
+
type: "Point",
|
|
593
|
+
coordinates: coordinates[0],
|
|
594
|
+
},
|
|
595
|
+
times: $(node, "when").map((elem) => nodeVal(elem)),
|
|
596
|
+
};
|
|
597
|
+
}
|
|
598
|
+
function fixRing(ring) {
|
|
599
|
+
if (ring.length === 0)
|
|
600
|
+
return ring;
|
|
601
|
+
const first = ring[0];
|
|
602
|
+
const last = ring[ring.length - 1];
|
|
603
|
+
let equal = true;
|
|
604
|
+
for (let i = 0; i < Math.max(first.length, last.length); i++) {
|
|
605
|
+
if (first[i] !== last[i]) {
|
|
606
|
+
equal = false;
|
|
607
|
+
break;
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
if (!equal) {
|
|
611
|
+
return ring.concat([ring[0]]);
|
|
612
|
+
}
|
|
613
|
+
return ring;
|
|
614
|
+
}
|
|
615
|
+
const GEO_TYPES = [
|
|
616
|
+
"Polygon",
|
|
617
|
+
"LineString",
|
|
618
|
+
"Point",
|
|
619
|
+
"Track",
|
|
620
|
+
"gx:Track",
|
|
621
|
+
];
|
|
622
|
+
function getCoordinates(node) {
|
|
623
|
+
return nodeVal(get1(node, "coordinates"));
|
|
624
|
+
}
|
|
625
|
+
function getGeometry(node) {
|
|
626
|
+
const geometries = [];
|
|
627
|
+
const coordTimes = [];
|
|
628
|
+
for (const t of ["MultiGeometry", "MultiTrack", "gx:MultiTrack"]) {
|
|
629
|
+
const elem = get1(node, t);
|
|
630
|
+
if (elem) {
|
|
631
|
+
return getGeometry(elem);
|
|
632
|
+
}
|
|
633
|
+
}
|
|
634
|
+
for (const geoType of GEO_TYPES) {
|
|
635
|
+
for (const geomNode of $(node, geoType)) {
|
|
636
|
+
switch (geoType) {
|
|
637
|
+
case "Point": {
|
|
638
|
+
const coordinates = coord1(getCoordinates(geomNode));
|
|
639
|
+
if (coordinates.length >= 2) {
|
|
640
|
+
geometries.push({
|
|
641
|
+
type: "Point",
|
|
642
|
+
coordinates,
|
|
643
|
+
});
|
|
644
|
+
}
|
|
645
|
+
break;
|
|
646
|
+
}
|
|
647
|
+
case "LineString": {
|
|
648
|
+
const coordinates = coord(getCoordinates(geomNode));
|
|
649
|
+
if (coordinates.length >= 2) {
|
|
650
|
+
geometries.push({
|
|
651
|
+
type: "LineString",
|
|
652
|
+
coordinates,
|
|
653
|
+
});
|
|
654
|
+
}
|
|
655
|
+
break;
|
|
656
|
+
}
|
|
657
|
+
case "Polygon": {
|
|
658
|
+
const coords = [];
|
|
659
|
+
for (const linearRing of $(geomNode, "LinearRing")) {
|
|
660
|
+
const ring = fixRing(coord(getCoordinates(linearRing)));
|
|
661
|
+
if (ring.length >= 4) {
|
|
662
|
+
coords.push(ring);
|
|
663
|
+
}
|
|
664
|
+
}
|
|
665
|
+
if (coords.length) {
|
|
666
|
+
geometries.push({
|
|
667
|
+
type: "Polygon",
|
|
668
|
+
coordinates: coords,
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
break;
|
|
672
|
+
}
|
|
673
|
+
case "Track":
|
|
674
|
+
case "gx:Track": {
|
|
675
|
+
const gx = gxCoords(geomNode);
|
|
676
|
+
if (!gx)
|
|
677
|
+
break;
|
|
678
|
+
const { times, geometry } = gx;
|
|
679
|
+
geometries.push(geometry);
|
|
680
|
+
if (times.length)
|
|
681
|
+
coordTimes.push(times);
|
|
682
|
+
break;
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
}
|
|
686
|
+
}
|
|
687
|
+
return {
|
|
688
|
+
geometries,
|
|
689
|
+
coordTimes,
|
|
690
|
+
};
|
|
691
|
+
}
|
|
692
|
+
|
|
693
|
+
function extractExtendedData(node) {
|
|
694
|
+
return get(node, "ExtendedData", (extendedData, properties) => {
|
|
695
|
+
for (const data of $(extendedData, "Data")) {
|
|
696
|
+
properties[data.getAttribute("name") || ""] = nodeVal(get1(data, "value"));
|
|
697
|
+
}
|
|
698
|
+
for (const simpleData of $(extendedData, "SimpleData")) {
|
|
699
|
+
properties[simpleData.getAttribute("name") || ""] = nodeVal(simpleData);
|
|
700
|
+
}
|
|
701
|
+
return properties;
|
|
702
|
+
});
|
|
703
|
+
}
|
|
704
|
+
function geometryListToGeometry(geometries) {
|
|
705
|
+
return geometries.length === 0
|
|
706
|
+
? null
|
|
707
|
+
: geometries.length === 1
|
|
708
|
+
? geometries[0]
|
|
709
|
+
: {
|
|
710
|
+
type: "GeometryCollection",
|
|
711
|
+
geometries,
|
|
712
|
+
};
|
|
713
|
+
}
|
|
714
|
+
function extractTimeSpan(node) {
|
|
715
|
+
return get(node, "TimeSpan", (timeSpan) => {
|
|
716
|
+
return {
|
|
717
|
+
timespan: {
|
|
718
|
+
begin: nodeVal(get1(timeSpan, "begin")),
|
|
719
|
+
end: nodeVal(get1(timeSpan, "end")),
|
|
720
|
+
},
|
|
721
|
+
};
|
|
722
|
+
});
|
|
723
|
+
}
|
|
724
|
+
function extractTimeStamp(node) {
|
|
725
|
+
return get(node, "TimeStamp", (timeStamp) => {
|
|
726
|
+
return { timestamp: nodeVal(get1(timeStamp, "when")) };
|
|
727
|
+
});
|
|
728
|
+
}
|
|
729
|
+
function extractCascadedStyle(node, styleMap) {
|
|
730
|
+
return val1(node, "styleUrl", (styleUrl) => {
|
|
731
|
+
styleUrl = normalizeId(styleUrl);
|
|
732
|
+
if (styleMap[styleUrl]) {
|
|
733
|
+
return Object.assign({ styleUrl }, styleMap[styleUrl]);
|
|
734
|
+
}
|
|
735
|
+
// For backward-compatibility. Should we still include
|
|
736
|
+
// styleUrl even if it's not resolved?
|
|
737
|
+
return { styleUrl };
|
|
738
|
+
});
|
|
739
|
+
}
|
|
740
|
+
function getPlacemark(node, styleMap) {
|
|
741
|
+
const { coordTimes, geometries } = getGeometry(node);
|
|
742
|
+
const feature = {
|
|
743
|
+
type: "Feature",
|
|
744
|
+
geometry: geometryListToGeometry(geometries),
|
|
745
|
+
properties: Object.assign(getMulti(node, [
|
|
746
|
+
"name",
|
|
747
|
+
"address",
|
|
748
|
+
"visibility",
|
|
749
|
+
"open",
|
|
750
|
+
"phoneNumber",
|
|
751
|
+
"description",
|
|
752
|
+
]), extractCascadedStyle(node, styleMap), extractStyle(node), extractExtendedData(node), extractTimeSpan(node), extractTimeStamp(node), coordTimes.length
|
|
753
|
+
? {
|
|
754
|
+
coordinateProperties: {
|
|
755
|
+
times: coordTimes.length === 1 ? coordTimes[0] : coordTimes,
|
|
756
|
+
},
|
|
757
|
+
}
|
|
758
|
+
: {}),
|
|
759
|
+
};
|
|
760
|
+
const id = node.getAttribute("id");
|
|
761
|
+
if (id !== null && id !== "")
|
|
762
|
+
feature.id = id;
|
|
763
|
+
return feature;
|
|
764
|
+
}
|
|
765
|
+
|
|
766
|
+
function getStyleId(style) {
|
|
767
|
+
let id = style.getAttribute("id");
|
|
768
|
+
const parentNode = style.parentNode;
|
|
769
|
+
if (!id &&
|
|
770
|
+
isElement(parentNode) &&
|
|
771
|
+
parentNode.localName === "CascadingStyle") {
|
|
772
|
+
id = parentNode.getAttribute("kml:id") || parentNode.getAttribute("id");
|
|
773
|
+
}
|
|
774
|
+
return normalizeId(id || "");
|
|
775
|
+
}
|
|
776
|
+
function buildStyleMap(node) {
|
|
777
|
+
const styleMap = {};
|
|
778
|
+
for (const style of $(node, "Style")) {
|
|
779
|
+
styleMap[getStyleId(style)] = extractStyle(style);
|
|
780
|
+
}
|
|
781
|
+
for (const map of $(node, "StyleMap")) {
|
|
782
|
+
const id = normalizeId(map.getAttribute("id") || "");
|
|
783
|
+
val1(map, "styleUrl", (styleUrl) => {
|
|
784
|
+
styleUrl = normalizeId(styleUrl);
|
|
785
|
+
if (styleMap[styleUrl]) {
|
|
786
|
+
styleMap[id] = styleMap[styleUrl];
|
|
787
|
+
}
|
|
788
|
+
});
|
|
789
|
+
}
|
|
790
|
+
return styleMap;
|
|
791
|
+
}
|
|
792
|
+
const FOLDER_PROPS = [
|
|
793
|
+
"name",
|
|
794
|
+
"visibility",
|
|
795
|
+
"open",
|
|
796
|
+
"address",
|
|
797
|
+
"description",
|
|
798
|
+
"phoneNumber",
|
|
799
|
+
"visibility",
|
|
800
|
+
];
|
|
801
|
+
function getFolder(node) {
|
|
802
|
+
const meta = {};
|
|
803
|
+
for (const child of Array.from(node.childNodes)) {
|
|
804
|
+
if (isElement(child) && FOLDER_PROPS.includes(child.tagName)) {
|
|
805
|
+
meta[child.tagName] = nodeVal(child);
|
|
806
|
+
}
|
|
807
|
+
}
|
|
808
|
+
return {
|
|
809
|
+
type: "folder",
|
|
810
|
+
meta,
|
|
811
|
+
children: [],
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
/**
|
|
815
|
+
* Yield a nested tree with KML folder structure
|
|
816
|
+
*
|
|
817
|
+
* This generates a tree with the given structure:
|
|
818
|
+
*
|
|
819
|
+
* ```js
|
|
820
|
+
* {
|
|
821
|
+
* "type": "root",
|
|
822
|
+
* "children": [
|
|
823
|
+
* {
|
|
824
|
+
* "type": "folder",
|
|
825
|
+
* "meta": {
|
|
826
|
+
* "name": "Test"
|
|
827
|
+
* },
|
|
828
|
+
* "children": [
|
|
829
|
+
* // ...features and folders
|
|
830
|
+
* ]
|
|
831
|
+
* }
|
|
832
|
+
* // ...features
|
|
833
|
+
* ]
|
|
834
|
+
* }
|
|
835
|
+
* ```
|
|
836
|
+
*/
|
|
837
|
+
function kmlWithFolders(node) {
|
|
838
|
+
const styleMap = buildStyleMap(node);
|
|
839
|
+
const tree = { type: "root", children: [] };
|
|
840
|
+
function traverse(node, pointer) {
|
|
841
|
+
if (node.childNodes) {
|
|
842
|
+
for (let i = 0; i < node.childNodes.length; i++) {
|
|
843
|
+
traverse(node.childNodes[i], pointer);
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
if (!isElement(node))
|
|
847
|
+
return;
|
|
848
|
+
switch (node.tagName) {
|
|
849
|
+
case "Placemark": {
|
|
850
|
+
const placemark = getPlacemark(node, styleMap);
|
|
851
|
+
if (placemark) {
|
|
852
|
+
pointer.children.push(placemark);
|
|
853
|
+
}
|
|
854
|
+
break;
|
|
855
|
+
}
|
|
856
|
+
case "Folder": {
|
|
857
|
+
const folder = getFolder(node);
|
|
858
|
+
pointer.children.push(folder);
|
|
859
|
+
pointer = folder;
|
|
860
|
+
break;
|
|
861
|
+
}
|
|
862
|
+
}
|
|
863
|
+
}
|
|
864
|
+
traverse(node, tree);
|
|
865
|
+
return tree;
|
|
866
|
+
}
|
|
867
|
+
/**
|
|
868
|
+
* Convert KML to GeoJSON incrementally, returning
|
|
869
|
+
* a [Generator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Guide/Iterators_and_Generators)
|
|
870
|
+
* that yields output feature by feature.
|
|
871
|
+
*/
|
|
872
|
+
function* kmlGen(node) {
|
|
873
|
+
const styleMap = buildStyleMap(node);
|
|
874
|
+
for (const placemark of $(node, "Placemark")) {
|
|
875
|
+
const feature = getPlacemark(placemark, styleMap);
|
|
876
|
+
if (feature)
|
|
877
|
+
yield feature;
|
|
878
|
+
}
|
|
879
|
+
}
|
|
880
|
+
/**
|
|
881
|
+
* Convert a KML document to GeoJSON. The first argument, `doc`, must be a KML
|
|
882
|
+
* document as an XML DOM - not as a string. You can get this using jQuery's default
|
|
883
|
+
* `.ajax` function or using a bare XMLHttpRequest with the `.response` property
|
|
884
|
+
* holding an XML DOM.
|
|
885
|
+
*
|
|
886
|
+
* The output is a JavaScript object of GeoJSON data. You can convert it to a string
|
|
887
|
+
* with [JSON.stringify](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify)
|
|
888
|
+
* or use it directly in libraries.
|
|
889
|
+
*/
|
|
890
|
+
function kml(node) {
|
|
891
|
+
return {
|
|
892
|
+
type: "FeatureCollection",
|
|
893
|
+
features: Array.from(kmlGen(node)),
|
|
894
|
+
};
|
|
895
|
+
}
|
|
896
|
+
|
|
897
|
+
export { gpx, gpxGen, kml, kmlGen, kmlWithFolders, tcx, tcxGen };
|
|
2
898
|
//# sourceMappingURL=togeojson.es.js.map
|