Registry / serialization / wkx
library0.5.0jsnpmunverified

wkx is a robust JavaScript library designed for comprehensive parsing and serialization of spatial data in various formats. It supports the Open Geospatial Consortium (OGC) well-known text (WKT), well-known binary (WKB), extended well-known text (EWKT), extended well-known binary (EWKB), and tiny well-known binary (TWKB) standards, alongside GeoJSON. The library handles all fundamental OGC geometry types, including Point, LineString, Polygon, MultiPoint, MultiLineString, MultiPolygon, and GeometryCollection, providing a unified API for interacting with these diverse spatial representations. The current stable version is 0.5.0, with the last publish being six years ago as of April 2026. While specific release cadence is not detailed, the presence of build status badges suggests active maintenance. A key differentiator of wkx is its ability to seamlessly convert between these numerous formats, offering flexibility for developers working with disparate geospatial data sources and output requirements, from database storage to web mapping applications.

npm install wkx
INSTALL
IMPORT
SIG · WKX
W
wkx
serializationjavascriptv0.5.0
Install
Import
Disk
Pass rate
0/ 6
Env Coverage0 / 6
glibc
1822
musl
1822
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18226 runs
build_error
glibc
node 18226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Geometry
import { Geometry } from 'wkx';
import wkx from 'wkx'; // Geometry is a named export, not default const Geometry = require('wkx').Geometry;
Geometry exports static methods like `parse`, `parseTwkb`, and `parseGeoJSON` for converting various formats into wkx geometry objects. While CommonJS `require` works, ESM `import` is preferred in modern JavaScript/TypeScript projects.
Point
import { Point } from 'wkx';
import wkx from 'wkx'; new wkx.Point(1, 2); // Point is a named export, not a property of a default export const Point = require('wkx').Point;
Represents a point geometry and provides methods for serialization (`toWkt`, `toWkb`, etc.). Other geometry types like `LineString`, `Polygon`, `MultiPoint` are similarly exported as named imports.
* as wkx
import * as wkx from 'wkx';
const wkx = require('wkx');
This pattern imports all named exports into a single `wkx` namespace object, allowing access like `wkx.Geometry.parse()`. The `require` syntax is CommonJS, which is prevalent in older Node.js projects but is being superseded by ESM.

This quickstart demonstrates parsing various spatial formats (WKT, EWKT, WKB, GeoJSON) into wkx geometry objects and then serializing a Point geometry into different output formats. It also highlights the usage of `Buffer` for binary formats.

