@tmcw/togeojson 6.0.0-0 → 6.0.1

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/dist/index.d.ts CHANGED
@@ -31,7 +31,7 @@ declare function tcxGen(node: Document): Generator<Feature>;
31
31
  */
32
32
  declare function tcx(node: Document): FeatureCollection;
33
33
 
34
- declare type F = Feature<Geometry | null>;
34
+ type F = Feature<Geometry | null>;
35
35
 
36
36
  /**
37
37
  * Options to customize KML output.
@@ -141,4 +141,4 @@ declare function kmlGen(node: Document, options?: KMLOptions): Generator<F>;
141
141
  */
142
142
  declare function kml(node: Document, options?: KMLOptions): FeatureCollection<Geometry | null>;
143
143
 
144
- export { F, Folder, KMLOptions, Root, gpx, gpxGen, kml, kmlGen, kmlWithFolders, tcx, tcxGen };
144
+ export { type F, type Folder, type KMLOptions, type Root, gpx, gpxGen, kml, kmlGen, kmlWithFolders, tcx, tcxGen };
@@ -1,7 +1,5 @@
1
1
  'use strict';
2
2
 
3
- Object.defineProperty(exports, '__esModule', { value: true });
4
-
5
3
  function $(element, tagName) {
6
4
  return Array.from(element.getElementsByTagName(tagName));
7
5
  }
