@ac-kit/format-gpx 0.0.0-stage → 0.1.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 +45 -0
- package/LICENSE +28 -0
- package/README.md +70 -2
- package/dist/errors.d.ts +74 -0
- package/dist/errors.d.ts.map +1 -0
- package/dist/errors.js +34 -0
- package/dist/errors.js.map +1 -0
- package/dist/geometry/gpx-to-geometries.d.ts +54 -0
- package/dist/geometry/gpx-to-geometries.d.ts.map +1 -0
- package/dist/geometry/gpx-to-geometries.js +84 -0
- package/dist/geometry/gpx-to-geometries.js.map +1 -0
- package/dist/gpx-namespaces.d.ts +9 -0
- package/dist/gpx-namespaces.d.ts.map +1 -0
- package/dist/gpx-namespaces.js +9 -0
- package/dist/gpx-namespaces.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +10 -0
- package/dist/index.js.map +1 -0
- package/dist/limits.d.ts +38 -0
- package/dist/limits.d.ts.map +1 -0
- package/dist/limits.js +7 -0
- package/dist/limits.js.map +1 -0
- package/dist/model/create-gpx.d.ts +32 -0
- package/dist/model/create-gpx.d.ts.map +1 -0
- package/dist/model/create-gpx.js +101 -0
- package/dist/model/create-gpx.js.map +1 -0
- package/dist/model/gpx-document.d.ts +169 -0
- package/dist/model/gpx-document.d.ts.map +1 -0
- package/dist/model/gpx-document.js +2 -0
- package/dist/model/gpx-document.js.map +1 -0
- package/dist/parse/_reader.d.ts +77 -0
- package/dist/parse/_reader.d.ts.map +1 -0
- package/dist/parse/_reader.js +1014 -0
- package/dist/parse/_reader.js.map +1 -0
- package/dist/parse/_shared.d.ts +24 -0
- package/dist/parse/_shared.d.ts.map +1 -0
- package/dist/parse/_shared.js +61 -0
- package/dist/parse/_shared.js.map +1 -0
- package/dist/parse/_sink.d.ts +5 -0
- package/dist/parse/_sink.d.ts.map +1 -0
- package/dist/parse/_sink.js +10 -0
- package/dist/parse/_sink.js.map +1 -0
- package/dist/parse/parse-gpx.d.ts +54 -0
- package/dist/parse/parse-gpx.d.ts.map +1 -0
- package/dist/parse/parse-gpx.js +130 -0
- package/dist/parse/parse-gpx.js.map +1 -0
- package/dist/parse/read-gpx-points.d.ts +80 -0
- package/dist/parse/read-gpx-points.d.ts.map +1 -0
- package/dist/parse/read-gpx-points.js +220 -0
- package/dist/parse/read-gpx-points.js.map +1 -0
- package/dist/print/print-gpx.d.ts +31 -0
- package/dist/print/print-gpx.d.ts.map +1 -0
- package/dist/print/print-gpx.js +290 -0
- package/dist/print/print-gpx.js.map +1 -0
- package/package.json +63 -3
|
@@ -0,0 +1,1014 @@
|
|
|
1
|
+
import { detachXmlElement, isXmlAttributeInNamespace, parseXsDateTime, parseXsGYear, trimXsdWhitespace, XMLNS_NAMESPACE, xmlTextContent, createXmlSequence, XmlSequenceChecker, XSI_NAMESPACE, xsDateTimeToTemporal, } from "@ac-kit/format-xml";
|
|
2
|
+
import { GpxLimitError } from "../errors.js";
|
|
3
|
+
import { GPX_1_0_NAMESPACE, GPX_1_1_NAMESPACE } from "../gpx-namespaces.js";
|
|
4
|
+
export const NONE_ = Object.freeze([]);
|
|
5
|
+
const DECIMAL_ = /^[+-]?(?:\d+(?:\.\d*)?|\.\d+)$/u;
|
|
6
|
+
const NON_NEGATIVE_INTEGER_ = /^\+?\d+$/u;
|
|
7
|
+
const FIXES_ = new Set([
|
|
8
|
+
"none",
|
|
9
|
+
"2d",
|
|
10
|
+
"3d",
|
|
11
|
+
"dgps",
|
|
12
|
+
"pps",
|
|
13
|
+
]);
|
|
14
|
+
const sequence_ = createXmlSequence;
|
|
15
|
+
export const GPX_11_SEQUENCE_ = sequence_([
|
|
16
|
+
"metadata",
|
|
17
|
+
"wpt*",
|
|
18
|
+
"rte*",
|
|
19
|
+
"trk*",
|
|
20
|
+
"extensions",
|
|
21
|
+
]);
|
|
22
|
+
export const GPX_10_SEQUENCE_ = sequence_([
|
|
23
|
+
"name",
|
|
24
|
+
"desc",
|
|
25
|
+
"author",
|
|
26
|
+
"email",
|
|
27
|
+
"url",
|
|
28
|
+
"urlname",
|
|
29
|
+
"time",
|
|
30
|
+
"keywords",
|
|
31
|
+
"bounds",
|
|
32
|
+
"wpt*",
|
|
33
|
+
"rte*",
|
|
34
|
+
"trk*",
|
|
35
|
+
]);
|
|
36
|
+
const METADATA_SEQUENCE_ = sequence_([
|
|
37
|
+
"name",
|
|
38
|
+
"desc",
|
|
39
|
+
"author",
|
|
40
|
+
"copyright",
|
|
41
|
+
"link*",
|
|
42
|
+
"time",
|
|
43
|
+
"keywords",
|
|
44
|
+
"bounds",
|
|
45
|
+
"extensions",
|
|
46
|
+
]);
|
|
47
|
+
const POINT_11_SEQUENCE_ = sequence_([
|
|
48
|
+
"ele",
|
|
49
|
+
"time",
|
|
50
|
+
"magvar",
|
|
51
|
+
"geoidheight",
|
|
52
|
+
"name",
|
|
53
|
+
"cmt",
|
|
54
|
+
"desc",
|
|
55
|
+
"src",
|
|
56
|
+
"link*",
|
|
57
|
+
"sym",
|
|
58
|
+
"type",
|
|
59
|
+
"fix",
|
|
60
|
+
"sat",
|
|
61
|
+
"hdop",
|
|
62
|
+
"vdop",
|
|
63
|
+
"pdop",
|
|
64
|
+
"ageofdgpsdata",
|
|
65
|
+
"dgpsid",
|
|
66
|
+
"extensions",
|
|
67
|
+
]);
|
|
68
|
+
const POINT_10_SEQUENCE_ = sequence_([
|
|
69
|
+
"ele",
|
|
70
|
+
"time",
|
|
71
|
+
"magvar",
|
|
72
|
+
"geoidheight",
|
|
73
|
+
"name",
|
|
74
|
+
"cmt",
|
|
75
|
+
"desc",
|
|
76
|
+
"src",
|
|
77
|
+
"url",
|
|
78
|
+
"urlname",
|
|
79
|
+
"sym",
|
|
80
|
+
"type",
|
|
81
|
+
"fix",
|
|
82
|
+
"sat",
|
|
83
|
+
"hdop",
|
|
84
|
+
"vdop",
|
|
85
|
+
"pdop",
|
|
86
|
+
"ageofdgpsdata",
|
|
87
|
+
"dgpsid",
|
|
88
|
+
]);
|
|
89
|
+
const TRACK_POINT_10_SEQUENCE_ = sequence_([
|
|
90
|
+
"ele",
|
|
91
|
+
"time",
|
|
92
|
+
"course",
|
|
93
|
+
"speed",
|
|
94
|
+
"magvar",
|
|
95
|
+
"geoidheight",
|
|
96
|
+
"name",
|
|
97
|
+
"cmt",
|
|
98
|
+
"desc",
|
|
99
|
+
"src",
|
|
100
|
+
"url",
|
|
101
|
+
"urlname",
|
|
102
|
+
"sym",
|
|
103
|
+
"type",
|
|
104
|
+
"fix",
|
|
105
|
+
"sat",
|
|
106
|
+
"hdop",
|
|
107
|
+
"vdop",
|
|
108
|
+
"pdop",
|
|
109
|
+
"ageofdgpsdata",
|
|
110
|
+
"dgpsid",
|
|
111
|
+
]);
|
|
112
|
+
export const ROUTE_11_SEQUENCE_ = sequence_([
|
|
113
|
+
"name",
|
|
114
|
+
"cmt",
|
|
115
|
+
"desc",
|
|
116
|
+
"src",
|
|
117
|
+
"link*",
|
|
118
|
+
"number",
|
|
119
|
+
"type",
|
|
120
|
+
"extensions",
|
|
121
|
+
"rtept*",
|
|
122
|
+
]);
|
|
123
|
+
export const ROUTE_10_SEQUENCE_ = sequence_([
|
|
124
|
+
"name",
|
|
125
|
+
"cmt",
|
|
126
|
+
"desc",
|
|
127
|
+
"src",
|
|
128
|
+
"url",
|
|
129
|
+
"urlname",
|
|
130
|
+
"number",
|
|
131
|
+
"rtept*",
|
|
132
|
+
]);
|
|
133
|
+
const TRACK_11_SEQUENCE_ = sequence_([
|
|
134
|
+
"name",
|
|
135
|
+
"cmt",
|
|
136
|
+
"desc",
|
|
137
|
+
"src",
|
|
138
|
+
"link*",
|
|
139
|
+
"number",
|
|
140
|
+
"type",
|
|
141
|
+
"extensions",
|
|
142
|
+
"trkseg*",
|
|
143
|
+
]);
|
|
144
|
+
const TRACK_10_SEQUENCE_ = sequence_([
|
|
145
|
+
"name",
|
|
146
|
+
"cmt",
|
|
147
|
+
"desc",
|
|
148
|
+
"src",
|
|
149
|
+
"url",
|
|
150
|
+
"urlname",
|
|
151
|
+
"number",
|
|
152
|
+
"trkseg*",
|
|
153
|
+
]);
|
|
154
|
+
export const SEGMENT_11_SEQUENCE_ = sequence_(["trkpt*", "extensions"]);
|
|
155
|
+
export const SEGMENT_10_SEQUENCE_ = sequence_(["trkpt*"]);
|
|
156
|
+
const PERSON_SEQUENCE_ = sequence_(["name", "email", "link"]);
|
|
157
|
+
const EMPTY_SEQUENCE_ = sequence_([]);
|
|
158
|
+
const COPYRIGHT_SEQUENCE_ = sequence_(["year", "license"]);
|
|
159
|
+
const LINK_SEQUENCE_ = sequence_(["text", "type"]);
|
|
160
|
+
/**
|
|
161
|
+
* Checks children against a sequence as they come, one at a time, reporting in
|
|
162
|
+
* GPX's terms.
|
|
163
|
+
*/
|
|
164
|
+
export class SequenceChecker_ {
|
|
165
|
+
context;
|
|
166
|
+
parent;
|
|
167
|
+
checker;
|
|
168
|
+
constructor(context, sequence, parent) {
|
|
169
|
+
this.context = context;
|
|
170
|
+
this.parent = parent;
|
|
171
|
+
this.checker = new XmlSequenceChecker(sequence);
|
|
172
|
+
}
|
|
173
|
+
/**
|
|
174
|
+
* Whether the child named `local` is to be read: `false` for an unknown or a
|
|
175
|
+
* repeated one, reported (strict mode throws), whose element the caller keeps
|
|
176
|
+
* in `otherElements`. An out-of-order child is reported and read.
|
|
177
|
+
*/
|
|
178
|
+
accept(local, span) {
|
|
179
|
+
const result = this.checker.accept(local);
|
|
180
|
+
if (result === "accepted") {
|
|
181
|
+
return true;
|
|
182
|
+
}
|
|
183
|
+
this.context.sink.violation("unexpected-element", result === "unknown"
|
|
184
|
+
? `<${local}> is not allowed in <${this.parent}>`
|
|
185
|
+
: result === "repeated"
|
|
186
|
+
? `<${this.parent}> has more than one <${local}>`
|
|
187
|
+
: `<${local}> is out of order in <${this.parent}>`, span);
|
|
188
|
+
return result === "out-of-order";
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
/**
|
|
192
|
+
* Sorts `element`'s children: GPX elements accepted by `sequence` in order,
|
|
193
|
+
* extensions (GPX 1.1's `<extensions>` children, GPX 1.0's foreign elements)
|
|
194
|
+
* and the rest. Charges one unit per child element.
|
|
195
|
+
*/
|
|
196
|
+
function children_(context, element, sequence, checkOrder = true) {
|
|
197
|
+
const result = { gpx: [], extensions: [], otherElements: [] };
|
|
198
|
+
const checker = checkOrder
|
|
199
|
+
? new SequenceChecker_(context, sequence, element.name.local)
|
|
200
|
+
: null;
|
|
201
|
+
for (const child of element.children) {
|
|
202
|
+
if (child.kind === "text" || child.kind === "cdata") {
|
|
203
|
+
if (trimXsdWhitespace(child.value) !== "") {
|
|
204
|
+
context.sink.violation("unexpected-element", `<${element.name.local}> holds text`, child.span ?? element.span ?? null);
|
|
205
|
+
}
|
|
206
|
+
continue;
|
|
207
|
+
}
|
|
208
|
+
if (child.kind !== "element") {
|
|
209
|
+
continue;
|
|
210
|
+
}
|
|
211
|
+
context.budget?.spend(1);
|
|
212
|
+
const span = child.span ?? element.span ?? null;
|
|
213
|
+
if (!isGpxElement_(context, child)) {
|
|
214
|
+
if (context.version === "1.0") {
|
|
215
|
+
result.extensions.push(detachXmlElement(context.namespaces, child));
|
|
216
|
+
}
|
|
217
|
+
else {
|
|
218
|
+
context.sink.violation("unexpected-element", `<${child.name.local}> of another namespace outside <extensions>`, span);
|
|
219
|
+
result.otherElements.push(detachXmlElement(context.namespaces, child));
|
|
220
|
+
}
|
|
221
|
+
continue;
|
|
222
|
+
}
|
|
223
|
+
const local = child.name.local;
|
|
224
|
+
if (checker !== null && !checker.accept(local, span)) {
|
|
225
|
+
result.otherElements.push(detachXmlElement(context.namespaces, child));
|
|
226
|
+
continue;
|
|
227
|
+
}
|
|
228
|
+
// Checked as it streamed by: refused then, kept silently now.
|
|
229
|
+
if (checker === null &&
|
|
230
|
+
(!sequence.slots.has(local) ||
|
|
231
|
+
context.rejected?.has(child.span?.start ?? -1) === true)) {
|
|
232
|
+
result.otherElements.push(detachXmlElement(context.namespaces, child));
|
|
233
|
+
continue;
|
|
234
|
+
}
|
|
235
|
+
if (local === "extensions" && context.version === "1.1") {
|
|
236
|
+
for (const extension of childElements_(child)) {
|
|
237
|
+
context.budget?.spend(1);
|
|
238
|
+
result.extensions.push(detachXmlElement(context.namespaces, extension));
|
|
239
|
+
}
|
|
240
|
+
continue;
|
|
241
|
+
}
|
|
242
|
+
result.gpx.push({ local, element: child });
|
|
243
|
+
}
|
|
244
|
+
return result;
|
|
245
|
+
}
|
|
246
|
+
function childElements_(element) {
|
|
247
|
+
return element.children.filter((child) => child.kind === "element");
|
|
248
|
+
}
|
|
249
|
+
export function isGpxElement_(context, element) {
|
|
250
|
+
const namespace = context.namespaces.namespaceOf(element);
|
|
251
|
+
return context.gpxNamespace === null
|
|
252
|
+
? namespace === null && element.name.prefix === null
|
|
253
|
+
: namespace === context.gpxNamespace;
|
|
254
|
+
}
|
|
255
|
+
// The attributes in no namespace `known` names, and the others (namespace
|
|
256
|
+
// declarations left out).
|
|
257
|
+
function attributes_(context, element, known) {
|
|
258
|
+
if (element.attributes.length === 0) {
|
|
259
|
+
return { values: new Map(), others: NONE_ };
|
|
260
|
+
}
|
|
261
|
+
const values = new Map();
|
|
262
|
+
const others = [];
|
|
263
|
+
for (const attribute of element.attributes) {
|
|
264
|
+
const namespace = context.namespaces.attributeNamespaceOf(attribute);
|
|
265
|
+
if (namespace === XMLNS_NAMESPACE) {
|
|
266
|
+
continue;
|
|
267
|
+
}
|
|
268
|
+
const span = attribute.span ?? element.span ?? null;
|
|
269
|
+
if (isXmlAttributeInNamespace(context.namespaces, attribute, null)) {
|
|
270
|
+
const local = attribute.name.local;
|
|
271
|
+
if (known.includes(local)) {
|
|
272
|
+
values.set(local, { value: attribute.value, span });
|
|
273
|
+
continue;
|
|
274
|
+
}
|
|
275
|
+
context.sink.violation("unexpected-attribute", `<${element.name.local}> has no attribute ${local}`, span);
|
|
276
|
+
}
|
|
277
|
+
others.push(attribute);
|
|
278
|
+
}
|
|
279
|
+
return { values, others: others.length === 0 ? NONE_ : others };
|
|
280
|
+
}
|
|
281
|
+
function spanOf_(element, fallback) {
|
|
282
|
+
return element.span ?? fallback;
|
|
283
|
+
}
|
|
284
|
+
function text_(context, element) {
|
|
285
|
+
if (element.children.some((child) => child.kind === "element")) {
|
|
286
|
+
context.sink.violation("invalid-value", `<${element.name.local}> holds elements where text is expected`, element.span ?? null);
|
|
287
|
+
}
|
|
288
|
+
return xmlTextContent(element);
|
|
289
|
+
}
|
|
290
|
+
// A number as its schema type reads it; `undefined` when unreadable (reported).
|
|
291
|
+
export function number_(context, text, what, span, range = {}) {
|
|
292
|
+
const trimmed = trimXsdWhitespace(text);
|
|
293
|
+
const lexical = range.integer === true ? NON_NEGATIVE_INTEGER_ : DECIMAL_;
|
|
294
|
+
const value = Number(trimmed);
|
|
295
|
+
if (!lexical.test(trimmed)) {
|
|
296
|
+
const readable = trimmed !== "" &&
|
|
297
|
+
Number.isFinite(value) &&
|
|
298
|
+
(range.integer !== true || Number.isSafeInteger(value));
|
|
299
|
+
context.sink.violation("invalid-value", `${what} "${trimmed}" is not ${range.integer === true ? "a non-negative integer" : "a decimal"}`, span);
|
|
300
|
+
if (!readable) {
|
|
301
|
+
return undefined;
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
if (range.integer === true && !Number.isSafeInteger(value)) {
|
|
305
|
+
context.sink.violation("invalid-value", `${what} ${trimmed} is past 2^53-1`, span);
|
|
306
|
+
return undefined;
|
|
307
|
+
}
|
|
308
|
+
if ((range.min !== undefined && value < range.min) ||
|
|
309
|
+
(range.max !== undefined &&
|
|
310
|
+
(range.maxExclusive === true ? value >= range.max : value > range.max))) {
|
|
311
|
+
context.sink.violation("invalid-value", `${what} ${trimmed} is outside ${range.min ?? "-∞"} to ${range.max ?? "∞"}${range.maxExclusive === true ? " (excluded)" : ""}`, span);
|
|
312
|
+
}
|
|
313
|
+
return value;
|
|
314
|
+
}
|
|
315
|
+
export function time_(context, text, span) {
|
|
316
|
+
const trimmed = trimXsdWhitespace(text);
|
|
317
|
+
const lexical = parseXsDateTime(trimmed);
|
|
318
|
+
if (lexical === null) {
|
|
319
|
+
context.sink.violation("invalid-value", `"${trimmed}" is not an xs:dateTime`, span);
|
|
320
|
+
return undefined;
|
|
321
|
+
}
|
|
322
|
+
let value;
|
|
323
|
+
try {
|
|
324
|
+
value = xsDateTimeToTemporal(lexical);
|
|
325
|
+
}
|
|
326
|
+
catch (error) {
|
|
327
|
+
if (!(error instanceof RangeError)) {
|
|
328
|
+
throw error;
|
|
329
|
+
}
|
|
330
|
+
context.sink.violation("invalid-value", `"${trimmed}" is outside the range of an instant`, span);
|
|
331
|
+
return undefined;
|
|
332
|
+
}
|
|
333
|
+
if (value instanceof Temporal.PlainDateTime) {
|
|
334
|
+
context.sink.advisory("missing-time-zone", `"${trimmed}" has no timezone; read as UTC`, span);
|
|
335
|
+
return value.toZonedDateTime("UTC").toInstant();
|
|
336
|
+
}
|
|
337
|
+
return value;
|
|
338
|
+
}
|
|
339
|
+
function required_(context, attributes, name, element, span) {
|
|
340
|
+
const attribute = attributes.values.get(name);
|
|
341
|
+
if (attribute === undefined) {
|
|
342
|
+
context.sink.violation("missing-attribute", `<${element.name.local}> has no ${name}`, span);
|
|
343
|
+
}
|
|
344
|
+
return attribute;
|
|
345
|
+
}
|
|
346
|
+
export function countPoint_(context) {
|
|
347
|
+
context.points++;
|
|
348
|
+
if (context.points > context.maxPoints) {
|
|
349
|
+
throw new GpxLimitError("maxPoints", context.maxPoints);
|
|
350
|
+
}
|
|
351
|
+
}
|
|
352
|
+
/**
|
|
353
|
+
* A point (`wpt`, `rtept` or `trkpt`); `null` for one without a readable
|
|
354
|
+
* position in lenient mode.
|
|
355
|
+
*/
|
|
356
|
+
export function readPoint_(context, element, kind, fallback) {
|
|
357
|
+
context.budget?.spend(1);
|
|
358
|
+
countPoint_(context);
|
|
359
|
+
const span = spanOf_(element, fallback);
|
|
360
|
+
const attributes = attributes_(context, element, ["lat", "lon"]);
|
|
361
|
+
const latitudeText = required_(context, attributes, "lat", element, span);
|
|
362
|
+
const longitudeText = required_(context, attributes, "lon", element, span);
|
|
363
|
+
const latitude = latitudeText === undefined
|
|
364
|
+
? undefined
|
|
365
|
+
: number_(context, latitudeText.value, "Latitude", latitudeText.span, {
|
|
366
|
+
min: -90,
|
|
367
|
+
max: 90,
|
|
368
|
+
});
|
|
369
|
+
const longitude = longitudeText === undefined
|
|
370
|
+
? undefined
|
|
371
|
+
: number_(context, longitudeText.value, "Longitude", longitudeText.span, {
|
|
372
|
+
min: -180,
|
|
373
|
+
max: 180,
|
|
374
|
+
maxExclusive: true,
|
|
375
|
+
});
|
|
376
|
+
const sequence = context.version === "1.1"
|
|
377
|
+
? POINT_11_SEQUENCE_
|
|
378
|
+
: kind === "trkpt"
|
|
379
|
+
? TRACK_POINT_10_SEQUENCE_
|
|
380
|
+
: POINT_10_SEQUENCE_;
|
|
381
|
+
const children = children_(context, element, sequence);
|
|
382
|
+
const point = {
|
|
383
|
+
latitude: latitude ?? Number.NaN,
|
|
384
|
+
longitude: longitude ?? Number.NaN,
|
|
385
|
+
elevation: null,
|
|
386
|
+
time: null,
|
|
387
|
+
magneticVariation: null,
|
|
388
|
+
geoidHeight: null,
|
|
389
|
+
name: null,
|
|
390
|
+
comment: null,
|
|
391
|
+
description: null,
|
|
392
|
+
source: null,
|
|
393
|
+
links: NONE_,
|
|
394
|
+
symbol: null,
|
|
395
|
+
type: null,
|
|
396
|
+
fix: null,
|
|
397
|
+
satellites: null,
|
|
398
|
+
hdop: null,
|
|
399
|
+
vdop: null,
|
|
400
|
+
pdop: null,
|
|
401
|
+
ageOfDgpsData: null,
|
|
402
|
+
dgpsId: null,
|
|
403
|
+
course: null,
|
|
404
|
+
speed: null,
|
|
405
|
+
extensions: children.extensions.length === 0 ? NONE_ : children.extensions,
|
|
406
|
+
otherElements: children.otherElements,
|
|
407
|
+
otherAttributes: attributes.others,
|
|
408
|
+
span,
|
|
409
|
+
};
|
|
410
|
+
const links = new LinkCollector_(context);
|
|
411
|
+
for (const { local, element: child } of children.gpx) {
|
|
412
|
+
const childSpan = spanOf_(child, span);
|
|
413
|
+
if (local === "link" || local === "url" || local === "urlname") {
|
|
414
|
+
links.add(local, child, childSpan);
|
|
415
|
+
continue;
|
|
416
|
+
}
|
|
417
|
+
if (!readPointField_(context, point, local, child, childSpan)) {
|
|
418
|
+
pushOther_(context, point, child);
|
|
419
|
+
}
|
|
420
|
+
}
|
|
421
|
+
const linkList = links.finish(point);
|
|
422
|
+
if (linkList.length > 0) {
|
|
423
|
+
point.links = linkList;
|
|
424
|
+
}
|
|
425
|
+
if (latitude === undefined || longitude === undefined) {
|
|
426
|
+
return null;
|
|
427
|
+
}
|
|
428
|
+
return point;
|
|
429
|
+
}
|
|
430
|
+
function isGpxFix_(text) {
|
|
431
|
+
return FIXES_.has(text);
|
|
432
|
+
}
|
|
433
|
+
// Reads one child into `point`; `false` when its value is unreadable.
|
|
434
|
+
function readPointField_(context, point, local, child, span) {
|
|
435
|
+
const text = text_(context, child);
|
|
436
|
+
const decimal = (range) => number_(context, text, `<${local}>`, span, range);
|
|
437
|
+
const angle = () => decimal({ min: 0, max: 360, maxExclusive: true });
|
|
438
|
+
let value;
|
|
439
|
+
switch (local) {
|
|
440
|
+
case "ele":
|
|
441
|
+
value = decimal();
|
|
442
|
+
point.elevation = value ?? null;
|
|
443
|
+
break;
|
|
444
|
+
case "time":
|
|
445
|
+
value = time_(context, text, span);
|
|
446
|
+
point.time = value ?? null;
|
|
447
|
+
break;
|
|
448
|
+
case "magvar":
|
|
449
|
+
value = angle();
|
|
450
|
+
point.magneticVariation = value ?? null;
|
|
451
|
+
break;
|
|
452
|
+
case "geoidheight":
|
|
453
|
+
value = decimal();
|
|
454
|
+
point.geoidHeight = value ?? null;
|
|
455
|
+
break;
|
|
456
|
+
case "course":
|
|
457
|
+
value = angle();
|
|
458
|
+
point.course = value ?? null;
|
|
459
|
+
break;
|
|
460
|
+
case "speed":
|
|
461
|
+
value = decimal();
|
|
462
|
+
point.speed = value ?? null;
|
|
463
|
+
break;
|
|
464
|
+
case "sat":
|
|
465
|
+
value = decimal({ integer: true });
|
|
466
|
+
point.satellites = value ?? null;
|
|
467
|
+
break;
|
|
468
|
+
case "hdop":
|
|
469
|
+
value = decimal();
|
|
470
|
+
point.hdop = value ?? null;
|
|
471
|
+
break;
|
|
472
|
+
case "vdop":
|
|
473
|
+
value = decimal();
|
|
474
|
+
point.vdop = value ?? null;
|
|
475
|
+
break;
|
|
476
|
+
case "pdop":
|
|
477
|
+
value = decimal();
|
|
478
|
+
point.pdop = value ?? null;
|
|
479
|
+
break;
|
|
480
|
+
case "ageofdgpsdata":
|
|
481
|
+
value = decimal();
|
|
482
|
+
point.ageOfDgpsData = value ?? null;
|
|
483
|
+
break;
|
|
484
|
+
case "dgpsid":
|
|
485
|
+
value = decimal({ integer: true, min: 0, max: 1023 });
|
|
486
|
+
point.dgpsId = value ?? null;
|
|
487
|
+
break;
|
|
488
|
+
case "fix": {
|
|
489
|
+
const fix = trimXsdWhitespace(text);
|
|
490
|
+
if (!isGpxFix_(fix)) {
|
|
491
|
+
context.sink.violation("invalid-value", `<fix> "${fix}" is not none, 2d, 3d, dgps or pps`, span);
|
|
492
|
+
return false;
|
|
493
|
+
}
|
|
494
|
+
point.fix = fix;
|
|
495
|
+
return true;
|
|
496
|
+
}
|
|
497
|
+
case "name":
|
|
498
|
+
point.name = text;
|
|
499
|
+
return true;
|
|
500
|
+
case "cmt":
|
|
501
|
+
point.comment = text;
|
|
502
|
+
return true;
|
|
503
|
+
case "desc":
|
|
504
|
+
point.description = text;
|
|
505
|
+
return true;
|
|
506
|
+
case "src":
|
|
507
|
+
point.source = text;
|
|
508
|
+
return true;
|
|
509
|
+
case "sym":
|
|
510
|
+
point.symbol = text;
|
|
511
|
+
return true;
|
|
512
|
+
default:
|
|
513
|
+
point.type = text;
|
|
514
|
+
return true;
|
|
515
|
+
}
|
|
516
|
+
return value !== undefined;
|
|
517
|
+
}
|
|
518
|
+
function pushOther_(context, holder, element) {
|
|
519
|
+
holder.otherElements = [
|
|
520
|
+
...holder.otherElements,
|
|
521
|
+
detachXmlElement(context.namespaces, element),
|
|
522
|
+
];
|
|
523
|
+
}
|
|
524
|
+
// GPX 1.1's `link` elements, or GPX 1.0's `url` and `urlname` pair.
|
|
525
|
+
class LinkCollector_ {
|
|
526
|
+
context;
|
|
527
|
+
links = [];
|
|
528
|
+
url = null;
|
|
529
|
+
urlName = null;
|
|
530
|
+
constructor(context) {
|
|
531
|
+
this.context = context;
|
|
532
|
+
}
|
|
533
|
+
add(local, element, span) {
|
|
534
|
+
if (local === "link") {
|
|
535
|
+
const link = readLink_(this.context, element, span);
|
|
536
|
+
if (link !== null) {
|
|
537
|
+
this.links.push(link);
|
|
538
|
+
}
|
|
539
|
+
return;
|
|
540
|
+
}
|
|
541
|
+
if (local === "url") {
|
|
542
|
+
this.url = trimXsdWhitespace(text_(this.context, element));
|
|
543
|
+
return;
|
|
544
|
+
}
|
|
545
|
+
this.urlName = element;
|
|
546
|
+
}
|
|
547
|
+
/** The links; a GPX 1.0 `urlname` without `url` goes to `otherElements`. */
|
|
548
|
+
finish(holder) {
|
|
549
|
+
if (this.url !== null) {
|
|
550
|
+
this.links.push({
|
|
551
|
+
href: this.url,
|
|
552
|
+
text: this.urlName === null ? null : text_(this.context, this.urlName),
|
|
553
|
+
type: null,
|
|
554
|
+
});
|
|
555
|
+
}
|
|
556
|
+
else if (this.urlName !== null) {
|
|
557
|
+
pushOther_(this.context, holder, this.urlName);
|
|
558
|
+
}
|
|
559
|
+
return this.links;
|
|
560
|
+
}
|
|
561
|
+
}
|
|
562
|
+
function readLink_(context, element, fallback) {
|
|
563
|
+
const span = spanOf_(element, fallback);
|
|
564
|
+
const attributes = attributes_(context, element, ["href"]);
|
|
565
|
+
const href = required_(context, attributes, "href", element, span);
|
|
566
|
+
const children = children_(context, element, LINK_SEQUENCE_);
|
|
567
|
+
let text = null;
|
|
568
|
+
let type = null;
|
|
569
|
+
for (const { local, element: child } of children.gpx) {
|
|
570
|
+
if (local === "text") {
|
|
571
|
+
text = text_(context, child);
|
|
572
|
+
}
|
|
573
|
+
else {
|
|
574
|
+
type = text_(context, child);
|
|
575
|
+
}
|
|
576
|
+
}
|
|
577
|
+
if (href === undefined) {
|
|
578
|
+
return null;
|
|
579
|
+
}
|
|
580
|
+
return { href: trimXsdWhitespace(href.value), text, type };
|
|
581
|
+
}
|
|
582
|
+
function readEmail_(context, element, fallback) {
|
|
583
|
+
const span = spanOf_(element, fallback);
|
|
584
|
+
if (context.version === "1.0") {
|
|
585
|
+
const address = trimXsdWhitespace(text_(context, element));
|
|
586
|
+
const at = address.lastIndexOf("@");
|
|
587
|
+
if (at === -1) {
|
|
588
|
+
context.sink.violation("invalid-value", `"${address}" is not an e-mail address`, span);
|
|
589
|
+
return null;
|
|
590
|
+
}
|
|
591
|
+
return { id: address.slice(0, at), domain: address.slice(at + 1) };
|
|
592
|
+
}
|
|
593
|
+
const attributes = attributes_(context, element, ["id", "domain"]);
|
|
594
|
+
const id = required_(context, attributes, "id", element, span);
|
|
595
|
+
const domain = required_(context, attributes, "domain", element, span);
|
|
596
|
+
children_(context, element, EMPTY_SEQUENCE_);
|
|
597
|
+
if (id === undefined || domain === undefined) {
|
|
598
|
+
return null;
|
|
599
|
+
}
|
|
600
|
+
return { id: id.value, domain: domain.value };
|
|
601
|
+
}
|
|
602
|
+
function readPerson_(context, element, fallback) {
|
|
603
|
+
const span = spanOf_(element, fallback);
|
|
604
|
+
const children = children_(context, element, PERSON_SEQUENCE_);
|
|
605
|
+
let name = null;
|
|
606
|
+
let email = null;
|
|
607
|
+
let link = null;
|
|
608
|
+
for (const { local, element: child } of children.gpx) {
|
|
609
|
+
if (local === "name") {
|
|
610
|
+
name = text_(context, child);
|
|
611
|
+
}
|
|
612
|
+
else if (local === "email") {
|
|
613
|
+
email = readEmail_(context, child, span);
|
|
614
|
+
}
|
|
615
|
+
else {
|
|
616
|
+
link = readLink_(context, child, span);
|
|
617
|
+
}
|
|
618
|
+
}
|
|
619
|
+
return { name, email, link };
|
|
620
|
+
}
|
|
621
|
+
function readCopyright_(context, element, fallback) {
|
|
622
|
+
const span = spanOf_(element, fallback);
|
|
623
|
+
const attributes = attributes_(context, element, ["author"]);
|
|
624
|
+
const author = required_(context, attributes, "author", element, span);
|
|
625
|
+
const children = children_(context, element, COPYRIGHT_SEQUENCE_);
|
|
626
|
+
let year = null;
|
|
627
|
+
let license = null;
|
|
628
|
+
for (const { local, element: child } of children.gpx) {
|
|
629
|
+
const childSpan = spanOf_(child, span);
|
|
630
|
+
if (local === "year") {
|
|
631
|
+
const text = trimXsdWhitespace(text_(context, child));
|
|
632
|
+
const lexical = parseXsGYear(text);
|
|
633
|
+
if (lexical === null || !Number.isSafeInteger(Number(lexical.year))) {
|
|
634
|
+
context.sink.violation("invalid-value", `<year> "${text}" is not an xs:gYear`, childSpan);
|
|
635
|
+
}
|
|
636
|
+
else {
|
|
637
|
+
const magnitude = Number(lexical.year);
|
|
638
|
+
year = lexical.negativeYear ? -magnitude : magnitude;
|
|
639
|
+
}
|
|
640
|
+
}
|
|
641
|
+
else {
|
|
642
|
+
license = trimXsdWhitespace(text_(context, child));
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
return { author: author?.value ?? "", year, license };
|
|
646
|
+
}
|
|
647
|
+
function readBounds_(context, element, fallback) {
|
|
648
|
+
const span = spanOf_(element, fallback);
|
|
649
|
+
const names = ["minlat", "minlon", "maxlat", "maxlon"];
|
|
650
|
+
const attributes = attributes_(context, element, names);
|
|
651
|
+
children_(context, element, EMPTY_SEQUENCE_);
|
|
652
|
+
const values = [];
|
|
653
|
+
for (const name of names) {
|
|
654
|
+
const attribute = required_(context, attributes, name, element, span);
|
|
655
|
+
if (attribute === undefined) {
|
|
656
|
+
return null;
|
|
657
|
+
}
|
|
658
|
+
const value = number_(context, attribute.value, name, attribute.span, name.endsWith("lat")
|
|
659
|
+
? { min: -90, max: 90 }
|
|
660
|
+
: { min: -180, max: 180, maxExclusive: true });
|
|
661
|
+
if (value === undefined) {
|
|
662
|
+
return null;
|
|
663
|
+
}
|
|
664
|
+
values.push(value);
|
|
665
|
+
}
|
|
666
|
+
return {
|
|
667
|
+
minLatitude: values[0],
|
|
668
|
+
minLongitude: values[1],
|
|
669
|
+
maxLatitude: values[2],
|
|
670
|
+
maxLongitude: values[3],
|
|
671
|
+
};
|
|
672
|
+
}
|
|
673
|
+
/** GPX 1.1's `<metadata>`. */
|
|
674
|
+
function readMetadata_(context, element, fallback) {
|
|
675
|
+
const span = spanOf_(element, fallback);
|
|
676
|
+
const attributes = attributes_(context, element, []);
|
|
677
|
+
const children = children_(context, element, METADATA_SEQUENCE_);
|
|
678
|
+
const metadata = {
|
|
679
|
+
...emptyMetadata_(),
|
|
680
|
+
extensions: children.extensions,
|
|
681
|
+
otherElements: children.otherElements,
|
|
682
|
+
otherAttributes: attributes.others,
|
|
683
|
+
span,
|
|
684
|
+
};
|
|
685
|
+
const links = [];
|
|
686
|
+
for (const { local, element: child } of children.gpx) {
|
|
687
|
+
const childSpan = spanOf_(child, span);
|
|
688
|
+
switch (local) {
|
|
689
|
+
case "name":
|
|
690
|
+
metadata.name = text_(context, child);
|
|
691
|
+
break;
|
|
692
|
+
case "desc":
|
|
693
|
+
metadata.description = text_(context, child);
|
|
694
|
+
break;
|
|
695
|
+
case "author":
|
|
696
|
+
metadata.author = readPerson_(context, child, childSpan);
|
|
697
|
+
break;
|
|
698
|
+
case "copyright":
|
|
699
|
+
metadata.copyright = readCopyright_(context, child, childSpan);
|
|
700
|
+
break;
|
|
701
|
+
case "link": {
|
|
702
|
+
const link = readLink_(context, child, childSpan);
|
|
703
|
+
if (link === null) {
|
|
704
|
+
pushOther_(context, metadata, child);
|
|
705
|
+
}
|
|
706
|
+
else {
|
|
707
|
+
links.push(link);
|
|
708
|
+
}
|
|
709
|
+
break;
|
|
710
|
+
}
|
|
711
|
+
case "time": {
|
|
712
|
+
const time = time_(context, text_(context, child), childSpan);
|
|
713
|
+
if (time === undefined) {
|
|
714
|
+
pushOther_(context, metadata, child);
|
|
715
|
+
}
|
|
716
|
+
else {
|
|
717
|
+
metadata.time = time;
|
|
718
|
+
}
|
|
719
|
+
break;
|
|
720
|
+
}
|
|
721
|
+
case "keywords":
|
|
722
|
+
metadata.keywords = text_(context, child);
|
|
723
|
+
break;
|
|
724
|
+
default: {
|
|
725
|
+
const bounds = readBounds_(context, child, childSpan);
|
|
726
|
+
if (bounds === null) {
|
|
727
|
+
pushOther_(context, metadata, child);
|
|
728
|
+
}
|
|
729
|
+
else {
|
|
730
|
+
metadata.bounds = bounds;
|
|
731
|
+
}
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
}
|
|
735
|
+
metadata.links = links;
|
|
736
|
+
return metadata;
|
|
737
|
+
}
|
|
738
|
+
export function emptyMetadata_() {
|
|
739
|
+
return {
|
|
740
|
+
name: null,
|
|
741
|
+
description: null,
|
|
742
|
+
author: null,
|
|
743
|
+
copyright: null,
|
|
744
|
+
links: NONE_,
|
|
745
|
+
time: null,
|
|
746
|
+
keywords: null,
|
|
747
|
+
bounds: null,
|
|
748
|
+
extensions: NONE_,
|
|
749
|
+
otherElements: NONE_,
|
|
750
|
+
otherAttributes: NONE_,
|
|
751
|
+
span: null,
|
|
752
|
+
};
|
|
753
|
+
}
|
|
754
|
+
function readDescription_(context, children, span, holder, pointsName) {
|
|
755
|
+
const points = [];
|
|
756
|
+
const links = new LinkCollector_(context);
|
|
757
|
+
for (const { local, element: child } of children.gpx) {
|
|
758
|
+
const childSpan = spanOf_(child, span);
|
|
759
|
+
switch (local) {
|
|
760
|
+
case "name":
|
|
761
|
+
holder.name = text_(context, child);
|
|
762
|
+
break;
|
|
763
|
+
case "cmt":
|
|
764
|
+
holder.comment = text_(context, child);
|
|
765
|
+
break;
|
|
766
|
+
case "desc":
|
|
767
|
+
holder.description = text_(context, child);
|
|
768
|
+
break;
|
|
769
|
+
case "src":
|
|
770
|
+
holder.source = text_(context, child);
|
|
771
|
+
break;
|
|
772
|
+
case "type":
|
|
773
|
+
holder.type = text_(context, child);
|
|
774
|
+
break;
|
|
775
|
+
case "number": {
|
|
776
|
+
const number = number_(context, text_(context, child), "<number>", childSpan, { integer: true });
|
|
777
|
+
if (number === undefined) {
|
|
778
|
+
pushOther_(context, holder, child);
|
|
779
|
+
}
|
|
780
|
+
else {
|
|
781
|
+
holder.number = number;
|
|
782
|
+
}
|
|
783
|
+
break;
|
|
784
|
+
}
|
|
785
|
+
case "link":
|
|
786
|
+
case "url":
|
|
787
|
+
case "urlname":
|
|
788
|
+
links.add(local, child, childSpan);
|
|
789
|
+
break;
|
|
790
|
+
default:
|
|
791
|
+
if (local === pointsName) {
|
|
792
|
+
points.push(child);
|
|
793
|
+
}
|
|
794
|
+
}
|
|
795
|
+
}
|
|
796
|
+
holder.links = links.finish(holder);
|
|
797
|
+
return points;
|
|
798
|
+
}
|
|
799
|
+
function emptyDescription_(children, attributes, span) {
|
|
800
|
+
return {
|
|
801
|
+
name: null,
|
|
802
|
+
comment: null,
|
|
803
|
+
description: null,
|
|
804
|
+
source: null,
|
|
805
|
+
links: NONE_,
|
|
806
|
+
number: null,
|
|
807
|
+
type: null,
|
|
808
|
+
extensions: children.extensions,
|
|
809
|
+
otherElements: children.otherElements,
|
|
810
|
+
otherAttributes: attributes.others,
|
|
811
|
+
span,
|
|
812
|
+
};
|
|
813
|
+
}
|
|
814
|
+
export function readRoute_(context, element, fallback) {
|
|
815
|
+
context.budget?.spend(1);
|
|
816
|
+
const span = spanOf_(element, fallback);
|
|
817
|
+
const attributes = attributes_(context, element, []);
|
|
818
|
+
const children = children_(context, element, context.version === "1.1" ? ROUTE_11_SEQUENCE_ : ROUTE_10_SEQUENCE_, context.rejected === null);
|
|
819
|
+
const route = {
|
|
820
|
+
...emptyDescription_(children, attributes, span),
|
|
821
|
+
points: NONE_,
|
|
822
|
+
};
|
|
823
|
+
const points = [];
|
|
824
|
+
for (const child of readDescription_(context, children, span, route, "rtept")) {
|
|
825
|
+
const point = readPoint_(context, child, "rtept", span);
|
|
826
|
+
if (point === null) {
|
|
827
|
+
pushOther_(context, route, child);
|
|
828
|
+
}
|
|
829
|
+
else {
|
|
830
|
+
points.push(point);
|
|
831
|
+
}
|
|
832
|
+
}
|
|
833
|
+
route.points = points;
|
|
834
|
+
return route;
|
|
835
|
+
}
|
|
836
|
+
export function readTrack_(context, element, fallback) {
|
|
837
|
+
context.budget?.spend(1);
|
|
838
|
+
const span = spanOf_(element, fallback);
|
|
839
|
+
const attributes = attributes_(context, element, []);
|
|
840
|
+
const children = children_(context, element, context.version === "1.1" ? TRACK_11_SEQUENCE_ : TRACK_10_SEQUENCE_);
|
|
841
|
+
const track = {
|
|
842
|
+
...emptyDescription_(children, attributes, span),
|
|
843
|
+
segments: NONE_,
|
|
844
|
+
};
|
|
845
|
+
const segments = [];
|
|
846
|
+
for (const child of readDescription_(context, children, span, track, "trkseg")) {
|
|
847
|
+
segments.push(readSegment_(context, child, span));
|
|
848
|
+
}
|
|
849
|
+
track.segments = segments;
|
|
850
|
+
return track;
|
|
851
|
+
}
|
|
852
|
+
function readSegment_(context, element, fallback) {
|
|
853
|
+
context.budget?.spend(1);
|
|
854
|
+
const span = spanOf_(element, fallback);
|
|
855
|
+
const attributes = attributes_(context, element, []);
|
|
856
|
+
const children = children_(context, element, context.version === "1.1" ? SEGMENT_11_SEQUENCE_ : SEGMENT_10_SEQUENCE_, context.rejected === null);
|
|
857
|
+
const segment = {
|
|
858
|
+
points: NONE_,
|
|
859
|
+
extensions: children.extensions,
|
|
860
|
+
otherElements: children.otherElements,
|
|
861
|
+
otherAttributes: attributes.others,
|
|
862
|
+
span,
|
|
863
|
+
};
|
|
864
|
+
const points = [];
|
|
865
|
+
for (const { element: child } of children.gpx) {
|
|
866
|
+
const point = readPoint_(context, child, "trkpt", span);
|
|
867
|
+
if (point === null) {
|
|
868
|
+
pushOther_(context, segment, child);
|
|
869
|
+
}
|
|
870
|
+
else {
|
|
871
|
+
points.push(point);
|
|
872
|
+
}
|
|
873
|
+
}
|
|
874
|
+
segment.points = points;
|
|
875
|
+
return segment;
|
|
876
|
+
}
|
|
877
|
+
// `xsi:schemaLocation` out of the root's other attributes, as pairs, GPX's own
|
|
878
|
+
// left out.
|
|
879
|
+
function schemaLocations_(context, attributes, span) {
|
|
880
|
+
const index = attributes.findIndex((attribute) => attribute.name.local === "schemaLocation" &&
|
|
881
|
+
context.namespaces.attributeNamespaceOf(attribute) === XSI_NAMESPACE);
|
|
882
|
+
if (index === -1) {
|
|
883
|
+
return { schemaLocations: NONE_, others: attributes };
|
|
884
|
+
}
|
|
885
|
+
const attribute = attributes[index];
|
|
886
|
+
const tokens = trimXsdWhitespace(attribute.value)
|
|
887
|
+
.split(/[ \t\r\n]+/u)
|
|
888
|
+
.filter((token) => token !== "");
|
|
889
|
+
if (tokens.length % 2 !== 0) {
|
|
890
|
+
context.sink.violation("invalid-value", "xsi:schemaLocation does not hold namespace and location pairs", attribute.span ?? span);
|
|
891
|
+
}
|
|
892
|
+
const schemaLocations = [];
|
|
893
|
+
for (let pair = 0; pair + 1 < tokens.length; pair += 2) {
|
|
894
|
+
const namespace = tokens[pair];
|
|
895
|
+
if (namespace !== GPX_1_1_NAMESPACE && namespace !== GPX_1_0_NAMESPACE) {
|
|
896
|
+
schemaLocations.push({ namespace, location: tokens[pair + 1] });
|
|
897
|
+
}
|
|
898
|
+
}
|
|
899
|
+
const others = attributes.filter((_attribute, at) => at !== index);
|
|
900
|
+
return {
|
|
901
|
+
schemaLocations: schemaLocations.length === 0 ? NONE_ : schemaLocations,
|
|
902
|
+
others: others.length === 0 ? NONE_ : others,
|
|
903
|
+
};
|
|
904
|
+
}
|
|
905
|
+
/** The whole document from its root, once its version is known. */
|
|
906
|
+
export function readGpx_(context, root) {
|
|
907
|
+
const span = root.span ?? null;
|
|
908
|
+
const attributes = attributes_(context, root, ["version", "creator"]);
|
|
909
|
+
const creator = required_(context, attributes, "creator", root, span);
|
|
910
|
+
const { schemaLocations, others } = schemaLocations_(context, attributes.others, span);
|
|
911
|
+
const children = children_(context, root, context.version === "1.1" ? GPX_11_SEQUENCE_ : GPX_10_SEQUENCE_, context.rejected === null);
|
|
912
|
+
const document = {
|
|
913
|
+
version: context.version,
|
|
914
|
+
creator: creator?.value ?? "",
|
|
915
|
+
schemaLocations,
|
|
916
|
+
metadata: emptyMetadata_(),
|
|
917
|
+
waypoints: NONE_,
|
|
918
|
+
routes: NONE_,
|
|
919
|
+
tracks: NONE_,
|
|
920
|
+
extensions: children.extensions,
|
|
921
|
+
otherElements: children.otherElements,
|
|
922
|
+
otherAttributes: others,
|
|
923
|
+
span,
|
|
924
|
+
};
|
|
925
|
+
const waypoints = [];
|
|
926
|
+
const routes = [];
|
|
927
|
+
const tracks = [];
|
|
928
|
+
const metadata10 = context.version === "1.0" ? emptyMetadata_() : null;
|
|
929
|
+
const links = new LinkCollector_(context);
|
|
930
|
+
let authorName = null;
|
|
931
|
+
let authorEmail = null;
|
|
932
|
+
for (const { local, element: child } of children.gpx) {
|
|
933
|
+
const childSpan = spanOf_(child, span);
|
|
934
|
+
switch (local) {
|
|
935
|
+
case "metadata":
|
|
936
|
+
document.metadata = readMetadata_(context, child, childSpan);
|
|
937
|
+
continue;
|
|
938
|
+
case "wpt": {
|
|
939
|
+
const point = readPoint_(context, child, "wpt", span);
|
|
940
|
+
if (point === null) {
|
|
941
|
+
pushOther_(context, document, child);
|
|
942
|
+
}
|
|
943
|
+
else {
|
|
944
|
+
waypoints.push(point);
|
|
945
|
+
}
|
|
946
|
+
continue;
|
|
947
|
+
}
|
|
948
|
+
case "rte":
|
|
949
|
+
routes.push(readRoute_(context, child, span));
|
|
950
|
+
continue;
|
|
951
|
+
case "trk":
|
|
952
|
+
tracks.push(readTrack_(context, child, span));
|
|
953
|
+
continue;
|
|
954
|
+
default:
|
|
955
|
+
}
|
|
956
|
+
// GPX 1.0's metadata, on the root.
|
|
957
|
+
const metadata = metadata10;
|
|
958
|
+
switch (local) {
|
|
959
|
+
case "name":
|
|
960
|
+
metadata.name = text_(context, child);
|
|
961
|
+
break;
|
|
962
|
+
case "desc":
|
|
963
|
+
metadata.description = text_(context, child);
|
|
964
|
+
break;
|
|
965
|
+
case "author":
|
|
966
|
+
authorName = text_(context, child);
|
|
967
|
+
break;
|
|
968
|
+
case "email":
|
|
969
|
+
authorEmail = readEmail_(context, child, childSpan);
|
|
970
|
+
if (authorEmail === null) {
|
|
971
|
+
pushOther_(context, document, child);
|
|
972
|
+
}
|
|
973
|
+
break;
|
|
974
|
+
case "url":
|
|
975
|
+
case "urlname":
|
|
976
|
+
links.add(local, child, childSpan);
|
|
977
|
+
break;
|
|
978
|
+
case "time": {
|
|
979
|
+
const time = time_(context, text_(context, child), childSpan);
|
|
980
|
+
if (time === undefined) {
|
|
981
|
+
pushOther_(context, document, child);
|
|
982
|
+
}
|
|
983
|
+
else {
|
|
984
|
+
metadata.time = time;
|
|
985
|
+
}
|
|
986
|
+
break;
|
|
987
|
+
}
|
|
988
|
+
case "keywords":
|
|
989
|
+
metadata.keywords = text_(context, child);
|
|
990
|
+
break;
|
|
991
|
+
default: {
|
|
992
|
+
const bounds = readBounds_(context, child, childSpan);
|
|
993
|
+
if (bounds === null) {
|
|
994
|
+
pushOther_(context, document, child);
|
|
995
|
+
}
|
|
996
|
+
else {
|
|
997
|
+
metadata.bounds = bounds;
|
|
998
|
+
}
|
|
999
|
+
}
|
|
1000
|
+
}
|
|
1001
|
+
}
|
|
1002
|
+
if (metadata10 !== null) {
|
|
1003
|
+
metadata10.links = links.finish(document);
|
|
1004
|
+
if (authorName !== null || authorEmail !== null) {
|
|
1005
|
+
metadata10.author = { name: authorName, email: authorEmail, link: null };
|
|
1006
|
+
}
|
|
1007
|
+
document.metadata = metadata10;
|
|
1008
|
+
}
|
|
1009
|
+
document.waypoints = waypoints;
|
|
1010
|
+
document.routes = routes;
|
|
1011
|
+
document.tracks = tracks;
|
|
1012
|
+
return document;
|
|
1013
|
+
}
|
|
1014
|
+
//# sourceMappingURL=_reader.js.map
|