Changes between Version 7 and Version 8 of FAQ


Ignore:
Timestamp:
04/17/09 18:38:21 (16 years ago)
Author:
mloskot
Comment:

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

Legend:

Unmodified
Added
Removed
Modified
  • FAQ

    v7 v8  
    55[[TOC(depth=2, noheading, inline)]]
    66
    7 == How to create geometry from hex-encoded WKB using SQL? ==
     7== How to create geometry from hex-encoded Well-Known-Binary string using SQL? ==
    88
    9 This is two-steps operation which can be executed in single SQL statement. The steps include:
    10  1. Convert WKB data in hexadecimal string form to raw binary using PostgreSQL function [http://www.postgresql.org/docs/8.3/static/functions-binarystring.html decode].
    11  2. Construct geometry object from WKB in raw binary form.
     9This is two-steps operation which can be executed in single [http://www.postgresql.org/docs/8.3/static/sql-commands.html SQL] statement. The steps include:
     10 * Convert [http://postgis.refractions.net/documentation/manual-svn/ch04.html Well-Known-Binary] data in hexadecimal string form to raw binary using PostgreSQL function [http://www.postgresql.org/docs/8.3/static/functions-binarystring.html decode].
     11 * Construct geometry object from WKB in raw binary form.
    1212
    1313Example:
     
    1515{{{
    1616#!sql
    17 =# SELECT ST_AsText(ST_GeomFromWKB(decode('0101000000e5d022dbf93e2e40dbf97e6abc743540', 'hex'), 4326));
     17=# SELECT ST_AsText(ST_GeomFromWKB(decode('0101000000e5d022dbf93e2e40dbf97e6abc743540', 'hex'), -1));
    1818      st_astext       
    1919----------------------
    2020 POINT(15.123 21.456)
    2121}}}
     22
     23== How to write geometry Well-Known-Binary to hex-encoded string? ==
     24
     25First, query for geometry object in [http://postgis.refractions.net/documentation/manual-svn/ch04.html Well-Known-Binary] format using [http://postgis.refractions.net/documentation/manual-svn/ST_AsBinary.html ST_AsBinary] function. Then, [http://www.postgresql.org/docs/8.3/static/functions-binarystring.html encode] raw WKB to hex string.
     26
     27Examples:
     28
     29 * selecting geometry from table:
     30
     31{{{
     32#!sql
     33# SELECT encode(ST_AsBinary(the_geom), 'hex');
     34                   encode                   
     35--------------------------------------------
     36 0101000000e5d022dbf93e2e40dbf97e6abc743540
     37}}}
     38
     39 * constructing geometry in-place from given Well-Known-Text:
     40
     41{{{
     42#!sql
     43# SELECT encode(ST_AsBinary(ST_GeomFromText('POINT(15.123 21.456)', -1)), 'hex');
     44                   encode                   
     45--------------------------------------------
     46 0101000000e5d022dbf93e2e40dbf97e6abc743540
     47}}}