Adds z-axis values to a geometry object.
Syntax
geometry ST_AddZ(geometry g1, float[] zvalues)Parameters
| Parameter | Description |
|---|---|
g1 | The input geometry object. |
zvalues | An array of z-axis values (float8[]). |
Usage notes
If the geometry already has z-axis values, the function returns it unchanged.
For a 3DM geometry (which has M values but no Z), the function adds z-axis values and returns a ZM geometry.
Each element in
zvaluesmaps to a point in the geometry by position. Ifzvalueshas fewer elements than the geometry has points, the function pads remaining z-axis values with0.
Examples
All examples use ST_AsText to display the result in Well-Known Text (WKT) format.
Point and PointM
-- Add a z value to a 2D point → POINT Z
SELECT ST_AsText(ST_addZ(ST_MakePoint(116.345, 31.257), '{3.129}'::float8[]));
-- st_astext
-- --------------------------------
-- POINT Z (116.345 31.257 3.129)
-- Add a z value to a point with M → POINT ZM
SELECT ST_AsText(ST_addZ(ST_MakePointM(116.345, 31.257, 0.006), '{3.129}'::float8[]));
-- st_astext
-- ---------------------------------------
-- POINT ZM (116.345 31.257 3.129 0.006)Linestring and LinestringM
-- Add z values to a 2D linestring → LINESTRING Z
SELECT ST_AsText(ST_addZ('LINESTRING(110.268550710707 19.912797729366,110.26842843605 19.912673441389,110.268391117929 19.912635081628)'::geometry, '{68.890150670893,69.183771011419,69.281459503807}'::float8[]));
-- st_astext
-- ------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- LINESTRING Z (110.268550710707 19.912797729366 68.890150670893,110.26842843605 19.912673441389 69.183771011419,110.268391117929 19.912635081628 69.281459503807)
-- Add z values to a linestring with M → LINESTRING ZM
SELECT ST_AsText(ST_addZ('LINESTRING M(110.268550710707 19.912797729366 0.0002,110.26842843605 19.912673441389 0.0008,110.268391117929 19.912635081628 0.0019)'::geometry, '{68.890150670893,69.183771011419,69.281459503807}'::float8[]));
-- st_astext
-- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
-- LINESTRING ZM (110.268550710707 19.912797729366 68.890150670893 0.0002,110.26842843605 19.912673441389 69.183771011419 0.0008,110.268391117929 19.912635081628 69.281459503807 0.0019)Polygon and PolygonM
-- Add z values to a 2D polygon → POLYGON Z
SELECT ST_AsText(ST_addZ('POLYGON((110.271437131412 19.916296059835,110.271458864211 19.916327373408,110.271472618849 19.916347078401,110.271494504499 19.916378842523,110.271525107774 19.916422797481,110.271559942483 19.916473012678,110.271588184066 19.916514021908,110.271608951741 19.916544548085,110.271637533517 19.916586350525,110.271662184676 19.916622302994,110.27168697878 19.916658943238,110.271690317318 19.916663834658,110.271437131412 19.916296059835))'::geometry, '{66.754327018745,66.76812185145,66.775424819085,66.788278159307,66.806879613697,66.82753102727,66.844599057935,66.857178772322,66.86311322099,66.869851471601,66.876122904615,66.8772665672,66.754327018745}'::float8[]));
-- POLYGON Z ((110.271437131412 19.916296059835 66.754327018745,110.271458864211 19.916327373408 66.76812185145,110.271472618849 19.916347078401 66.775424819085,110.271494504499 19.916378842523 66.788278159307,110.271525107774 19.916422797481 66.806879613697,110.271559942483 19.916473012678 66.82753102727,110.271588184066 19.916514021908 66.844599057935,110.271608951741 19.916544548085 66.857178772322,110.271637533517 19.916586350525 66.86311322099,110.271662184676 19.916622302994 66.869851471601,110.27168697878 19.916658943238 66.876122904615,110.271690317318 19.916663834658 66.8772665672,110.271437131412 19.916296059835 66.754327018745))
-- Add z values to a polygon with M → POLYGON ZM
SELECT ST_AsText(ST_addZ('POLYGON M((110.271437131412 19.916296059835 0.001,110.271458864211 19.916327373408 0.001,110.271472618849 19.916347078401 0.001,110.271494504499 19.916378842523 0.001,110.271525107774 19.916422797481 0.001,110.271559942483 19.916473012678 0.001,110.271588184066 19.916514021908 0.001,110.271608951741 19.916544548085 0.001,110.271637533517 19.916586350525 0.001,110.271662184676 19.916622302994 0.001,110.27168697878 19.916658943238 0.001,110.271690317318 19.916663834658 0.001,110.271437131412 19.916296059835 0.001))'::geometry, '{66.754327018745,66.76812185145,66.775424819085,66.788278159307,66.806879613697,66.82753102727,66.844599057935,66.857178772322,66.86311322099,66.869851471601,66.876122904615,66.8772665672,66.754327018745}'::float8[]));
-- POLYGON ZM ((110.271437131412 19.916296059835 66.754327018745 0.001,110.271458864211 19.916327373408 66.76812185145 0.001,110.271472618849 19.916347078401 66.775424819085 0.001,110.271494504499 19.916378842523 66.788278159307 0.001,110.271525107774 19.916422797481 66.806879613697 0.001,110.271559942483 19.916473012678 66.82753102727 0.001,110.271588184066 19.916514021908 66.844599057935 0.001,110.271608951741 19.916544548085 66.857178772322 0.001,110.271637533517 19.916586350525 66.86311322099 0.001,110.271662184676 19.916622302994 66.869851471601 0.001,110.27168697878 19.916658943238 66.876122904615 0.001,110.271690317318 19.916663834658 66.8772665672 0.001,110.271437131412 19.916296059835 66.754327018745 0.001))See also
ST_MakePoint— constructs a 2D or 3D point geometryST_MakePointM— constructs a point geometry with an M valueST_AsText— returns the WKT representation of a geometry