ST_AsText

ST_AsText takes a geometry as an input parameter and returns its well-known text representation.

If the given geometry is null, then null is returned.

Syntax

Read syntax diagramSkip visual syntax diagramdb2gse.ST_AsText(geometry)

Parameter

geometry
A value of one of the seven distinct spatial data types to be converted to the corresponding well-known text representation.

Return type

CLOB(8M)

Example

In the following example, the lines of results have been reformatted for readability.

After capturing and inserting the data into the SAMPLE_GEOMETRIES table, an analyst wants to verify that the values inserted are correct by looking at the well-known text representation of the geometries.

SET CURRENT PATH = CURRENT PATH, db2gse; 

CREATE TABLE sample_geometries(id SMALLINT, spatial_type varchar(18),
    geometry ST_GEOMETRY);

INSERT INTO sample_geometries(id, spatial_type, geometry)
VALUES
    (1, 'st_point', ST_GEOMETRY(ST_Point(50,50,1)));

INSERT INTO sample_geometries(id, spatial_type, geometry)
VALUES
    (2, 'st_linestring', ST_GEOMETRY(ST_LineString('linestring
        (200 100, 210 130, 220 140)',  1)));

INSERT INTO sample_geometries(id, spatial_type, geometry)
VALUES
    (3, 'st_polygon', ST_GEOMETRY(ST_Polygon('polygon((110 120, 110 140, 
        130 140, 130 120, 110 120))', 1)));

The following SELECT statement lists the spatial type and the WKT representation of the geometries. The geometry is converted to text by the ST_AsText function. It is then cast to a varchar(150), because the default output of the ST_AsText function is CLOB(8M).

SELECT id, spatial_type, cast(ST_AsText(geometry) 
   AS varchar(150)) AS wkt
FROM   sample_geometries

Results:

ID     SPATIAL_TYPE       WKT                                                         
------ ------------------ ------------------------------------------------
     1 st_point           POINT ( 50.000000 50.000000)
                            
     2 st_linestring      LINESTRING ( 200.000000 100.000000, 
                            210.000000 130.000000, 220.000000
                            140.000000)

     3 st_polygon         POLYGON (( 110.000000 120.000000, 
                            130.000000 120.000000, 130.000000 
                            140.000000, 110.000000 140.000000, 
                            110.000000 120.000000))