12

Turns out json isn't so good at transporting binary data. But with HTML5, XHR2 is now capable of transferring blobs cleanly. I'm looking to transfer binary geometry (to save bandwidth) and decode it on the client.

To no avail, I've scoured the web for a javascript-based WKB (Well-known Binary) to WKT (Well-known Text) function. Before I re-invent the wheel -- is anyone aware of any open-source solutions?

1
  • Btw, you should not use blobs but arraybuffer. May 13, 2013 at 21:17

6 Answers 6

12

It looks like a new and better supported JS WKB parsing library has since appeared.

https://github.com/cschwarz/wkx

I've been able to use it to convert WKB directly from postgres into JS objects that can be mapped in the browser. You'll need to include https://github.com/cschwarz/wkx/blob/master/dist/wkx.js in your webpage for this to work.

// Required imports (works in browser, too)
var wkx = require('wkx');
var buffer = require('buffer');

// Sample data to convert
var wkbLonlat = '010100000072675909D36C52C0E151BB43B05E4440';

// Split WKB into array of integers (necessary to turn it into buffer)
var hexAry = wkbLonlat.match(/.{2}/g);
var intAry = [];
for (var i in hexAry) {
  intAry.push(parseInt(hexAry[i], 16));
}

// Generate the buffer
var buf = new buffer.Buffer(intAry);

// Parse buffer into geometric object
var geom = wkx.Geometry.parse(buf);

// Should log '-73.700380647'
console.log(geom.x)

// Should log '40.739754168'
console.log(geom.y)
1
  • 1
    Hey, OP here. Asked this 1.5 years ago. Awesome! Feb 6, 2015 at 1:02
7

The only solution pure javascript solution I've found so far (and I did not try) is https://github.com/thejefflarson/wkb.js. It's only an incomplete WKB parser (it converts WKB to a js object you can transform to WKT)

An alternative way to wkb on javascript side can be the experimental twkb (not a standard at the moment) http://blog.jordogskog.no/2013/05/05/mapservice-from-websocket-with-twkb/ but it requires to play with a custom PostGIS build (so really not for beginners)

3

Another possibility might be to use TopoJSON instead of plain GeoJSON:

TopoJSON is an extension of GeoJSON that encodes topology. Rather than representing geometries discretely, geometries in TopoJSON files are stitched together from shared line segments called arcs. TopoJSON eliminates redundancy, offering much more compact representations of geometry than with GeoJSON; typical TopoJSON files are 80% smaller than their GeoJSON equivalents.

0
3

As mentioned by ThomasG77 I have been playing with binary data in this "twkb" format.

you can see it in action here (a websocket example)

or here, a php implementation.

If you want to study the parsing check the file twkb.js. It is a little cleaner in the twkb_node example I think.

In this blog post you can find link to the source code of the PostGIS part and some description of the format.

I have done some reworking since and will soon put a new description on github. I have a believe in twkb, but it needs more brains to get good.

You can of course also parse wkb but you will gain no bandwidth compared to gzipped geojson. I was surprised how small that did get. See the second link and check the sizes of the geojson vs twkb. WKB is about 2-6 times bigger than twkb.

0

This answer is not about wkb to wkt function.

I'd say you shouldn't use conversion from wkt to wkb just to save bandwith - gzipping wkt (or other format you have there) on the server should be more than enough (and most probably - more efficient) and browsers can do unzipping on the fly and out of the box.

Look also at browser support tables for XHTMLRequest2, as it's not supported in some older, yet still used browsers.

0

GeoScript has a Javascript API that reads and writes WKT and WKB. The methods are part of geom.io.

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.