@tmcw/togeojson 7.1.3 → 7.1.4

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/README.md CHANGED
@@ -38,6 +38,7 @@ also encodes parts of KML, GPX, and TCX files that otherwise would be lost.
38
38
 
39
39
  - Line properties: `totalTimeSeconds`, `distanceMeters`, `maxSpeed`,
40
40
  `avgHeartRate`, `maxHeartRate`, `avgSpeed`, `avgWatts`, `maxWatts`
41
+ - Per-point properties: `heartRates`, `cadences`, `speeds`, `watts`
41
42
 
42
43
  This also emits the [geojson-coordinate-properties](https://github.com/mapbox/geojson-coordinate-properties) format
43
44
  to include time and other attributes that apply to each coordinate of a LineString.
@@ -9,6 +9,12 @@ function normalizeId(id) {
9
9
  function $ns(element, tagName, ns) {
10
10
  return Array.from(element.getElementsByTagNameNS(ns, tagName));
11
11
  }
12
+ /**
13
+ * True if any of the per-coordinate arrays has a value that is not null
14
+ */
15
+ function hasValues(arrays) {
16
+ return arrays.some((values) => values.some((value) => value !== null));
17
+ }
12
18
  /**
13
19
  * get the content of a text node, if any
14
20
  */
@@ -133,8 +139,35 @@ function getLineStyle(node) {
133
139
  });
134
140
  }
135
141
 
