@tmcw/togeojson 5.8.1 → 6.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.
@@ -12,7 +12,7 @@ function $ns(element, tagName, ns) {
12
12
  */
13
13
  function nodeVal(node) {
14
14
  node?.normalize();
15
- return (node && node.textContent) || "";
15
+ return node?.textContent || "";
16
16
  }
17
17
  /**
18
18
  * Get one Y child of X, if any, otherwise null
@@ -42,16 +42,16 @@ function val1(node, tagName, callback) {
42
42
  return {};
43
43
  }
44
44
  function $num(node, tagName, callback) {
45
- const val = parseFloat(nodeVal(get1(node, tagName)));
46
- if (isNaN(val))
45
+ const val = Number.parseFloat(nodeVal(get1(node, tagName)));
46
+ if (Number.isNaN(val))
47
47
  return undefined;
48
48
  if (val && callback)
49
49
  return callback(val) || {};
50
50
  return {};
51
51
  }
52
52
  function num1(node, tagName, callback) {
53
- const val = parseFloat(nodeVal(get1(node, tagName)));
54
- if (isNaN(val))
53
+ const val = Number.parseFloat(nodeVal(get1(node, tagName)));
54
+ if (Number.isNaN(val))
55
55
  return undefined;
56
56
  if (callback)
57
57
  callback(val);
@@ -70,20 +70,6 @@ function isElement(node) {
70
70
  return node?.nodeType === 1;
71
71
  }
72
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
73
  function getExtensions(node) {
88
74
  let values = [];
89
75
  if (node === null)
@@ -108,16 +94,16 @@ function abbreviateName(name) {
108
94
  return ["heart", "gpxtpx:hr", "hr"].includes(name) ? "heart" : name;
109
95
  }
110
96
  function parseNumeric(val) {
111
- const num = parseFloat(val);
112
- return isNaN(num) ? val : num;
97
+ const num = Number.parseFloat(val);
98
+ return Number.isNaN(num) ? val : num;
113
99
  }
114
100
 
115
101
  function coordPair$1(node) {
116
102
  const ll = [
117
- parseFloat(node.getAttribute("lon") || ""),
118
- parseFloat(node.getAttribute("lat") || ""),
103
+ Number.parseFloat(node.getAttribute("lon") || ""),
104
+ Number.parseFloat(node.getAttribute("lat") || ""),
119
105
  ];
120
- if (isNaN(ll[0]) || isNaN(ll[1])) {
106
+ if (Number.isNaN(ll[0]) || Number.isNaN(ll[1])) {
121
107
  return null;
122
108
  }
123
109
  num1(node, "ele", (val) => {
@@ -131,7 +117,21 @@ function coordPair$1(node) {
131
117
  };
132
118
  }
133
119
 
134
- function extractProperties(node) {
120
+ function getLineStyle(node) {
121
+ return get(node, "line", (lineStyle) => {
122
+ const val = Object.assign({}, val1(lineStyle, "color", (color) => {
123
+ return { stroke: `#${color}` };
124
+ }), $num(lineStyle, "opacity", (opacity) => {
125
+ return { "stroke-opacity": opacity };
126
+ }), $num(lineStyle, "width", (width) => {
127
+ // GPX width is in mm, convert to px with 96 px per inch
128
+ return { "stroke-width": (width * 96) / 25.4 };
129
+ }));
130
+ return val;
131
+ });
132
+ }
133
+
134
+ function extractProperties(ns, node) {
135
135
  const properties = getMulti(node, [
136
136
  "name",
137
137
  "cmt",
@@ -140,10 +140,9 @@ function extractProperties(node) {
140
140
  "time",
141
141
  "keywords",
142
142
  ]);
143
- const extensions = Array.from(node.getElementsByTagNameNS("http://www.garmin.com/xmlschemas/GpxExtensions/v3", "*"));
144
- for (const child of extensions) {
145
- if (child.parentNode?.parentNode === node) {
146
- properties[child.tagName.replace(":", "_")] = nodeVal(child);
143
+ for (const [n, url] of ns) {
144
+ for (const child of Array.from(node.getElementsByTagNameNS(url, "*"))) {
145
+ properties[child.tagName.replace(":", "_")] = nodeVal(child)?.trim();
147
146
  }
148
147
  }
149
148
  const links = $(node, "link");
@@ -170,7 +169,7 @@ function getPoints$1(node, pointname) {
170
169
  if (c.time)
171
170
  times.push(c.time);
172
171
  for (const [name, val] of c.extendedValues) {
173
- const plural = name === "heart" ? name : name.replace("gpxtpx:", "") + "s";
172
+ const plural = name === "heart" ? name : `${name.replace("gpxtpx:", "")}s`;
174
173
  if (!extendedValues[plural]) {
175
174
  extendedValues[plural] = Array(pts.length).fill(null);
176
175
  }
@@ -189,20 +188,20 @@ function getPoints$1(node, pointname) {
189
188
  * Extract a LineString geometry from a rte
190
189
  * element.
191
190
  */
192
- function getRoute(node) {
191
+ function getRoute(ns, node) {
193
192
  const line = getPoints$1(node, "rtept");
194
193
  if (!line)
195
194
  return;
196
195
  return {
197
196
  type: "Feature",
198
- properties: Object.assign({ _gpxType: "rte" }, extractProperties(node), getLineStyle(get1(node, "extensions"))),
197
+ properties: Object.assign({ _gpxType: "rte" }, extractProperties(ns, node), getLineStyle(get1(node, "extensions"))),
199
198
  geometry: {
200
199
  type: "LineString",
201
200
  coordinates: line.line,
202
201
  },
203
202
  };
204
203
  }
205
- function getTrack(node) {
204
+ function getTrack(ns, node) {
206
205
  const segments = $(node, "trkseg");
207
206
  const track = [];
208
207
  const times = [];
@@ -211,14 +210,14 @@ function getTrack(node) {
211
210
  const line = getPoints$1(segment, "trkpt");
212
211
  if (line) {
213
212
  extractedLines.push(line);
214
- if (line.times && line.times.length)
213
+ if (line.times?.length)
215
214
  times.push(line.times);
216
215
  }
217
216
  }
218
217
  if (extractedLines.length === 0)
219
218
  return null;
220
219
  const multi = extractedLines.length > 1;
221
- const properties = Object.assign({ _gpxType: "trk" }, extractProperties(node), getLineStyle(get1(node, "extensions")), times.length
220
+ const properties = Object.assign({ _gpxType: "trk" }, extractProperties(ns, node), getLineStyle(get1(node, "extensions")), times.length
222
221
  ? {
223
222
  coordinateProperties: {
224
223
  times: multi ? times : times[0],
@@ -263,8 +262,8 @@ function getTrack(node) {
263
262
  * Extract a point, if possible, from a given node,
264
263
  * which is usually a wpt or trkpt
265
264
  */
266
- function getPoint(node) {
267
- const properties = Object.assign(extractProperties(node), getMulti(node, ["sym"]));
265
+ function getPoint(ns, node) {
266
+ const properties = Object.assign(extractProperties(ns, node), getMulti(node, ["sym"]));
268
267
  const pair = coordPair$1(node);
269
268
  if (!pair)
270
269
  return null;
@@ -283,18 +282,30 @@ function getPoint(node) {
283
282
  * that yields output feature by feature.
284
283
  */
285
284
  function* gpxGen(node) {
285
+ const GPXX = "gpxx";
286
+ const GPXX_URI = "http://www.garmin.com/xmlschemas/GpxExtensions/v3";
287
+ // Namespaces
288
+ const ns = [[GPXX, GPXX_URI]];
289
+ const attrs = node.getElementsByTagName("gpx")[0]?.attributes;
290
+ if (attrs) {
291
+ for (const attr of Array.from(attrs)) {
292
+ if (attr.name?.startsWith("xmlns:") && attr.value !== GPXX_URI) {
293
+ ns.push([attr.name, attr.value]);
294
+ }
295
+ }
296
+ }
286
297
  for (const track of $(node, "trk")) {
287
- const feature = getTrack(track);
298
+ const feature = getTrack(ns, track);
288
299
  if (feature)
289
300
  yield feature;
290
301
  }
291
302
  for (const route of $(node, "rte")) {
292
- const feature = getRoute(route);
303
+ const feature = getRoute(ns, route);
293
304
  if (feature)
294
305
  yield feature;
295
306
  }
296
307
  for (const waypoint of $(node, "wpt")) {
297
- const point = getPoint(waypoint);
308
+ const point = getPoint(ns, waypoint);
298
309
  if (point)
299
310
  yield point;
300
311
  }
@@ -346,8 +357,8 @@ function getProperties(node, attributeNames) {
346
357
  elem = elements[0];
347
358
  }
348
359
  }
349
- const val = parseFloat(nodeVal(elem));
350
- if (!isNaN(val)) {
360
+ const val = Number.parseFloat(nodeVal(elem));
361
+ if (!Number.isNaN(val)) {
351
362
  properties.push([alias, val]);
352
363
  }
353
364
  }
@@ -356,23 +367,23 @@ function getProperties(node, attributeNames) {
356
367
  function coordPair(node) {
357
368
  const ll = [num1(node, "LongitudeDegrees"), num1(node, "LatitudeDegrees")];
358
369
  if (ll[0] === undefined ||
359
- isNaN(ll[0]) ||
370
+ Number.isNaN(ll[0]) ||
360
371
  ll[1] === undefined ||
361
- isNaN(ll[1])) {
372
+ Number.isNaN(ll[1])) {
362
373
  return null;
363
374
  }
364
375
  const heartRate = get1(node, "HeartRateBpm");
365
376
  const time = nodeVal(get1(node, "Time"));
366
377
  get1(node, "AltitudeMeters", (alt) => {
367
- const a = parseFloat(nodeVal(alt));
368
- if (!isNaN(a)) {
378
+ const a = Number.parseFloat(nodeVal(alt));
379
+ if (!Number.isNaN(a)) {
369
380
  ll.push(a);
370
381
  }
371
382
  });
372
383
  return {
373
384
  coordinates: ll,
374
385
  time: time || null,
375
- heartRate: heartRate ? parseFloat(nodeVal(heartRate)) : null,
386
+ heartRate: heartRate ? Number.parseFloat(nodeVal(heartRate)) : null,
376
387
  extensions: getProperties(node, TRACKPOINT_ATTRIBUTES),
377
388
  };
378
389
  }
@@ -504,17 +515,18 @@ function tcx(node) {
504
515
 
505
516
  function fixColor(v, prefix) {
506
517
  const properties = {};
507
- const colorProp = prefix == "stroke" || prefix === "fill" ? prefix : prefix + "-color";
518
+ const colorProp = prefix === "stroke" || prefix === "fill" ? prefix : `${prefix}-color`;
508
519
  if (v[0] === "#") {
509
520
  v = v.substring(1);
510
521
  }
511
522
  if (v.length === 6 || v.length === 3) {
512
- properties[colorProp] = "#" + v;
523
+ properties[colorProp] = `#${v}`;
513
524
  }
514
525
  else if (v.length === 8) {
515
- properties[prefix + "-opacity"] = parseInt(v.substring(0, 2), 16) / 255;
526
+ properties[`${prefix}-opacity`] =
527
+ Number.parseInt(v.substring(0, 2), 16) / 255;
516
528
  properties[colorProp] =
517
- "#" + v.substring(6, 8) + v.substring(4, 6) + v.substring(2, 4);
529
+ `#${v.substring(6, 8)}${v.substring(4, 6)}${v.substring(2, 4)}`;
518
530
  }
519
531
  return properties;
520
532
  }
@@ -540,11 +552,11 @@ function extractIconHref(node) {
540
552
  function extractIcon(node) {
541
553
  return get(node, "IconStyle", (iconStyle) => {
542
554
  return Object.assign(getColor(iconStyle, "icon"), numericProperty(iconStyle, "scale", "icon-scale"), numericProperty(iconStyle, "heading", "icon-heading"), get(iconStyle, "hotSpot", (hotspot) => {
543
- const left = parseFloat(hotspot.getAttribute("x") || "");
544
- const top = parseFloat(hotspot.getAttribute("y") || "");
555
+ const left = Number.parseFloat(hotspot.getAttribute("x") || "");
556
+ const top = Number.parseFloat(hotspot.getAttribute("y") || "");
545
557
  const xunits = hotspot.getAttribute("xunits") || "";
546
558
  const yunits = hotspot.getAttribute("yunits") || "";
547
- if (!isNaN(left) && !isNaN(top))
559
+ if (!Number.isNaN(left) && !Number.isNaN(top))
548
560
  return {
549
561
  "icon-offset": [left, top],
550
562
  "icon-offset-units": [xunits, yunits],
@@ -578,71 +590,6 @@ function extractStyle(node) {
578
590
  return Object.assign({}, extractPoly(node), extractLine(node), extractLabel(node), extractIcon(node));
579
591
  }
580
592
 
581
- const toNumber = (x) => Number(x);
582
- const typeConverters = {
583
- string: (x) => x,
584
- int: toNumber,
585
- uint: toNumber,
586
- short: toNumber,
587
- ushort: toNumber,
588
- float: toNumber,
589
- double: toNumber,
590
- bool: (x) => Boolean(x),
591
- };
592
- function extractExtendedData(node, schema) {
593
- return get(node, "ExtendedData", (extendedData, properties) => {
594
- for (const data of $(extendedData, "Data")) {
595
- properties[data.getAttribute("name") || ""] = nodeVal(get1(data, "value"));
596
- }
597
- for (const simpleData of $(extendedData, "SimpleData")) {
598
- const name = simpleData.getAttribute("name") || "";
599
- const typeConverter = schema[name] || typeConverters.string;
600
- properties[name] = typeConverter(nodeVal(simpleData));
601
- }
602
- return properties;
603
- });
604
- }
605
- function getMaybeHTMLDescription(node) {
606
- const descriptionNode = get1(node, "description");
607
- for (const c of Array.from(descriptionNode?.childNodes || [])) {
608
- if (c.nodeType === 4) {
609
- return {
610
- description: {
611
- "@type": "html",
612
- value: nodeVal(c),
613
- },
614
- };
615
- }
616
- }
617
- return {};
618
- }
619
- function extractTimeSpan(node) {
620
- return get(node, "TimeSpan", (timeSpan) => {
621
- return {
622
- timespan: {
623
- begin: nodeVal(get1(timeSpan, "begin")),
624
- end: nodeVal(get1(timeSpan, "end")),
625
- },
626
- };
627
- });
628
- }
629
- function extractTimeStamp(node) {
630
- return get(node, "TimeStamp", (timeStamp) => {
631
- return { timestamp: nodeVal(get1(timeStamp, "when")) };
632
- });
633
- }
634
- function extractCascadedStyle(node, styleMap) {
635
- return val1(node, "styleUrl", (styleUrl) => {
636
- styleUrl = normalizeId(styleUrl);
637
- if (styleMap[styleUrl]) {
638
- return Object.assign({ styleUrl }, styleMap[styleUrl]);
639
- }
640
- // For backward-compatibility. Should we still include
641
- // styleUrl even if it's not resolved?
642
- return { styleUrl };
643
- });
644
- }
645
-
646
593
  const removeSpace = /\s*/g;
647
594
  const trimSpace = /^\s*|\s*$/g;
648
595
  const splitSpace = /\s+/;
@@ -653,8 +600,8 @@ function coord1(value) {
653
600
  return value
654
601
  .replace(removeSpace, "")
655
602
  .split(",")
656
- .map(parseFloat)
657
- .filter((num) => !isNaN(num))
603
+ .map(Number.parseFloat)
604
+ .filter((num) => !Number.isNaN(num))
658
605
  .slice(0, 3);
659
606
  }
660
607
  /**
@@ -675,7 +622,7 @@ function gxCoords(node) {
675
622
  elems = $ns(node, "coord", "*");
676
623
  }
677
624
  const coordinates = elems.map((elem) => {
678
- return nodeVal(elem).split(" ").map(parseFloat);
625
+ return nodeVal(elem).split(" ").map(Number.parseFloat);
679
626
  });
680
627
  if (coordinates.length === 0) {
681
628
  return null;
@@ -785,47 +732,69 @@ function getGeometry(node) {
785
732
  };
786
733
  }
787
734
 
788
- function geometryListToGeometry(geometries) {
789
- return geometries.length === 0
790
- ? null
791
- : geometries.length === 1
792
- ? geometries[0]
793
- : {
794
- type: "GeometryCollection",
795
- geometries,
796
- };
735
+ const toNumber = (x) => Number(x);
736
+ const typeConverters = {
737
+ string: (x) => x,
738
+ int: toNumber,
739
+ uint: toNumber,
740
+ short: toNumber,
741
+ ushort: toNumber,
742
+ float: toNumber,
743
+ double: toNumber,
744
+ bool: (x) => Boolean(x),
745
+ };
746
+ function extractExtendedData(node, schema) {
747
+ return get(node, "ExtendedData", (extendedData, properties) => {
748
+ for (const data of $(extendedData, "Data")) {
749
+ properties[data.getAttribute("name") || ""] = nodeVal(get1(data, "value"));
750
+ }
751
+ for (const simpleData of $(extendedData, "SimpleData")) {
752
+ const name = simpleData.getAttribute("name") || "";
753
+ const typeConverter = schema[name] || typeConverters.string;
754
+ properties[name] = typeConverter(nodeVal(simpleData));
755
+ }
756
+ return properties;
757
+ });
797
758
  }
798
- function getPlacemark(node, styleMap, schema, options) {
799
- const { coordTimes, geometries } = getGeometry(node);
800
- const geometry = geometryListToGeometry(geometries);
801
- if (!geometry && options.skipNullGeometry) {
802
- return null;
803
- }
804
- const feature = {
805
- type: "Feature",
806
- geometry,
807
- properties: Object.assign(getMulti(node, [
808
- "name",
809
- "address",
810
- "visibility",
811
- "open",
812
- "phoneNumber",
813
- "description",
814
- ]), getMaybeHTMLDescription(node), extractCascadedStyle(node, styleMap), extractStyle(node), extractExtendedData(node, schema), extractTimeSpan(node), extractTimeStamp(node), coordTimes.length
815
- ? {
816
- coordinateProperties: {
817
- times: coordTimes.length === 1 ? coordTimes[0] : coordTimes,
759
+ function getMaybeHTMLDescription(node) {
760
+ const descriptionNode = get1(node, "description");
761
+ for (const c of Array.from(descriptionNode?.childNodes || [])) {
762
+ if (c.nodeType === 4) {
763
+ return {
764
+ description: {
765
+ "@type": "html",
766
+ value: nodeVal(c),
818
767
  },
819
- }
820
- : {}),
821
- };
822
- if (feature.properties?.visibility !== undefined) {
823
- feature.properties.visibility = feature.properties.visibility !== "0";
768
+ };
769
+ }
824
770
  }
825
- const id = node.getAttribute("id");
826
- if (id !== null && id !== "")
827
- feature.id = id;
828
- return feature;
771
+ return {};
772
+ }
773
+ function extractTimeSpan(node) {
774
+ return get(node, "TimeSpan", (timeSpan) => {
775
+ return {
776
+ timespan: {
777
+ begin: nodeVal(get1(timeSpan, "begin")),
778
+ end: nodeVal(get1(timeSpan, "end")),
779
+ },
780
+ };
781
+ });
782
+ }
783
+ function extractTimeStamp(node) {
784
+ return get(node, "TimeStamp", (timeStamp) => {
785
+ return { timestamp: nodeVal(get1(timeStamp, "when")) };
786
+ });
787
+ }
788
+ function extractCascadedStyle(node, styleMap) {
789
+ return val1(node, "styleUrl", (styleUrl) => {
790
+ styleUrl = normalizeId(styleUrl);
791
+ if (styleMap[styleUrl]) {
792
+ return Object.assign({ styleUrl }, styleMap[styleUrl]);
793
+ }
794
+ // For backward-compatibility. Should we still include
795
+ // styleUrl even if it's not resolved?
796
+ return { styleUrl };
797
+ });
829
798
  }
830
799
 
831
800
  function getGroundOverlayBox(node) {
@@ -848,7 +817,7 @@ function rotateBox(bbox, coordinates, rotation) {
848
817
  coordinates[0].map((coordinate) => {
849
818
  const dy = coordinate[1] - center[1];
850
819
  const dx = coordinate[0] - center[0];
851
- const distance = Math.sqrt(Math.pow(dy, 2) + Math.pow(dx, 2));
820
+ const distance = Math.sqrt(dy ** 2 + dx ** 2);
852
821
  const angle = Math.atan2(dy, dx) + rotation * DEGREES_TO_RADIANS;
853
822
  return [
854
823
  center[0] + Math.cos(angle) * distance,
@@ -872,10 +841,10 @@ function getLatLonBox(node) {
872
841
  const bbox = [west, south, east, north];
873
842
  let coordinates = [
874
843
  [
875
- [west, north],
876
- [east, north],
877
- [east, south],
878
- [west, south],
844
+ [west, north], // top left
845
+ [east, north], // top right
846
+ [east, south], // top right
847
+ [west, south], // bottom left
879
848
  [west, north], // top left (again)
880
849
  ],
881
850
  ];
@@ -928,6 +897,49 @@ function getGroundOverlay(node, styleMap, schema, options) {
928
897
  return feature;
929
898
  }
930
899
 
900
+ function geometryListToGeometry(geometries) {
901
+ return geometries.length === 0
902
+ ? null
903
+ : geometries.length === 1
904
+ ? geometries[0]
905
+ : {
906
+ type: "GeometryCollection",
907
+ geometries,
908
+ };
909
+ }
910
+ function getPlacemark(node, styleMap, schema, options) {
911
+ const { coordTimes, geometries } = getGeometry(node);
912
+ const geometry = geometryListToGeometry(geometries);
913
+ if (!geometry && options.skipNullGeometry) {
914
+ return null;
915
+ }
916
+ const feature = {
917
+ type: "Feature",
918
+ geometry,
919
+ properties: Object.assign(getMulti(node, [
920
+ "name",
921
+ "address",
922
+ "visibility",
923
+ "open",
924
+ "phoneNumber",
925
+ "description",
926
+ ]), getMaybeHTMLDescription(node), extractCascadedStyle(node, styleMap), extractStyle(node), extractExtendedData(node, schema), extractTimeSpan(node), extractTimeStamp(node), coordTimes.length
927
+ ? {
928
+ coordinateProperties: {
929
+ times: coordTimes.length === 1 ? coordTimes[0] : coordTimes,
930
+ },
931
+ }
932
+ : {}),
933
+ };
934
+ if (feature.properties?.visibility !== undefined) {
935
+ feature.properties.visibility = feature.properties.visibility !== "0";
936
+ }
937
+ const id = node.getAttribute("id");
938
+ if (id !== null && id !== "")
939
+ feature.id = id;
940
+ return feature;
941
+ }
942
+
931
943
  function getStyleId(style) {
932
944
  let id = style.getAttribute("id");
933
945
  const parentNode = style.parentNode;
@@ -958,8 +970,7 @@ function buildSchema(node) {
958
970
  const schema = {};
959
971
  for (const field of $(node, "SimpleField")) {
960
972
  schema[field.getAttribute("name") || ""] =
961
- typeConverters[field.getAttribute("type") || ""] ||
962
- typeConverters["string"];
973
+ typeConverters[field.getAttribute("type") || ""] || typeConverters.string;
963
974
  }
964
975
  return schema;
965
976
  }