[GIS] How to interpret a WKB (Well-known-binary)

postgiswell-known-binary

I read this example of a WKB (well know binary) from "PostGIS in Action" 2ed.:

SELECT ST_AsBinary(
ST_GeomFromWKB(
  E'\\001\\001\\000\\000\\000\\321\\256B\\312O\\304Q\\300\\347\\030\\220\\275\\336%E@',
  4326
  )
);

I understand the E'..' is an escape string, and the escaped backslash is a separator. But I don't understand the rest of the well-known binary.

Can anyone explain what are the numbers and letters?

They do not look like hexidecimal according to wikipedia consider e.g. 304Q. Also the 336%E@ looks weird.

Best Answer

ST_GeomFromWKB() takes the a WKB as input data type bytea.

Not sure why your example uses the old escape string syntax for the string literal. Maybe because the examples in the manual still do so, too.

(The manual has eventually been updated. Postgres 9.3 or later show modern notation.)

It's not wrong, just needlessly noisy. Unless you are running a very old Postgres instance with the setting standard_conforming_strings = off, you can replace it with the modern, standard form:

SELECT ST_AsBinary(ST_GeomFromWKB('\001\001\000\000\000\321\256B\312O\304Q\300\347\030\220\275\336%E@', 4326));

The above is the escape format for a bytea literal. The manual advises:

... this format should probably be avoided for most new applications.

So, double "old-school" syntax. Alternatively, you can use the shorter hex format:

SELECT ST_AsBinary(ST_GeomFromWKB('\x0101000000d1ae42ca4fc451c0e71890bdde254540', 4326));

Related:

Related Question