Tableau function Spatial

Tableau PARSE_WKT to SQL

PARSE_WKT([Wkt]) turns text such as POINT (106.7009 10.7769) into a point you can map. WKT puts longitude first. In SQL it is DuckDB ST_GeomFromText(Wkt), Snowflake TO_GEOGRAPHY(Wkt) or BigQuery ST_GEOGFROMTEXT(Wkt). If you parse WKT by hand, remember that the space after the type name is optional. The runnable SQL builds the same GeoJSON the engine returns with json_object, because this page's DuckDB has no spatial extension. With the extension loaded, the DuckDB call is shown in the FAQ.

Updated · Checked on the xmetric engine

What is the syntax?

PARSE_WKT(string)
string
Well-Known Text, such as POINT (106.7009 10.7769), longitude first.

Returns a spatial object read from Well-Known Text. Null when the string is null.

What does it return, and what is the SQL?

Each example runs on the same 4 sample rows twice: the Tableau formula on the xmetric engine, and the SQL in DuckDB.

Where is each depot, read from its WKT?

Tableau

PARSE_WKT([Wkt])

SQL (DuckDB)

SELECT
  CASE
    WHEN Wkt IS NULL THEN NULL
    ELSE json_object('type', 'Point', 'coordinates', list_value(CAST (regexp_extract(Wkt, '(?i)POINT\s*\(\s*(\S+)\s+([^\s)]+)\s*\)', 1) AS DOUBLE), CAST (regexp_extract(Wkt, '(?i)POINT\s*\(\s*(\S+)\s+([^\s)]+)\s*\)', 2) AS DOUBLE)))
    END AS result
FROM input
ORDER BY id

Row 2 is written POINT(105.8542 21.0285), with no space, and still parses. Hue has no WKT, so it gets null.

WktResult
POINT (106.7009 10.7769)[object Object]
POINT(105.8542 21.0285)[object Object]
POINT (108.2022 16.0544)[object Object]
null

The Tableau formula and the SQL return the same 4 rows.

Where does a straight translation go wrong?

Common mistake

Why does a pattern with a space reject a valid point?

WKT allows POINT(x y) as well as POINT (x y), and Tableau reads both. A pattern that requires the space flags Hanoi's row as unreadable. Allow any whitespace, and any letter case, after the type name.

Tableau

ISNULL(PARSE_WKT([Wkt]))

Tempting SQL

SELECT
  Wkt IS NULL OR NOT regexp_matches(Wkt, '^POINT \(') AS result
FROM input
ORDER BY id

SQL that matches

SELECT
  Wkt IS NULL OR NOT regexp_matches(Wkt, '(?i)^\s*POINT\s*\(') AS result
FROM input
ORDER BY id
WktTableauTempting SQLSQL that matches
POINT (106.7009 10.7769)falsefalsefalse
POINT(105.8542 21.0285)falsetruefalse
POINT (108.2022 16.0544)falsefalsefalse
truetruetrue

The SQL that matches returns the Tableau result on every row; the marked cells are where the tempting SQL differs.

How do I write it in Snowflake or BigQuery?

The first example, written for each warehouse from its documentation. These are not run here; DuckDB is.

Snowflake

SELECT TO_GEOGRAPHY(Wkt) AS result
FROM input
ORDER BY id

Snowflake documentation

BigQuery

SELECT ST_GEOGFROMTEXT(Wkt) AS result
FROM input
ORDER BY id

BigQuery documentation

What does your own PARSE_WKT formula return?

Edit the formula and run it on the 4 sample rows. Fields: [id], [Depot], [Lat], [Lon], [DestLat], [DestLon], [Wkt], [Route].

Runs in your browser on the xmetric engine. Nothing is uploaded.

Which functions go with it?

What do people ask about PARSE_WKT?

Is WKT latitude first?

No. WKT writes x then y, which is longitude then latitude: POINT (106.7009 10.7769) is Saigon. That is the opposite of MAKEPOINT's argument order.

What is the DuckDB call with the spatial extension?

INSTALL spatial; LOAD spatial; then ST_GeomFromText(Wkt). It reads every WKT type (points, lines, polygons) without a hand-written parser.

Where is this documented?