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 wkxVerified import paths — ran on the pinned version, not inferred.
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.
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.
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');`.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.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.
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()`.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.
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)`).
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`.