Skip to content

Add ST_SharedPaths function #2800

@jiayuasu

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions