Geometry in SQL
Description
Section titled “Description”Not every geospatial operation needs a dedicated step. Anything that is a single-row column expression runs as a Table Extract expression cell, in the database, with no rows leaving the warehouse — so it scales with the table rather than with the workflow engine. This is the route Alteryx conversion picks for the Alteryx Create Points and Distance tools.
Portable Geometry Functions
Section titled “Portable Geometry Functions”Lakehouse v1 and v2 spell their geometry functions differently — ST_MAKEGEOMPOINT
against ST_POINT, ST_ASWKT against ST_ASTEXT. The geom_* family resolves
to the right one for the workspace you are running on, so an expression written
once works on both.
| Expression | Lakehouse v1 | Lakehouse v2 |
|---|---|---|
func.geom_from_wkt(x) |
ST_GEOMETRYFROMWKT |
ST_GEOMETRYFROMTEXT |
func.geom_point(lon, lat) |
ST_MAKEGEOMPOINT |
ST_POINT |
func.geom_as_wkt(g) |
ST_ASWKT |
ST_ASTEXT |
func.geom_contains(a, b) |
ST_CONTAINS |
ST_CONTAINS |
func.geom_within(a, b) |
ST_WITHIN |
ST_CONTAINS, arguments swapped |
func.geom_x(g) |
ST_X |
ST_X |
func.geom_y(g) |
ST_Y |
ST_Y |
Create Points
Section titled “Create Points”Build a WKT point from a longitude column and a latitude column:
func.geom_as_wkt( func.geom_point( get_column(table, 'Longitude'), get_column(table, 'Latitude'), ))geom_as_wkt writes the result as WKT, the form every PlaidCloud spatial step
reads and writes.
Distance Between Two Points
Section titled “Distance Between Two Points”Distance is the one operation here with no portable spelling — the two lakehouses disagree on both the function and its return unit, so the expression differs by version.
Lakehouse v2
Section titled “Lakehouse v2”ST_DISTANCE_SPHERE takes four coordinates and returns meters:
case( ( and_( get_column(table, 'Origin').like('POINT%'), get_column(table, 'Destination').like('POINT%'), ), func.st_distance_sphere( func.geom_x(func.geom_from_wkt(get_column(table, 'Origin'))), func.geom_y(func.geom_from_wkt(get_column(table, 'Origin'))), func.geom_x(func.geom_from_wkt(get_column(table, 'Destination'))), func.geom_y(func.geom_from_wkt(get_column(table, 'Destination'))), ) * 0.0006213711922373339, ), else_=null,)| Unit | Factor from meters |
|---|---|
| Miles | 0.0006213711922373339 |
| Kilometers | 0.001 |
| Meters | 1 |
| Feet | 3.280839895013123 |
Lakehouse v1
Section titled “Lakehouse v1”geom_distance is planar over two geometries and returns degrees, so the
factor converts from degrees rather than meters:
func.geom_distance( func.geom_from_wkt(get_column(table, 'Origin')), func.geom_from_wkt(get_column(table, 'Destination')),) * 69.1709| Unit | Factor from degrees |
|---|---|
| Miles | 69.1709 |
| Kilometers | 111.32 |
| Meters | 111320.0 |
| Feet | 364566.929 |
Being planar, this is an approximation that degrades away from the equator — fine over the local distances typical of catchment analysis, less so across a continent.
The LIKE 'POINT%' guard on the v2 form matters: ST_DISTANCE_SPHERE takes four
coordinates, so a non-point geometry on either side would silently measure the
wrong thing. The CASE returns null for those rows instead.
When to Use a Step Instead
Section titled “When to Use a Step Instead”| You want | Use |
|---|---|
| Distance from each row to its nearest rows in another table | Spatial Find Nearest |
| Distance as a filter in a spatial join | Spatial Match |
| Area, length, centroid, or bounding box | Spatial Info |
| Anything that changes the shape of a geometry | Geospatial steps |
Related
Section titled “Related”- Geospatial steps
- Geometry Functions (Lakehouse v1)
- Geography Functions (Lakehouse v1) — H3 and geohash indexing
- Spatial Functions (Lakehouse v2)