A WKT/WKB/EWKT/EWKB/TWKB/GeoJSON parser and serializer with support for
- Point
- LineString
- Polygon
- MultiPoint
- MultiLineString
- MultiPolygon
- GeometryCollection
The following examples show you how to work with wkx.
varwkx=require('wkx');//Parsing a WKT stringvargeometry=wkx.Geometry.parse('POINT(1 2)');//Parsing an EWKT stringvargeometry=wkx.Geometry.parse('SRID=4326;POINT(1 2)');//Parsing a node Buffer containing a WKB objectvargeometry=wkx.Geometry.parse(wkbBuffer);//Parsing a node Buffer containing an EWKB objectvargeometry=wkx.Geometry.parse(ewkbBuffer);//Parsing a node Buffer containing a TWKB objectvargeometry=wkx.Geometry.parseTwkb(twkbBuffer);//Parsing a GeoJSON objectvargeometry=wkx.Geometry.parseGeoJSON({type: 'Point',coordinates: [1,2]});//Serializing a Point geometry to WKTvarwktString=newwkx.Point(1,2).toWkt();//Serializing a Point geometry to WKBvarwkbBuffer=newwkx.Point(1,2).toWkb();//Serializing a Point geometry to EWKTvarewktString=newwkx.Point(1,2,undefined,undefined,4326).toEwkt();//Serializing a Point geometry to EWKBvarewkbBuffer=newwkx.Point(1,2,undefined,undefined,4326).toEwkb();//Serializing a Point geometry to TWKBvartwkbBuffer=newwkx.Point(1,2).toTwkb();//Serializing a Point geometry to GeoJSONvargeoJSONObject=newwkx.Point(1,2).toGeoJSON();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:
<scriptsrc="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:
varwkx=require('wkx');vargeometry=wkx.Geometry.parse('POINT(1 2)');console.log(geometry.toGeoJSON());In addition to the wkx module, the browser versions also export buffer, which is useful for parsing WKB:
varBuffer=require('buffer').Buffer;varwkx=require('wkx');varwkbBuffer=newBuffer('0101000000000000000000f03f0000000000000040','hex');vargeometry=wkx.Geometry.parse(wkbBuffer);console.log(geometry.toGeoJSON());