docs/sql-ref-geospatial-types.md
Spark SQL supports GEOMETRY and GEOGRAPHY types for spatial data, as defined in the Open Geospatial Consortium (OGC) Simple Feature Access specification. At runtime, values are represented as Well-Known Binary (WKB) and are associated with a Spatial Reference Identifier (SRID) that defines the coordinate system. How values are persisted is determined by each data source.
| Type | Coordinate system | Typical use and notes |
|---|---|---|
| GEOMETRY | Cartesian (planar) | Projected or local coordinates; planar calculations. Represents points, lines, polygons in a flat coordinate system. Suitable for Web Mercator (SRID 3857), UTM, or local grids (e.g. engineering/CAD). Accepts any SRID in the registry, including SRID 0 (unspecified CRS). |
| GEOGRAPHY | Geographic (latitude/longitude) | Earth-based data; distances and areas on the sphere/ellipsoid. Coordinates in longitude and latitude (degrees). Edge interpolation is always SPHERICAL. Only geographic SRIDs are accepted; the most common is 4326 (WGS 84). |
In SQL, GEOMETRY and GEOGRAPHY columns must always be declared with an explicit SRID
(or ANY); see Type Syntax in SQL below. When a value is constructed
via ST_GeomFromWKB(wkb) without an explicit SRID, the value's SRID is 0 (unspecified),
while ST_GeogFromWKB(wkb) always returns a value with SRID 4326.
Choose GEOMETRY when:
Choose GEOGRAPHY when:
Using the wrong type can give misleading results: for example, the shortest path between London and New York on a sphere crosses Canada, whereas a planar GEOMETRY may suggest a path that does not.
In SQL you must specify the type with an SRID or ANY:
GEOMETRY(srid) — e.g. GEOMETRY(4326), GEOMETRY(3857)GEOGRAPHY(srid) — e.g. GEOGRAPHY(4326)GEOMETRY(ANY)GEOGRAPHY(ANY)Unparameterized GEOMETRY or GEOGRAPHY (without (srid) or (ANY)) is not supported in SQL.
-- Fixed SRID: all values must use the given SRID (e.g. WGS 84)
CREATE TABLE points (
id BIGINT,
pt GEOMETRY(4326)
);
CREATE TABLE locations (
id BIGINT,
loc GEOGRAPHY(4326)
);
-- Mixed SRID: each row can have a different SRID
CREATE TABLE mixed_geoms (
id BIGINT,
geom GEOMETRY(ANY)
);
Values are created from Well-Known Binary (WKB) using built-in functions. WKB is a standard binary encoding for spatial shapes (points, lines, polygons, etc.). See Well-known binary for the format.
From WKB (binary):
ST_GeomFromWKB(wkb) — returns GEOMETRY with default SRID 0.ST_GeomFromWKB(wkb, srid) — returns GEOMETRY with the given SRID.ST_GeogFromWKB(wkb) — returns GEOGRAPHY with SRID 4326.Example (point in WKB, then use in a table):
-- Point (1, 2) in WKB (little-endian point, 2D)
SELECT ST_GeomFromWKB(X'0101000000000000000000F03F0000000000000040');
SELECT ST_GeomFromWKB(X'0101000000000000000000F03F0000000000000040', 4326);
SELECT ST_GeogFromWKB(X'0101000000000000000000F03F0000000000000040');
INSERT INTO points (id, pt)
VALUES (1, ST_GeomFromWKB(X'0101000000000000000000F03F0000000000000040', 4326));
When parsing WKB, Spark applies the following rules. Violations result in a parse error.
POINT EMPTY in Well-Known Text). LineString and Polygon (and points inside them) do not allow NaN in coordinate values.ST_GeogFromWKB), longitude must be in [-180, 180] (inclusive) and latitude in [-90, 90] (inclusive). GEOMETRY does not enforce these bounds.Spark SQL provides scalar functions for working with GEOMETRY and GEOGRAPHY values. The full list, with detailed argument descriptions and examples, is on the Built-in Functions page under Geospatial ST Functions. The functions provided in the current release are summarized here:
| Function | Description |
|---|---|
ST_AsBinary(geo[, endianness]) | Returns the GEOMETRY or GEOGRAPHY value as WKB (BINARY). The optional endianness argument is 'NDR' for little-endian (default) or 'XDR' for big-endian. |
ST_GeomFromWKB(wkb[, srid]) | Parses WKB and returns a GEOMETRY. The optional srid argument sets the SRID; if omitted, the SRID is 0. |
ST_GeogFromWKB(wkb) | Parses WKB and returns a GEOGRAPHY with SRID 4326. |
ST_Srid(geo) | Returns the SRID of the GEOMETRY or GEOGRAPHY value (NULL if input is NULL). |
ST_SetSrid(geo, srid) | Returns a new GEOMETRY or GEOGRAPHY with the given SRID. The new SRID must be valid for the value's type. |
Examples:
SELECT hex(ST_AsBinary(ST_GeogFromWKB(X'0101000000000000000000F03F0000000000000040')));
-- 0101000000000000000000F03F0000000000000040
SELECT hex(ST_AsBinary(ST_GeomFromWKB(X'0101000000000000000000F03F0000000000000040'), 'XDR'));
-- 00000000013FF00000000000004000000000000000
SELECT ST_Srid(ST_GeogFromWKB(X'0101000000000000000000F03F0000000000000040'));
-- 4326
SELECT ST_Srid(ST_SetSrid(ST_GeomFromWKB(X'0101000000000000000000F03F0000000000000040'), 3857));
-- 3857
GEO_ENCODER_SRID_MISMATCH_ERROR. Use ST_SetSrid to change a value's SRID to match the column.GEOMETRY(ANY) or GEOGRAPHY(ANY)): Values can have different SRIDs per row. Each value must still have a valid SRID for the type; an invalid SRID raises ST_INVALID_SRID_VALUE.GEOMETRY(ANY) or GEOGRAPHY(ANY); mixed-SRID types exist for in-memory/query use only.Spark includes a pre-built SRID registry that combines coordinate systems from the PROJ database with OGC standard overrides. This registry enables validation and proper handling of coordinate systems for geospatial data.
SRID Compatibility Rules:
| Spark Version | PROJ Version |
|---|---|
| 4.2.0 | 9.8.1 |
The SRID registry is pinned to the PROJ version shown above and is not synced live with external databases.
Spark applies the following OGC standard overrides to specific SRIDs from the PROJ database:
| SRID | PROJ CRS Identifier | OGC CRS Identifier | Description |
|---|---|---|---|
| 4326 | EPSG:4326 | OGC:CRS84 | WGS 84 (longitude/latitude order per OGC standard) |
| 4267 | EPSG:4267 | OGC:CRS27 | NAD27 |
| 4269 | EPSG:4269 | OGC:CRS83 | NAD83 |
| SRID | CRS Identifier | Name | CRS Type | Description |
|---|---|---|---|---|
| 0 | SRID:0 | Unspecified | Cartesian | Coordinates with no defined CRS (default for ST_GeomFromWKB(wkb)) |
| 4326 | OGC:CRS84 | WGS 84 | Geographic | World Geodetic System 1984 (longitude/latitude), GPS coordinates, global data (default for GEOGRAPHY) |
| 4267 | OGC:CRS27 | NAD27 | Geographic | North American Datum 1927 |
| 4269 | OGC:CRS83 | NAD83 | Geographic | North American Datum 1983 |
| 3857 | EPSG:3857 | Web Mercator | Projected | Pseudo-Mercator projection used by web mapping services |
Notes:
GEOMETRY(0) means a fixed SRID of 0. For mixed per-row SRIDs, use GEOMETRY(ANY).GEOMETRY(ANY) or GEOGRAPHY(ANY).Invalid SRID (not in registry):
SELECT ST_GeomFromWKB(X'0101000000000000000000F03F0000000000000040', 99999);
-- Throws [ST_INVALID_SRID_VALUE]
Projected SRID with GEOGRAPHY type:
CREATE TABLE invalid_geo (id BIGINT, loc GEOGRAPHY(3857));
-- Throws [ST_INVALID_SRID_VALUE] (3857 is projected, not geographic)
For the full list of supported data types and API usage in Scala, Java, Python, and SQL, see Data Types.