The Definitive Guide to Well-Known Text (WKT) Geometry
An exhaustive technical specification of OGC Well-Known Text, syntax rules, coordinate dimensions, spatial SQL queries, and common parsing errors.
- 1. What is Well-Known Text (WKT)?
- 2. Fundamental Grammar & Coordinate Syntax
- 3. Coordinate Dimensions: 2D, 3D (Z), and Measured (M)
- 4. The 18 WKT Geometry Types with Examples
- 5. Polygons with Inner Rings (Holes) and Winding Rules
- 6. Using WKT in Databases (PostGIS, MySQL, SpatiaLite)
- 7. Top 5 WKT Errors and How to Resolve Them
- 8. Visualizing WKT in Spatial-Sandbox
1. What is Well-Known Text (WKT)?
Well-Known Text (WKT) is a standardized text markup language defined by the Open Geospatial Consortium (OGC) in its Simple Feature Access specification (ISO/IEC 13249-3). It is designed to represent vector spatial geometries in an unambiguous, human-readable ASCII format.
WKT serves as the universal lingua franca for spatial databases, GIS engines, and geographic software. Whether querying PostgreSQL/PostGIS using ST_AsText(geom), executing spatial queries in Snowflake or BigQuery, or inspecting geometry columns in MySQL and SQLite/SpatiaLite, WKT is the primary representation returned for spatial geometries.
Did You Know? WKT has a binary equivalent called WKB (Well-Known Binary), which stores the identical coordinate topology in high-performance byte arrays with little-endian or big-endian encoding for fast disk and network transmission.
2. Fundamental Grammar & Coordinate Syntax
The grammar of WKT is deceptively simple but enforces strict formatting rules:
- Type Keyword: Every geometry begins with its uppercase geometry type keyword (e.g.
POINT,LINESTRING,POLYGON). Case sensitivity varies across database parsers, but uppercase is the universal standard. - Coordinate Pair Delimiters: Within a coordinate, the X (Longitude) and Y (Latitude) ordinates are separated by whitespace only (e.g.
-73.985 40.748). Never use a comma between the X and Y ordinates. - Vertex Delimiters: Consecutive vertices in a path or ring are separated by a comma followed by a space (e.g.
0 0, 10 0, 10 10, 0 10, 0 0). - Boundary Parentheses: Coordinate arrays are enclosed within matching parentheses:
(). Compound geometries like Polygons with holes or MultiPolygons use nested parentheses:((...))and(((...))). - Empty Geometries: An uninitialized or null geometry is represented using the
EMPTYkeyword (e.g.POINT EMPTY,POLYGON EMPTY).
-- Example: Point geometry
POINT(-73.985130 40.748817)
-- Example: Multi-vertex LineString
LINESTRING(-73.985 40.748, -73.975 40.755, -73.960 40.770)
-- Example: Closed Polygon (First coordinate matches last coordinate)
POLYGON((-73.98 40.74, -73.96 40.74, -73.96 40.76, -73.98 40.76, -73.98 40.74))
3. Coordinate Dimensions: 2D, 3D (Z), and Measured (M)
Modern OGC WKT supports up to four dimensions per coordinate vertex:
| Dimension | Suffix | Description | Example Syntax |
|---|---|---|---|
| 2D (Standard) | None | Standard planar coordinates: X (Longitude) and Y (Latitude). | POINT(10 20) |
| 3D (Elevation / Z) | Z |
Adds altitude or elevation above sea level: X, Y, Z. | POINT Z (10 20 150.5) |
| 3D (Measured / M) | M |
Adds a linear referencing measurement (e.g. kilometer post, time epoch): X, Y, M. | POINT M (10 20 42.1) |
| 4D (Z + M) | ZM |
Combines both elevation and linear measurement: X, Y, Z, M. | POINT ZM (10 20 150.5 42.1) |
4. The 18 WKT Geometry Types with Examples
The OGC Simple Feature Access specification enumerates a complete hierarchy of geometries:
1. Basic Primitives
POINT(x y)— A single geographic point.LINESTRING(x1 y1, x2 y2, ...)— A connected sequence of line segments. Must have at least two vertices.POLYGON((x1 y1, x2 y2, ..., x1 y1))— A closed planar surface bounded by an exterior linear ring. Must have at least 4 coordinates with the first and last identical.
2. Multi-Part Collections
MULTIPOINT((x1 y1), (x2 y2))— A collection of independent points. Note that PostGIS also accepts the unparenthesized syntaxMULTIPOINT(x1 y1, x2 y2).MULTILINESTRING((x1 y1, x2 y2), (x3 y3, x4 y4))— A set of unconnected or branching lines.MULTIPOLYGON(((exterior1)), ((exterior2)))— Multiple discrete polygons grouped into a single spatial entity (e.g. an archipelago of islands).GEOMETRYCOLLECTION(POINT(0 0), LINESTRING(1 1, 2 2))— A heterogeneous container holding any mixture of geometric types.
3. Advanced & 3D Surfaces
CIRCULARSTRING(x1 y1, x2 y2, x3 y3)— A circular arc defined by three points (start, midpoint on arc, end).COMPOUNDCURVE(CIRCULARSTRING(...), LINESTRING(...))— A continuous curve composed of circular arcs and straight segments.CURVEPOLYGON(CIRCULARSTRING(...))— A planar polygon bounded by curved edges.POLYHEDRALSURFACE(...)— A 3D surface composed of contiguous planar polygons sharing edges (e.g. building footprints with vertical walls).TIN(...)— Triangulated Irregular Network representing digital elevation models (DEM).
5. Polygons with Inner Rings (Holes) and Winding Rules
One of the most frequent sources of GIS confusion is defining polygons with interior holes (e.g. a donut shape or a lake inside an island).
In WKT, holes are expressed by adding additional coordinate rings separated by commas inside the outer POLYGON(...) parentheses:
-- Donut Polygon: Outer square (0 to 10) with Inner hole (3 to 7)
POLYGON(
(0 0, 10 0, 10 10, 0 10, 0 0), -- Ring 1: Outer boundary
(3 3, 7 3, 7 7, 3 7, 3 3) -- Ring 2: Inner hole
)
OGC Topological Rules for Holes:
- The first ring is always the exterior boundary.
- All subsequent rings are holes completely contained within the exterior ring.
- Holes must not cross each other or the exterior ring (they may touch at a single tangent vertex).
- Winding Order: In standard OGC conventions, the exterior ring proceeds counter-clockwise (CCW), while interior rings proceed clockwise (CW). Modern spatial engines like PostGIS will automatically normalize winding order during validation via
ST_ForcePolygonCCW().
6. Using WKT in Databases (PostGIS, MySQL, SpatiaLite)
Spatial databases store geometries internally in optimized binary formats, but accept WKT for insertions, updates, and spatial filtering.
PostgreSQL / PostGIS
-- Inserting WKT with an explicit Spatial Reference System (SRID 4326 for WGS84)
INSERT INTO parcels (id, name, geom)
VALUES (1, 'Central Park Zone', ST_GeomFromText('POLYGON((-73.97 40.76, -73.95 40.79, -73.98 40.80, -73.97 40.76))', 4326));
-- Querying as WKT
SELECT id, name, ST_AsText(geom) AS wkt_geometry
FROM parcels
WHERE ST_Intersects(geom, ST_MakeEnvelope(-74.0, 40.7, -73.9, 40.8, 4326));
MySQL Spatial
-- Inserting in MySQL 8.0+
INSERT INTO locations (title, location)
VALUES ('Times Square', ST_GeomFromText('POINT(-73.9855 40.7580)', 4326));
7. Top 5 WKT Errors and How to Resolve Them
1. Inverted Coordinate Order (The "Null Island" Bug)
Symptom: Your New York or London geometry renders in the ocean off the coast of West Africa (0°N, 0°E).
Cause: Entering Latitude first instead of Longitude. WKT requires X Y (Longitude, Latitude).
Fix: Swap coordinates: POINT(40.7128 -74.0060) ➔ POINT(-74.0060 40.7128).
2. Unclosed Linear Rings
Symptom: Database error: ERROR: points must form a closed linestring.
Cause: The last coordinate of a polygon does not match the first coordinate exactly.
Fix: Repeat the first vertex at the end of the coordinate sequence.
3. Self-Intersecting Polygons (Bow-tie shapes)
Symptom: ST_IsValid(geom) returns false with reason Self-intersection at or near....
Cause: Boundary lines cross over themselves like a figure-8.
Fix: Run PostGIS ST_MakeValid(geom) or split the geometry into a MULTIPOLYGON.
4. Commas Between X and Y Coordinates
Symptom: Parse error: parse error at position ... expected number.
Cause: Writing POINT(-73.985, 40.748) with a comma between X and Y.
Fix: Use space between X and Y: POINT(-73.985 40.748).
5. Missing Double Parentheses on Polygons
Symptom: Syntax error when parsing polygon.
Cause: Writing POLYGON(0 0, 10 0, 10 10, 0 0) with single parentheses. Polygons require nested parentheses because they support multiple rings.
Fix: Use double parentheses: POLYGON((0 0, 10 0, 10 10, 0 0)).
8. Visualizing WKT in Spatial-Sandbox
Need to inspect or troubleshoot a WKT string immediately? Use our free interactive tools:
- WKT Viewer & Plotter — Paste any WKT string to render on satellite imagery, inspect vertices, and export.
- WKT Syntax Validator — Lints your WKT string against OGC Simple Features rules and provides instant error diagnostics.
- GeoJSON Viewer — Convert between WKT and GeoJSON seamlessly in your browser session.