@@ -16,7 +14,7 @@ function $ns(element, tagName, ns) {
16
14
  */
17
15
  function nodeVal(node) {
18
16
  node?.normalize();
19
- return (node && node.textContent) || "";
17
+ return node?.textContent || "";
20
18
  }
21
19
  /**
22
20
  * Get one Y child of X, if any, otherwise null
@@ -46,16 +44,16 @@ function val1(node, tagName, callback) {
46
44
  return {};
47
45
  }
48
46
  function $num(node, tagName, callback) {
49
- const val = parseFloat(nodeVal(get1(node, tagName)));
50
- if (isNaN(val))
47
+ const val = Number.parseFloat(nodeVal(get1(node, tagName)));
48
+ if (Number.isNaN(val))
51
49
  return undefined;
52
50
  if (val && callback)
53
51
  return callback(val) || {};
54
52
  return {};
55
53
  }
56
54
  function num1(node, tagName, callback) {
57
- const val = parseFloat(nodeVal(get1(node, tagName)));
58
- if (isNaN(val))
55
+ const val = Number.parseFloat(nodeVal(get1(node, tagName)));
56
+ if (Number.isNaN(val))
59
57
  return undefined;
60
58
  if (callback)
61
59
  callback(val);
@@ -74,20 +72,6 @@ function isElement(node) {
74
72
  return node?.nodeType === 1;
75
73
  }
76
74
 
77
- function getLineStyle(node) {
78
- return get(node, "line", (lineStyle) => {
79
- const val = Object.assign({}, val1(lineStyle, "color", (color) => {
80
- return { stroke: `#${color}` };
81
- }), $num(lineStyle, "opacity", (opacity) => {
82
- return { "stroke-opacity": opacity };
83
- }), $num(lineStyle, "width", (width) => {
84
- // GPX width is in mm, convert to px with 96 px per inch
85
- return { "stroke-width": (width * 96) / 25.4 };
86
- }));
87
- return val;
88
- });
89
- }
90
-
91
75
  function getExtensions(node) {
92
76
  let values = [];
93
77
  if (node === null)
@@ -112,16 +96,16 @@ function abbreviateName(name) {
112
96
  return ["heart", "gpxtpx:hr", "hr"].includes(name) ? "heart" : name;
113
97
  }
114
98
  function parseNumeric(val) {
115
- const num = parseFloat(val);
116
- return isNaN(num) ? val : num;
99
+ const num = Number.parseFloat(val);
100
+ return Number.isNaN(num) ? val : num;
117
101
  }
118
102
 
119
103
  function coordPair$1(node) {
120
104
  const ll = [
121
- parseFloat(node.getAttribute("lon") || ""),
122
- parseFloat(node.getAttribute("lat") || ""),
105
+ Number.parseFloat(node.getAttribute("lon") || ""),
106
+ Number.parseFloat(node.getAttribute("lat") || ""),
123
107
  ];
124
- if (isNaN(ll[0]) || isNaN(ll[1])) {
108
+ if (Number.isNaN(ll[0]) || Number.isNaN(ll[1])) {
125
109
  return null;
126
110
  }
127
111
  num1(node, "ele", (val) => {
@@ -135,6 +119,20 @@ function coordPair$1(node) {
135
119
  };
136
120
  }
137
121
 
122
+ function getLineStyle(node) {
123
+ return get(node, "line", (lineStyle) => {
124
+ const val = Object.assign({}, val1(lineStyle, "color", (color) => {
125
+ return { stroke: `#${color}` };
126
+ }), $num(lineStyle, "opacity", (opacity) => {
127
+ return { "stroke-opacity": opacity };
128
+ }), $num(lineStyle, "width", (width) => {
129
+ // GPX width is in mm, convert to px with 96 px per inch
130
+ return { "stroke-width": (width * 96) / 25.4 };
131
+ }));
132
+ return val;
133
+ });
134
+ }
135
+
138
136
  function extractProperties(ns, node) {
139
137
  const properties = getMulti(node, [
140
138
  "name",
@@ -173,7 +171,7 @@ function getPoints$1(node, pointname) {
173
171
  if (c.time)
174
172
  times.push(c.time);
175
173
  for (const [name, val] of c.extendedValues) {
176
- const plural = name === "heart" ? name : name.replace("gpxtpx:", "") + "s";
174
+ const plural = name === "heart" ? name : `${name.replace("gpxtpx:", "")}s`;
177
175
  if (!extendedValues[plural]) {
178
176
  extendedValues[plural] = Array(pts.length).fill(null);
179
177
  }
@@ -214,7 +212,7 @@ function getTrack(ns, node) {
214
212
  const line = getPoints$1(segment, "trkpt");
215
213
  if (line) {
216
214
  extractedLines.push(line);
217
- if (line.times && line.times.length)
215
+ if (line.times?.length)
218
216
  times.push(line.times);
219
217
  }
220
218
  }
@@ -361,8 +359,8 @@ function getProperties(node, attributeNames) {
361
359
  elem = elements[0];
362
360
  }
363
361
  }
364
- const val = parseFloat(nodeVal(elem));
365
- if (!isNaN(val)) {
362
+ const val = Number.parseFloat(nodeVal(elem));
363
+ if (!Number.isNaN(val)) {
366
364
  properties.push([alias, val]);
367
365
  }
368
366
  }
@@ -371,23 +369,23 @@ function getProperties(node, attributeNames) {
371
369
  function coordPair(node) {
372
370
  const ll = [num1(node, "LongitudeDegrees"), num1(node, "LatitudeDegrees")];
373
371
  if (ll[0] === undefined ||
374
- isNaN(ll[0]) ||
372
+ Number.isNaN(ll[0]) ||
375
373
  ll[1] === undefined ||
376
- isNaN(ll[1])) {
374
+ Number.isNaN(ll[1])) {
377
375
  return null;
378
376
  }
379
377
  const heartRate = get1(node, "HeartRateBpm");
380
378
  const time = nodeVal(get1(node, "Time"));
381
379
  get1(node, "AltitudeMeters", (alt) => {
382
- const a = parseFloat(nodeVal(alt));
383
- if (!isNaN(a)) {
380
+ const a = Number.parseFloat(nodeVal(alt));
381
+ if (!Number.isNaN(a)) {
384
382
  ll.push(a);
385
383
  }
386
384
  });
387
385
  return {
388
386
  coordinates: ll,
389
387
  time: time || null,
390
- heartRate: heartRate ? parseFloat(nodeVal(heartRate)) : null,
388
+ heartRate: heartRate ? Number.parseFloat(nodeVal(heartRate)) : null,
391
389
  extensions: getProperties(node, TRACKPOINT_ATTRIBUTES),
392
390
  };
393
391
  }
@@ -519,17 +517,18 @@ function tcx(node) {
519
517
 
520
518
  function fixColor(v, prefix) {
521
519
  const properties = {};
522
- const colorProp = prefix == "stroke" || prefix === "fill" ? prefix : prefix + "-color";
520
+ const colorProp = prefix === "stroke" || prefix === "fill" ? prefix : `${prefix}-color`;
523
521
  if (v[0] === "#") {
524
522
  v = v.substring(1);
525
523
  }
526
524
  if (v.length === 6 || v.length === 3) {
527
- properties[colorProp] = "#" + v;
525
+ properties[colorProp] = `#${v}`;
528
526
  }
529
527
  else if (v.length === 8) {
530
- properties[prefix + "-opacity"] = parseInt(v.substring(0, 2), 16) / 255;
528
+ properties[`${prefix}-opacity`] =
529
+ Number.parseInt(v.substring(0, 2), 16) / 255;
531
530
  properties[colorProp] =
532
- "#" + v.substring(6, 8) + v.substring(4, 6) + v.substring(2, 4);
531
+ `#${v.substring(6, 8)}${v.substring(4, 6)}${v.substring(2, 4)}`;
533
532
  }
534
533
  return properties;
535
534
  }
@@ -555,11 +554,11 @@ function extractIconHref(node) {
555
554
  function extractIcon(node) {
556
555
  return get(node, "IconStyle", (iconStyle) => {
557
556
  return Object.assign(getColor(iconStyle, "icon"), numericProperty(iconStyle, "scale", "icon-scale"), numericProperty(iconStyle, "heading", "icon-heading"), get(iconStyle, "hotSpot", (hotspot) => {
558
- const left = parseFloat(hotspot.getAttribute("x") || "");
559
- const top = parseFloat(hotspot.getAttribute("y") || "");
557
+ const left = Number.parseFloat(hotspot.getAttribute("x") || "");
558
+ const top = Number.parseFloat(hotspot.getAttribute("y") || "");
560
559
  const xunits = hotspot.getAttribute("xunits") || "";
561
560
  const yunits = hotspot.getAttribute("yunits") || "";
562
- if (!isNaN(left) && !isNaN(top))
561
+ if (!Number.isNaN(left) && !Number.isNaN(top))
563
562
  return {
564
563
  "icon-offset": [left, top],
565
564
  "icon-offset-units": [xunits, yunits],
@@ -593,71 +592,6 @@ function extractStyle(node) {
593
592
  return Object.assign({}, extractPoly(node), extractLine(node), extractLabel(node), extractIcon(node));
594
593
  }
595
594
 
596
- const toNumber = (x) => Number(x);
597
- const typeConverters = {
598
- string: (x) => x,
599
- int: toNumber,
600
- uint: toNumber,
601
- short: toNumber,
602
- ushort: toNumber,
603
- float: toNumber,
604
- double: toNumber,
605
- bool: (x) => Boolean(x),
606
- };
607
- function extractExtendedData(node, schema) {
608
- return get(node, "ExtendedData", (extendedData, properties) => {
609
- for (const data of $(extendedData, "Data")) {
610
- properties[data.getAttribute("name") || ""] = nodeVal(get1(data, "value"));
611
- }
612
- for (const simpleData of $(extendedData, "SimpleData")) {
613
- const name = simpleData.getAttribute("name") || "";
614
- const typeConverter = schema[name] || typeConverters.string;
615
- properties[name] = typeConverter(nodeVal(simpleData));
616
- }
617
- return properties;
618
- });
619
- }
620
- function getMaybeHTMLDescription(node) {
621
- const descriptionNode = get1(node, "description");
622
- for (const c of Array.from(descriptionNode?.childNodes || [])) {
623
- if (c.nodeType === 4) {
624
- return {
625
- description: {
626
- "@type": "html",
627
- value: nodeVal(c),
628
- },
629
- };
630
- }
631
- }
632
- return {};
633
- }
634
- function extractTimeSpan(node) {
635
- return get(node, "TimeSpan", (timeSpan) => {
636
- return {
637
- timespan: {
638
- begin: nodeVal(get1(timeSpan, "begin")),
639
- end: nodeVal(get1(timeSpan, "end")),
640
- },
641
- };
642
- });
643
- }
644
- function extractTimeStamp(node) {
645
- return get(node, "TimeStamp", (timeStamp) => {
646
- return { timestamp: nodeVal(get1(timeStamp, "when")) };
647
- });
648
- }
649
- function extractCascadedStyle(node, styleMap) {
650
- return val1(node, "styleUrl", (styleUrl) => {
651
- styleUrl = normalizeId(styleUrl);
652
- if (styleMap[styleUrl]) {
653
- return Object.assign({ styleUrl }, styleMap[styleUrl]);
654
- }
655
- // For backward-compatibility. Should we still include
656
- // styleUrl even if it's not resolved?
657
- return { styleUrl };
658
- });
659
- }
660
-
661
595
  const removeSpace = /\s*/g;
662
596
  const trimSpace = /^\s*|\s*$/g;
663
597
  const splitSpace = /\s+/;
@@ -668,8 +602,8 @@ function coord1(value) {
668
602
  return value
669
603
  .replace(removeSpace, "")
670
604
  .split(",")
671
- .map(parseFloat)
672
- .filter((num) => !isNaN(num))
605
+ .map(Number.parseFloat)
606
+ .filter((num) => !Number.isNaN(num))
673
607
  .slice(0, 3);
674
608
  }
675
609
  /**
@@ -690,7 +624,7 @@ function gxCoords(node) {
690
624
  elems = $ns(node, "coord", "*");
691
625
  }
692
626
  const coordinates = elems.map((elem) => {
693
- return nodeVal(elem).split(" ").map(parseFloat);
627
+ return nodeVal(elem).split(" ").map(Number.parseFloat);
694
628
  });
695
629
  if (coordinates.length === 0) {
696
630
  return null;
@@ -800,47 +734,69 @@ function getGeometry(node) {
800
734
  };
801
735
  }
802
736
 
803
- function geometryListToGeometry(geometries) {
804
- return geometries.length === 0
805
- ? null
806
- : geometries.length === 1
807
- ? geometries[0]
808
- : {
809
- type: "GeometryCollection",
810
- geometries,
811
- };
737
+ const toNumber = (x) => Number(x);
738
+ const typeConverters = {
739
+ string: (x) => x,
740
+ int: toNumber,
741
+ uint: toNumber,
742
+ short: toNumber,
743
+ ushort: toNumber,
744
+ float: toNumber,
745
+ double: toNumber,
746
+ bool: (x) => Boolean(x),
747
+ };
748
+ function extractExtendedData(node, schema) {
749
+ return get(node, "ExtendedData", (extendedData, properties) => {
750
+ for (const data of $(extendedData, "Data")) {
751
+ properties[data.getAttribute("name") || ""] = nodeVal(get1(data, "value"));
752
+ }
753
+ for (const simpleData of $(extendedData, "SimpleData")) {
754
+ const name = simpleData.getAttribute("name") || "";
755
+ const typeConverter = schema[name] || typeConverters.string;
756
+ properties[name] = typeConverter(nodeVal(simpleData));
757
+ }
758
+ return properties;
759
+ });
812
760
  }
813
- function getPlacemark(node, styleMap, schema, options) {
814
- const { coordTimes, geometries } = getGeometry(node);
815
- const geometry = geometryListToGeometry(geometries);
816
- if (!geometry && options.skipNullGeometry) {
817
- return null;
818
- }
819
- const feature = {
820
- type: "Feature",
821
- geometry,
822
- properties: Object.assign(getMulti(node, [
823
- "name",
824
- "address",
825
- "visibility",
826
- "open",
827
- "phoneNumber",
828
- "description",
829
- ]), getMaybeHTMLDescription(node), extractCascadedStyle(node, styleMap), extractStyle(node), extractExtendedData(node, schema), extractTimeSpan(node), extractTimeStamp(node), coordTimes.length
830
- ? {
831
- coordinateProperties: {
832
- times: coordTimes.length === 1 ? coordTimes[0] : coordTimes,
761
+ function getMaybeHTMLDescription(node) {
762
+ const descriptionNode = get1(node, "description");
763
+ for (const c of Array.from(descriptionNode?.childNodes || [])) {
764
+ if (c.nodeType === 4) {
765
+ return {
766
+ description: {
767
+ "@type": "html",
768
+ value: nodeVal(c),
833
769
  },
834
- }
835
- : {}),
836
- };
837
- if (feature.properties?.visibility !== undefined) {
838
- feature.properties.visibility = feature.properties.visibility !== "0";
770
+ };
771
+ }
839
772
  }
840
- const id = node.getAttribute("id");
841
- if (id !== null && id !== "")
842
- feature.id = id;
843
- return feature;
773
+ return {};
774
+ }
775
+ function extractTimeSpan(node) {
776
+ return get(node, "TimeSpan", (timeSpan) => {
777
+ return {
778
+ timespan: {
779
+ begin: nodeVal(get1(timeSpan, "begin")),
780
+ end: nodeVal(get1(timeSpan, "end")),
781
+ },
782
+ };
783
+ });
784
+ }
785
+ function extractTimeStamp(node) {
786
+ return get(node, "TimeStamp", (timeStamp) => {
787
+ return { timestamp: nodeVal(get1(timeStamp, "when")) };
788
+ });
789
+ }
790
+ function extractCascadedStyle(node, styleMap) {
791
+ return val1(node, "styleUrl", (styleUrl) => {
792
+ styleUrl = normalizeId(styleUrl);
793
+ if (styleMap[styleUrl]) {
794
+ return Object.assign({ styleUrl }, styleMap[styleUrl]);
795
+ }
796
+ // For backward-compatibility. Should we still include
797
+ // styleUrl even if it's not resolved?
798
+ return { styleUrl };
799
+ });
844
800
  }
845
801
 
846
802
  function getGroundOverlayBox(node) {
@@ -863,7 +819,7 @@ function rotateBox(bbox, coordinates, rotation) {
863
819
  coordinates[0].map((coordinate) => {
864
820
  const dy = coordinate[1] - center[1];
865
821
  const dx = coordinate[0] - center[0];
866
- const distance = Math.sqrt(Math.pow(dy, 2) + Math.pow(dx, 2));
822
+ const distance = Math.sqrt(dy ** 2 + dx ** 2);
867
823
  const angle = Math.atan2(dy, dx) + rotation * DEGREES_TO_RADIANS;
868
824
  return [
869
825
  center[0] + Math.cos(angle) * distance,
@@ -887,10 +843,10 @@ function getLatLonBox(node) {
887
843
  const bbox = [west, south, east, north];
888
844
  let coordinates = [
889
845
  [
890
- [west, north],
891
- [east, north],
892
- [east, south],
893
- [west, south],
846
+ [west, north], // top left
847
+ [east, north], // top right
848
+ [east, south], // top right
849
+ [west, south], // bottom left
894
850
  [west, north], // top left (again)
895
851
  ],
896
852
  ];
@@ -943,6 +899,49 @@ function getGroundOverlay(node, styleMap, schema, options) {
943
899
  return feature;
944
900
  }
945
901
 
902
+ function geometryListToGeometry(geometries) {
903
+ return geometries.length === 0
904
+ ? null
905
+ : geometries.length === 1
906
+ ? geometries[0]
907
+ : {
908
+ type: "GeometryCollection",
909
+ geometries,
910
+ };
911
+ }
912
+ function getPlacemark(node, styleMap, schema, options) {
913
+ const { coordTimes, geometries } = getGeometry(node);
914
+ const geometry = geometryListToGeometry(geometries);
915
+ if (!geometry && options.skipNullGeometry) {
916
+ return null;
917
+ }
918
+ const feature = {
919
+ type: "Feature",
920
+ geometry,
921
+ properties: Object.assign(getMulti(node, [
922
+ "name",
923
+ "address",
924
+ "visibility",
925
+ "open",
926
+ "phoneNumber",
927
+ "description",
928
+ ]), getMaybeHTMLDescription(node), extractCascadedStyle(node, styleMap), extractStyle(node), extractExtendedData(node, schema), extractTimeSpan(node), extractTimeStamp(node), coordTimes.length
929
+ ? {
930
+ coordinateProperties: {
931
+ times: coordTimes.length === 1 ? coordTimes[0] : coordTimes,
932
+ },
933
+ }
934
+ : {}),
935
+ };
936
+ if (feature.properties?.visibility !== undefined) {
937
+ feature.properties.visibility = feature.properties.visibility !== "0";
938
+ }
939
+ const id = node.getAttribute("id");
940
+ if (id !== null && id !== "")
941
+ feature.id = id;
942
+ return feature;
943
+ }
944
+
946
945
  function getStyleId(style) {
947
946
  let id = style.getAttribute("id");
948
947
  const parentNode = style.parentNode;
@@ -973,8 +972,7 @@ function buildSchema(node) {
973
972
  const schema = {};
974
973
  for (const field of $(node, "SimpleField")) {
975
974
  schema[field.getAttribute("name") || ""] =
976
- typeConverters[field.getAttribute("type") || ""] ||
977
- typeConverters["string"];
975
+ typeConverters[field.getAttribute("type") || ""] || typeConverters.string;
978
976
  }
979
977
  return schema;
980
978
  }