I was reading a Postgres/PostGIS statement like this:
SELECT ST_AsBinary(
ST_GeomFromWKB(
E'\\001\\001\\000\\000\\000\\321\\256B\\312O\\304Q\\300\\347\\030\\220\\275\\336%E@',
4326
)
);
The above creates something from a Well Known Binary (WKB). I haven't seen the specific way of quoting here where the string is single quoted with a E
preceding the beginning quote.
What is this format called? And what are the formatting rules for this? e.g. is the 336%E@
at the very end special or just some binary value?
This is with Postgres9.3/9.4; PostGIS 2.1.
What you see does not look like hexadecimal, because the
bytea
string literal is in escape string syntax (which is rather outdated nowadays).The same as "standard conforming string":
Both are in "escape format", which can be represented more efficiently in "hex format" as:
You can use
encode()
anddecode()
to transform one form into the other.I answered your follow-up question on gis.SE with more details.
As per the PostgreSQL documentation http://www.postgresql.org/docs/9.0/static/sql-syntax-lexical.html (emphasis mine)
The use of
\\
in your string means that it's escaping an escape sequence, probably to be safe in transit and storage in a.sql
file. The verbatim string actually passed into theST_GeomFromWKB
function will be:These sequences of 3 or 4 characters between slashes would then be interpreted by
ST_GeoFromWKB
directly.The documentation for
ST_GeoFromWKB
( http://postgis.org/docs/ST_GeomFromWKB.html ) states:Unfortunately it doesn't state what format, exactly, the "well-known binary representation" actually is.
It turns out that the content of the string depends on the coordinate system you're using, which is specified by the
SRID
parameter. In this case4326
corresponds toWGS84
: https://en.wikipedia.org/wiki/World_Geodetic_System#WGS84You'll need to do further reading and research to untangle that.