@tmcw/togeojson 4.6.1 → 5.0.0-0
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 +18 -0
- package/README.md +2 -60
- package/dist/gpx/coord_pair.d.ts +9 -0
- package/dist/gpx/extensions.d.ts +2 -0
- package/dist/gpx/line.d.ts +3 -0
- package/dist/gpx/properties.d.ts +3 -0
- package/dist/gpx.d.ts +19 -0
- package/dist/index.d.ts +3 -0
- package/dist/kml/extractStyle.d.ts +13 -0
- package/dist/kml/fixColor.d.ts +2 -0
- package/dist/kml/geometry.d.ts +16 -0
- package/dist/kml/placemark.d.ts +3 -0
- package/dist/kml.d.ts +61 -0
- package/dist/shared.d.ts +27 -0
- package/dist/tcx.d.ts +12 -0
- package/dist/togeojson.cjs +748 -717
- package/dist/togeojson.cjs.map +1 -1
- package/dist/togeojson.es.js +1 -776
- package/dist/togeojson.es.js.map +1 -1
- package/dist/togeojson.js +1 -785
- package/dist/togeojson.js.map +1 -1
- package/dist/togeojson.modern.js +2 -0
- package/dist/togeojson.modern.js.map +1 -0
- package/dist/togeojson.umd.js +1 -1
- package/dist/togeojson.umd.js.map +1 -1
- package/package.json +21 -10
package/dist/togeojson.js
CHANGED
|
@@ -1,786 +1,2 @@
|
|
|
1
|
-
'use strict';
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, '__esModule', { value: true });
|
|
4
|
-
|
|
5
|
-
// cast array x into numbers
|
|
6
|
-
// get the content of a text node, if any
|
|
7
|
-
function nodeVal(x) {
|
|
8
|
-
if (x && x.normalize) {
|
|
9
|
-
x.normalize();
|
|
10
|
-
}
|
|
11
|
-
return (x && x.textContent) || "";
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
// one Y child of X, if any, otherwise null
|
|
15
|
-
function get1(x, y) {
|
|
16
|
-
const n = x.getElementsByTagName(y);
|
|
17
|
-
return n.length ? n[0] : null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
function getLineStyle(extensions) {
|
|
21
|
-
const style = {};
|
|
22
|
-
if (extensions) {
|
|
23
|
-
const lineStyle = get1(extensions, "line");
|
|
24
|
-
if (lineStyle) {
|
|
25
|
-
const color = nodeVal(get1(lineStyle, "color")),
|
|
26
|
-
opacity = parseFloat(nodeVal(get1(lineStyle, "opacity"))),
|
|
27
|
-
width = parseFloat(nodeVal(get1(lineStyle, "width")));
|
|
28
|
-
if (color) style.stroke = color;
|
|
29
|
-
if (!isNaN(opacity)) style["stroke-opacity"] = opacity;
|
|
30
|
-
// GPX width is in mm, convert to px with 96 px per inch
|
|
31
|
-
if (!isNaN(width)) style["stroke-width"] = (width * 96) / 25.4;
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
return style;
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
function getExtensions(node) {
|
|
38
|
-
let values = [];
|
|
39
|
-
if (node !== null) {
|
|
40
|
-
for (let i = 0; i < node.childNodes.length; i++) {
|
|
41
|
-
const child = node.childNodes[i];
|
|
42
|
-
if (child.nodeType !== 1) continue;
|
|
43
|
-
const name = ["heart", "gpxtpx:hr", "hr"].includes(child.nodeName)
|
|
44
|
-
? "heart"
|
|
45
|
-
: child.nodeName;
|
|
46
|
-
if (name === "gpxtpx:TrackPointExtension") {
|
|
47
|
-
// loop again for nested garmin extensions (eg. "gpxtpx:hr")
|
|
48
|
-
values = values.concat(getExtensions(child));
|
|
49
|
-
} else {
|
|
50
|
-
// push custom extension (eg. "power")
|
|
51
|
-
const val = nodeVal(child);
|
|
52
|
-
values.push([name, isNaN(val) ? val : parseFloat(val)]);
|
|
53
|
-
}
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
return values;
|
|
57
|
-
}
|
|
58
|
-
|
|
59
|
-
function getMulti(x, ys) {
|
|
60
|
-
const o = {};
|
|
61
|
-
let n;
|
|
62
|
-
let k;
|
|
63
|
-
for (k = 0; k < ys.length; k++) {
|
|
64
|
-
n = get1(x, ys[k]);
|
|
65
|
-
if (n) o[ys[k]] = nodeVal(n);
|
|
66
|
-
}
|
|
67
|
-
return o;
|
|
68
|
-
}
|
|
69
|
-
function getProperties$1(node) {
|
|
70
|
-
const prop = getMulti(node, [
|
|
71
|
-
"name",
|
|
72
|
-
"cmt",
|
|
73
|
-
"desc",
|
|
74
|
-
"type",
|
|
75
|
-
"time",
|
|
76
|
-
"keywords",
|
|
77
|
-
]);
|
|
78
|
-
// Parse additional data from our Garmin extension(s)
|
|
79
|
-
const extensions = node.getElementsByTagNameNS(
|
|
80
|
-
"http://www.garmin.com/xmlschemas/GpxExtensions/v3",
|
|
81
|
-
"*"
|
|
82
|
-
);
|
|
83
|
-
for (let i = 0; i < extensions.length; i++) {
|
|
84
|
-
const extension = extensions[i];
|
|
85
|
-
// Ignore nested extensions, like those on routepoints or trackpoints
|
|
86
|
-
if (extension.parentNode.parentNode === node) {
|
|
87
|
-
prop[extension.tagName.replace(":", "_")] = nodeVal(extension);
|
|
88
|
-
}
|
|
89
|
-
}
|
|
90
|
-
const links = node.getElementsByTagName("link");
|
|
91
|
-
if (links.length) prop.links = [];
|
|
92
|
-
for (let i = 0; i < links.length; i++) {
|
|
93
|
-
prop.links.push(
|
|
94
|
-
Object.assign(
|
|
95
|
-
{ href: links[i].getAttribute("href") },
|
|
96
|
-
getMulti(links[i], ["text", "type"])
|
|
97
|
-
)
|
|
98
|
-
);
|
|
99
|
-
}
|
|
100
|
-
return prop;
|
|
101
|
-
}
|
|
102
|
-
|
|
103
|
-
function coordPair$1(x) {
|
|
104
|
-
const ll = [
|
|
105
|
-
parseFloat(x.getAttribute("lon")),
|
|
106
|
-
parseFloat(x.getAttribute("lat")),
|
|
107
|
-
];
|
|
108
|
-
const ele = get1(x, "ele");
|
|
109
|
-
const time = get1(x, "time");
|
|
110
|
-
if (ele) {
|
|
111
|
-
const e = parseFloat(nodeVal(ele));
|
|
112
|
-
if (!isNaN(e)) {
|
|
113
|
-
ll.push(e);
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
return {
|
|
118
|
-
coordinates: ll,
|
|
119
|
-
time: time ? nodeVal(time) : null,
|
|
120
|
-
extendedValues: getExtensions(get1(x, "extensions")),
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
function getRoute(node) {
|
|
124
|
-
const line = getPoints$1(node, "rtept");
|
|
125
|
-
if (!line) return;
|
|
126
|
-
return {
|
|
127
|
-
type: "Feature",
|
|
128
|
-
properties: Object.assign(
|
|
129
|
-
getProperties$1(node),
|
|
130
|
-
getLineStyle(get1(node, "extensions")),
|
|
131
|
-
{ _gpxType: "rte" }
|
|
132
|
-
),
|
|
133
|
-
geometry: {
|
|
134
|
-
type: "LineString",
|
|
135
|
-
coordinates: line.line,
|
|
136
|
-
},
|
|
137
|
-
};
|
|
138
|
-
}
|
|
139
|
-
|
|
140
|
-
function getPoints$1(node, pointname) {
|
|
141
|
-
const pts = node.getElementsByTagName(pointname);
|
|
142
|
-
if (pts.length < 2) return; // Invalid line in GeoJSON
|
|
143
|
-
|
|
144
|
-
const line = [];
|
|
145
|
-
const times = [];
|
|
146
|
-
const extendedValues = {};
|
|
147
|
-
for (let i = 0; i < pts.length; i++) {
|
|
148
|
-
const c = coordPair$1(pts[i]);
|
|
149
|
-
line.push(c.coordinates);
|
|
150
|
-
if (c.time) times.push(c.time);
|
|
151
|
-
for (let j = 0; j < c.extendedValues.length; j++) {
|
|
152
|
-
const [name, val] = c.extendedValues[j];
|
|
153
|
-
const plural =
|
|
154
|
-
name === "heart" ? name : name.replace("gpxtpx:", "") + "s";
|
|
155
|
-
if (!extendedValues[plural]) {
|
|
156
|
-
extendedValues[plural] = Array(pts.length).fill(null);
|
|
157
|
-
}
|
|
158
|
-
extendedValues[plural][i] = val;
|
|
159
|
-
}
|
|
160
|
-
}
|
|
161
|
-
return {
|
|
162
|
-
line: line,
|
|
163
|
-
times: times,
|
|
164
|
-
extendedValues: extendedValues,
|
|
165
|
-
};
|
|
166
|
-
}
|
|
167
|
-
|
|
168
|
-
function getTrack(node) {
|
|
169
|
-
const segments = node.getElementsByTagName("trkseg");
|
|
170
|
-
const track = [];
|
|
171
|
-
const times = [];
|
|
172
|
-
const extractedLines = [];
|
|
173
|
-
|
|
174
|
-
for (let i = 0; i < segments.length; i++) {
|
|
175
|
-
const line = getPoints$1(segments[i], "trkpt");
|
|
176
|
-
if (line) {
|
|
177
|
-
extractedLines.push(line);
|
|
178
|
-
if (line.times && line.times.length) times.push(line.times);
|
|
179
|
-
}
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
if (extractedLines.length === 0) return;
|
|
183
|
-
|
|
184
|
-
const multi = extractedLines.length > 1;
|
|
185
|
-
|
|
186
|
-
const properties = Object.assign(
|
|
187
|
-
getProperties$1(node),
|
|
188
|
-
getLineStyle(get1(node, "extensions")),
|
|
189
|
-
{ _gpxType: "trk" },
|
|
190
|
-
times.length
|
|
191
|
-
? {
|
|
192
|
-
coordinateProperties: {
|
|
193
|
-
times: multi ? times : times[0],
|
|
194
|
-
},
|
|
195
|
-
}
|
|
196
|
-
: {}
|
|
197
|
-
);
|
|
198
|
-
|
|
199
|
-
for (let i = 0; i < extractedLines.length; i++) {
|
|
200
|
-
const line = extractedLines[i];
|
|
201
|
-
track.push(line.line);
|
|
202
|
-
for (const [name, val] of Object.entries(line.extendedValues)) {
|
|
203
|
-
if (!properties.coordinateProperties) {
|
|
204
|
-
properties.coordinateProperties = {};
|
|
205
|
-
}
|
|
206
|
-
const props = properties.coordinateProperties;
|
|
207
|
-
if (multi) {
|
|
208
|
-
if (!props[name])
|
|
209
|
-
props[name] = extractedLines.map((line) =>
|
|
210
|
-
new Array(line.line.length).fill(null)
|
|
211
|
-
);
|
|
212
|
-
props[name][i] = val;
|
|
213
|
-
} else {
|
|
214
|
-
props[name] = val;
|
|
215
|
-
}
|
|
216
|
-
}
|
|
217
|
-
}
|
|
218
|
-
|
|
219
|
-
return {
|
|
220
|
-
type: "Feature",
|
|
221
|
-
properties: properties,
|
|
222
|
-
geometry: multi
|
|
223
|
-
? {
|
|
224
|
-
type: "MultiLineString",
|
|
225
|
-
coordinates: track,
|
|
226
|
-
}
|
|
227
|
-
: {
|
|
228
|
-
type: "LineString",
|
|
229
|
-
coordinates: track[0],
|
|
230
|
-
},
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
function getPoint(node) {
|
|
235
|
-
return {
|
|
236
|
-
type: "Feature",
|
|
237
|
-
properties: Object.assign(getProperties$1(node), getMulti(node, ["sym"])),
|
|
238
|
-
geometry: {
|
|
239
|
-
type: "Point",
|
|
240
|
-
coordinates: coordPair$1(node).coordinates,
|
|
241
|
-
},
|
|
242
|
-
};
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
function* gpxGen(doc) {
|
|
246
|
-
const tracks = doc.getElementsByTagName("trk");
|
|
247
|
-
const routes = doc.getElementsByTagName("rte");
|
|
248
|
-
const waypoints = doc.getElementsByTagName("wpt");
|
|
249
|
-
|
|
250
|
-
for (let i = 0; i < tracks.length; i++) {
|
|
251
|
-
const feature = getTrack(tracks[i]);
|
|
252
|
-
if (feature) yield feature;
|
|
253
|
-
}
|
|
254
|
-
for (let i = 0; i < routes.length; i++) {
|
|
255
|
-
const feature = getRoute(routes[i]);
|
|
256
|
-
if (feature) yield feature;
|
|
257
|
-
}
|
|
258
|
-
for (let i = 0; i < waypoints.length; i++) {
|
|
259
|
-
yield getPoint(waypoints[i]);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
function gpx(doc) {
|
|
264
|
-
return {
|
|
265
|
-
type: "FeatureCollection",
|
|
266
|
-
features: Array.from(gpxGen(doc)),
|
|
267
|
-
};
|
|
268
|
-
}
|
|
269
|
-
|
|
270
|
-
const EXTENSIONS_NS = "http://www.garmin.com/xmlschemas/ActivityExtension/v2";
|
|
271
|
-
|
|
272
|
-
const TRACKPOINT_ATTRIBUTES = [
|
|
273
|
-
["heartRate", "heartRates"],
|
|
274
|
-
["Cadence", "cadences"],
|
|
275
|
-
// Extended Trackpoint attributes
|
|
276
|
-
["Speed", "speeds"],
|
|
277
|
-
["Watts", "watts"],
|
|
278
|
-
];
|
|
279
|
-
|
|
280
|
-
const LAP_ATTRIBUTES = [
|
|
281
|
-
["TotalTimeSeconds", "totalTimeSeconds"],
|
|
282
|
-
["DistanceMeters", "distanceMeters"],
|
|
283
|
-
["MaximumSpeed", "maxSpeed"],
|
|
284
|
-
["AverageHeartRateBpm", "avgHeartRate"],
|
|
285
|
-
["MaximumHeartRateBpm", "maxHeartRate"],
|
|
286
|
-
|
|
287
|
-
// Extended Lap attributes
|
|
288
|
-
["AvgSpeed", "avgSpeed"],
|
|
289
|
-
["AvgWatts", "avgWatts"],
|
|
290
|
-
["MaxWatts", "maxWatts"],
|
|
291
|
-
];
|
|
292
|
-
|
|
293
|
-
function fromEntries(arr) {
|
|
294
|
-
const obj = {};
|
|
295
|
-
for (const [key, value] of arr) {
|
|
296
|
-
obj[key] = value;
|
|
297
|
-
}
|
|
298
|
-
return obj;
|
|
299
|
-
}
|
|
300
|
-
|
|
301
|
-
function getProperties(node, attributeNames) {
|
|
302
|
-
const properties = [];
|
|
303
|
-
|
|
304
|
-
for (const [tag, alias] of attributeNames) {
|
|
305
|
-
let elem = get1(node, tag);
|
|
306
|
-
if (!elem) {
|
|
307
|
-
const elements = node.getElementsByTagNameNS(EXTENSIONS_NS, tag);
|
|
308
|
-
if (elements.length) {
|
|
309
|
-
elem = elements[0];
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
const val = parseFloat(nodeVal(elem));
|
|
313
|
-
if (!isNaN(val)) {
|
|
314
|
-
properties.push([alias, val]);
|
|
315
|
-
}
|
|
316
|
-
}
|
|
317
|
-
|
|
318
|
-
return properties;
|
|
319
|
-
}
|
|
320
|
-
|
|
321
|
-
function coordPair(x) {
|
|
322
|
-
const lon = nodeVal(get1(x, "LongitudeDegrees"));
|
|
323
|
-
const lat = nodeVal(get1(x, "LatitudeDegrees"));
|
|
324
|
-
if (!lon.length || !lat.length) {
|
|
325
|
-
return null;
|
|
326
|
-
}
|
|
327
|
-
const ll = [parseFloat(lon), parseFloat(lat)];
|
|
328
|
-
const alt = get1(x, "AltitudeMeters");
|
|
329
|
-
const heartRate = get1(x, "HeartRateBpm");
|
|
330
|
-
const time = get1(x, "Time");
|
|
331
|
-
let a;
|
|
332
|
-
if (alt) {
|
|
333
|
-
a = parseFloat(nodeVal(alt));
|
|
334
|
-
if (!isNaN(a)) {
|
|
335
|
-
ll.push(a);
|
|
336
|
-
}
|
|
337
|
-
}
|
|
338
|
-
return {
|
|
339
|
-
coordinates: ll,
|
|
340
|
-
time: time ? nodeVal(time) : null,
|
|
341
|
-
heartRate: heartRate ? parseFloat(nodeVal(heartRate)) : null,
|
|
342
|
-
extensions: getProperties(x, TRACKPOINT_ATTRIBUTES),
|
|
343
|
-
};
|
|
344
|
-
}
|
|
345
|
-
|
|
346
|
-
function getPoints(node, pointname) {
|
|
347
|
-
const pts = node.getElementsByTagName(pointname);
|
|
348
|
-
const line = [];
|
|
349
|
-
const times = [];
|
|
350
|
-
const heartRates = [];
|
|
351
|
-
if (pts.length < 2) return null; // Invalid line in GeoJSON
|
|
352
|
-
const result = { extendedProperties: {} };
|
|
353
|
-
for (let i = 0; i < pts.length; i++) {
|
|
354
|
-
const c = coordPair(pts[i]);
|
|
355
|
-
if (c === null) continue;
|
|
356
|
-
line.push(c.coordinates);
|
|
357
|
-
if (c.time) times.push(c.time);
|
|
358
|
-
if (c.heartRate) heartRates.push(c.heartRate);
|
|
359
|
-
for (const [alias, value] of c.extensions) {
|
|
360
|
-
if (!result.extendedProperties[alias]) {
|
|
361
|
-
result.extendedProperties[alias] = Array(pts.length).fill(null);
|
|
362
|
-
}
|
|
363
|
-
result.extendedProperties[alias][i] = value;
|
|
364
|
-
}
|
|
365
|
-
}
|
|
366
|
-
return Object.assign(result, {
|
|
367
|
-
line: line,
|
|
368
|
-
times: times,
|
|
369
|
-
heartRates: heartRates,
|
|
370
|
-
});
|
|
371
|
-
}
|
|
372
|
-
|
|
373
|
-
function getLap(node) {
|
|
374
|
-
const segments = node.getElementsByTagName("Track");
|
|
375
|
-
const track = [];
|
|
376
|
-
const times = [];
|
|
377
|
-
const heartRates = [];
|
|
378
|
-
const allExtendedProperties = [];
|
|
379
|
-
let line;
|
|
380
|
-
const properties = fromEntries(getProperties(node, LAP_ATTRIBUTES));
|
|
381
|
-
|
|
382
|
-
const nameElement = get1(node, "Name");
|
|
383
|
-
if (nameElement) {
|
|
384
|
-
properties.name = nodeVal(nameElement);
|
|
385
|
-
}
|
|
386
|
-
|
|
387
|
-
for (let i = 0; i < segments.length; i++) {
|
|
388
|
-
line = getPoints(segments[i], "Trackpoint");
|
|
389
|
-
if (line) {
|
|
390
|
-
track.push(line.line);
|
|
391
|
-
if (line.times.length) times.push(line.times);
|
|
392
|
-
if (line.heartRates.length) heartRates.push(line.heartRates);
|
|
393
|
-
allExtendedProperties.push(line.extendedProperties);
|
|
394
|
-
}
|
|
395
|
-
}
|
|
396
|
-
for (let i = 0; i < allExtendedProperties.length; i++) {
|
|
397
|
-
const extendedProperties = allExtendedProperties[i];
|
|
398
|
-
for (const property in extendedProperties) {
|
|
399
|
-
if (segments.length === 1) {
|
|
400
|
-
properties[property] = line.extendedProperties[property];
|
|
401
|
-
} else {
|
|
402
|
-
if (!properties[property]) {
|
|
403
|
-
properties[property] = track.map((track) =>
|
|
404
|
-
Array(track.length).fill(null)
|
|
405
|
-
);
|
|
406
|
-
}
|
|
407
|
-
properties[property][i] = extendedProperties[property];
|
|
408
|
-
}
|
|
409
|
-
}
|
|
410
|
-
}
|
|
411
|
-
if (track.length === 0) return;
|
|
412
|
-
|
|
413
|
-
if (times.length || heartRates.length) {
|
|
414
|
-
properties.coordinateProperties = Object.assign(
|
|
415
|
-
times.length
|
|
416
|
-
? {
|
|
417
|
-
times: track.length === 1 ? times[0] : times,
|
|
418
|
-
}
|
|
419
|
-
: {},
|
|
420
|
-
heartRates.length
|
|
421
|
-
? {
|
|
422
|
-
heart: track.length === 1 ? heartRates[0] : heartRates,
|
|
423
|
-
}
|
|
424
|
-
: {}
|
|
425
|
-
);
|
|
426
|
-
}
|
|
427
|
-
|
|
428
|
-
return {
|
|
429
|
-
type: "Feature",
|
|
430
|
-
properties: properties,
|
|
431
|
-
geometry: {
|
|
432
|
-
type: track.length === 1 ? "LineString" : "MultiLineString",
|
|
433
|
-
coordinates: track.length === 1 ? track[0] : track,
|
|
434
|
-
},
|
|
435
|
-
};
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
function* tcxGen(doc) {
|
|
439
|
-
const laps = doc.getElementsByTagName("Lap");
|
|
440
|
-
|
|
441
|
-
for (let i = 0; i < laps.length; i++) {
|
|
442
|
-
const feature = getLap(laps[i]);
|
|
443
|
-
if (feature) yield feature;
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
const courses = doc.getElementsByTagName("Courses");
|
|
447
|
-
|
|
448
|
-
for (let i = 0; i < courses.length; i++) {
|
|
449
|
-
const feature = getLap(courses[i]);
|
|
450
|
-
if (feature) yield feature;
|
|
451
|
-
}
|
|
452
|
-
}
|
|
453
|
-
|
|
454
|
-
function tcx(doc) {
|
|
455
|
-
return {
|
|
456
|
-
type: "FeatureCollection",
|
|
457
|
-
features: Array.from(tcxGen(doc)),
|
|
458
|
-
};
|
|
459
|
-
}
|
|
460
|
-
|
|
461
|
-
const removeSpace = /\s*/g;
|
|
462
|
-
const trimSpace = /^\s*|\s*$/g;
|
|
463
|
-
const splitSpace = /\s+/;
|
|
464
|
-
|
|
465
|
-
// generate a short, numeric hash of a string
|
|
466
|
-
function okhash(x) {
|
|
467
|
-
if (!x || !x.length) return 0;
|
|
468
|
-
let h = 0;
|
|
469
|
-
for (let i = 0; i < x.length; i++) {
|
|
470
|
-
h = ((h << 5) - h + x.charCodeAt(i)) | 0;
|
|
471
|
-
}
|
|
472
|
-
return h;
|
|
473
|
-
}
|
|
474
|
-
|
|
475
|
-
// get one coordinate from a coordinate array, if any
|
|
476
|
-
function coord1(v) {
|
|
477
|
-
return v.replace(removeSpace, "").split(",").map(parseFloat);
|
|
478
|
-
}
|
|
479
|
-
|
|
480
|
-
// get all coordinates from a coordinate array as [[],[]]
|
|
481
|
-
function coord(v) {
|
|
482
|
-
return v.replace(trimSpace, "").split(splitSpace).map(coord1);
|
|
483
|
-
}
|
|
484
|
-
|
|
485
|
-
function xml2str(node) {
|
|
486
|
-
if (node.xml !== undefined) return node.xml;
|
|
487
|
-
if (node.tagName) {
|
|
488
|
-
let output = node.tagName;
|
|
489
|
-
for (let i = 0; i < node.attributes.length; i++) {
|
|
490
|
-
output += node.attributes[i].name + node.attributes[i].value;
|
|
491
|
-
}
|
|
492
|
-
for (let i = 0; i < node.childNodes.length; i++) {
|
|
493
|
-
output += xml2str(node.childNodes[i]);
|
|
494
|
-
}
|
|
495
|
-
return output;
|
|
496
|
-
}
|
|
497
|
-
if (node.nodeName === "#text") {
|
|
498
|
-
return (node.nodeValue || node.value || "").trim();
|
|
499
|
-
}
|
|
500
|
-
if (node.nodeName === "#cdata-section") {
|
|
501
|
-
return node.nodeValue;
|
|
502
|
-
}
|
|
503
|
-
return "";
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
const geotypes = ["Polygon", "LineString", "Point", "Track", "gx:Track"];
|
|
507
|
-
|
|
508
|
-
function kmlColor(properties, elem, prefix) {
|
|
509
|
-
let v = nodeVal(get1(elem, "color")) || "";
|
|
510
|
-
const colorProp =
|
|
511
|
-
prefix == "stroke" || prefix === "fill" ? prefix : prefix + "-color";
|
|
512
|
-
if (v.substr(0, 1) === "#") {
|
|
513
|
-
v = v.substr(1);
|
|
514
|
-
}
|
|
515
|
-
if (v.length === 6 || v.length === 3) {
|
|
516
|
-
properties[colorProp] = v;
|
|
517
|
-
} else if (v.length === 8) {
|
|
518
|
-
properties[prefix + "-opacity"] = parseInt(v.substr(0, 2), 16) / 255;
|
|
519
|
-
properties[colorProp] =
|
|
520
|
-
"#" + v.substr(6, 2) + v.substr(4, 2) + v.substr(2, 2);
|
|
521
|
-
}
|
|
522
|
-
}
|
|
523
|
-
|
|
524
|
-
function numericProperty(properties, elem, source, target) {
|
|
525
|
-
const val = parseFloat(nodeVal(get1(elem, source)));
|
|
526
|
-
if (!isNaN(val)) properties[target] = val;
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
function gxCoords(root) {
|
|
530
|
-
let elems = root.getElementsByTagName("coord");
|
|
531
|
-
const coords = [];
|
|
532
|
-
const times = [];
|
|
533
|
-
if (elems.length === 0) elems = root.getElementsByTagName("gx:coord");
|
|
534
|
-
for (let i = 0; i < elems.length; i++) {
|
|
535
|
-
coords.push(nodeVal(elems[i]).split(" ").map(parseFloat));
|
|
536
|
-
}
|
|
537
|
-
const timeElems = root.getElementsByTagName("when");
|
|
538
|
-
for (let j = 0; j < timeElems.length; j++) times.push(nodeVal(timeElems[j]));
|
|
539
|
-
return {
|
|
540
|
-
coords: coords,
|
|
541
|
-
times: times,
|
|
542
|
-
};
|
|
543
|
-
}
|
|
544
|
-
|
|
545
|
-
function getGeometry(root) {
|
|
546
|
-
let geomNode;
|
|
547
|
-
let geomNodes;
|
|
548
|
-
let i;
|
|
549
|
-
let j;
|
|
550
|
-
let k;
|
|
551
|
-
const geoms = [];
|
|
552
|
-
const coordTimes = [];
|
|
553
|
-
if (get1(root, "MultiGeometry")) {
|
|
554
|
-
return getGeometry(get1(root, "MultiGeometry"));
|
|
555
|
-
}
|
|
556
|
-
if (get1(root, "MultiTrack")) {
|
|
557
|
-
return getGeometry(get1(root, "MultiTrack"));
|
|
558
|
-
}
|
|
559
|
-
if (get1(root, "gx:MultiTrack")) {
|
|
560
|
-
return getGeometry(get1(root, "gx:MultiTrack"));
|
|
561
|
-
}
|
|
562
|
-
for (i = 0; i < geotypes.length; i++) {
|
|
563
|
-
geomNodes = root.getElementsByTagName(geotypes[i]);
|
|
564
|
-
if (geomNodes) {
|
|
565
|
-
for (j = 0; j < geomNodes.length; j++) {
|
|
566
|
-
geomNode = geomNodes[j];
|
|
567
|
-
if (geotypes[i] === "Point") {
|
|
568
|
-
geoms.push({
|
|
569
|
-
type: "Point",
|
|
570
|
-
coordinates: coord1(nodeVal(get1(geomNode, "coordinates"))),
|
|
571
|
-
});
|
|
572
|
-
} else if (geotypes[i] === "LineString") {
|
|
573
|
-
geoms.push({
|
|
574
|
-
type: "LineString",
|
|
575
|
-
coordinates: coord(nodeVal(get1(geomNode, "coordinates"))),
|
|
576
|
-
});
|
|
577
|
-
} else if (geotypes[i] === "Polygon") {
|
|
578
|
-
const rings = geomNode.getElementsByTagName("LinearRing"),
|
|
579
|
-
coords = [];
|
|
580
|
-
for (k = 0; k < rings.length; k++) {
|
|
581
|
-
coords.push(coord(nodeVal(get1(rings[k], "coordinates"))));
|
|
582
|
-
}
|
|
583
|
-
geoms.push({
|
|
584
|
-
type: "Polygon",
|
|
585
|
-
coordinates: coords,
|
|
586
|
-
});
|
|
587
|
-
} else if (geotypes[i] === "Track" || geotypes[i] === "gx:Track") {
|
|
588
|
-
const track = gxCoords(geomNode);
|
|
589
|
-
geoms.push({
|
|
590
|
-
type: "LineString",
|
|
591
|
-
coordinates: track.coords,
|
|
592
|
-
});
|
|
593
|
-
if (track.times.length) coordTimes.push(track.times);
|
|
594
|
-
}
|
|
595
|
-
}
|
|
596
|
-
}
|
|
597
|
-
}
|
|
598
|
-
return {
|
|
599
|
-
geoms: geoms,
|
|
600
|
-
coordTimes: coordTimes,
|
|
601
|
-
};
|
|
602
|
-
}
|
|
603
|
-
|
|
604
|
-
function getPlacemark(root, styleIndex, styleMapIndex, styleByHash) {
|
|
605
|
-
const geomsAndTimes = getGeometry(root);
|
|
606
|
-
let i;
|
|
607
|
-
const properties = {};
|
|
608
|
-
const name = nodeVal(get1(root, "name"));
|
|
609
|
-
const address = nodeVal(get1(root, "address"));
|
|
610
|
-
let styleUrl = nodeVal(get1(root, "styleUrl"));
|
|
611
|
-
const description = nodeVal(get1(root, "description"));
|
|
612
|
-
const timeSpan = get1(root, "TimeSpan");
|
|
613
|
-
const timeStamp = get1(root, "TimeStamp");
|
|
614
|
-
const extendedData = get1(root, "ExtendedData");
|
|
615
|
-
let iconStyle = get1(root, "IconStyle");
|
|
616
|
-
let labelStyle = get1(root, "LabelStyle");
|
|
617
|
-
let lineStyle = get1(root, "LineStyle");
|
|
618
|
-
let polyStyle = get1(root, "PolyStyle");
|
|
619
|
-
const visibility = get1(root, "visibility");
|
|
620
|
-
|
|
621
|
-
if (name) properties.name = name;
|
|
622
|
-
if (address) properties.address = address;
|
|
623
|
-
if (styleUrl) {
|
|
624
|
-
if (styleUrl[0] !== "#") {
|
|
625
|
-
styleUrl = "#" + styleUrl;
|
|
626
|
-
}
|
|
627
|
-
|
|
628
|
-
properties.styleUrl = styleUrl;
|
|
629
|
-
if (styleIndex[styleUrl]) {
|
|
630
|
-
properties.styleHash = styleIndex[styleUrl];
|
|
631
|
-
}
|
|
632
|
-
if (styleMapIndex[styleUrl]) {
|
|
633
|
-
properties.styleMapHash = styleMapIndex[styleUrl];
|
|
634
|
-
properties.styleHash = styleIndex[styleMapIndex[styleUrl].normal];
|
|
635
|
-
}
|
|
636
|
-
// Try to populate the lineStyle or polyStyle since we got the style hash
|
|
637
|
-
const style = styleByHash[properties.styleHash];
|
|
638
|
-
if (style) {
|
|
639
|
-
if (!iconStyle) iconStyle = get1(style, "IconStyle");
|
|
640
|
-
if (!labelStyle) labelStyle = get1(style, "LabelStyle");
|
|
641
|
-
if (!lineStyle) lineStyle = get1(style, "LineStyle");
|
|
642
|
-
if (!polyStyle) polyStyle = get1(style, "PolyStyle");
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
if (description) properties.description = description;
|
|
646
|
-
if (timeSpan) {
|
|
647
|
-
const begin = nodeVal(get1(timeSpan, "begin"));
|
|
648
|
-
const end = nodeVal(get1(timeSpan, "end"));
|
|
649
|
-
properties.timespan = { begin: begin, end: end };
|
|
650
|
-
}
|
|
651
|
-
if (timeStamp) {
|
|
652
|
-
properties.timestamp = nodeVal(get1(timeStamp, "when"));
|
|
653
|
-
}
|
|
654
|
-
if (iconStyle) {
|
|
655
|
-
kmlColor(properties, iconStyle, "icon");
|
|
656
|
-
numericProperty(properties, iconStyle, "scale", "icon-scale");
|
|
657
|
-
numericProperty(properties, iconStyle, "heading", "icon-heading");
|
|
658
|
-
|
|
659
|
-
const hotspot = get1(iconStyle, "hotSpot");
|
|
660
|
-
if (hotspot) {
|
|
661
|
-
const left = parseFloat(hotspot.getAttribute("x"));
|
|
662
|
-
const top = parseFloat(hotspot.getAttribute("y"));
|
|
663
|
-
if (!isNaN(left) && !isNaN(top)) properties["icon-offset"] = [left, top];
|
|
664
|
-
}
|
|
665
|
-
const icon = get1(iconStyle, "Icon");
|
|
666
|
-
if (icon) {
|
|
667
|
-
const href = nodeVal(get1(icon, "href"));
|
|
668
|
-
if (href) properties.icon = href;
|
|
669
|
-
}
|
|
670
|
-
}
|
|
671
|
-
if (labelStyle) {
|
|
672
|
-
kmlColor(properties, labelStyle, "label");
|
|
673
|
-
numericProperty(properties, labelStyle, "scale", "label-scale");
|
|
674
|
-
}
|
|
675
|
-
if (lineStyle) {
|
|
676
|
-
kmlColor(properties, lineStyle, "stroke");
|
|
677
|
-
numericProperty(properties, lineStyle, "width", "stroke-width");
|
|
678
|
-
}
|
|
679
|
-
if (polyStyle) {
|
|
680
|
-
kmlColor(properties, polyStyle, "fill");
|
|
681
|
-
const fill = nodeVal(get1(polyStyle, "fill"));
|
|
682
|
-
const outline = nodeVal(get1(polyStyle, "outline"));
|
|
683
|
-
if (fill)
|
|
684
|
-
properties["fill-opacity"] =
|
|
685
|
-
fill === "1" ? properties["fill-opacity"] || 1 : 0;
|
|
686
|
-
if (outline)
|
|
687
|
-
properties["stroke-opacity"] =
|
|
688
|
-
outline === "1" ? properties["stroke-opacity"] || 1 : 0;
|
|
689
|
-
}
|
|
690
|
-
if (extendedData) {
|
|
691
|
-
const datas = extendedData.getElementsByTagName("Data"),
|
|
692
|
-
simpleDatas = extendedData.getElementsByTagName("SimpleData");
|
|
693
|
-
|
|
694
|
-
for (i = 0; i < datas.length; i++) {
|
|
695
|
-
properties[datas[i].getAttribute("name")] = nodeVal(
|
|
696
|
-
get1(datas[i], "value")
|
|
697
|
-
);
|
|
698
|
-
}
|
|
699
|
-
for (i = 0; i < simpleDatas.length; i++) {
|
|
700
|
-
properties[simpleDatas[i].getAttribute("name")] = nodeVal(simpleDatas[i]);
|
|
701
|
-
}
|
|
702
|
-
}
|
|
703
|
-
if (visibility) {
|
|
704
|
-
properties.visibility = nodeVal(visibility);
|
|
705
|
-
}
|
|
706
|
-
if (geomsAndTimes.coordTimes.length) {
|
|
707
|
-
properties.coordinateProperties = {
|
|
708
|
-
times:
|
|
709
|
-
geomsAndTimes.coordTimes.length === 1
|
|
710
|
-
? geomsAndTimes.coordTimes[0]
|
|
711
|
-
: geomsAndTimes.coordTimes,
|
|
712
|
-
};
|
|
713
|
-
}
|
|
714
|
-
const feature = {
|
|
715
|
-
type: "Feature",
|
|
716
|
-
geometry:
|
|
717
|
-
geomsAndTimes.geoms.length === 0
|
|
718
|
-
? null
|
|
719
|
-
: geomsAndTimes.geoms.length === 1
|
|
720
|
-
? geomsAndTimes.geoms[0]
|
|
721
|
-
: {
|
|
722
|
-
type: "GeometryCollection",
|
|
723
|
-
geometries: geomsAndTimes.geoms,
|
|
724
|
-
},
|
|
725
|
-
properties: properties,
|
|
726
|
-
};
|
|
727
|
-
if (root.getAttribute("id")) feature.id = root.getAttribute("id");
|
|
728
|
-
return feature;
|
|
729
|
-
}
|
|
730
|
-
|
|
731
|
-
function* kmlGen(doc) {
|
|
732
|
-
// styleindex keeps track of hashed styles in order to match feature
|
|
733
|
-
const styleIndex = {};
|
|
734
|
-
const styleByHash = {};
|
|
735
|
-
// stylemapindex keeps track of style maps to expose in properties
|
|
736
|
-
const styleMapIndex = {};
|
|
737
|
-
// atomic geospatial types supported by KML - MultiGeometry is
|
|
738
|
-
// handled separately
|
|
739
|
-
// all root placemarks in the file
|
|
740
|
-
const placemarks = doc.getElementsByTagName("Placemark");
|
|
741
|
-
const styles = doc.getElementsByTagName("Style");
|
|
742
|
-
const styleMaps = doc.getElementsByTagName("StyleMap");
|
|
743
|
-
|
|
744
|
-
for (let k = 0; k < styles.length; k++) {
|
|
745
|
-
const hash = okhash(xml2str(styles[k])).toString(16);
|
|
746
|
-
styleIndex["#" + styles[k].getAttribute("id")] = hash;
|
|
747
|
-
styleByHash[hash] = styles[k];
|
|
748
|
-
}
|
|
749
|
-
for (let l = 0; l < styleMaps.length; l++) {
|
|
750
|
-
styleIndex["#" + styleMaps[l].getAttribute("id")] = okhash(
|
|
751
|
-
xml2str(styleMaps[l])
|
|
752
|
-
).toString(16);
|
|
753
|
-
const pairs = styleMaps[l].getElementsByTagName("Pair");
|
|
754
|
-
const pairsMap = {};
|
|
755
|
-
for (let m = 0; m < pairs.length; m++) {
|
|
756
|
-
pairsMap[nodeVal(get1(pairs[m], "key"))] = nodeVal(
|
|
757
|
-
get1(pairs[m], "styleUrl")
|
|
758
|
-
);
|
|
759
|
-
}
|
|
760
|
-
styleMapIndex["#" + styleMaps[l].getAttribute("id")] = pairsMap;
|
|
761
|
-
}
|
|
762
|
-
for (let j = 0; j < placemarks.length; j++) {
|
|
763
|
-
const feature = getPlacemark(
|
|
764
|
-
placemarks[j],
|
|
765
|
-
styleIndex,
|
|
766
|
-
styleMapIndex,
|
|
767
|
-
styleByHash
|
|
768
|
-
);
|
|
769
|
-
if (feature) yield feature;
|
|
770
|
-
}
|
|
771
|
-
}
|
|
772
|
-
|
|
773
|
-
function kml(doc) {
|
|
774
|
-
return {
|
|
775
|
-
type: "FeatureCollection",
|
|
776
|
-
features: Array.from(kmlGen(doc)),
|
|
777
|
-
};
|
|
778
|
-
}
|
|
779
|
-
|
|
780
|
-
exports.gpx = gpx;
|
|
781
|
-
exports.gpxGen = gpxGen;
|
|
782
|
-
exports.kml = kml;
|
|
783
|
-
exports.kmlGen = kmlGen;
|
|
784
|
-
exports.tcx = tcx;
|
|
785
|
-
exports.tcxGen = tcxGen;
|
|
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 p(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 g(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(g(i));else{var u=a(i);t.push([o,m(u)])}}}return t}function d(e){return["heart","gpxtpx:hr","hr"].includes(e)?"heart":e}function m(e){var t=parseFloat(e);return isNaN(t)?e:t}function v(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:g(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=v(a[s]);i.push(l.coordinates),l.time&&o.push(l.time);for(var c,f=t(l.extendedValues);!(c=f()).done;){var p=c.value,g=p[0],d=p[1],m="heart"===g?g:g.replace("gpxtpx:","")+"s";u[m]||(u[m]=Array(a.length).fill(null)),u[m][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),p(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),p(i(e,"extensions")),o.length?{coordinateProperties:{times:c?o:o[0]}}:{}),g=0,d=u;g<d.length;g++){var m=d[g];a.push(m.line),f.coordinateProperties||(f.coordinateProperties={});for(var v=f.coordinateProperties,y=Object.entries(m.extendedValues),x=0;x<y.length;x++){var k=y[x],N=k[0],A=k[1];c?(v[N]||(v[N]=u.map(function(e){return new Array(e.line.length).fill(null)})),v[N][x]=A):v[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:v(e).coordinates}}}function A(e){var n,a,i,o,u,s,l,c,f;return regeneratorRuntime.wrap(function(p){for(;;)switch(p.prev=p.next){case 0:n=t(r(e,"trk"));case 1:if((a=n()).done){p.next=9;break}if(!(i=k(a.value))){p.next=7;break}return p.next=7,i;case 7:p.next=1;break;case 9:o=t(r(e,"rte"));case 10:if((u=o()).done){p.next=18;break}if(!(s=x(u.value))){p.next=16;break}return p.next=16,s;case 16:p.next=10;break;case 18:l=t(r(e,"wpt"));case 19:if((c=l()).done){p.next=25;break}return f=c.value,p.next=23,N(f);case 23:p.next=19;break;case 25:case"end":return p.stop()}},y)}var S=/*#__PURE__*/regeneratorRuntime.mark(R),w=[["heartRate","heartRates"],["Cadence","cadences"],["Speed","speeds"],["Watts","watts"]],T=[["TotalTimeSeconds","totalTimeSeconds"],["DistanceMeters","distanceMeters"],["MaximumSpeed","maxSpeed"],["AverageHeartRateBpm","avgHeartRate"],["MaximumHeartRateBpm","maxHeartRate"],["AvgSpeed","avgSpeed"],["AvgWatts","avgWatts"],["MaxWatts","maxWatts"]];function F(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 p=e.getElementsByTagNameNS("http://www.garmin.com/xmlschemas/ActivityExtension/v2",l);p.length&&(f=p[0])}var g=parseFloat(a(f));isNaN(g)||o.push([c,g])}return o}function P(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:F(e,w)}}function j(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=P(n[l]);if(null!==c){a.push(c.coordinates);var f=c.time,p=c.heartRate,g=c.extensions;f&&i.push(f),p&&o.push(p);for(var d,m=t(g);!(d=m()).done;){var v=d.value,h=v[0],y=v[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 O(e){for(var n,i,u=r(e,"Track"),s=[],l=[],c=[],f=[],p=Object.assign(Object.fromEntries(F(e,T)),o(e,"Name",function(e){return{name:a(e)}})),g=t(u);!(i=g()).done;)(n=j(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 m=f[d];for(var v in m)1===u.length?n&&(p[v]=n.extendedProperties[v]):(p[v]||(p[v]=s.map(function(e){return Array(e.length).fill(null)})),p[v][d]=m[v])}return 0===s.length?null:((l.length||c.length)&&(p.coordinateProperties=Object.assign(l.length?{times:1===s.length?l[0]:l}:{},c.length?{heart:1===s.length?c[0]:c}:{})),{type:"Feature",properties:p,geometry:1===s.length?{type:"LineString",coordinates:s[0]}:{type:"MultiLineString",coordinates:s}})}function R(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=O(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=O(u.value))){l.next=16;break}return l.next=16,s;case 16:l.next=10;break;case 18:case"end":return l.stop()}},S)}function L(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 M(e,t,r){var n={};return l(e,t,function(e){n[r]=e}),n}function E(e,t){return o(e,"color",function(e){return L(a(e),t)})}function B(e){return Object.assign({},function(e){return o(e,"PolyStyle",function(e,t){return Object.assign(t,o(e,"color",function(e){return L(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(E(e,"stroke"),M(t,"width","stroke-width"))})}(e),function(e){return o(e,"LabelStyle",function(t){return Object.assign(E(e,"label"),M(t,"scale","label-scale"))})}(e),function(e){return o(e,"IconStyle",function(t){return Object.assign(E(e,"icon"),M(t,"scale","icon-scale"),M(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 C=/\s*/g,D=/^\s*|\s*$/g,G=/\s+/;function I(e){return e.replace(C,"").split(",").map(parseFloat).filter(function(e){return!isNaN(e)})}function W(e){return e.replace(D,"").split(G).map(I).filter(function(e){return e.length>=2})}function H(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 U(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 V=["Polygon","LineString","Point","Track","gx:Track"];function _(e){return a(i(e,"coordinates"))}function $(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 $(s)}for(var l,c=t(V);!(l=c()).done;)for(var f,p=l.value,g=t(r(e,p));!(f=g()).done;){var d=f.value;switch(p){case"Point":var m=I(_(d));m.length>=2&&n.push({type:"Point",coordinates:m});break;case"LineString":var v=W(_(d));v.length>=2&&n.push({type:"LineString",coordinates:v});break;case"Polygon":for(var h,y=[],b=t(r(d,"LinearRing"));!(h=b()).done;){var x=U(W(_(h.value)));x.length>=4&&y.push(x)}y.length&&n.push({type:"Polygon",coordinates:y});break;case"Track":case"gx:Track":var k=H(d);if(!k)break;var N=k.times;n.push(k.geometry),N.length&&a.push(N)}}return{geometries:n,coordTimes:a}}function z(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 q(e){return o(e,"TimeSpan",function(e){return{timespan:{begin:a(i(e,"begin")),end:a(i(e,"end"))}}})}function J(e){return o(e,"TimeStamp",function(e){return{timestamp:a(i(e,"when"))}})}function K(e,t){return u(e,"styleUrl",function(e){return e=n(e),t[e]?Object.assign({styleUrl:e},t[e]):{styleUrl:e}})}function Q(e,t){var r,n=$(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"]),K(e,t),B(e),z(e),q(e),J(e),a.length?{coordinateProperties:{times:1===a.length?a[0]:a}}:{})},o=e.getAttribute("id");return null!==o&&""!==o&&(i.id=o),i}var X=/*#__PURE__*/regeneratorRuntime.mark(ee);function Y(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 Z(e){for(var a,i={},o=t(r(e,"Style"));!(a=o()).done;){var s=a.value;i[Y(s)]=B(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 ee(e){var n,a,i,o;return regeneratorRuntime.wrap(function(u){for(;;)switch(u.prev=u.next){case 0:n=Z(e),a=t(r(e,"Placemark"));case 2:if((i=a()).done){u.next=10;break}if(!(o=Q(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()}},X)}exports.gpx=function(e){return{type:"FeatureCollection",features:Array.from(A(e))}},exports.gpxGen=A,exports.kml=function(e){return{type:"FeatureCollection",features:Array.from(ee(e))}},exports.kmlGen=ee,exports.kmlWithFolders=function(e){var t=Z(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=Q(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},exports.tcx=function(e){return{type:"FeatureCollection",features:Array.from(R(e))}},exports.tcxGen=R;
|
|
786
2
|
//# sourceMappingURL=togeojson.js.map
|