@loaders.gl/kml
Version:
Framework-independent loader for the KML format
62 lines (61 loc) • 1.92 kB
JavaScript
// loaders.gl
// SPDX-License-Identifier: MIT
// Copyright (c) vis.gl contributors
import { kml } 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 KML_HEADER = `\
xml version="1.0" encoding="UTF-8"
<kml xmlns="http://www.opengis.net/kml/2.2">`;
/**
* Loader for KML (Keyhole Markup Language)
*/
export const KMLLoader = {
dataType: null,
batchType: null,
name: 'KML (Keyhole Markup Language)',
id: 'kml',
module: 'kml',
version: VERSION,
extensions: ['kml'],
mimeTypes: ['application/vnd.google-earth.kml+xml'],
text: true,
tests: [KML_HEADER],
parse: async (arrayBuffer, options) => parseTextSync(new TextDecoder().decode(arrayBuffer), options),
parseTextSync,
options: {
kml: { shape: 'geojson-table' },
gis: {}
}
};
function parseTextSync(text, options) {
const doc = new DOMParser().parseFromString(text, 'text/xml');
const geojson = kml(doc);
const kmlOptions = { ...KMLLoader.options.kml, ...options?.kml };
switch (kmlOptions.shape) {
case 'geojson-table': {
const table = {
shape: 'geojson-table',
type: 'FeatureCollection',
features: geojson.features
};
return table;
}
// case 'geojson':
// return geojson;
// case 'binary':
// return geojsonToBinary(geojson.features);
// case 'raw':
// return doc;
case 'object-row-table':
const table = {
shape: 'object-row-table',
data: geojson.features
};
return table;
default:
throw new Error(kmlOptions.shape);
}
}