postgresqlpostgiswkb

Is there a difference between WKB and the hex value returned in PostGIS


I've been doing some experimenting with PostGIS, and here's what I've noticed:

Suppose I have a table defined as follows:

CREATE TABLE IF NOT EXISTS geomtest (
    id SERIAL PRIMARY KEY,
    name TEXT NOT NULL,
    geom geometry(POLYGON, 4326) NOT NULL
);

And I add the following polygon:

SRID=4326;POLYGON((0 0,0 10,10 10,10 0,0 0))

If I query the geom column on its own, I get a Hex representation of the geometry. If I instead call ST_AsBinary(geom), I get a binary representation.

However, when I convert both the hex and binary representations to an array of bytes, the results I get are slightly different. The first comment is the result I get by converting the hex into binary, and the next is straight from ST_AsBinary()

//[1 3 0 0 32 230 16 0 0 1 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 36 64 0 0 0 0 0 0 36 64 0 0 0 0 0 0 36 64 0 0 0 0 0 0 36 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
//[1 3 0 0 0 1 0 0 0 5 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 36 64 0 0 0 0 0 0 36 64 0 0 0 0 0 0 36 64 0 0 0 0 0 0 36 64 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]

As you can see, the first 4 bytes are identical; representing whether it's in big or little endian, and the type of geometry (3, in this case a Polygon). The rest of the bytes are the same too. The only difference is there are a few extra bytes added after the first 4.

I wondered if this had to do with representing the projection (SRID=4326), but I haven't found any evidence to that.

If someone could shed some light on this, I'd greatly appreciate it.


Solution

  • I didn't examine the bytes, but I am sure that the difference is the SRID that's not included in the WKB format.

    Try st_asewkb instead.