Skip to main content
Returns the paths shared by two lineal geometries, grouped by traversal direction. Both inputs must be a LineString or MultiLineString and must have the same SRID. The result is a GeometryCollection containing exactly two MultiLineString elements:
  • Element 0 contains paths traversed in the same direction by both inputs.
  • Element 1 contains paths traversed in opposite directions.
Coordinates in both elements follow the direction of the first input, A. If the inputs share no path, including when they intersect only at points, both elements are empty.

Signatures

Parameters

Geometry
required
The first LineString or MultiLineString, which determines the direction of returned paths.
Geometry
required
The second LineString or MultiLineString, with the same SRID as A.

Return type

A GeometryCollection of same-direction and opposite-direction paths, or NULL if either input is NULL.
Non-lineal inputs or mismatched SRIDs raise an error. The result retains the matching input SRID. A non-empty result retains Z only when at least one input has Z and every returned coordinate resolves to a finite Z from the inputs. Otherwise, the entire result is two-dimensional. M values are always dropped, and a result with no shared paths is two-dimensional. For inputs that traverse the same path more than once, direction follows the first matching source traversal. Swapping A and B can therefore change which element contains a shared path.

Visual examples

Same and opposite directions

Input B first follows A, leaves it, and later traverses another part of A backwards. The single result therefore populates both direction buckets. Notice that the path in element 1 is reoriented to follow A, even though B traverses it in the opposite direction. Same-direction and opposite-direction paths returned together by ST_SharedPaths

Multipart input and noded output

Input A can be a MultiLineString. In this example, B shares part of two different components of A. The vertex at (90 161) belongs to B and splits the shared diagonal into two paths in element 0. ST_SharedPaths over multipart input with a shared path split at an input vertex

Point-only intersections

Crossing at an interior point and touching at an endpoint are both zero-dimensional contacts. Neither is a shared path, so both MultiLineString elements are empty. ST_SharedPaths ignores interior crossings and endpoint-only touches

Examples

Same direction

Both inputs traverse the shared interval from left to right, so it appears in element 0.

Opposite direction

The second input traverses the shared interval in reverse, so it appears in element 1. Its returned coordinates still follow A.

Point-only intersection

Crossing at a point does not create a shared path.