Skip to content

Function: readShapefile() ​

ts
function readShapefile(source, options?): Promise<FeatureCollection>;

Defined in: read.ts:59

Reads shapefile components into a GeoJSON FeatureCollection.

The .shx is not needed — it is only an index into the .shp, which this reader walks sequentially.

Shapefile polygons store their rings in one flat list with no nesting, so holes are matched back to the ring that actually contains them (by point-in- polygon test, choosing the smallest containing ring) and then rewound to RFC 7946 winding order: exteriors counter-clockwise, holes clockwise.

Single-part geometries come back as the simple GeoJSON type — LineString rather than a one-element MultiLineString, and likewise for Polygon.

.dbf text is decoded as UTF-8 unless the .cpg or options.encoding names a legacy code page such as cp1252. dBase pads character columns to a fixed width; that padding is always stripped.

The WebAssembly module is instantiated on first use — no init() required.

Parameters ​

ParameterTypeDescription
sourceShapefileSourceThe .shp bytes, and optionally the .dbf, .cpg and .prj. Without a .dbf, features come back with empty properties.
optionsReadOptionsDecoding settings. See ReadOptions.

Returns ​

Promise<FeatureCollection>

A FeatureCollection. When a .prj was supplied its text is carried on the non-standard wkt member, since GeoJSON has nowhere else to put it.

Throws ​

If the .shp is truncated, malformed, or holds a shape type this reader does not understand.

Example ​

ts
const geojson = await readShapefile({
  shp: await readFile('roads.shp'),
  dbf: await readFile('roads.dbf'),
  prj: await readFile('roads.prj', 'utf8'),
});

See ​

readShapefileZip to read a whole archive at once.

Released under the MIT License.