Skip to content

ST_LineMerge returns GEOMETRYCOLLECTION EMPTY for LineString input instead of passing it through #2702

@jiayuasu

Description

@jiayuasu

Expected behavior

ST_LineMerge should return a LineString unchanged when given a single LineString as input, consistent with PostGIS and GEOS (GEOSLineMerge).

A single LineString is already "merged" — there is nothing to merge.

PostGIS / GEOS behavior

SELECT ST_AsText(ST_LineMerge('LINESTRING(0 0, 1 1)'));
-- Returns: LINESTRING(0 0, 1 1)
>>> from shapely import line_merge
>>> from shapely.geometry import LineString
>>> line_merge(LineString([(0, 0), (1, 1)]))
<LINESTRING (0 0, 1 1)>

Actual behavior

Sedona returns GEOMETRYCOLLECTION EMPTY for LineString input:

>>> df.selectExpr("ST_LineMerge(ST_GeomFromText('LINESTRING(0 0, 1 1)'))").show()
# Returns: GEOMETRYCOLLECTION EMPTY

Root cause

In Functions.java line 1198, lineMerge only handles MultiLineString and falls through to returning an empty GeometryCollection for all other types — including LineString:

public static Geometry lineMerge(Geometry geometry) {
    if (geometry instanceof MultiLineString) {
      // ... merge logic ...
    }
    return geometry.getFactory().createGeometryCollection(); // <-- LineString hits this
}

Suggested fix

Add a check for LineString before the MultiLineString block:

public static Geometry lineMerge(Geometry geometry) {
    if (geometry instanceof LineString) {
      return geometry;  // A single LineString is already merged
    }
    if (geometry instanceof MultiLineString) {
      // ... existing merge logic ...
    }
    return geometry.getFactory().createGeometryCollection();
}

Environment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions