Returns a GeometryCollection of paths shared by two lineal geometry objects. The collection contains two elements: same-direction shared paths in the first element, and opposite-direction shared paths in the second.
Syntax
geometry ST_SharedPaths(geometry lineal1, geometry lineal2);Parameters
| Parameter | Description |
|---|---|
lineal1 | The first lineal geometry. The direction of paths in the returned collection is based on the direction of this geometry. |
lineal2 | The second lineal geometry. |
Description
ST_SharedPaths compares two lineal geometry objects (LINESTRING or MULTILINESTRING) and returns a GeometryCollection with two MULTILINESTRING elements:
First element: paths that run in the same direction as
lineal1Second element: paths that run in the opposite direction to
lineal1
The direction of paths in the returned collection is determined by the direction of lineal1. Reversing the vertex order of lineal1 moves paths between the two elements.
Examples
Basic usage
The following example finds shared paths between two line strings that overlap along the segment (0 0, 0 1).
SELECT
ST_AsText(ST_GeometryN(g, 1)) AS same_direction,
ST_AsText(ST_GeometryN(g, 2)) AS opposite_direction
FROM (
SELECT ST_SharedPaths(
'LINESTRING(-1 0, 0 0, 0 1)'::geometry,
'LINESTRING(0 0, 0 1, 1 1)'::geometry
) AS g
) AS t;Result:
same_direction | opposite_direction
----------------------------+-----------------------
MULTILINESTRING((0 0,0 1)) | MULTILINESTRING EMPTY
(1 row)Both inputs traverse the segment (0 0, 0 1) in the same direction, so the shared path appears in the first element. The second element is empty.
What's next
After retrieving a GeometryCollection from ST_SharedPaths, use ST_GeometryN(g, n) to extract the first (same-direction) or second (opposite-direction) element.