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