import { Geometry, Point } from 'wkx'; import { Buffer } from 'buffer'; // Required for WKB/EWKB parsing in some environments // Parsing examples const pointWkt = 'POINT(1 2)'; const geometryFromWkt = Geometry.parse(pointWkt); console.log(`Parsed WKT: ${pointWkt} -> GeoJSON:`, geometryFromWkt.toGeoJSON()); const ewktString = 'SRID=4326;POINT(1 2)'; const geometryFromEwkt = Geometry.parse(ewktString); console.log(`Parsed EWKT: ${ewktString} -> GeoJSON:`, geometryFromEwkt.toGeoJSON()); const wkbBuffer = Buffer.from('0101000000000000000000f03f0000000000000040', 'hex'); const geometryFromWkb = Geometry.parse(wkbBuffer); console.log(`Parsed WKB Buffer -> GeoJSON:`, geometryFromWkb.toGeoJSON()); const geoJsonObject = { type: 'Point', coordinates: [3, 4] }; const geometryFromGeoJSON = Geometry.parseGeoJSON(geoJsonObject); console.log(`Parsed GeoJSON: ${JSON.stringify(geoJsonObject)} -> WKT:`, geometryFromGeoJSON.toWkt()); // Serializing examples const myPoint = new Point(5, 6, undefined, undefined, 4326); // X, Y, Z, M, SRID console.log('Created Point:', myPoint); console.log('To WKT:', myPoint.toWkt()); console.log('To WKB:', myPoint.toWkb().toString('hex')); console.log('To EWKT:', myPoint.toEwkt()); console.log('To GeoJSON:', myPoint.toGeoJSON());
Debug
Known issues
gotchaAs of version 0.5.0, `wkx` is still in pre-1.0 development and its last release was six years ago. While efforts are made for stability, future minor versions (if any) could introduce breaking changes to the API without a major version bump. Developers should pin exact versions and review release notes carefully when upgrading.
fix
Pin your `wkx` dependency to an exact version (e.g., `"wkx": "0.5.0"`) to ensure consistent behavior across deployments. Monitor the project's GitHub repository for new releases or forks, such as `@dfsj/wkx` or `@pieterprovoost/wkx`, which might offer more recent updates or ESM support.
affects: <1.0.0
gotchaThe README examples predominantly use CommonJS `require('wkx')`. In modern JavaScript projects targeting ESM (e.g., `"type": "module"` in `package.json`), you must use `import { Geometry, Point } from 'wkx';` for named exports. Mixing `require` and `import` in a hybrid environment can lead to unexpected behavior or `TypeError`s if not correctly configured by bundlers.
fix
Always use ESM `import` statements (e.g., `import { Geometry } from 'wkx';`) in projects configured for ES Modules. If your project is CommonJS, stick to `const wkx = require('wkx');`.
affects: >=0.1.0
gotchaWhen using `wkx` in a browser environment, particularly for parsing WKB or EWKB, the `Buffer` object (a Node.js native) is required. If not using a bundler like Browserify (which often provides a polyfill) or explicitly including a `Buffer` polyfill, `ReferenceError: Buffer is not defined` will occur.
fix
For browser environments, ensure a `Buffer` polyfill is included in your build process or directly (e.g., `import { Buffer } from 'buffer';`). If using a bundler like Webpack or Rollup, configure it to polyfill Node.js globals.
affects: >=0.1.0
gotchaWhile `wkx` supports parsing and serializing SRID (Spatial Reference ID) with EWKT and EWKB, developers must explicitly provide the SRID when constructing new geometries or ensure it's present in the input format. Forgetting to set SRID for a geometry intended to be EWKT/EWKB will result in plain WKT/WKB output or default SRID values (e.g., `undefined`), potentially losing critical spatial reference metadata.
fix
When creating new `wkx.Geometry` instances that require an SRID, pass the SRID as the last argument to the constructor (e.g., `new wkx.Point(1, 2, undefined, undefined, 4326)`). Always validate the output format if SRID is critical for your application.
affects: >=0.1.0
Errors
Common errors & fixes
TypeError: wkx.Geometry.parse is not a function
Attempting to call `parse` on a default import or a mis-structured import when `Geometry` is a named export.
fix
Ensure you are using `import { Geometry } from 'wkx';` or `import * as wkx from 'wkx';` then `wkx.Geometry.parse()` in ESM. In CommonJS, use `const { Geometry } = require('wkx');` or `const wkx = require('wkx'); wkx.Geometry.parse()`.
ReferenceError: Buffer is not defined
Attempting to parse WKB/EWKB in a browser environment without the Node.js `Buffer` object being polyfilled or available.
fix
For browser usage, explicitly import `Buffer` from a polyfill or ensure your bundler (e.g., Webpack, Rollup) provides a `Buffer` polyfill. You might need to `npm install buffer` and configure your build.
Error: Invalid WKT format
The input string passed to `Geometry.parse()` is not a valid Well-Known Text (WKT) or Extended WKT (EWKT) string.
fix
Verify that the WKT/EWKT string adheres strictly to the OGC specifications. Check for missing parentheses, incorrect keyword spellings, or improper coordinate formats. Ensure SRID is correctly prefixed if using EWKT (e.g., `SRID=4326;POINT(1 2)`).
TypeError: geometry.toWkt is not a function
Attempting to call serialization methods (`toWkt`, `toGeoJSON`, etc.) on an object that is not a `wkx` geometry instance, or a geometry object that was not successfully parsed.
fix
Always check that `Geometry.parse()` or a geometry constructor successfully returned a valid `wkx` geometry object before attempting to call its serialization methods. This often happens if parsing fails and returns `null` or `undefined`.
Upgrade
Version history
0.5.0latest on npm
Audit
Dependencies
bufferoptionalRequired for handling WKB/EWKB in browser environments without a native or polyfilled Buffer object (e.g., without Browserify).
Agent activity
32 hits · last 30 days
node
27
OpenAI (training)
1
Resources
wkx — npm install wkx · libregistry