Skip to content

Function: readShapefileZip() ​

ts
function readShapefileZip(archive, options?): Promise<ShapefileLayer[]>;

Defined in: read.ts:107

Reads every shapefile inside a zip archive.

Archives routinely hold more than one layer, and just as routinely nest them in folders, so components are grouped by their full path minus extension. a/roads.shp and b/roads.shp therefore stay separate layers.

Archives nested one level deep are descended into, so an archive written with layout: 'nested' — one .zip per layer — reads back the same as any other.

Each layer is decoded using its own .cpg, and carries its own .prj. Directory entries, __MACOSX/ resource forks and files with no matching .shp are ignored.

Parameters ​

ParameterTypeDescription
archiveUint8ArrayThe zip file as bytes.
optionsReadOptionsDecoding settings. An explicit encoding here overrides the .cpg of every layer.

Returns ​

Promise<ShapefileLayer[]>

One entry per layer, sorted by name.

Throws ​

If the archive cannot be unzipped, or holds no .shp at all.

Examples ​

ts
const layers = await readShapefileZip(bytes);
for (const layer of layers) {
  console.log(layer.name, layer.geojson.features.length, layer.prj);
}

Read an older archive written in a Windows code page

ts
const layers = await readShapefileZip(bytes, { encoding: 'cp1252' });

Released under the MIT License.