ST_Reverse — Return the geometry with vertex order reversed.
geometry ST_Reverse(geometry g1);
Can be used on any geometry and reverses the order of the vertices.
Reversing a line preserves the set of points it covers. Use ST_OrderingEquals to compare the order of its vertices and ST_Equals to compare the covered points.
Enhanced: 2.4.0 support for curves was introduced.
This function supports 3d and will not drop the z-index.
This function supports Polyhedral surfaces.
WITH data(geom) AS (VALUES (
ST_MakeLine(ST_Point(1, 2), ST_Point(1, 10))
))
SELECT geom AS input_line,
ST_Reverse(geom) AS reversed_line
FROM data;
input_line | reversed_line ----------------------+---------------------- LINESTRING(1 2,1 10) | LINESTRING(1 10,1 2) (1 row)
Compare the shape and the vertex order of a reversed line.
WITH data(geom) AS (
VALUES ('LINESTRING(0 0,1 0,2 1)'::geometry)
)
SELECT ST_Equals(geom, ST_Reverse(geom)) AS same_point_set,
ST_OrderingEquals(geom, ST_Reverse(geom)) AS same_vertex_order
FROM data;
same_point_set | same_vertex_order ----------------+------------------- t | f (1 row)