wiki:FAQ

Version 8 (modified by mloskot, 16 years ago) ( diff )

FAQ: How to write geometry Well-Known-Binary to hex-encoded string?

PostGIS FAQ

Frequently Asked Questions about PostGIS extension.

How to create geometry from hex-encoded Well-Known-Binary string using SQL?

This is two-steps operation which can be executed in single SQL statement. The steps include:

  • Convert Well-Known-Binary data in hexadecimal string form to raw binary using PostgreSQL function decode.
  • Construct geometry object from WKB in raw binary form.

Example:

=# SELECT ST_AsText(ST_GeomFromWKB(decode('0101000000e5d022dbf93e2e40dbf97e6abc743540', 'hex'), -1));
      st_astext       
----------------------
 POINT(15.123 21.456)

How to write geometry Well-Known-Binary to hex-encoded string?

First, query for geometry object in Well-Known-Binary format using ST_AsBinary function. Then, encode raw WKB to hex string.

Examples:

  • selecting geometry from table:
# SELECT encode(ST_AsBinary(the_geom), 'hex');
                   encode                   
--------------------------------------------
 0101000000e5d022dbf93e2e40dbf97e6abc743540
  • constructing geometry in-place from given Well-Known-Text:
# SELECT encode(ST_AsBinary(ST_GeomFromText('POINT(15.123 21.456)', -1)), 'hex');
                   encode                   
--------------------------------------------
 0101000000e5d022dbf93e2e40dbf97e6abc743540
Note: See TracWiki for help on using the wiki.