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