@loaders.gl/kml
Version:
Framework-independent loader for the KML format
61 lines (60 loc) • 1.92 kB
JavaScript
// loaders.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import { geojsonToBinary } from '@loaders.gl/gis';
import { tcx } from '@tmcw/togeojson';
import { DOMParser } from '@xmldom/xmldom';
// __VERSION__ is injected by babel-plugin-version-inline
// @ts-ignore TS2304: Cannot find name '__VERSION__'.
const VERSION = typeof "4.3.2" !== 'undefined' ? "4.3.2" : 'latest';
const TCX_HEADER = `\
<?xml version="1.0" encoding="UTF-8"?>
<TrainingCenterDatabase`;
/**
* Loader for TCX (Training Center XML) - Garmin GPS track format
*/
export const TCXLoader = {
dataType: null,
batchType: null,
name: 'TCX (Training Center XML)',
id: 'tcx',
module: 'kml',
version: VERSION,
extensions: ['tcx'],
mimeTypes: ['application/vnd.garmin.tcx+xml'],
text: true,
tests: [TCX_HEADER],
parse: async (arrayBuffer, options) => parseTextSync(new TextDecoder().decode(arrayBuffer), options),
parseTextSync,
options: {
tcx: { shape: 'geojson-table' },
gis: {}
}
};
function parseTextSync(text, options) {
const doc = new DOMParser().parseFromString(text, 'text/xml');
const geojson = tcx(doc);
const tcxOptions = { ...TCXLoader.options.tcx, ...options?.tcx };
switch (tcxOptions.shape) {
case 'object-row-table': {
const table = {
shape: 'object-row-table',
data: geojson.features
};
return table;
}
case 'geojson-table': {
const table = {
shape: 'geojson-table',
type: 'FeatureCollection',
schema: { metadata: {}, fields: [] },
features: geojson.features
};
return table;
}
case 'binary':
return geojsonToBinary(geojson.features);
default:
throw new Error(tcxOptions.shape);
}
}