Published
Edited
Sep 3, 2019
1 fork
Importers
7 stars
Insert cell
Insert cell
async function shpToGeoJSON(zip) {
// prepare to peek into archive
const archive = await JSZip.loadAsync(zip);
// find all .shp files in the archive
const files = archive.file(/\.shp$/);
const results = {};
for (const file of files) {
// the ID of this file will be the file name minus the ".shp"
const name = file.name.replace('.shp', '');
// read out the .shp file to a Buffer
const shp = await file.async('arraybuffer');
// build the file path to the possible .dbf file, and seek it
const dbfFile = archive.file(`${name}.dbf`);

// if we succeeded in finding the dbfFile, read it as a Buffer as well, otherwise continue
const dbf = dbfFile ? await dbfFile.async('arraybuffer') : undefined;
// convert the .shp file to GeoJSON
const geojson = await shapefile.read(shp, dbf);
// ensure that the winding order is correct
results[name] = rewind(geojson, true);
}
return results;
}
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
rewind = require('https://bundle.run/geojson-rewind@0.3')
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