UNPKG

@loaders.gl/kml

Version:

Framework-independent loader for the KML format

60 lines (59 loc) 1.81 kB
// loaders.gl // SPDX-License-Identifier: MIT // Copyright (c) vis.gl contributors import { geojsonToBinary } from '@loaders.gl/gis'; import { gpx } 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 GPX_HEADER = `\ <?xml version="1.0" encoding="UTF-8"?> <gpx`; /** * Loader for GPX (GPS exchange format) */ export const GPXLoader = { dataType: null, batchType: null, name: 'GPX (GPS exchange format)', id: 'gpx', module: 'kml', version: VERSION, extensions: ['gpx'], mimeTypes: ['application/gpx+xml'], text: true, tests: [GPX_HEADER], parse: async (arrayBuffer, options) => parseTextSync(new TextDecoder().decode(arrayBuffer), options), parseTextSync, options: { gpx: { shape: 'geojson-table' }, gis: {} } }; function parseTextSync(text, options) { const doc = new DOMParser().parseFromString(text, 'text/xml'); const geojson = gpx(doc); const gpxOptions = { ...GPXLoader.options.gpx, ...options?.gpx }; switch (gpxOptions.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', features: geojson.features }; return table; } case 'binary': return geojsonToBinary(geojson.features); default: throw new Error(gpxOptions.shape); } }