ST_AS_TEXT

Serialize a geometry to its Well-Known Text (WKT) representation.

Category: geospatialReturns: STRINGDialect: Standard

Syntax

ST_AS_TEXT(geometry)

Description

## Overview Returns the Well-Known Text (WKT) representation of a geometry value. WKT is a human-readable, portable text format for geometries defined by the OGC Simple Features specification. It is the inverse of ST_GEOM_FROM_TEXT, which parses WKT into an internal geometry. Use ST_AS_TEXT whenever you need to expose a geometry value outside the query engine (log lines, debug output, CSV exports, API payloads) or whenever you want a deterministic textual key for comparison that is more stable than a binary encoding. ## When to use ST_AS_TEXT vs. alternatives - **ST_AS_TEXT** (this function): standard WKT output. Best for interoperability, human inspection, and round-trips through ST_GEOM_FROM_TEXT. - **ST_X / ST_Y**: extract a single coordinate from a POINT. Faster than parsing WKT when you only need one scalar. - **CAST to STRING**: not a substitute. The default cast of a geometry to string is implementation-defined and may not round-trip. Always use ST_AS_TEXT when you need WKT. ## Behavior - Output follows the OGC Simple Features WKT grammar: POINT(x y), LINESTRING(x1 y1, x2 y2, ...), POLYGON((x1 y1, ..., x1 y1)), and the corresponding MULTI* and GEOMETRYCOLLECTION forms. - Coordinate order is (x y), which corresponds to (longitude latitude) for geographic data. - Numeric precision preserves the underlying double-precision value. Trailing zeros are trimmed but all significant digits are retained, so round-tripping through ST_GEOM_FROM_TEXT produces a geometrically identical result within floating-point tolerance. - Empty geometries serialize as 'POINT EMPTY', 'POLYGON EMPTY', etc. - Returns NULL if the input geometry is NULL. - Output does not include an SRID prefix. If you need SRID metadata you must track it in a separate column. ## Input format - Accepts any GEOMETRY value regardless of how it was constructed. - Ring winding order of polygons is preserved on output; callers that depend on a specific orientation should normalize on write rather than on read. ## Compatibility - Output is compatible with the OGC Simple Features WKT specification and parses cleanly in other SQL engines and GIS libraries that accept WKT input. - Whitespace in the output (single space between coordinates, no trailing newline) matches the canonical OGC form.

Parameters

NameTypeDescription
geometrySpecifies the geometry value to serialize. Accepts any geometry produced by ST_GEOM_FROM_TEXT, ST_MAKE_POINT, or other ST_ functions (POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING, MULTIPOLYGON, GEOMETRYCOLLECTION).

Examples

-- Converts a point geometry back to its text form.
SELECT ST_AS_TEXT(ST_MAKE_POINT(-74.0060, 40.7128)) AS wkt;
-- Parsing and re-serializing should produce an equivalent WKT string.
SELECT ST_AS_TEXT(ST_GEOM_FROM_TEXT('POINT(139.6503 35.6762)')) AS wkt;
-- Useful for debugging unexpected geometry values in a catalog column.
SELECT parcel_id, ST_AS_TEXT(geom) AS wkt
FROM gis.catalog.parcels
WHERE parcel_id = 'P-00421';
-- WKT is portable across SQL engines and GIS libraries.
SELECT zone_id, ST_AS_TEXT(zone_geom) AS wkt
FROM gis.catalog.service_zones;
-- Turn raw coordinate columns into WKT strings.
SELECT id, ST_AS_TEXT(ST_MAKE_POINT(lng, lat)) AS point_wkt
FROM geo.catalog.poi;
-- WKT output preserves interior rings.
SELECT ST_AS_TEXT(ST_GEOM_FROM_TEXT(
  'POLYGON((0 0, 10 0, 10 10, 0 10, 0 0), (2 2, 4 2, 4 4, 2 4, 2 2))'
)) AS wkt;

Pitfalls

See Also

Open in interactive docs →   DeltaForge home →