Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Turns out json ain'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?

share|improve this question
    
Btw, you should not use blobs but arraybuffer. –  Nicklas Avén May 13 '13 at 21:17

5 Answers 5

up vote 5 down vote accepted

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)

share|improve this answer

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.

share|improve this answer
    
+1 TopoJSON seems very interesting –  Nicklas Avén May 13 '13 at 13:56

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.

share|improve this answer

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.

share|improve this answer

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

share|improve this answer
1  
FIY only in a shell environment with java dependencies (cf pom.xml at github.com/tschaub/geoscript-js) not in the browser –  ThomasG77 May 19 '13 at 0:24

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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