142
+ /**
143
+ * Tags inside these elements belong to a track or route point,
144
+ * not to the track or route itself.
145
+ */
146
+ const POINT_CONTAINERS = ["trkseg", "rtept"];
147
+ function children(node, tagName) {
148
+ return Array.from(node.childNodes).filter((child) => isElement(child) && child.tagName === tagName);
149
+ }
150
+ function childValues(node, propertyNames) {
151
+ const properties = {};
152
+ for (const property of propertyNames) {
153
+ const val = nodeVal(children(node, property)[0] || null);
154
+ if (val)
155
+ properties[property] = val;
156
+ }
157
+ return properties;
158
+ }
159
+ /**
160
+ * True if `child` sits inside a point or segment of `node`.
161
+ */
162
+ function isInPoint(node, child) {
163
+ for (let parent = child.parentNode; parent && parent !== node; parent = parent.parentNode) {
164
+ if (isElement(parent) && POINT_CONTAINERS.includes(parent.localName))
165
+ return true;
166
+ }
167
+ return false;
168
+ }
136
169
  function extractProperties(ns, node) {
137
- const properties = getMulti(node, [
170
+ const properties = childValues(node, [
138
171
  "name",
139
172
  "cmt",
140
173
  "desc",
@@ -144,12 +177,14 @@ function extractProperties(ns, node) {
144
177
  ]);
145
178
  for (const [n, url] of ns) {
146
179
  for (const child of Array.from(node.getElementsByTagNameNS(url, "*"))) {
180
+ if (isInPoint(node, child))
181
+ continue;
147
182
  properties[child.tagName.replace(":", "_")] = nodeVal(child)?.trim();
148
183
  }
149
184
  }
150
- const links = $(node, "link");
185
+ const links = children(node, "link");
151
186
  if (links.length) {
152
- properties.links = links.map((link) => Object.assign({ href: link.getAttribute("href") }, getMulti(link, ["text", "type"])));
187
+ properties.links = links.map((link) => Object.assign({ href: link.getAttribute("href") }, childValues(link, ["text", "type"])));
153
188
  }
154
189
  return properties;
155
190
  }
@@ -168,18 +203,24 @@ function getPoints$1(node, pointname) {
168
203
  continue;
169
204
  }
170
205
  line.push(c.coordinates);
171
- if (c.time)
172
- times.push(c.time);
206
+ // Push null for missing values, so that every array has one
207
+ // entry per coordinate.
208
+ times.push(c.time || null);
173
209
  for (const [name, val] of c.extendedValues) {
174
210
  const plural = name === "heart" ? name : `${name.replace("gpxtpx:", "")}s`;
175
211
  if (!extendedValues[plural]) {
176
212
  extendedValues[plural] = Array(pts.length).fill(null);
177
213
  }
178
- extendedValues[plural][i] = val;
214
+ extendedValues[plural][line.length - 1] = val;
179
215
  }
180
216
  }
181
217
  if (line.length < 2)
182
218
  return; // Invalid line in GeoJSON
219
+ // Points without a position are skipped, so cut the
220
+ // extension arrays down to the number of coordinates.
221
+ for (const name in extendedValues) {
222
+ extendedValues[name].length = line.length;
223
+ }
183
224
  return {
184
225
  line: line,
185
226
  times: times,
@@ -212,14 +253,13 @@ function getTrack(ns, node) {
212
253
  const line = getPoints$1(segment, "trkpt");
213
254
  if (line) {
214
255
  extractedLines.push(line);
215
- if (line.times?.length)
216
- times.push(line.times);
256
+ times.push(line.times);
217
257
  }
218
258
  }
219
259
  if (extractedLines.length === 0)
220
260
  return null;
221
261
  const multi = extractedLines.length > 1;
222
- const properties = Object.assign({ _gpxType: "trk" }, extractProperties(ns, node), getLineStyle(get1(node, "extensions")), times.length
262
+ const properties = Object.assign({ _gpxType: "trk" }, extractProperties(ns, node), getLineStyle(get1(node, "extensions")), hasValues(times)
223
263
  ? {
224
264
  coordinateProperties: {
225
265
  times: multi ? times : times[0],
@@ -376,7 +416,7 @@ function coordPair(node) {
376
416
  Number.isNaN(ll[1])) {
377
417
  return null;
378
418
  }
379
- const heartRate = get1(node, "HeartRateBpm");
419
+ const heartRate = Number.parseFloat(nodeVal(get1(node, "HeartRateBpm")));
380
420
  const time = nodeVal(get1(node, "Time"));
381
421
  get1(node, "AltitudeMeters", (alt) => {
382
422
  const a = Number.parseFloat(nodeVal(alt));
@@ -387,7 +427,7 @@ function coordPair(node) {
387
427
  return {
388
428
  coordinates: ll,
389
429
  time: time || null,
390
- heartRate: heartRate ? Number.parseFloat(nodeVal(heartRate)) : null,
430
+ heartRate: Number.isNaN(heartRate) ? null : heartRate,
391
431
  extensions: getProperties(node, TRACKPOINT_ATTRIBUTES),
392
432
  };
393
433
  }
@@ -406,19 +446,24 @@ function getPoints(node) {
406
446
  continue;
407
447
  line.push(c.coordinates);
408
448
  const { time, heartRate, extensions } = c;
409
- if (time)
410
- times.push(time);
411
- if (heartRate)
412
- heartRates.push(heartRate);
449
+ // Push null for missing values, so that every array has one
450
+ // entry per coordinate.
451
+ times.push(time);
452
+ heartRates.push(heartRate);
413
453
  for (const [alias, value] of extensions) {
414
454
  if (!extendedProperties[alias]) {
415
455
  extendedProperties[alias] = Array(pts.length).fill(null);
416
456
  }
417
- extendedProperties[alias][i] = value;
457
+ extendedProperties[alias][line.length - 1] = value;
418
458
  }
419
459
  }
420
460
  if (line.length < 2)
421
461
  return null;
462
+ // Trackpoints without a position are skipped, so cut the
463
+ // extension arrays down to the number of coordinates.
464
+ for (const alias in extendedProperties) {
465
+ extendedProperties[alias].length = line.length;
466
+ }
422
467
  return Object.assign(result, {
423
468
  line: line,
424
469
  times: times,
@@ -431,28 +476,23 @@ function getLap(node) {
431
476
  const times = [];
432
477
  const heartRates = [];
433
478
  const allExtendedProperties = [];
434
- let line;
435
479
  const properties = Object.assign(Object.fromEntries(getProperties(node, LAP_ATTRIBUTES)), get(node, "Name", (nameElement) => {
436
480
  return { name: nodeVal(nameElement) };
437
481
  }));
438
482
  for (const segment of segments) {
439
- line = getPoints(segment);
483
+ const line = getPoints(segment);
440
484
  if (line) {
441
485
  track.push(line.line);
442
- if (line.times.length)
443
- times.push(line.times);
444
- if (line.heartRates.length)
445
- heartRates.push(line.heartRates);
486
+ times.push(line.times);
487
+ heartRates.push(line.heartRates);
446
488
  allExtendedProperties.push(line.extendedProperties);
447
489
  }
448
490
  }
449
491
  for (let i = 0; i < allExtendedProperties.length; i++) {
450
492
  const extendedProperties = allExtendedProperties[i];
451
493
  for (const property in extendedProperties) {
452
- if (segments.length === 1) {
453
- if (line) {
454
- properties[property] = line.extendedProperties[property];
455
- }
494
+ if (track.length === 1) {
495
+ properties[property] = extendedProperties[property];
456
496
  }
457
497
  else {
458
498
  if (!properties[property]) {
@@ -464,12 +504,14 @@ function getLap(node) {
464
504
  }
465
505
  if (track.length === 0)
466
506
  return null;
467
- if (times.length || heartRates.length) {
468
- properties.coordinateProperties = Object.assign(times.length
507
+ const hasTimes = hasValues(times);
508
+ const hasHeartRates = hasValues(heartRates);
509
+ if (hasTimes || hasHeartRates) {
510
+ properties.coordinateProperties = Object.assign(hasTimes
469
511
  ? {
470
512
  times: track.length === 1 ? times[0] : times,
471
513
  }
472
- : {}, heartRates.length
514
+ : {}, hasHeartRates
473
515
  ? {
474
516
  heart: track.length === 1 ? heartRates[0] : heartRates,
475
517
  }
@@ -500,7 +542,8 @@ function* tcxGen(node) {
500
542
  if (feature)
501
543
  yield feature;
502
544
  }
503
- for (const course of $(node, "Courses")) {
545
+ // Each Course is its own route, so it gets its own feature.
546
+ for (const course of $(node, "Course")) {
504
547
  const feature = getLap(course);
505
548
  if (feature)
506
549
  yield feature;
@@ -745,7 +788,8 @@ const typeConverters = {
745
788
  ushort: toNumber,
746
789
  float: toNumber,
747
790
  double: toNumber,
748
- bool: (x) => Boolean(x),
791
+ // xsd:boolean: "true" or "1" is true, "false" or "0" is false
792
+ bool: (x) => ["true", "1"].includes(x.trim().toLowerCase()),
749
793
  };
750
794
  function extractExtendedData(node, schema) {
751
795
  return get(node, "ExtendedData", (extendedData, properties) => {