ST_AsText function

The ST_AsText function takes a geometry object as an input parameter and returns its Well-known text (WKT) format.

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

Syntax

Read syntax diagramSkip visual syntax diagramST_AsText(geometry)

Parameter

geometry
A value of type ST_Geometry or one of its subtypes to be converted to the corresponding WKT format.

Return type

CLOB(2G)

Example

In the following example, the lines of results have been reformatted for readability. The spacing in your results will vary according to your online display.

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 WKT format of the geometries.


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_Point(50, 50, 0)),
    (2, 'st_linestring', ST_LineString('linestring
        (200 100, 210 130, 220 140)',  0)),
    (3, 'st_polygon', ST_Polygon('polygon((110 120, 110 140, 
        130 140, 130 120, 110 120))', 0))

The following SELECT statement lists the spatial type and the WKT format of the geometries. The geometry is converted to text by the ST_AsText function. It is then cast to a varchar(120) because the default output of the ST_AsText function is CLOB(2G).

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 50)                                                                                                                             
     2 st_linestring      LINESTRING (200 100, 210 130, 220 140)                                                                                                    
     3 st_polygon         POLYGON ((110 120, 130 120, 130 140, 110 140, 110 120))