> ## Documentation Index
> Fetch the complete documentation index at: https://docs.wherobots.com/llms.txt
> Use this file to discover all available pages before exploring further.

# ST_SharedPaths

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

```sql theme={"system"}
ST_SharedPaths(A: Geometry, B: Geometry)
```

## Parameters

<ParamField body="A" type="Geometry" required>The first `LineString` or `MultiLineString`, which determines the direction of returned paths.</ParamField>
<ParamField body="B" type="Geometry" required>The second `LineString` or `MultiLineString`, with the same SRID as `A`.</ParamField>

## Return type

<ResponseField type="Geometry">A `GeometryCollection` of same-direction and opposite-direction paths, or `NULL` if either input is `NULL`.</ResponseField>

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.

<img src="https://mintcdn.com/wherobots/7aw_3sK08Olbx71U/images/sql-functions/ST_SharedPaths/ST_SharedPaths.svg?fit=max&auto=format&n=7aw_3sK08Olbx71U&q=85&s=8f3f04d548b3835a5ce8fecdcb914018" alt="Same-direction and opposite-direction paths returned together by ST_SharedPaths" width="1100" height="720" data-path="images/sql-functions/ST_SharedPaths/ST_SharedPaths.svg" />

### 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.

<img src="https://mintcdn.com/wherobots/7aw_3sK08Olbx71U/images/sql-functions/ST_SharedPaths/ST_SharedPaths_multipart.svg?fit=max&auto=format&n=7aw_3sK08Olbx71U&q=85&s=093cd327926069f15b106cba09a791d6" alt="ST_SharedPaths over multipart input with a shared path split at an input vertex" width="1100" height="820" data-path="images/sql-functions/ST_SharedPaths/ST_SharedPaths_multipart.svg" />

### 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.

<img src="https://mintcdn.com/wherobots/7aw_3sK08Olbx71U/images/sql-functions/ST_SharedPaths/ST_SharedPaths_point_intersections.svg?fit=max&auto=format&n=7aw_3sK08Olbx71U&q=85&s=517936fae7cc1c8ec8dda8f9face6153" alt="ST_SharedPaths ignores interior crossings and endpoint-only touches" width="1100" height="780" data-path="images/sql-functions/ST_SharedPaths/ST_SharedPaths_point_intersections.svg" />

## Examples

### Same direction

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

```sql theme={"system"}
SELECT ST_AsText(ST_SharedPaths(
    ST_GeomFromWKT('LINESTRING (0 0, 10 0)'),
    ST_GeomFromWKT('LINESTRING (5 0, 15 0)')
));
```

```text theme={"system"}
GEOMETRYCOLLECTION (MULTILINESTRING ((5 0, 10 0)), MULTILINESTRING EMPTY)
```

### Opposite direction

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

```sql theme={"system"}
SELECT ST_AsText(ST_SharedPaths(
    ST_GeomFromWKT('LINESTRING (0 0, 10 0)'),
    ST_GeomFromWKT('LINESTRING (15 0, 5 0)')
));
```

```text theme={"system"}
GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MULTILINESTRING ((5 0, 10 0)))
```

### Point-only intersection

Crossing at a point does not create a shared path.

```sql theme={"system"}
SELECT ST_AsText(ST_SharedPaths(
    ST_GeomFromWKT('LINESTRING (0 0, 10 0)'),
    ST_GeomFromWKT('LINESTRING (5 -5, 5 5)')
));
```

```text theme={"system"}
GEOMETRYCOLLECTION (MULTILINESTRING EMPTY, MULTILINESTRING EMPTY)
```
