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