[postgis-devel] Tiny WKB

Nicklas Avén nicklas.aven at jordogskog.no
Tue Mar 5 15:27:17 PST 2013


This question on gis.stackexchange started some thinking some time ago:

It is about not sending vektor-data to a web-client in text-format.

In the post he discusses putting binary vektor data in an image.

But that seems a little complicated I thought so I played around with
parsing wkb in javascript. That works good. But to make it more slim I
have tried to create a suggestion of a format that I have called TWKB
(Tiny WKB)

The idea is to handle relative coordinates, just like you can do with
svg. But there was a problem. The relative distance from one coordinate
to another can vary very much. So I have done some testing to have
variable sizes in the binary stream. 

The design right now is not optimal at all, but reduces the size from
the original geometry quite a lot depending on the need of precision.

I tested on a dataset with 10000 linestrings. With wkb the client
recived 2.14 mb. With twkb and 1 decimal precision 670 kb and twkb with
integer values 420 kb. 

Then is also an integer32 id value included in the TWKB.

I think it is a good idea for a web-optimized format to include the id
for several reasons.

One reason is that it would be possible to nest geometries with an
aggregate variant of the function. With id preserved, that could be a
way of creating vektor tiles in the database.

I think there is many possibilities. This is just a test and some

I have created a spike with the code, both for the st_astwkb function
and the javscript reader ( in extras):

There is no sql-definitions of the function there, but this is the one
with most arguments:

CREATE OR REPLACE FUNCTION st_astwkb(geometry, integer, integer, text)
  RETURNS bytea AS
'$libdir/postgis-2.1', 'TWKBFromLWGEOM'
  COST 1;
ALTER FUNCTION st_astwkb(geometry, integer, integer, text)
  OWNER TO postgres;

where the arguments are in order:

1. the geometry
2. precision, how many decimals the output should have. Negative values
gives integers with trailing zeros
3. the id to be included in the geometry
4. big or little endian, like ST_ASBinary

So, the function is used like:

SELECT ST_AsTWKB(geom,1,gid,'NDR') from my_table;

to get a result with 1 decimal and gid as id.

All feedback is welcome. Is it a waste of time or has it some potential.



More information about the postgis-devel mailing list