ST_NDims

更新时间:
复制 MD 格式

Returns the coordinate dimension of a geometry object as an integer: 2 for (X,Y), 3 for (X,Y,Z) or (X,Y,M), and 4 for (X,Y,Z,M).

Syntax

integer ST_NDims(geometry g1);

Parameters

ParameterDescription
g1The geometry object to check.

Description

ST_NDims returns the number of coordinate dimensions in the geometry:

  • 2D geometry (X,Y) returns 2

  • 3D geometry (X,Y,Z) or (X,Y,M) returns 3

  • 4D geometry (X,Y,Z,M) returns 4

Z coordinates are preserved — this function supports 3D geometry without dropping the Z value.

Examples

The following query returns the coordinate dimension for a 2D point, a 3DZ point, a 3DM point, and a 4D point side by side:

SELECT
  ST_NDims('POINT(0 1)'::geometry)        AS d2,
  ST_NDims('POINT(0 1 2)'::geometry)      AS d3z,
  ST_NDims('POINTM(0 1 2)'::geometry)     AS d3m,
  ST_NDims('POINT(0 1 2 3)'::geometry)    AS d4;
 d2 | d3z | d3m | d4
----+-----+-----+----
  2 |   3 |   3 |  4
(1 row)

Both 3DZ (X,Y,Z) and 3DM (X,Y,M) return 3, confirming they have the same coordinate dimension.