Published
Edited
Aug 28, 2020
Importers
Insert cell
Insert cell
function kmzToGeoJSON(kmz) {
return new Promise(resolve => {
const worker = new Worker(workerScript);

function messaged({ data }) {
resolve(data);
}

invalidation.then(() => worker.terminate());
worker.addEventListener('message', messaged);
worker.postMessage(kmz);
});
}
Insert cell
workerScript = {
const blob = new Blob(
[
`
importScripts("${await require.resolve("jszip@3/dist/jszip.min.js")}");
importScripts("${await require.resolve("@tmcw/togeojson@3")}");
importScripts("${await require.resolve("https://bundle.run/xmldom@0.3.0")}");

onmessage = async (event) => {
// prepare to peek into archive
const archive = await JSZip.loadAsync(event.data);

// find all the files that end with .kml (in case there is other noise like PNGs)
const files = archive.file(/\.kml$/);

const results = {};

for (const file of files) {
// the generated ID of this file
const name = file.name.replace('.kml', '');

// get the raw XML string...
const str = await file.async('string');
// ...and pass it through DOMParser
const kml = new xmldom.DOMParser().parseFromString(str, 'text/xml');
results[name] = toGeoJSON.kml(kml);
}

postMessage(results);
close();
};
`
],
{ type: "text/javascript" }
);
const script = URL.createObjectURL(blob);
invalidation.then(() => URL.revokeObjectURL(script));
return script;
}
Insert cell
Insert cell
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more