PostGISGeometry Input

ST_GeomFromTWKB

What is ST_GeomFromTWKB?

ST_GeomFromTWKB parses Tiny WKB bytes and returns a geometry. TWKB is a compact binary format designed for size-constrained contexts like web APIs.

SQL
ST_GeomFromTWKB(bytea twkb)geometry

TWKB does not carry an SRID; the output has SRID 0 and must be tagged with ST_SetSRID if needed.

When would you use ST_GeomFromTWKB?

Use ST_GeomFromTWKB when reading TWKB-encoded payloads from web services or mobile clients that prioritise payload size. TWKB with varint encoding is typically 30–60% smaller than OGC WKB for the same geometry, making it popular for map tile delivery and offline data packs.

It is the inverse of ST_AsTWKB and pairs with that function for compact geometry storage or transfer.

FAQs

Does TWKB preserve SRID?

No. TWKB omits SRID to save bytes. Tag the parsed geometry with ST_SetSRID.

Is TWKB lossy?

Yes — TWKB encodes integer-scaled coordinates, so precision depends on the scale used during encoding. Set prec explicitly in ST_AsTWKB for controlled precision.

Is it faster than WKB?

Parsing TWKB is comparably fast to WKB. The main advantage is size, not speed.

Does it support Z and M?

Yes. TWKB optionally encodes Z and M with independent precisions.