Function: writeShapefile()
function writeShapefile(geojson, options?): Promise<ShapefileParts>;Defined in: write.ts:68
Converts GeoJSON into the raw components of a shapefile.
A shapefile holds exactly one geometry type, so mixed input is rejected — with the exception of Point and MultiPoint, which are promoted to a single Multipoint file. Polygon and MultiPolygon share the Polygon type, as do LineString and MultiLineString under Polyline.
Dimensionality follows the coordinates: a third ordinate becomes Z and a fourth becomes a measure. Override with WriteOptions.dimensions.
The .dbf schema is inferred from the properties of every feature — column types from the values seen, and column widths sized to the widest value, since dBase silently crops anything that overruns its column. Property names are truncated to the 11-byte dBase limit and de-duplicated; the mapping comes back in ShapefileParts.fields.
Features whose geometry is null are skipped and counted in ShapefileParts.skippedCount; writing them would desynchronise the .shp and .dbf record numbering.
The WebAssembly module is instantiated on first use — no init() required.
Parameters
| Parameter | Type | Description |
|---|---|---|
geojson | string | GeoJsonInput | A FeatureCollection, a single Feature, a bare geometry, an array of any of those, or a JSON string of the same. Passing a string skips a JavaScript-side parse. |
options | WriteOptions & { epsg?: number; wkt?: string; } | Geometry, schema and projection settings. |
Returns
Promise<ShapefileParts>
The .shp, .shx, .dbf and .cpg bytes; the .prj text when a projection was given; and the resolved shape type, bounds and field map.
Throws
If the input mixes incompatible geometry types, holds no writable features, contains a malformed coordinate or a degenerate ring, uses GeometryCollection, or names an EPSG code that cannot be resolved.
Examples
Write the components to disk
const parts = await writeShapefile(featureCollection, { epsg: 4326 });
await writeFile('roads.shp', parts.shp);
await writeFile('roads.dbf', parts.dbf);Report any property names that had to be renamed
const { fields } = await writeShapefile(data);
for (const field of fields) {
if (field.source !== field.name) {
console.warn(`${field.source} was written as ${field.name}`);
}
}See
writeShapefileZip to get one archive instead of loose parts.