What is wkx?
The wkx npm package provides tools to parse and serialize Well-Known Text (WKT) and Well-Known Binary (WKB) formats, commonly used for representing geometric objects in Geographic Information Systems (GIS). It supports conversion between these formats and GeoJSON, making it useful for applications involving geospatial data manipulations.
What are wkx's main functionalities?
Parsing WKT to Geometry
This feature allows the parsing of Well-Known Text (WKT) into a Geometry object, which can then be converted to other formats like GeoJSON.
const wkx = require('wkx');
const wkt = 'POINT (30 10)';
const geometry = wkx.Geometry.parse(wkt);
console.log(geometry.toGeoJSON());
Serializing Geometry to WKT
This feature enables the serialization of Geometry objects (e.g., Point) into the Well-Known Text (WKT) format.
const wkx = require('wkx');
const point = new wkx.Point(30, 10);
const wkt = point.toWkt();
console.log(wkt);
Parsing WKB to Geometry
This feature allows the parsing of Well-Known Binary (WKB) into a Geometry object, which can then be converted to formats like GeoJSON.
const wkx = require('wkx');
const wkbBuffer = Buffer.from('0101000000000000000000F03F000000000000F03F', 'hex');
const geometry = wkx.Geometry.parse(wkbBuffer);
console.log(geometry.toGeoJSON());
Serializing Geometry to WKB
This feature enables the serialization of Geometry objects into the Well-Known Binary (WKB) format.
const wkx = require('wkx');
const point = new wkx.Point(30, 10);
const wkb = point.toWkb();
console.log(wkb.toString('hex'));
Other packages similar to wkx
terraformer-wkt-parser
This package also parses and serializes WKT. It is part of the Terraformer project, which provides tools for converting between different geospatial formats. Compared to wkx, it is specifically focused on WKT and does not handle WKB.
wellknown
Similar to wkx, 'wellknown' parses and serializes WKT. It is lightweight and focuses solely on WKT, without support for WKB, making it less versatile for applications that require WKB handling.
wkx
A WKT/WKB/EWKT/EWKB parser and serializer with support for
- Point
- LineString
- Polygon
- MultiPoint
- MultiLineString
- MultiPolygon
- GeometryCollection
Examples
The following examples show you how to work with wkx.
var wkx = require('wkx');
var geometry = wkx.Geometry.parse('POINT(1 2)');
var geometry = wkx.Geometry.parse('SRID=4326;POINT(1 2)');
var geometry = wkx.Geometry.parse(wkbBuffer);
var geometry = wkx.Geometry.parse(ewkbBuffer);
var wktString = new wkx.Point(1, 2).toWkt();
var wkbBuffer = new wkx.Point(1, 2).toWkb();
var ewktString = new wkx.Point(1, 2).toEwkt();
var ewkbBuffer = new wkx.Point(1, 2).toEwkb();
Browser
To use wkx
in a webpage, simply copy a built browser version from dist/
into your project, and use a script
tag
to include it:
<script src="wkx.js"></script>
You may also use RawGit as a CDN, so you don't need to copy wkx
into your project:
<script src="https://cdn.rawgit.com/cschwarz/wkx/v0.0.7/dist/wkx.js"></script>
If you use browserify for your project, you can simply npm install wkx --save
, and just require wkx
as usual in
your code.
Regardless of which of the preceeding options you choose, using wkx
in the browser will look the same:
var wkx = require('wkx');
var geometry = wkx.Geometry.parse('POINT(1 2)');
document.getElementById('output').innerText = JSON.stringify(geometry, null, ' ');
In addition to the wkx
module, the browser versions also export buffer
, which is useful for parsing WKB:
var Buffer = require('buffer');
var wkx = require('wkx');
var geometry = wkx.Geometry.parse(new Buffer([1,1,0,0,0,0,0,0,0,0,0,240,63,0,0,0,0,0,0,0,64]));
document.getElementById('output').innerText = JSON.stringify(geometry, null, ' ');
(buffer
is also made available by browserify, so it will also work the same way regardless of your choice above)
Building the browser version
To rebuild the browser version of wkx.js
based on the current node modules, either install and use [gulp][]...
gulp
...or use the configured npm
script:
npm run build
To enable source maps for debugging and development, rebuild using the --debug
flag:
gulp --debug
This will generate dist/wkx-debug.js
and dist/wkx-debug.min.js
Todo
- Add support for parsing and serializing 3D/4D geometries (Z, M and ZM)