Affine TransformationsThese functions change the position and shape of geometries using
affine transformations.ST_AffineApply a 3D affine transformation to a geometry.geometry ST_Affinegeometry geomAfloat afloat bfloat cfloat dfloat efloat ffloat gfloat hfloat ifloat xofffloat yofffloat zoffgeometry ST_Affinegeometry geomAfloat afloat bfloat dfloat efloat xofffloat yoffDescriptionApplies a 3D affine transformation to the geometry to do things like translate, rotate, scale in one step.
Version 1: The
call ST_Affine(geom, a, b, c, d, e, f, g, h, i, xoff, yoff, zoff)
represents the transformation matrix / a b c xoff \
| d e f yoff |
| g h i zoff |
\ 0 0 0 1 / and the vertices are transformed as
follows: x' = a*x + b*y + c*z + xoff
y' = d*x + e*y + f*z + yoff
z' = g*x + h*y + i*z + zoff All of the translate / scale
functions below are expressed via such an affine
transformation.Version 2: Applies a 2d affine transformation to the geometry. The
call ST_Affine(geom, a, b, d, e, xoff, yoff)
represents the transformation matrix / a b 0 xoff \ / a b xoff \
| d e 0 yoff | rsp. | d e yoff |
| 0 0 1 0 | \ 0 0 1 /
\ 0 0 0 1 / and the vertices are transformed as
follows: x' = a*x + b*y + xoff
y' = d*x + e*y + yoff
z' = z This method is a subcase of the 3D method
above.Enhanced: 2.0.0 support for Polyhedral surfaces, Triangles and TIN was introduced.Availability: 1.1.2. Name changed from Affine to ST_Affine in 1.2.2Prior to 1.3.4, this function crashes if used with geometries that contain CURVES. This is fixed in 1.3.4+&P_support;&T_support;&Z_support;&curve_support;ExamplesRotate a 3D line 180 degrees about the z axis. Note this is long-hand for doing ST_Rotate();
SELECT ST_Affine(geom, cos(pi()), -sin(pi()), 0, sin(pi()), cos(pi()), 0, 0, 0, 1, 0, 0, 0) As using_affine,
ST_Rotate(geom, pi()) As using_rotate
FROM (SELECT 'LINESTRING(1 2 3,1 4 3)'::geometry As geom) As foo; using_affine | using_rotate
-----------------------------+-----------------------------
LINESTRING(-1 -2 3,-1 -4 3) | LINESTRING(-1 -2 3,-1 -4 3)
(1 row)Rotate a 3D line 180 degrees in both the x and z axis.
SELECT ST_Affine(geom, cos(pi()), -sin(pi()), 0, sin(pi()), cos(pi()), -sin(pi()), 0, sin(pi()), cos(pi()), 0, 0, 0)
FROM (SELECT 'LINESTRING(1 2 3,1 4 3)'::geometry As geom) As foo;LINESTRING(-1 -2 -3,-1 -4 -3)See Also, , , ST_RotateRotates a geometry about an origin point.geometry ST_RotategeometrygeomAfloatrotRadiansgeometry ST_RotategeometrygeomAfloatrotRadiansfloatx0floaty0geometry ST_RotategeometrygeomAfloatrotRadiansgeometrypointOriginDescriptionRotates geometry rotRadians counter-clockwise about the origin point. The rotation origin can be
specified either as a POINT geometry, or as x and y coordinates. If the origin is not
specified, the geometry is rotated about POINT(0 0).Enhanced: 2.0.0 support for Polyhedral surfaces, Triangles and TIN was introduced.Enhanced: 2.0.0 additional parameters for specifying the origin of rotation were added.Availability: 1.1.2. Name changed from Rotate to ST_Rotate in 1.2.2&Z_support;&curve_support;&P_support;&T_support;ExamplesThis example rotates a geometry by 180 degrees.SELECT ST_Rotate('LINESTRING (50 160,50 50,100 50)'::geometry, pi());LINESTRING(-50 -160,-50 -50,-100 -50)This example rotates a geometry 30 degrees counter-clockwise around x=50, y=160.SELECT ST_Rotate('LINESTRING (50 160,50 50,100 50)'::geometry, pi()/6, 50, 160);LINESTRING(50 160,105 64.737206,148.30127 89.737206)This example rotates a geometry 60 degrees clockwise around its centroid.
SELECT ST_Rotate(geom, -pi()/3, ST_Centroid(geom))
FROM (SELECT 'LINESTRING (50 160,50 50,100 50)'::geometry AS geom) AS foo;LINESTRING(116.4225 130.6721,21.1597 75.6721,46.1597 32.3708)See Also, , , ST_RotateXRotates a geometry about the X axis.geometry ST_RotateXgeometrygeomAfloatrotRadiansDescriptionRotates a geometry geomA - rotRadians about the X axis.ST_RotateX(geomA, rotRadians)
is short-hand for ST_Affine(geomA, 1, 0, 0, 0, cos(rotRadians), -sin(rotRadians), 0, sin(rotRadians), cos(rotRadians), 0, 0, 0).Enhanced: 2.0.0 support for Polyhedral surfaces, Triangles and TIN was introduced.Availability: 1.1.2. Name changed from RotateX to ST_RotateX in 1.2.2&P_support;&Z_support;&T_support;ExamplesThis example rotates a line 90 degrees around the X axis.SELECT ST_RotateX('LINESTRING(1 2 3,1 1 1)', pi()/2);LINESTRING(1 -3 2,1 -1 1)See Also, , ST_RotateYRotates a geometry about the Y axis.geometry ST_RotateYgeometrygeomAfloatrotRadiansDescriptionRotates a geometry geomA - rotRadians about the y axis.ST_RotateY(geomA, rotRadians)
is short-hand for ST_Affine(geomA, cos(rotRadians), 0, sin(rotRadians), 0, 1, 0, -sin(rotRadians), 0, cos(rotRadians), 0, 0, 0).Availability: 1.1.2. Name changed from RotateY to ST_RotateY in 1.2.2Enhanced: 2.0.0 support for Polyhedral surfaces, Triangles and TIN was introduced.&P_support;&Z_support;&T_support;ExamplesThis example rotates a line 90 degrees around the Y axis.SELECT ST_RotateY('LINESTRING(1 2 3,1 1 1)', pi()/2);LINESTRING(3 2 -1,1 1 -1)See Also, , ST_RotateZRotates a geometry about the Z axis.geometry ST_RotateZgeometrygeomAfloatrotRadiansDescriptionRotates a geometry geomA - rotRadians about the Z axis.This is a synonym for ST_RotateST_RotateZ(geomA, rotRadians)
is short-hand for SELECT ST_Affine(geomA, cos(rotRadians), -sin(rotRadians), 0, sin(rotRadians), cos(rotRadians), 0, 0, 0, 1, 0, 0, 0).Enhanced: 2.0.0 support for Polyhedral surfaces, Triangles and TIN was introduced.Availability: 1.1.2. Name changed from RotateZ to ST_RotateZ in 1.2.2Prior to 1.3.4, this function crashes if used with geometries that contain CURVES. This is fixed in 1.3.4+&Z_support;&curve_support;&P_support;&T_support;ExamplesThis example rotates a line 90 degrees around the Z axis.SELECT ST_RotateZ('LINESTRING(1 2 3,1 1 1)', pi()/2);LINESTRING(-2 1 3,-1 1 1)This example rotates a curved circle around the Z axis.
SELECT ST_RotateZ(geom, pi()/2)
FROM (SELECT ST_LineToCurve(ST_Buffer('POINT(234 567)'::geometry, 3)) As geom) As foo;CURVEPOLYGON(CIRCULARSTRING(-567 237.00000000000003,-567 231.00000000000003,-567 237.00000000000003))See Also, , ST_ScaleScales a geometry by given factors.geometry ST_Scalegeometry geomAfloatXFactorfloatYFactorfloatZFactorgeometry ST_Scalegeometry geomAfloatXFactorfloatYFactorgeometry ST_Scalegeometry geomgeometryfactorgeometry ST_Scalegeometry geomgeometryfactorgeometryoriginDescriptionScales the geometry to a new size by multiplying the
ordinates with the corresponding factor parameters.
The version taking a geometry as the factor parameter
allows passing a 2d, 3dm, 3dz or 4d point to set scaling factor for all
supported dimensions. Missing dimensions in the factor
point are equivalent to no scaling the corresponding dimension.
The three-geometry variant allows a "false origin" for the scaling to be passed in. This allows "scaling in place", for example using the centroid of the geometry as the false origin. Without a false origin, scaling takes place relative to the actual origin, so all coordinates are just multiplied by the scale factor.
Prior to 1.3.4, this function crashes if used with geometries that contain CURVES. This is fixed in 1.3.4+Availability: 1.1.0.Enhanced: 2.0.0 support for Polyhedral surfaces, Triangles and TIN was introduced.Enhanced: 2.2.0 support for scaling all dimension (factor parameter) was introduced.Enhanced: 2.5.0 support for scaling relative to a local origin (origin parameter) was introduced.&P_support;&Z_support;&curve_support;&T_support;&M_support;ExamplesThis example scales X, Y, and Z values.SELECT ST_Scale('LINESTRING(1 2 3,1 1 1)', 0.5, 0.75, 0.8);LINESTRING(0.5 1.5 2.4,0.5 0.75 0.8)This example scales X and Y values.SELECT ST_Scale('LINESTRING(1 2 3,1 1 1)', 0.5, 0.75);LINESTRING(0.5 1.5 3,0.5 0.75 1)This example scales X, Y, Z, and M values.
SELECT ST_Scale('LINESTRING(1 2 3 4,1 1 1 1)',
ST_MakePoint(0.5, 0.75, 2, -1));LINESTRING(0.5 1.5 6 -4,0.5 0.75 2 -1)This example scales X and Y values using a false origin.SELECT ST_Scale('LINESTRING(1 1,2 2)', 'POINT(2 2)', 'POINT(1 1)'::geometry);LINESTRING(1 1,3 3)This example constructs an ellipse centered at (10, 20), with X radius 8,
Y radius 4, and rotated 30 degrees counter-clockwise. Scaling and rotation use
the center as their origin, so the ellipse keeps both its position and SRID.WITH params AS (
SELECT
ST_SetSRID(ST_Point(10, 20), 3857) AS center,
8.0 AS x_radius,
4.0 AS y_radius,
radians(30) AS angle
)
SELECT ST_Rotate(
ST_Scale(
ST_Buffer(center, 1, 'quad_segs=32'),
ST_MakePoint(x_radius, y_radius),
center
),
angle,
center
) AS ellipse
FROM params;See Also, , , , ST_TranslateTranslates a geometry by given offsets.geometry ST_Translategeometry g1float deltaxfloat deltaygeometry ST_Translategeometry g1float deltaxfloat deltayfloat deltazDescriptionReturns a new geometry whose coordinates are translated delta x,delta y,delta z units. Units are
based on the units defined in spatial reference (SRID) for this geometry.
M coordinates are preserved.Prior to 1.3.4, this function crashes if used with geometries that contain CURVES. This is fixed in 1.3.4+Availability: 1.2.2&Z_support;&curve_support;&M_support;ExamplesMove a point 1 degree longitudeSELECT ST_Translate(ST_GeomFromText('POINT(-71.01 42.37)', 4326), 1, 0) AS wgs_transgeomtxt;POINT(-70.01 42.37)Move a linestring 1 degree longitude and 1/2 degree latitudeSELECT ST_Translate(ST_GeomFromText('LINESTRING(-71.01 42.37,-71.11 42.38)', 4326), 1, 0.5) AS wgs_transgeomtxt;LINESTRING(-70.01 42.87,-70.11 42.88)Move a 3d pointSELECT ST_Translate(CAST('POINT(0 0 0)' AS geometry), 5, 12, 3);POINT(5 12 3)Move points with a measure coordinateSELECT ST_Translate('POINTM(0 0 11)', 5, 12);POINTM(5 12 11)SELECT ST_Translate('POINT(0 0 7 11)', 5, 12, 3);POINT(5 12 10 11)Move a curve and a pointSELECT ST_Translate(ST_Collect('CURVEPOLYGON(CIRCULARSTRING(4 3,3.12 0.878,1 0,-1.121 5.1213,6 7,8 9,4 3))', 'POINT(1 3)'), 1, 2);GEOMETRYCOLLECTION(CURVEPOLYGON(CIRCULARSTRING(5 5,4.12 2.878,2 2,-0.121 7.1213,7 9,9 11,5 5)), POINT(2 5))See Also, , ST_TransScaleTranslates and scales a geometry by given offsets and factors.geometry ST_TransScalegeometry geomAfloatdeltaXfloatdeltaYfloatXFactorfloatYFactorDescriptionTranslates the geometry using the deltaX and deltaY args,
then scales it using the XFactor, YFactor args, working in 2D only.ST_TransScale(geomA, deltaX, deltaY, XFactor, YFactor)
is short-hand for ST_Affine(geomA, XFactor, 0, 0, 0, YFactor, 0,
0, 0, 1, deltaX*XFactor, deltaY*YFactor, 0).Prior to 1.3.4, this function crashes if used with geometries that contain CURVES. This is fixed in 1.3.4+Availability: 1.1.0.&Z_support;&curve_support;ExamplesSELECT ST_TransScale('LINESTRING(1 2 3,1 1 1)', 0.5, 1, 1, 2);LINESTRING(1.5 6 3,1.5 4 1)This example buffers a point to approximate a circle, converts it to a curve, translates it by 1,2, and scales it by 3,4.SELECT ST_TransScale(ST_LineToCurve(ST_Buffer('POINT(234 567)', 3)), 1, 2, 3, 4);CURVEPOLYGON(CIRCULARSTRING(714 2276,696 2276,714 2276))See Also,