The ST_Dimension() function
The ST_Dimension() function returns the dimension of a geometry object.
A geometry can have one of three dimensions:
- 0
- The geometry has neither length nor area.
- 1
- The geometry has length.
- 2
- The geometry has area.
Syntax
ST_Dimension(g1 ST_Geometry)
Return type
INTEGER
Example
The dimension_test table
is created with the columns geotype and g1. The geotype column
stores the name of the subclass stored in the g1 ST_Geometry
column:
CREATE TABLE dimension_test (geotype varchar(20),
g1 ST_Geometry);
The
following INSERT statements insert a sample subclass into the dimension_test table:
INSERT INTO dimension_test VALUES(
'Point',
ST_PointFromText('point (10.02 20.01)',1000)
);
INSERT INTO dimension_test VALUES(
'Linestring',
ST_LineFromText('linestring (10.02 20.01,10.32 23.98,11.92 25.64)',1000)
);
INSERT INTO dimension_test VALUES(
'Polygon',
ST_PolyFromText('polygon ((10.02 20.01,11.92 35.64,25.02 34.15,
19.15 33.94,10.02 20.01))',1000)
);
INSERT INTO dimension_test VALUES(
'Multipoint',
ST_MPointFromText('multipoint (10.02 20.01,10.32 23.98,11.92 25.64)',1000)
);
INSERT INTO dimension_test VALUES(
'Multilinestring',
ST_MLineFromText('multilinestring ((10.02 20.01,10.32
23.98,11.92 25.64),(9.55 23.75,15.36 30.11))',1000)
);
INSERT INTO dimension_test VALUES(
'Multipolygon',
ST_MPolyFromText('multipolygon (((10.02 20.01,11.92 35.64,25.02
34.15,19.15 33.94,10.02 20.01)),((51.71 21.73,73.36 27.04,71.52
32.87,52.43 31.90,51.71 21.73)))',1000)
);
The SELECT statement lists the subclass name
stored in the geotype column with the dimension of that geotype:
SELECT geotype, ST_Dimension(g1) Dimension
FROM dimension_test;
geotype dimension
Point 0
Linestring 1
Polygon 2
Multipoint 0
Multilinestring 1
Multipolygon 2