ST_SharedPaths
What is ST_SharedPaths?
ST_SharedPaths is a PostGIS function that returns the parts of two line geometries that coincide. The output is a GeometryCollection with two MultiLineStrings: paths traversed in the same direction, and paths traversed in opposite directions.
ST_SharedPaths(geometry lineal1, geometry lineal2) → geometryBoth inputs must be LineString or MultiLineString (or geography lineal types). The result is always a GeometryCollection(MultiLineString, MultiLineString).
When would you use ST_SharedPaths?
Use ST_SharedPaths to find overlapping segments between two linear networks — bus routes that share a corridor, bike paths that follow a road, or two digitisations of the same feature. It is the fastest way to quantify geometric overlap of lines while respecting direction.
1SELECT route_a.id AS a, route_b.id AS b,
2 ST_SharedPaths(route_a.geom, route_b.geom) AS overlap
3FROM bus_routes route_a, bus_routes route_b
4WHERE route_a.id < route_b.id
5 AND ST_Intersects(route_a.geom, route_b.geom);FAQs
How do I interpret the two components?
The first component of the returned GeometryCollection contains segments shared in the same direction (same start-to-end order in both inputs); the second contains segments shared in opposite directions. Extract them with ST_GeometryN(result, 1) and ST_GeometryN(result, 2).
Do the inputs need to be noded?
Yes — ST_SharedPaths compares edges, so segments must share vertices to be recognised as identical. Run ST_Node(ST_Union(a, b)) on the pair first if you suspect the inputs are not noded.
What happens if there are no shared paths?
Both components of the collection are returned as empty MultiLineStrings — the overall result is a non-empty GeometryCollection containing two empty parts. Always inspect the parts with ST_IsEmpty rather than the outer collection.
Does direction matter for routing analysis?
Often, yes. Two bus routes sharing a corridor in opposite directions generally indicate parallel services, while shared paths in the same direction indicate duplicated service. The two-component output lets you distinguish these cases cleanly.