Skip to content

Add ST_SharedPaths function #2800

Closed

Description

@jiayuasu

Summary

Add ST_SharedPaths(geom1, geom2) function that returns a GeometryCollection containing the paths shared between two linear geometries.

This is the PostGIS equivalent of ST_SharedPaths.

Parent issue: #2230

Design

JTS does not provide a SharedPathFinder class, so this requires a custom implementation. The algorithm:

  1. Compute the intersection of the two lineal geometries (g1.intersection(g2))
  2. Extract individual line components from the intersection result
  3. For each shared segment, determine if it has the same direction in both input geometries or the opposite direction
  4. Return a GeometryCollection with two child MultiLineStrings:
    • Element 0: paths shared in the same direction
    • Element 1: paths shared in the opposite direction

Signature

ST_SharedPaths(geometry lineal1, geometry lineal2) → geometry (GeometryCollection)

Implementation layers

  1. Java core (Functions.java): Custom algorithm using intersection() + direction comparison
  2. Scala Expression (Functions.scala): InferredExpression case class
  3. Catalog registration (Catalog.scala)
  4. Scala API (st_functions.scala): Column wrappers
  5. Python API (st_functions.py): Python wrapper
  6. Tests: Scala + Python

Example

SELECT ST_SharedPaths(
  ST_GeomFromWKT('LINESTRING(0 0, 10 0, 10 10)'),
  ST_GeomFromWKT('LINESTRING(0 0, 10 0)')
);
-- GEOMETRYCOLLECTION (MULTILINESTRING ((0 0, 10 0)), MULTILINESTRING EMPTY)

Direction detection

For each shared segment, walk along the segment in the direction it appears in g1. Check if the same segment traversal direction appears in g2. If same → element 0; if reversed → element 1.

Edge cases

  • Non-lineal geometries: throw IllegalArgumentException
  • No shared paths: return GEOMETRYCOLLECTION(MULTILINESTRING EMPTY, MULTILINESTRING EMPTY)
  • Empty geometries: return null
  • Self-intersecting lines: handle correctly by comparing individual segments

Activity

  1. changed the title [-][GH-2230] Add ST_SharedPaths function[/-] [+]Add ST_SharedPaths function[/+] on Mar 29, 2026
  2. added 7 commits that reference this issue on Aug 11, 2026
  3. added a commit that references this issue on Aug 12, 2026
  4. added 2 commits that reference this issue on Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions